-->
Page 1 of 3

*Solved* Using GET command to get data from ThingHTTP

PostPosted: Wed May 06, 2015 8:52 am
by oeyhaga
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 :)

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");
}

Re: Trying to GET data from ThingHTTP

PostPosted: Wed May 06, 2015 11:50 am
by torntrousers
I tried your code and also got nothing back. SO i went to the GET url in a browser http://api.thingspeak.com/apps/thinghtt ... HX9QN9SVOW and it says "Invalid API Key"

Re: Trying to GET data from ThingHTTP

PostPosted: Wed May 06, 2015 1:57 pm
by oeyhaga
Seems that you did'nt copy the entire url.
The url in the code is: http://api.thingspeak.com/apps/thinghttp/send_request?api_key=PAW43HX9QN9SVOW1

If I change host and url to something else like turay.ddns.net/data.php the code works fine.

Maybe ThinkHTTP require something more in the GET command line?

Re: Trying to GET data from ThingHTTP

PostPosted: Thu May 07, 2015 4:27 pm
by oeyhaga
Seem I'v solved the problem. As easy as adding a longer delay after GET command.
Changed delay(10); to delay(500)

Finaly I can continue with the project after many hours scratching my head :P

Code: Select all#include <ESP8266WiFi.h>
 
const char* ssid     = "NAME";
const char* password = "PASSWORD";
 
const char* host = "api.thingspeak.com";
 
int value = 1;
 
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());
 
}//end setup
 
void loop() {
  delay(5000);
   
  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(host + url);
  Serial.println(String("TRY: ") + value + ".");
 
  // This will send the request to the server
 Client.print(String("GET ") + url + "&headers=false" + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
  delay(500);
 
  // Read all the lines of the reply from server and print them to Serial
  while(Client.available()){
  String line = Client.readStringUntil('\r');
  Serial.println(line);
 }
  Serial.println("");
  Serial.println(String("Try nr. ") + value + " is finished.");
  Serial.println("Waiting for next try...");
  Serial.println("");
  value = value + 1;
  delay(20000);
}