Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By parasbhanot
#43394 hello everyone,
I am trying to GET a TalkBack Command on Thingspeak.com using Arduino IDE but i am not getting any response on Serial Monitor. I have tested the following GET request with the Poster. it works perfectly.

https://api.thingspeak.com/talkbacks/74 ... FUJBI6Z115


Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "XXX";
const char* password = "YYYYY";

const char* host = "api.thingspeak.com";

// Use WiFiClient class to create TCP connections
WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(100);

  // 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());
}


void loop() {

  Serial.print("connecting to ");
  Serial.println(host);

if (client.connect(host,80)) { // "184.106.153.149" or api.thingspeak.com

String postStr = "GET /talkbacks/7441/commands/1145081?api_key=WSMCMVFUJBI6Z115 HTTP/1.1\n";

client.print(postStr);
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
}

  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
   
    char c = client.read();
    Serial.print(c);
  }

  client.stop();
 
  Serial.println();
  Serial.println("closing connection");
  delay(15000);

}


i think something is wrong with my GET request
What is the correct way to send Talkback GET request ?
User avatar
By martinayotte
#43415 After the connect, you are checking client.available() and if nothing received yet you do a client.stop().
Don't expect a remote server to answer within microseconds, you need to have a delay between request and answer.
Personally, I don't simply add a dummy delay, but I'm doing a timeout loop such as :
Code: Select all    int timeout = millis() + 5000;
    client.print("GET /search?q=arduino HTTP/1.1\r\nHost: google.com\r\n\r\n");
    while (client.available() == 0) {
      if (timeout - millis() < 0) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }
    int size;
    while ((size = client.available()) > 0) {
      uint8_t* msg = (uint8_t*)malloc(size);
      size = client.read(msg, size);
      Serial.write(msg, size);
      free(msg);
    }
    client.stop();
    return;
User avatar
By Devx
#51886 I made a correct thingspeak get request using this code

Code: Select all  client.print("GET /channels/"+my_channel_id+"/fields/"+field_no+"/last?key="+apiKey+  " HTTP/1.1\r\n" +
               "Host: api.thingspeak.com" + "\r\n" +
               "Connection: close\r\n\r\n");


fill in channe_id, field_number and apikey, this will fetch the LAST entry in that field and Martin is right about remote server. You can use following function just after the above code to receive from thingspeak

Code: Select all
String Get_reply (void)
  {
    String reply="";
      while(!client.available())
     {
        Serial.print('.');
        delay(200);         //could be reduced
     }

     while (client.available())
    {
       char c = client.read();
       Serial.print(c);
        reply.concat(c);
    }
      return(reply);
  }