Hi.
I am working on a project where I periodically send data to a google app engine using an Arduino Uno and a mounted WiFi shield.
Here is my loop code:
void loop()
{
now = rtc.now();
Serial.println("Loop: "+getFormatedDateTime());
if(then.unixtime() < now.unixtime())
{
Serial.println("Measurement: "+getFormatedDateTime()+ " Number of measurements done: "+numberOfMeasurementsDone);
//Irms0 = 0.0; // Irms for phase 1
//Irms1 = 0.0; // Irms for phase 2
//Irms2 = 0.0; // Irms for phase 3
Irms0 += emon0.calcIrms(1480)*230.0; // Calculate Irms for phase 1
Irms1 += emon1.calcIrms(1480)*230.0; // Calculate Irms for phase 2
//Irms2 = emon2.calcIrms(1480)*230.0; // Calculate Irms for phase 3
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("Total: ");
lcd.print(Irms0+Irms1);
lcd.setCursor(10,1);
lcd.print(numberOfMeasurementsDone);
numberOfMeasurementsDone++;
//time = millis(); //Replaced by the RTC
then = rtc.now();
}
delay(450);
// Modulus of 10 means we send every ten seconds when the second shows 10, 20, 30 etc.
// The second argument is to ensure that it does not send two times in a row.
// Since the delay is 450 it can happen that "now.unixtime()" shows the same for two
// runs of the loop. numberOfMeasurement can only increment one time per second.
if(now.unixtime() % 10 == 0 && numberOfMeasurementsDone > 3)
{
Serial.println("Send: "+getFormatedDateTime());
long totalEnergyUsed = Irms0 + Irms1; // +Irms2
// watt-hour = watts * hours
// KILO watt-hour = (watts * hours) / 1000
// MOVE TO THE SERVER SIDE? Much easier to do it there.
// I need to convert double to String to be able to send the info to the server.
//double kilowatts = (totalEnergyUsed * 0.016666666666) / 1000;
String data = String("houseId="+houseId+"&timeStamp="+now.unixtime()+"&electricityUsed="+totalEnergyUsed+"&electricityUsedBlib=-1");
Serial.println("Data: "+data);
unsigned long t = millis();
if(client.connect(server,port))
{
Serial.println("connected to server");
client.println("POST /XXXXXXXXXXXXX HTTP/1.1");
//client.println("Host: www.XXXXXXX.com");
client.println("Host: XXXXXXXXXXXXXX");
client.println("Connection: keep-alive");
client.print("Content-Length: ");
client.println(data.length());
client.println("Cache-Control: max-age=0");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Accept-Language: en-US,en;q=0.8");
client.println();
client.print(data);
client.println();
}
Serial.print("Client method time: ");
Serial.println(millis()-t);
/* Only for debugging
while (client.available()) {
char c = client.read();
Serial.write(c);
}
*/
Irms0 = 0;
Irms1 = 0;
Irms2 = 0;
totalEnergyUsed = 0;
numberOfMeasurementsDone = 0;
then = rtc.now();
Serial.println("Finished send: "+getFormatedDateTime());
}
}
In the loop() I take measurement every second using the real time clock to do it accurately. Then every tenth second I send the data to my server.
The WiFi shield takes on average 3 seconds to send the data! I am not sure why it takes so long time. I really thought that in worst case scenario it would take something like 500ms.
When I ping the server it takes on average 28ms.
Is there something I am doing wrong? Have other people also had this experience when sending data to a server?