Nel mio progetto ho bisogno di inviare una get dal server (arduino) al client (altro arduino) per conoscere se la connessione è presente, per esempio ogni 60 secondi (questo mi serve per sapere se viene eliminata l'alimentazione del client oppure viene tagliato il cavo di rete): se questo non si verifica, si deve accendere un led sul server. Come posso implementare il codice presente sul server? Posto il codice e spero in un vostro aiuto. Grazie.
/*
Web server preso dall'esempio dell'ide e modificato da me
*/
#include <SPI.h>
#include <Ethernet.h>
#include <WString.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x3A, 0xA3 };
byte ip[] = { 192,168,0,107 };
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
int analogChannel = 5; //pin analogico dell LM35
int ledpin = 8; //pin del led
float tempC = 0; //temperatura in gradi celsius
int ledstatus = 0; //stato del led
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
pinMode(analogChannel,INPUT); //input dell' LM35
pinMode(ledpin,OUTPUT); //output del led
Serial.begin(9600);
}
void loop()
{
//leggiamo la temperatura
tempC = (analogRead(analogChannel) * 4.88 * 100)/1024;
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
String readstring; //stringa per la lettura dal browser
while (client.connected()) {
if (client.available()) {
char c = client.read();
readstring.concat(c); //aggiunge c alla stringa;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
readstring.concat('\0');
//controllo su seriale
Serial.println("stringa: ");
Serial.println(readstring);
Serial.print("indexOf(led=1): ");
Serial.println(readstring.indexOf("led=1"));
Serial.print("indexOf(led=0): ");
Serial.println(readstring.indexOf("led=0"));
if(readstring.indexOf("led=1") == 6){
digitalWrite(ledpin,HIGH);
ledstatus = 1;
Serial.println("LED ON"); //controllo
}
if(readstring.indexOf("led=0") == 6){
digitalWrite(ledpin,LOW);
ledstatus = 0;
Serial.println("LED OFF"); //controllo
}
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//Creazione pagina web
client.println("<html><head>");
client.println("<title>Domotic-Arduino</title>");
client.println("</head>");
client.println("<body bgcolor=""black"" text=""white"">");
client.println("<h1 align=""center"">Domotic-Home</h1><hr />");
client.println("
");
client.print("Temperatura interna : ");
client.print((int)tempC);
client.println(" C");
client.println("
");
client.println("<h3><a href=""/"">Aggiorna Sensore</a></h3>");
client.println("
");
client.println("<hr />");
client.println("
");
client.print("Comandi LED su pin ");
client.println(ledpin);
client.println("
");
client.print("Stato LED :");
if(ledstatus){
client.println("<font color=""green""> ON</font>");
}else{
client.println("<font color=""red""> OFF</font>");
}
client.println("
");
client.println("<h2><a href=""/?led=1"">ACCENDI</a> | <a href=""/?led=0"">SPEGNI</></h2>");
client.println("</body></html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}