O vídeo acima mostra o Arduino utilizando um Ethernet Shield e um ReleShield para acender uma lâmpada pela internet.
O fluxograma de funcionamento está demonstrado abaixo:
Lista de Materiais: Arduino, Ethernet Shield, ReleShield, lampada e um ventilador pequeno.
Adquira estes componentes na Loja do Lab de Garagem
Conecte o ReleShield no Ethernet Shield e por último, conecte no Arduino. Conecte um cabo RJ-45 no Ethernet Shield e depois conecte o cabo em um roteador.
Abra a IDE do Arduino e passe a seguinte programação:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x33, 0xA2, 0x33, 0x00, 0x33, 0x7A }; //Put here the Ethernet's Mac Address
byte ip[] = { 192,168,42,177 }; //Put here the IP of Ethernet
byte server[] = { 192,168,42,18 }; // Put the http adress that Arduino will access.
char temp[2];
int x=0;
int r1pin=7;
int r2pin=8;
int relay1=LOW;
int relay2=LOW;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client(server, 80);
void setup() {
// start the Ethernet connection:
pinMode(r1pin,OUTPUT);
pinMode(r2pin,OUTPUT);
Ethernet.begin(mac, ip);
// start the serial library:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect()) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /robo.php HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.connect()) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /robo.php HTTP/1.0"); //Put here the page to turn on/off the lamp
client.println();
}
do
{
if (client.available())
{
char c = client.read();
temp[x]=c;
Serial.print(temp[x]);
x++;
}
}while(x<2);
x=0;
if(temp[0]=='L' && temp[1]=='1') //Turn on relay 1
{
relay1=HIGH;
Serial.println("Liga relay1");
digitalWrite(r1pin,relay1);
}
if(temp[0]=='L' && temp[1]=='2') //Turn on relay 2
{
relay2=HIGH;
Serial.println("Liga relay2");
digitalWrite(r2pin,relay2);
}
if(temp[0]=='D' && temp[1]=='1') //Turn off relay 1
{
relay1=LOW;
Serial.println("Desliga relay1");
digitalWrite(r1pin,relay1);
}
if(temp[0]=='D' && temp[1]=='2') //Turn off relay 2
{
relay2=LOW;
Serial.println("Desliga relay2");
digitalWrite(r2pin,relay2);
}
// Disconnect the Ethernet Shield and delay 0.5 seconds
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(500);
}
}
Clique em Upload e espere terminar.
Aqui fizemos uma página em PHP. São três arquivos diferentes. Um para o controle, um para adquirir o dado e outra para armazenamento. Os códigos estão demonstrados abaixo:
Código do controle.php:
<?
if($_GET['c']) file_put_contents('comando.txt',$_GET['c']);
?>
<font size=50><a href='controle.php?c=L1'>Turn On Relay 1</a><br> <br> <br>
<a href='controle.php?c=D1'>Turn Off Relay 1</a><br> <br> <br>
<a href='controle.php?c=L2'>Turn On Relay 2</a><br> <br> <br>
<a href='controle.php?c=D2'>Turn Off Relay 2</a><br></font>
Código do robo.php:
<?
echo (file_get_contents('comando.txt'));
?>
O arquivo de armazenamento é apenas um arquivo de texto .txt. Apenas crie um arquivo .txt e salve como comando.txt.
Agora hospede esses arquivos em um servidor. Aqui no LdG apenas criamos um servidor pelo lamp ou wamp.
Depois de hospedado, pegue o ip do servidor e coloque na programação do Arduino como está exemplificado anteriormente.
Se estiver tudo certo, o acionamento vai funcionar.
Comentar
Carlos Kwiek
Eu tinha conseguido acender um led pela internet (com celular e com notebook estando num shopping de sp e o led em minha casa em s. andré). O grande problema são as portas para configurar o roteador.
Como resetei o roteador ( e burramente não guardei as configurações) agora não consigo mais repetir
o evento, somente pela intranet como vc falou. A Intelbras está me ajudando mas parece que a NET
está pondo dificuldade. Não acredito pois acesso de s. j. do rio preto as câmeras de casa pelo mesmo roteador + dvr
Isso se for IP fixo, mas a maioria tem ip rotativo.
Tem que ter um software como NO_IP rodando para poder ter acesso externo.
Dia ou voce tem um dvr com um DDNS ou um cliente do próprio no-ip , mas a pegunta é,como fazer este cliente no-ip rodar no arduino ????
Fiz um video com Arduino com AJAX, dar uma olhada :D
https://www.youtube.com/playlist?list=PLbUAcqHuByzfm9od5kHKArjKIAtf...
André obrigado,
Estou quase lá, mas vi que existe uma total falta de informação, assim que solucionar vou postar aqui.
Um abraço!
Carlos,
O acesso via internet é apenas uma configuração que vc faz no seu roteador. Isto varia de roteador/operadora. Tem uma galera que acessa via dyndns, no-ip..entre outros. Veja mais sobre isso.
Abs
Desculpe mas esta mal explicado e deixa duvidas quanto a veracidade do acesso.
Até agora não vejo ninguém que tenha acessado pela internet, as pessoas acessam pela intranet e postam que acessaram via internet
Angelo, voce conseguiu resolver o seu problema do GET?
Estou no mesmo impasse.
Obrigado!
Pessoal
Tenho abaixo parte de um código do arduino é uma comunicação via browser. O char c que pega a informação do browser ele vem da seguinte forma por exemplo eu digito 192.168.1.119/S_vdown ele retorna a seguinte informação na tela do client.print = "GET /S_vdown HTTP/1.1" esse é o valor do char "c". Então o que eu estou tentando fazer e nao consigo é criar uma variavel que peque somente o "S_vdown" eu preciso de uma variável semente com esta parte do texto. Se alguém puder me dar uma luz eu agradeço.
void setup(){
Ethernet.begin(mac, ip); // Inicia o Ethernet
Serial.begin(9600); // Inicia a comunicação Serial
}
void loop(){
EthernetClient client = server.available();// Criar uma conexão de cliente
if (client) {
while (client.connected()){
if (client.available()){
char c = client.read();
client.print(c);
if (readString.length() < 30)// ler caractere por caractere vindo do HTTP
{
readString += (c);// armazena os caracteres para string
}
if (c == '\n')//se o pedido HTTP terminou
{
readString="";//limpa string para a próxima leitura
client.stop(); // parar cliente
}
}
}
}
}
Pessoal adapetei o projeto em questao no meu endereco de ip. Criei os 3 arquivos. comando.txt robo.php e controle.php. No arquivo txt grava char c mas na acende o led. Inicio o monitor serial ele fica dando um loop com a seguinte mensagem:
disconnecting.
connected
<html>
<head>
<script language='javascript'>
top.location.href='mainlogin.html';
</script></head><body></body></html>
Alguem pode me dizer o que esta errado?
O codigo no arduino e o seguinte:
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x54, 0x55, 0x58, 0x10, 0x00, 0x24 }; //Put here the Ethernet's Mac Address
byte ip[] = { 192,168,1,119 }; //Put here the IP of Ethernet
byte server[] = { 192,168,1,1 }; // Put the http adress that Arduino will access.
char temp[2];
int x=0;
int r1pin=7;
int r2pin=8;
int relay1=LOW;
int relay2=LOW;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
// start the Ethernet connection:
pinMode(r1pin,OUTPUT);
pinMode(r2pin,OUTPUT);
Ethernet.begin(mac, ip);
// start the serial library:
Serial.begin(9600);
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server,80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /robo.php HTTP/1.0");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
void loop(){
// if there are incoming bytes available
// from the server, read them and print them:
if (client.connect(server,80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /robo.php HTTP/1.0"); //Put here the page to turn on/off the lamp //GET /robo.php HTTP/1.0
client.println();
}
do
{
if (client.available())
{
char c = client.read();
temp[x]=c;
Serial.print(temp[x]);
x++;
}
} while(x<2);
x=0;
if(temp[0]=='L' && temp[1]=='1') //Turn on relay 1
{
relay1=HIGH;
Serial.println("Liga relay1");
digitalWrite(r1pin,relay1);
}
if(temp[0]=='L' && temp[1]=='2') //Turn on relay 2
{
relay2=HIGH;
Serial.println("Liga relay2");
digitalWrite(r2pin,relay2);
}
if(temp[0]=='D' && temp[1]=='1') //Turn off relay 1
{
relay1=LOW;
Serial.println("Desliga relay1");
digitalWrite(r1pin,relay1);
}
if(temp[0]=='D' && temp[1]=='2') //Turn off relay 2
{
relay2=LOW;
Serial.println("Desliga relay2");
digitalWrite(r2pin,relay2);
}
// Disconnect the Ethernet Shield and delay 0.5 seconds
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(500);
}
}
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por
Você precisa ser um membro de Laboratorio de Garagem (arduino, eletrônica, robotica, hacking) para adicionar comentários!
Entrar em Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)