Hi all,
I wrote a program that should perform the following tasks:
- read the led status and send it to a php page which update a table in my db.
- receive commands from a webpage about turn on or off the led
The code is the following:
#include <SPI.h>
#include <UIPEthernet.h>
byte mac[] = { 0x90, 0xa2, 0xda, 0x0d, 0x05, 0x18 };
IPAddress ip(192,168,1,7);
char myserver[] = "www.miodominio.com";
const int DIMENSIONE_BUFFER = 10; // usato per salvare il parametro
char buffer [DIMENSIONE_BUFFER+1]; // termina con il carattere nullo
int pin;
int stato=0;
int ledPin = 6;
EthernetServer server(80);//server port
EthernetClient client;
void setup(){
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
pinMode(ledPin, OUTPUT);
Serial.println("ready");
}
void loop(){
UpdateStatus();
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if(client.available()){
if(client.find("led"))
{
// cancello il buffer salvato in precedenza, se esiste
memset(buffer,0,sizeof(buffer));
// leggo fino al carattere &
client.findUntil("&","\n\r");
//memorizzo il numero che ritrovo nella stringa passata dopo il carattere '&'
pin = client.parseInt();
}
if( pin == 0 ) { // search for 'GET'
Serial.print("Digital pin low ");
digitalWrite(ledPin, LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println("messaggio");
client.println();
}
if( pin == 1 ) { // search for 'GET'
Serial.print("Digital pin high ");
digitalWrite(ledPin, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Access-Control-Allow-Origin: *");
client.println();
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
}}
void UpdateStatus()
{
if (client.connect(myserver, 80))
{
stato = digitalRead(ledPin);
Serial.print("stato:");
Serial.print(stato);
// send the HTTP PUT request:
client.print("GET /progetti_arduino/ledon/update_status.php?status=");
client.print(stato);
client.println(" HTTP/1.0");
client.println("Host: www.miodominio.com");
client.println();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
}
while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read();
Serial.print(c);
}
}
When I tell to arduino to turn on or off the led by a web page, sometimes arduino reply correctly performing the command that I sent, sometimes not
Can someone explain me what it depends from?
Thank you very much.