Post topics, source code that relate to the Arduino Platform

User avatar
By tommi
#30882 Hello everyone,

I have been playing with the ESP8266 and it is really cool. Lately I am trying to interface with Thingspeak.com, which was partly successful. As I am not that familiar with http POST and GET requests I am somewhat flying blindly.

I was able to upload data to thingspeak, basically using the example from arduinoesp.com. Now, I am trying to read values from thingspeak via talkbackin order to control my device.

So what I need is to execute a command like this:
Code: Select allGET https://api.thingspeak.com/talkbacks/11111/commands/18?api_key=XXXXXX


or this:

Code: Select allPOST https://api.thingspeak.com/talkbacks/11111/commands/execute
     api_key=XXXXXXX



and get the returned value. I tried a couple of things, but could not get it to work. The internet mostly talks about uploading data to thingspeak, however was not able to find an example for the opposite direction.

Any ideas would be really helpful!

Thanks,
Tommi
User avatar
By Mmiscool
#36037 I have been using the following the following arduino code with thing speak for the basic interpreter project.

Posting to thingspeak.
Code: Select allFetchWebUrl(String("api.thingspeak.com/update?key=" + Param0 + "&field" + Param1 + "=" + Param2));



Reading a filed from thingspeak
Code: Select allMyOut =  FetchWebUrl(String("api.thingspeak.com/channels/" + Param1 + "/field/" + Param2 + "/last.xml?api_key=" + Param0));
    MyOut = MyOut.substring(MyOut.indexOf(String("<field" + Param2 + ">") ) + 8, MyOut.indexOf(String("</field" + Param2 + ">")));



function to fetch a url.
Code: Select all#include <WiFiClient.h>
WiFiClient client;

String FetchWebUrl(String URLtoGet)
{
  String str = "             ";
  String ServerToConnectTo = URLtoGet.substring(0, URLtoGet.indexOf("/"));
  String PageToGet = URLtoGet.substring(URLtoGet.indexOf("/"));
  // ServerToConnectTo ;
  //PageToGet = URLtoGet.substring(URLtoGet.indexOf("/"));

  Serial.println(ServerToConnectTo);
  Serial.println(PageToGet);


  if (client.connect(ServerToConnectTo.c_str() , 80))
  {
    client.print(String("GET " + PageToGet + " HTTP/1.1\r\nHost: " +  ServerToConnectTo + "\r\n\r\n"));
    delay(300);
    while (client.available())
    {
      delay(0);
      if (str.endsWith(String("\r\n\r\n")))  str = "";

      str.concat( (const char)client.read());
      delay(0);
    }



    client.stop();
    return str.substring(0, str.indexOf(String(String(char(10)) + "0" )  ));
  }
  client.stop();
  return "";
}