So I’m trying to get information from the openweathermap api (in XML), and I seem to be missing information in the reply. I’ve tried introducing longer delays, and more checks to make sure the client data is available, and nothing seems to be working. Does anyone have any ideas? Here’s the XML that it’s retrieving. Here’s the basic code I’m using:
int noCharCount = 0;
const char* server="api.openweathermap.org";
const char* url="/data/2.5/weather?lat=38&lon=-74.43&mode=xml&units=imperial";
// Start ethernet client
TCPClient client;
int bytesAvail=0;
int bytesPrevAvail=-1;
void setup()
{
pinMode(D7,OUTPUT); // Turn on the D7 led so we know it's time
digitalWrite(D7,HIGH); // to open the Serial Terminal.
Serial.begin(9600);
while(!Serial.available()) // Wait here until the user presses ENTER
SPARK_WLAN_Loop();
Serial.println("connecting...");
digitalWrite(D7,LOW);
Serial.println(server);
Serial.println("Version 5");
Serial.println(url);
if (sendGetRequest(server,url)) {
Serial.println("connected");
} else {
Serial.println("connection failed");
}
}
void loop() {
bytesAvail=client.available();
while ((bytesAvail != bytesPrevAvail) || (bytesAvail<=0)) {
Serial.println(bytesAvail);
bytesPrevAvail=bytesAvail;
delay(1000);
bytesAvail=client.available();
}
Serial.print("bytes:");
Serial.println(bytesAvail);
//Serial.println(client.available());
//delay(1000);
// Read serial data in from web:
while (client.connected()) {
while (client.available()) {
Serial.print((char) client.read());
noCharCount = 0;
}
delay(10);
noCharCount++;
if(noCharCount > 3000)
{
Serial.println();
Serial.println("Timeout");
client.stop();
}
}
bytesAvail=0;
bytesPrevAvail=-1;
if (!client.connected()) {
//Serial.println();
Serial.println("Disconnected");
client.stop();
// Time until next update
Serial.println("Waiting");
for (int t = 1; t <= 15; t++) {
delay( 1000);
}
if (sendGetRequest(server,url)) {
Serial.println("Reconnected");
// delay(2000);
} else {
Serial.println("Reconnect failed");
}
}
}
bool sendGetRequest(const char * server, const char * url)
{
if (client.connect(server, 80)) {
client.print("GET ");
client.print(url);
client.println(" HTTP/1.1");
//client.println("Connection: close");
client.print("Host: ");
client.println(server);
client.println("User-Agent: arduino-ethernet");
client.println("Accept: application/xml, text/xml, */*");
client.println("Connection: close");
//
client.println();
//client.flush();
return true;
}
else return false;
}
And here’s the output that I’m getting over serial (minus the debugging stuff)
e value="59.3" min="59.3" max="59.3" unit="fahrenheit"/>
<humidity value="93" unit="%"/>
<pressure value="1024.92" unit="hPa"/>
<wind>
<speed value="6.5" name="Moderate breeze"/>
<direction value="192.51" code="SSW" name="South-southwest"/>
</wind>
<clouds value="44" name="scattered clouds"/>
<precipitation mode="no"/>
<weather number="802" value="scattered clouds" icon="03d"/>
<lastupdate value="2014-05-22T15:06:32"/>
</current>
Thanks for any help!