Reading XML from api with ESP8266
Posted: Sat Jul 18, 2015 6:21 am
Hey everyone, I'm trying to parse some xml text from timezonedb.com to get the current timezone offset and daylight savings time for my clock. I'm getting 403 forbidden requests whenever I try get requests. Can anyone shed any light on this?
Code: Select all
void connectToDSTServer() {
String GETString;
// attempt to connect, and wait a millisecond:
IPAddress DSTServerIP = dns;
int rc = WiFi.hostByName(DSTTimeServer, DSTServerIP);
Serial.println("Connecting to DST server");
Serial.println(DSTServerIP);
DSTclient.connect(DSTServerIP, 80);
if (DSTclient.connect(DSTServerIP, 80)) {
// make HTTP GET request to twitter:
GETString += "GET /?lat=";
GETString += latitude;
GETString += "&lng=";
GETString += longitude;
GETString += "&key=N9XTPTVFZJFN\r\n";
Serial.println(GETString);
DSTclient.print(GETString + " HTTP/1.1\r\n");
DSTclient.print("Host: api.timezonedb.com\r\n");
DSTclient.print("Accept: */*\r\n");
DSTclient.print("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n");
DSTclient.print("Connection: close\r\n\r\n");
int i=0;
while((!DSTclient.available()) && (i<1000)){
delay(10);
i++;
}
}
}
void readDSTtime() {
String currentLine = "";
bool readingUTCOffset = false;
String UTCOffset;
connectToDSTServer();
Serial.print("DST.connected: ");
Serial.println(DSTclient.connected());
Serial.print("DST.available: ");
Serial.println(DSTclient.available());
while(DSTclient.connected()) {
if(DSTclient.available()) {
// read incoming bytes:
char inChar = DSTclient.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
Serial.println(currentLine);
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("<gmtOffset>")) {
// tweet is beginning. Clear the tweet string:
Serial.println(currentLine);
readingUTCOffset = true;
UTCOffset = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingUTCOffset) {
if (inChar != '<') {
UTCOffset += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingUTCOffset = false;
Serial.print("UTC Offset in seconds: ");
Serial.println(UTCOffset);
// close the connection to the server:
DSTclient.stop();
}
}
}
}
}