0

My Code is as follows->

#include <SoftwareSerial.h>

SoftwareSerial ESP8266(8,9);

String inData;
void setup() {
ESP8266.println("AT");
delay(1000);
ESP8266.println("AT+RST");

}

void loop() {
 String cmd = "AT+CIPSTART=\"TCP\",\"";
 cmd += "api.thingspeak.com";  // 184.106.153.149 thingspeak IP address
 cmd += "\",80";
 ESP8266.println(cmd);

 if(ESP8266.find("Error"))
 {
   Serial.println("AT+CIPSTART error");
   return;
}
String hostt = "GET /apps/thinghttp/send_request?api_key=689WYA85WW1VJSBA";
hostt += "\r\n";
hostt += "Host:api.thingspeak.com";
hostt += "\r\n\r\n\r\n\r\n\r\n\r\n";
ESP8266.println(hostt);

cmd = "AT+CIPSEND=";
cmd += String(hostt.length());
ESP8266.println(cmd);

if(ESP8266.find(">")){
ESP8266.print(hostt);
}
else{
ESP8266.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
delay(5000);
}

Is this the write code? Where am I going wrong? I am not able to see the required fetched data. Help me with this.

6
  • You seem to only have half a get request there. Where's the HTTP/1.0 ?
    – Majenko
    May 2, 2016 at 14:08
  • Where you are going wrong is you are not receiving a response. You are sending a request and then just walking away not caring what happens.
    – Majenko
    May 2, 2016 at 14:11
  • Okay, then what more code do I need to write along with this code? So, that I may keep getting the data in void loop() ?
    – Abhay Bh
    May 3, 2016 at 5:24
  • Okay. Guys, After obtaining this '>' symbol, I inserted GET /HTTP/1.1 and pressed Enter and then typed api.thingspeak.com/apps/thinghttp/send_request?api_key=************** .Now I got the Output as +IPD,728: <!DOCTYPE html> <html> <head> <title>The page you were looking for doesn't exist (404)</title> </head> <body> <!-- This file lives in public/404.html --> <div class="dialog"> <h1>The page you were looking for doesn't exist.</h1> <p>You may have mistyped the address or the page may have moved.</p> </div> </body> </html>
    – Abhay Bh
    May 3, 2016 at 9:10
  • Now what? Where am I going wrong ?
    – Abhay Bh
    May 3, 2016 at 9:11

1 Answer 1

0

There are two basic things you are doing wrong:

Firstly your HTTP request is wrongly formatted. You have:

GET /apps/thinghttp/send_request?api_key=689WYA85WW1VJSBA
Host:api.thingspeak.com

What it should look like is:

GET /apps/thinghttp/send_request?api_key=689WYA85WW1VJSBA HTTP/1.0
Host: api.thingspeak.com:80

Also you only need one blank line after your request, not 5 as you seem to have.

Secondly you are not actually doing anything with the data the site is sending you. You make the request with +CIPSEND= but then the response, which is in the form +IPD,<len>:<data> is completely ignored. You need to use the SoftwareSerial's .read() and associated functions to receive that data into the Arduino so you can actually do something with it - like send it down through Serial.

Tips:

  • Look for +IPD, first
  • Read up to the : and interpret it as a number
  • You now now how many bytes to read from the data section
  • Read that data
  • Do something with it
9
  • Okay. Thanks @majenko for the reply . What is the proper Syntax to send a proper HTTP request. Can you help me with it. ?
    – Abhay Bh
    May 3, 2016 at 9:39
  • That should actually have been a comment but the really annoying Android app is crap. You hav ethe right format in your code, but not in your manual test.
    – Majenko
    May 3, 2016 at 9:40
  • I am sorry, I don't understand. I am a Newbie. So, just help me with what should I write after the > Symbol , so that I can get my desired output? Help me with the syntax
    – Abhay Bh
    May 3, 2016 at 9:43
  • You have seen the kind of thing it responds with in your test... +IPD,<len>:<data>. You need to use the serial read commands to read that data in and interpret it so you can then display it how you want. At the moment the response is going nowhere because you just aren't reading it. I suggest you find a few more tutorials that show you how.
    – Majenko
    May 3, 2016 at 9:45
  • Yes, I will refer to how to Serial.read() my required command from that data after +IPD,<len> . Thanks for the heads up. And, just one question, to you. -> Now whenever I type : GET /HTTP/1.1 and then after \n\r I type in this: api.thingspeak.com/apps/thinghttp/send_request?api_key=VD9G5A50GGKH01J0 , why does it say "the Webpage is not available ? " I even tried googling for this, but didn't find any hopeful answer.
    – Abhay Bh
    May 3, 2016 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.