Cercherò di essere breve perchè lo sketch è già abbastanza lungo
Lo sketch è un WiFiWebServer che mostra su una pagina web il valore di una fotoresistenza, e inoltre ci sono 2 collegamenti ipertestuali "Accendi" e "Spegni" per accendere/spegnere, a comando, un led.
Fin qui tutto funziona bene.
Oltre a questo nello sketch ho inserito del codice (funzionamento simile a WiFiWebClient) che mi permette di inviare tramite GET un valore ad una pagina php che mi salva questo dato in un database. E anche questo funziona, cioè il dato viene effettivamente memorizzato nel db. Solo che quando eseguo il GET, lo sketch smette di aggiornarmi la pagina web (di cui sopra) che magari nel frattempo sto visualizzando. Non riesco proprio a capire cosa sbaglio.
Segue lo sketch:
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "miaWifi"; // your network SSID (name)
char pass[] = "miaPass"; // your network password
const int ledPin = 8; // pin collegato al led
const int analogPin = 0; //pin collegato alla fotoresistenza
String statoLed;
int sensorValue; // valore letto dalla fotoresistenza
String readString;
IPAddress serverPhp(192,168,0,112);
int status = WL_IDLE_STATUS;
//creo un server (su Arduino) che risponde al browser (client)
WiFiServer server(80);
//creo clientPhp (client) per inviare dati a serverPhp (server)
WiFiClient clientPhp;
void setup() {
pinMode(ledPin, OUTPUT);
//Initialize serial and wait for port to open:
Serial.begin(9600);
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
// ----> listen for incoming clients <----
WiFiClient client = server.available();
if (client) {
delay(1);
Serial.println("##########");
Serial.println("new client");
Serial.println("##########");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
//Arduino è in ascolto di ciò che viene dal browser
char c = client.read();
readString.concat(c);
//Serial.write(c);
// 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) {
Serial.print(readString);
//se sul browser clicco su "Spegni" spengo il led
if(readString.indexOf("Led=0") > 0){
digitalWrite(ledPin, LOW);
Serial.println("----> Led spento! <----");
}
//se sul browser clicco su "Accendi" accendo il led
if(readString.indexOf("Led=1") > 0){
digitalWrite(ledPin, HIGH);
Serial.println("----> Led acceso! <----");
}
//aggiorno lo stato del led per scriverlo sul browser
if(digitalRead(ledPin) == LOW){
statoLed = "Spento";
} else {statoLed = "Acceso";}
// se il valore della fotoresistenza supera il valore 800
// allora invio qualcosa a serverPhp
sensorValue = analogRead(analogPin);
if(sensorValue > 800){
Serial.println("##### ");
Serial.print(sensorValue);
Serial.println(" #####");
//inizio invio dati
// invio un GET a serverPhp
if (clientPhp.connect(serverPhp, 80)){
Serial.println("connected to server php");
clientPhp.println("GET /myget.php?Led=9 HTTP/1.1");
clientPhp.println("Host:www.google.com");
clientPhp.println("Connection: close");
clientPhp.println();
clientPhp.stop();
Serial.println("disconnected from server php");
} // fine invio dati
}
// invio al browser uno standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
//inizio codice html da visualizzare nel browser
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<title><h2>mio titolo</h2></title>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.println("</head>");
client.println("<body>");
client.println("<p>");
client.println("Valore del sensore: ");
client.print(sensorValue);
client.println("</p>");
client.println("<p>");
client.println("Stato del led: ");
client.print(statoLed);
client.println("</p>");
client.println("<h3><a href='Led=1'>Accendi</a>");
client.print(" | <a href='Led=0'>Spegni</a></h3>");
client.println("</body>");
client.println("</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:
readString="";
client.stop();
delay(1);
Serial.println("##################");
Serial.println("client disonnected");
Serial.println("##################");
Serial.println("\n");
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Questo è cioò che vedo dal monitor seriale:
Attempting to connect to SSID: miaRete
SSID: miaRete
IP Address: 192.168.0.100
signal strength (RSSI):-58 dBm
##########
new client
##########
GET / HTTP/1.1
User-Agent: Opera/9.80 (Macintosh; Intel Mac OS X 10.7.5) Presto/2.12.388 Version/12.10
Host: 192.168.0.100
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: it,en;q=0.9,en-US;q=0.8,ja;q=0.7,fr;q=0.6,de;q=0.5,es;q=0.4,pt;q=0.3,pt-PT;q=0.2,nl;q=0.1
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
DNT: 1
##################
client disonnected
##################
.....
##########
new client
##########
GET / HTTP/1.1
User-Agent: Opera/9.80 (Macintosh; Intel Mac OS X 10.7.5) Presto/2.12.388 Version/12.10
Host: 192.168.0.100
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: it,en;q=0.9,en-US;q=0.8,ja;q=0.7,fr;q=0.6,de;q=0.5,es;q=0.4,pt;q=0.3,pt-PT;q=0.2,nl;q=0.1
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.100/
Ca##########
913
##################
client disonnected
##################
dove 913 è il valore della fotoresistenza che ha superato la soglia fissata (800) e inizia il Get...poi si blocca!
In particolare, se elimino questo blocco di codice:
//inizio invio dati
// invio un GET a serverPhp
if (clientPhp.connect(serverPhp, 80)){
Serial.println("connected to server php");
clientPhp.println("GET /myget.php?Led=9 HTTP/1.1");
clientPhp.println("Host:www.google.com");
clientPhp.println("Connection: close");
clientPhp.println();
clientPhp.stop();
Serial.println("disconnected from server php");
}
tutto funziona bene, ovviamente non c'è nessuno invio di dati, ma il resto funziona.
Mi rendo conto della lunghezza post ...confido in qualche Santo armato di una pazienza fuori dal comune!
p.s.
ah dimenticavo, ho già letto questo post Arduino Forum