0
votes

I'm using an Arduino Uno and ethernet shield to pull xml data from OpenWeatherMap.org. I have the api link and a key, and it all works fine on the web browser. My code is below, however when I get to reading the response from the GET command, all i get is "ΓΏ", not anything I would expect.

I can connect to the arduino when using it as a server, and I can ping it from command prompt so I know it is on the network and the board is not at fault, so it must be something with my code. I have followed the tutorial on the arduino site and loaded the example to GET data, but this also does not work, all I get is a "did not connect" message.

Can someone please look over my code and see what I am doing wrong?

EDIT: I have added the extra loop to print all the xml data, however the program still gets stuck on the while(!client.available());. If i comment it out, I get to the "waiting for server response" but never any further than that. I have checked that the arduino si on the same subnet mask as all the other devices in the network.

// Based on:
// Read Yahoo Weather API XML
// 03.09.2012
// http://forum.arduino.cc/index.php?topic=121992.0
//


#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>



int cityID=2644487; //Lincoln, UK
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
byte ip[] = {192, 168, 1, 89};
byte gateway[] = {192, 168, 1, 254};
byte subnet[] = {255, 255, 255, 0};
//Open weather map xml
char server[] = "http://api.openweathermap.org";
int port = 80; //usually 80 for http.
char APIkey[33] = "HIDDENAPIKEY";


EthernetClient client;
char temperature[30];


void setup()
{
  pinMode(10, OUTPUT);
  digitalWrite(10,HIGH);
  Serial.begin(9600);
  Serial.println("Initialising...");

  // Start Ethernet
  if(!Ethernet.begin(mac)){//if DHCP does not automatically connect
     Serial.println("Invalid Connection");
  }


    Serial.println("");
    Serial.print("Connecting to OWM server using cityID: ");
    Serial.println(cityID);
    Serial.print("Using API key: ");
    Serial.println(APIkey); 

  if (client.connect(server,port))
  {
    client.println("GET /data/2.5/weather?id=2644487&appid=HIDDENAPIKEY&mode=xml&units=metric HTTP/1.1");
    client.println("HOST: api.openweathermap.org");
    client.println();
    Serial.println("Connected to XML data.");

   while(!client.available()); //wait for client data to be available
   Serial.println("Waiting for server response...");
   while(client.available()){

      char c = client.read();
      Serial.println(c);
   }
  } 


}

void loop()
{

}
1
Why you print single character only? You have to get entire response to see what happens. Keep in mind that you need to parse out headers from response. To find this, search for \r\n\r\n sequence. After that you have your body data with XML – tilz0R
Is your computer on the same network and able to access the internet? If so, please go to cmd and type in the command "ipconfig" . Check the subnet mask on your Arduino and be sure that it matches your computer. If it is different than other parts of your network, you could have only partial connectivity. – mberna
Thanks, I have changed the code after your suggestion. I have also checked the subnet mask, all devices are on the same subnet, I've edited the original post to explain. – quackers1

1 Answers

0
votes

For start, your Host header is wrong, use only domain name:

client.println("HOST: api.openweathermap.org");

Second problem I see here is that you do not read entire response from server but only one char. You have to wait for response data to be available, after that read all the data and parse it or print at start.

This part is wrong:

char c = client.read();
Serial.println(c);

Should be something similar to:

while (!client.available());  //Wait client data to be available
while (client.available()) {  //Print all the data
    char c = client.read();
    Serial.println(c);
}