Author Topic: How to send GET request with esp8266  (Read 4939 times)

0 Members and 1 Guest are viewing this topic.

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
How to send GET request with esp8266
« on: March 17, 2016, 01:02:05 pm »
hello everyone,
I am trying to GET a TalkBack Command 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/7441/commands/1145081?api_key=WSMCMVFUJBI6Z115

Code: [Select]
#include <ESP8266WiFi.h>

const char* ssid     = "xxxx";
const char* password = "yyyy";

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 ?
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: How to send GET request with esp8266
« Reply #1 on: March 17, 2016, 01:41:04 pm »
That means the connect call fails.
Put the url in with the host string? (I have no idea how that API works...)
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline borjam

  • Supporter
  • ****
  • Posts: 908
  • Country: es
  • EA2EKH
Re: How to send GET request with esp8266
« Reply #2 on: March 17, 2016, 02:35:20 pm »
client.print(postStr);
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");

Try adding an extra newline at the end. Either add an extra \n after Connection: close\n or, to make your intent more clear, add an extra client.print("\n")

 

Offline linux-works

  • Super Contributor
  • ***
  • Posts: 1999
  • Country: us
    • netstuff
Re: How to send GET request with esp8266
« Reply #3 on: March 17, 2016, 02:50:50 pm »
perhaps it will work better with char* instead of String type?

Offline parasbhanotTopic starter

  • Contributor
  • Posts: 16
  • Country: in
Re: How to send GET request with esp8266
« Reply #4 on: March 17, 2016, 04:39:46 pm »
Problem solved  :phew:

i have to remove header from GET request and neglect one empty line to get the actual response.

This is my final code : :popcorn:

Code: [Select]
#include <ESP8266WiFi.h>

const char* ssid     = "xxxxx";
const char* password = "yyyyy";

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

String TalkBackID ="7441";
String TalkBackAPIKey ="WSMCMVFUJBI6Z115";
String Command_ID ="1145081";

String url = "/talkbacks/"+TalkBackID+"/commands/"+Command_ID+"?api_key="+TalkBackAPIKey;


// 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.print("IP address: ");
  Serial.println(WiFi.localIP());
}


void loop() {

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

  if (client.connect(host, 80)) {
    Serial.println("Tcp Connection eastablished with thingSpeak API");
  }else {
    Serial.println("connection failed");
    return;
  }
 
  client.print(String("GET ") + url + "&headers=false" + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");

  delay(500); // apparently some timeout is needed here
 
  String messageBody = "";
  while (client.available()) {
    String line = client.readStringUntil('\n');
    if (line.length() == 1) { //actual content starts after empty line (that has length 1)
      messageBody = client.readStringUntil('\n');
      break;
    }
  }
 
  Serial.print("Found a message, the message is: ");
  Serial.print(messageBody);

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

}
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf