*Solved* Using GET command to get data from ThingHTTP
Posted: Wed May 06, 2015 8:52 am
Having problems with my code. Trying to use code from adafruit to get data from my ThingSpeak app. Does not seem to work. It connects, but returs no data. Im very new at programming so prob a stupid mistake or something...
Hope someone can help
Hope someone can help
Code: Select all
/*
* Simple HTTP get webclient test
*/
#include <ESP8266WiFi.h>
const char* ssid = "MYNET";
const char* password = "123456789";
const char* host = "api.thingspeak.com";
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/apps/thinghttp/send_request?api_key=PAW43HX9QN9SVOW1";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.0\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}