Bom dia a todos,
Estou com um projeto de automação, e preciso enviar comandos via PHP para o arduino para controlar TV, ligar luzes, coisas desse tipo.
Segue abaixo meu código (Que só está funcionando as luzes, a parte da TV não funciona)
Página PHP
<html>
<head></head>
<body>
<?php
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Se conecta ao IP e Porta:
socket_connect($sock,"192.168.1.201", 50700);
// Executa a ação correspondente ao botão apertado.
if(isset($_POST['bits'])) {
$msg = $_POST['bits'];
if(isset($_POST['Fora' ])){ if($msg[0]=='1') { $msg[0]='0'; } else { $msg[0]='1'; }}
if(isset($_POST['Quarto1'])){ if($msg[1]=='1') { $msg[1]='0'; } else { $msg[1]='1'; }}
if(isset($_POST['Quarto2'])){ if($msg[2]=='1') { $msg[2]='0'; } else { $msg[2]='1'; }}
if(isset($_POST['Sala' ])){ if($msg[3]=='1') { $msg[3]='0'; } else { $msg[3]='1'; }}
//daqui pra baixo são os testes que eu fiz
if(isset($_POST['TV1' ])){ socket_write($sock,'AT#',3);}
if(isset($_POST['TV2' ])){ $msg = '2T#'; }
if(isset($_POST['TV3' ])){ $msg = 'CT#'; }
if(isset($_POST['TV4' ])){ $msg = 'DT#'; }
if(isset($_POST['TV5' ])){ $msg = 'ET#'; }
if(isset($_POST['TV6' ])){ $msg = 'FT#'; }
if(isset($_POST['TV7' ])){ $msg = 'GT#'; }
if(isset($_POST['TV8' ])){ $msg = 'HT#'; }
if(isset($_POST['TV9' ])){ $msg = 'IT#'; }
if(isset($_POST['TV10' ])){ $msg = 'JT#'; }
//if(isset($_POST['Grande' ])){ $msg = 'G#'; } //Se eu enviar o comando assim ele funciona, mas aí fica possivel alterar só 1 letra
socket_write($sock,$msg,strlen($msg));
}
socket_write($sock,'R#',2); //Requisita o status do sistema.
// Espera e lê o status e define a cor dos botões de acordo.
$status = socket_read($sock,6);
if (($status[4]=='L')&&($status[5]=='#')) {
if ($status[0]=='0') $cor1 = lightcoral;
else $cor1 = lightgreen;
if ($status[1]=='0') $cor2 = lightcoral;
else $cor2 = lightgreen;
if ($status[2]=='0') $cor3 = lightcoral;
else $cor3 = lightgreen;
if ($status[3]=='0') $cor4 = lightcoral;
else $cor4 = lightgreen;
echo "<form method =\"post\" action=\"index.php\">";
echo "<input type=\"hidden\" name=\"bits\" value=\"$status\">";
echo "<button style=\"width:70; background-color: $cor1 ;font: bold 14px Arial\" type = \"Submit\" Name = \"Fora\">Fora</button>
";
echo "<button style=\"width:70; background-color: $cor2 ;font: bold 14px Arial\" type = \"Submit\" Name = \"Quarto1\">Quarto1</button>
";
echo "<button style=\"width:70; background-color: $cor3 ;font: bold 14px Arial\" type = \"Submit\" Name = \"Quarto2\">Quarto2</button>
";
echo "<button style=\"width:70; background-color: $cor4 ;font: bold 14px Arial\" type = \"Submit\" Name = \"Sala\">Sala</button>
";
//Os botões que eu inseri da TV
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV1</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV2</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV3</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV4</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV5</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV6</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV7</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV8</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV9</button>
";
echo "<button style=\"width:90;font: bold 14px Arial\" type = \"Submit\" Name = \"TV1\">TV10</button>
";
echo "</form>";
}
// Caso ele não receba o status corretamente, avisa erro.
else { echo "Falha ao receber status da casa."; }
socket_close($sock);
?>
</body>
</html>
Código do Arduino
//Bibliotecas
#include <SPI.h>
#include <Ethernet.h>
#include <IRremote.h>
//Configurações do Ethernet Shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1, 201 }; // ip que o arduino assumirá
byte gateway[] = { 192,168,1, 1 }; // ip do roteador
byte subnet[] = { 255, 255, 255, 0 };
//Variáveis para dispositivos e aparelhos
//char val;
IRsend irsend;
int Lamp1 = 2;
int Lamp2 = 4;
int Lamp3 = 5;
int Lamp4 = 6;
// String que representa o estado dos dispositivos
char Luz[7] = "0000L#";
char Tv[7] = "0000T#";
EthernetServer server(50700); // Cria o servidor na porta 50700
// String onde é guardada as msgs recebidas
char msg[7] = "0000L#";
void setup() {
//Iniciar o servidor ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
pinMode(Lamp1, OUTPUT); // Porta LED 1
pinMode(Lamp2, OUTPUT); // Porta LED 2
pinMode(Lamp3, OUTPUT); // Porta LED 3
pinMode(Lamp4, OUTPUT); // Porta LED 4
}
void loop() {
EthernetClient client = server.available();
// Se receber um caracter...
if (client) {
// guarda o caracter na string 'msg'
msg[1]=msg[2]; msg[2]=msg[3]; msg[3]=msg[4]; msg[4]=msg[5]; msg[5]=msg[6];
msg[6] = client.read();
if (msg[6]=='#') {
switch(msg[5]) {
case 'R':
// Se receber o comando 'R#' envia de volta o status dos
// dispositivos. (Que é a string 'Luz')
client.write(Luz);
break;
case 'L':
// Caso L#, ele copia os 4 bytes anteriores p/ a
// string 'Luz' e cada byte representa um
// dispositivo, onde '1'=ON e '0'=OFF
Luz[0]=msg[1];
Luz[1]=msg[2];
Luz[2]=msg[3];
Luz[3]=msg[4];
if (Luz[0]=='1') digitalWrite(Lamp1,HIGH); else digitalWrite(Lamp1,LOW);
if (Luz[1]=='1') digitalWrite(Lamp2,HIGH); else digitalWrite(Lamp2,LOW);
if (Luz[2]=='1') digitalWrite(Lamp3,HIGH); else digitalWrite(Lamp3,LOW);
if (Luz[3]=='1') digitalWrite(Lamp4,HIGH); else digitalWrite(Lamp4,LOW);
break;
case 'T':
// Caso T#, ele copia os 4 bytes anteriores p/ a
// string 'Tv' e cada byte representa um
// dispositivo, onde '1'=ON e '0'=OFF
/* Tv[0]=msg[1];
Tv[1]=msg[2];
Tv[2]=msg[3];
Tv[3]=msg[4];
if (Tv[0]=='1') irsend.sendNEC(0x20DF10EF, 32);
if (Tv[1]=='1') irsend.sendNEC(0x20DF40BF, 32);
if (Tv[2]=='1') irsend.sendNEC(0x20DFC03F, 32);
if (Tv[3]=='1') irsend.sendNEC(0x20DF00FF, 32);*/
irsend.sendNEC(0x20DF10EF, 32);
break;
}
}
}
}
Lembrando que só está funcionando as luzes, os comandos da TV não funcionam