This is the code, sorry it's not very elegant and can be shortened a lot.
Basically, I took the code from
http://arduino.cc/en/Tutorial/WebClientRepeating.
Then I perform 2 GET:
1. the first send sensors reading
2. the second reads a TXT file containing 2 values for switching on/off 2 leds. I took the method here
http://bildr.org/?s=client and improved the readPage() function with SurferTim code.
TXT file is a string like
<01> (= 4 chars). Each number represents the LED status (0 or 1).
The < and > are used to locate and extract the values, because the answer from server is about 120 chars (contains also headers).
// ---------------- IMPORT ------------------------------------
#include <SPI.h>
#include <Ethernet.h>
// ----------------- VARIABLES DEFINITIONS --------------------
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// variables used by millis
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
boolean lastConnected = false; // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; // time to wait before repeating connections
// variables for connection
char serverName[] = "myserver.example.org";
EthernetClient client;
// GET #2
String readValue = ""; // String to store the readPage() function result
// variables for sensors
const int tempPin = A0; // pin where temp sensors is connected
const int lightPin = A1; // pin where light sensor (photoresistor) is connected
const int ledPinR = 6; // pin of RED led (LED 1)
const int ledPinW = 2; // pinf of WHITE led (LED 2)
int tempRead = 0; // value read by temperature sensor (0-1023)
float voltage = 0.0; // voltage of the temp sensor
float temp = 0.0; // calculated value of the temperature (degrees °C)
int light = 0; // value read by light sensor
int LED1 = 0; // value to assign to LED 1
int LED2 = 0; // value to assign to LED 2
// -----------------SETUP --------------------------------------------
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
pinMode (4, OUTPUT); //disable SD card slot on ethernet shield
digitalWrite (4, HIGH);
pinMode (10, OUTPUT); //explicity enable ethernet
digitalWrite (10, LOW);
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;}
// give the Ethernet shield 1 seconds to initialize, then write debug info:
delay(1000);
Serial.print("Ethernet begins.. ");
Serial.println(Ethernet.localIP());
// initializes LEDs
pinMode(ledPinR, OUTPUT);
pinMode(ledPinW, OUTPUT);
digitalWrite(ledPinR, LOW);
digitalWrite(ledPinW, LOW);
}
// -----------------------------LOOP---------------------
void loop() {
// HTTP REQUEST
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// Read and calculate temp
tempRead = analogRead(tempPin);
voltage = (tempRead/1024.0) * 5.0;
temp = ((voltage - 0.5) * 100.0);
// Read Light
light = analogRead(lightPin);
// -------------------------- GET #1 -----------------------------------------------------
// --------------------------(sends sensors and errors status to server) -----------------
// ---------------------------------------------------------------------------------------
if (client.connect(serverName, 80)) {
Serial.println("Connected - GET #1");
client.print("GET /sensors.php?temp=");client.print(temp);
client.print("&light=");client.print(light);
client.println(" HTTP/1.1");
client.println("Host: myserver.example.org");
client.println("Connection: close");
client.println();
client.stop();
delay(50);
Serial.println("GET #1 - disconnecting.");
} else {
// if you didn't get a connection to the server:
Serial.println("GET #1 - connection failed");
}
// ------------------------- End of GET #1
//
//
// -------------------------- GET #2 --------------------------------------------------
// -------------------------- (reads the values to be assigned to the LEDs ------------
// ------------------------------------------------------------------------------------
if (client.connect(serverName, 80)) {
Serial.println("Connected - GET #2");
client.println("GET /led.txt HTTP/1.1");
client.println("Host: myserver.example.org");
client.println("Connection: close");
client.println();
client.stop();
delay(50);
// read the TXT file
readValue = readPage(); // Call to readPage(): get the values extracted from led.txt file
// assign the LEDs values
LED1 = readValue[0]-'0';
LED2 = readValue[1]-'0';
digitalWrite(ledPinR,LED1);
digitalWrite(ledPinW,LED2);
} else {
// if you didn't get a connection to the server:
Serial.println("GET #2 - connection failed");
client.stop();
}
// ------------------------------- End of GET #2 cycle
// note the time that the connection was made:
lastConnectionTime = millis();
delay(1000);
//
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
} // Close HTTP REQUEST
} // Close void LOOP
// ----------------- FUNCTIONS -----------------------------
String readPage(){
// This function
// 1. read a TXT file in the form "<xy>" and
// 2. extract xy and return them (xy can be "00", "01", "10" or "11")
// 3. or return an error code (example "88") in case reads NULL values
// connectLoop controls the hardware fail timeout
String inString;
int connectLoop = 0;
int stringPos = 0;
Serial.println("Read starting...");
while(client.connected())
{
while(client.available())
{
char inChar = client.read();
delay(1);
inString = inString + inChar;
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}
connectLoop++;
// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println("Timeout");
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
}
Serial.println();
Serial.println("Read finished.");
// close client
client.stop();
delay (1);
// extract and return valid LED values ("00", "01", "10" or "11") or "Error"
if (inString.length() != 0){ // if inString is not null
int a = inString.indexOf('<'); // find the position of "<"
int b = inString.indexOf('>'); // find the position of ">"
String extracted = inString.substring(a+1,b); // extract the text between "<" and ">"
delay(1000);
return extracted; // return valid values
} else {return "88"} // return some error code
}