I'm trying to make an Arduino receive data in a PHP
structure. Quickly explaining, when the user changes the status, between 0 and 1, it would send a signal to the Arduino according to what was chosen and the Arduino receiving this data, depending on it, would turn the LED on or off. The code I use in Arduino is as follows:
#include <SPI.h>
#include <Ethernet.h>
//Configurações do Ethernet Shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,0, 101 }; // ip que o arduino assumirá
byte gateway[] = { 192,168,0, 1 }; // ip do roteador
byte subnet[] = { 255, 255, 255, 0 };
// String que representa o estado dos dispositivos
//char Luz[7] = "0000L#";
EthernetServer server(80); // Cria o servidor na porta 8081
// String onde é guardada as msgs recebidas
char msg[7] = "0000L#";
void setup() {
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//pinMode(A0,OUTPUT);
//pinMode(A1,OUTPUT);
//pinMode(A2,OUTPUT);
//pinMode(A3,OUTPUT);
//pinMode(A4,OUTPUT);
//pinMode(A5,OUTPUT);
// Configura o pino d13 como saída
pinMode(13, OUTPUT);
}
void loop() {
EthernetClient client = server.available();
// SE receber um caracter...
if (client) {
// guarda o caracter na string 'msg'
msg[6] = client.read();
switch(msg[6]) {
case 0:
// Configura o pino 13 como HIGH
digitalWrite(13, HIGH);
break;
case 1:
// Configura o pino 13 como LOW
digitalWrite(13, LOW);
break;
}
}
}
And the code in my file PHP
, which communicates with the Arduino is as follows:
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Se conecta ao IP e Porta:
socket_connect($sock,"192.168.0.101", 80);
if (!socket_connect($sock, "192.168.0.101", 80)){
die('Socket error : '.socket_strerror(socket_last_error()));
}
$msg = 0;
// Executa a ação correspondente ao botão apertado.
if($status == 0) {
$msg = 0;
socket_write($sock,$msg,strlen($msg));
} else if($status == 1){
$msg = 1;
socket_write($sock,$msg,strlen($msg));
}
//socket_write($sock,'R#',2); //Requisita o status do sistema.
// Caso ele não receba o status corretamente, avisa erro.
//else { echo "Falha ao receber status da casa."; }
socket_close($sock);
When I change the status and the above code is executed, I get the following error:
socket_connect() unable to connect 113 no route to host
In this case, both the Arduino and my computer are connected to a router and the Arduino "picks up" the IP normally. When I start Arduino, I get the answer normally, but when I execute the code described above, I can not make this communication.