Tutorial acendendo um lampada através da internet

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>&nbsp;<br>&nbsp;<br>

<a href='controle.php?c=D1'>Turn Off Relay 1</a><br>&nbsp;<br>&nbsp;<br>

<a href='controle.php?c=L2'>Turn On Relay 2</a><br>&nbsp;<br>&nbsp;<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.

Exibições: 34471

Comentar

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)

Comentário de João de Souza Ribeiro em 19 fevereiro 2014 às 19:38

Meu servidor wamp está me dizendo que não consegue encontrar o controle.php de Teste de URL solicitado no servidor.

Comentário de Thiago Dias em 14 fevereiro 2014 às 11:15

alguém ai tem o esquema de montagem desse tutorial

Comentário de José Aleixo Ribeiro Neto em 6 novembro 2013 às 8:06

Bom dia Pessoal

estou tentando fazer como teste esse post aqui mais esta dando o seguinte erro

expected initializer before 'void' na linha byte server [ ] = { 192,168,0,10 };

meu codigo completo e o de baixo, lembrando q estou usando um rele de 2 canis e arduino leonardo com ethenet shild

#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,0,2 }; //Put here the IP of Ethernet
byte server[] = { 192,168,0,10 };
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()) {
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);
}
}

Comentário de Allyff Carneiro de Sousa em 7 agosto 2013 às 23:08

Alguém poderia me informar como hospedar esses arquivos em um servidor ?

Comentário de Tiago Henrique Vieira Pires em 20 março 2013 às 1:30

Fiz tentando ascender um led e não consegui pode me ajudar? Não consigo pingar o arduino e acesso  a pagina e clico nos botoes e nada é grava no comando.txt.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0x33, 0xA2, 0x33, 0x00, 0x33, 0x7A };
byte ip[] = { 192, 168, 0, 60 };
byte server[] = { 192, 168, 0, 19 };
char temp[2];
int x=0;
int r5pin=5;
int r6pin=6;
int r7pin=7;
int r8pin=8;
int r9pin=9;
int powerOn=HIGH;
int powerOff=LOW;

EthernetClient client;

void setup() {
pinMode(r5pin,OUTPUT);
pinMode(r6pin,OUTPUT);
pinMode(r7pin,OUTPUT);
pinMode(r8pin,OUTPUT);
pinMode(r9pin,OUTPUT);
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server,80)) {
Serial.println("connected");
client.println("GET /robo.php HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}

void loop(){
if (client.connect(server,80)) {
Serial.println("connected");
client.println("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') {
Serial.println("Liga relay1");
digitalWrite(r7pin,powerOn);
}
if(temp[0]=='L' && temp[1]=='2') {
Serial.println("Liga relay2");
digitalWrite(r7pin,powerOn);
}
if(temp[0]=='D' && temp[1]=='1') {
Serial.println("Desliga relay1");
digitalWrite(r7pin,powerOff);
}
if(temp[0]=='D' && temp[1]=='2') {
Serial.println("Desliga relay2");
digitalWrite(r7pin,powerOff);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
delay(500);
}
}

Comentário de Vinícius Luis de Carvalho Silva em 14 maio 2012 às 4:21

Bom dia galera. É o seguinte, seguindo o que foi postado acima...Consegui fazer o sistema rodar intranet(LAN) , agora estou tentando fazê-lo funcionar internet(WAN)... Sei que o ip da velox é dinâmico, porém já criei um dominio ""dlinkddns.com(grátis,super recomendo)"" , porém estou sem saber oq fazer agora, no programa do arduino tem o local pra colocar o ip do site(onde consigo?), tem como colocar o link do domínio ?

Alguém tem ideia?

Obrigado.Abraço.paz

Comentário de Ezequiel Fernanndes em 17 abril 2012 às 11:07

Marcelo, bacana.

O problema é que aí consigo ligar apenas 2 dispositivos, certo?
Preciso realmente saber como funciona as funções do rele, preciso achar um  tópico bem claro.
É que sou iniciante no Arduino e eletrônica, aí complica um pouquinho.

Você conhece algum lugar que oferece curso de Arduino Online?

Obrigado,
Ezequiel Fernandes 

Comentário de Marcelo Rodrigues em 17 abril 2012 às 11:04

Isso, o ReleShield é compatível com o EthernetShield e já vem com toda a eletrônica necessária para acionar cargas maiores direto do shield.

Abraço!!

Comentário de Ezequiel Fernanndes em 16 abril 2012 às 14:18

Marcelo,

Show de bola.. então nesse rele já está incluso esses itens ??

Comentário de Marcelo Rodrigues em 16 abril 2012 às 10:42

Ezequiel,

Nesse tutorial é usado o ReleShield: http://labdegarag1.lojatemporaria.com/releshield.html

Abraço!!

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço