ESP8266: HTTP GET Requests

The objective of this post is to explain how to perform GET Requests using the ESP8266.


Introduction

The objective of this post is to explain how to perform GET Requests using the ESP8266. If you prefer a video explanation, you can check my YouTube channel:

The tests were performed using a NodeMCU board, a very cheap and easy to use ESP8266 board.


Setup

First, we need do include some libraries. Naturally, we need the ESP8266WiFi library, which provides us the methods to connect to a WiFi network.

Then, we need the ESP8266HTTPClient library, which provides methods to send HTTP requests. The header file for the ESP8266HTTPClient library can be seen here.

#include <esp8266wifi.h>
#include <esp8266httpclient.h>

On the setup function, we connect to the WiFi Network. We also start a Serial connection for debugging. More details about how to connect to a WiFi network using the ESP8266 are explained in this previous post.

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

void setup () {

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);
Serial.print("Connecting..");

}

}


The main code

The code for the request will be specified in the main loop function. First, we declare an object of class HTTPClient, which we will simply call http. This class provides the methods to create and send the HTTP request.

HTTPClient http;

After that, we call the begin method on the http object and pass the URL that we want to connect to and make the GET request. The destination website specified here implements a dummy REST API for testing and prototyping.

http.begin("http://jsonplaceholder.typicode.com/users/1");

Then, we send the request by calling the GET method on the http object. This method will return the status of the operation, which is important to store for error handling. If the value is greater than 0, then it’s a standard HTTP code. If the value is less than 0, then itโ€™s a client error, related with the connection. All available error codes for this method are listed here.

int httpCode = http.GET();

So, if the code is greater than 0, we can get and print the response payload, by calling the getString method on the http object.

String payload = http.getString();
Serial.println(payload);

Finally, we call the end method. This is very important to close the TCP connection and thus free the resources.

http.end();

The final complete code is shown bellow. You just need to put your network name and network password.

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

void setup () {

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);
Serial.print("Connecting..");

}

}

void loop() {

if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

HTTPClient http;  //Declare an object of class HTTPClient

http.begin("http://jsonplaceholder.typicode.com/users/1");  //Specify request destination
int httpCode = http.GET();                                                                  //Send the request

if (httpCode > 0) { //Check the returning code

String payload = http.getString();   //Get the request response payload
Serial.println(payload);                     //Print the response payload

}

http.end();   //Close connection

}

delay(30000);    //Send a request every 30 seconds

}

The output of the program is shown in figure 1. The response corresponds to a JSON payload.

ESP8266 GET Request

Figure 1 – Output of the GET Request.

Finally, it’s important to take in consideration that the microcontroller has a limited amount of resources and thus it is not able to handle very large results. So, it is not expected that it will be used to make and process requests to sites that return a lot of information, as a typical browser would do.


Technical details

ESP8266 libraries: v2.3.0

311 Replies to “ESP8266: HTTP GET Requests”

    1. Hi! It doesn’t show anything after making the HTTP request or even at the beginning, when connecting to the WiFi network?

      If you don’t see a โ€œConnecting..โ€ message right after uploading the code, it’s probable that there is some problem with the hardware or the baud rate of the serial monitor is not matching the baud rate defined in the code. Is your serial monitor configured for 115200 baud?

      If it is after the request, it may have been some temporary problem with the destination website. Can you access the URL directly in your web browser?

      If you changed the example website provided in this tutorial to other website that returns a lot of content as response to the HTTP request, then the ESP may have not been capable of handling so much data.

        1. Hi,

          You are trying to do an HTTPS request, which means the connection is secured (the S stands for secure).

          The tutorial is applicable to HTTP requests, which is why it is not working for the URL you mentioned.

          I haven’t yet tried HTTPS on the ESP8266 but there was an example in the Arduino libraries:
          https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino

          https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/client-secure-examples.rst

          I remember that at the time the HTTPS support was very limited on the ESP8266 due to memory constraints. For example, it was not possible to validate the whole certificate chain, which is crucial when working with HTTPS.

          I don’t know if things have evolved since then on the ESP8266, but I can confirm however that HTTPS works well on the ESP32:
          https://techtutorialsx.com/2017/11/18/esp32-arduino-https-get-request/

          The ESP32 is more capable for handling the computation and memory requirements needed for secure connections, so if you are looking for something scalable which needs HTTPS I would recommend you to check the ESP32.

          Hope this helps,
          Nuno Santos

    2. Hello,
      I tried the code but the payload is empty, I tried using the browser and it shows the information as decribed in this webpage, what could be wrong ?

      1. Hi!

        Did you run the code multiple times? maybe it was a temporary unavailability on the website. Or is it occurring always?

        Are you able to send requests to other websites with the code from the tutorial?

    1. Hi! It doesn’t show anything after making the HTTP request or even at the beginning, when connecting to the WiFi network?
      If you don’t see a โ€œConnecting..โ€ message right after uploading the code, it’s probable that there is some problem with the hardware or the baud rate of the serial monitor is not matching the baud rate defined in the code. Is your serial monitor configured for 115200 baud?
      If it is after the request, it may have been some temporary problem with the destination website. Can you access the URL directly in your web browser?
      If you changed the example website provided in this tutorial to other website that returns a lot of content as response to the HTTP request, then the ESP may have not been capable of handling so much data.

        1. Hi,
          You are trying to do an HTTPS request, which means the connection is secured (the S stands for secure).
          The tutorial is applicable to HTTP requests, which is why it is not working for the URL you mentioned.
          I haven’t yet tried HTTPS on the ESP8266 but there was an example in the Arduino libraries:
          https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
          https://github.com/esp8266/Arduino/blob/master/doc/esp8266wifi/client-secure-examples.rst
          I remember that at the time the HTTPS support was very limited on the ESP8266 due to memory constraints. For example, it was not possible to validate the whole certificate chain, which is crucial when working with HTTPS.
          I don’t know if things have evolved since then on the ESP8266, but I can confirm however that HTTPS works well on the ESP32:
          https://techtutorialsx.com/2017/11/18/esp32-arduino-https-get-request/
          The ESP32 is more capable for handling the computation and memory requirements needed for secure connections, so if you are looking for something scalable which needs HTTPS I would recommend you to check the ESP32.
          Hope this helps,
          Nuno Santos

    2. Hello,
      I tried the code but the payload is empty, I tried using the browser and it shows the information as decribed in this webpage, what could be wrong ?

      1. Hi!
        Did you run the code multiple times? maybe it was a temporary unavailability on the website. Or is it occurring always?
        Are you able to send requests to other websites with the code from the tutorial?

  1. Pingback: techtutorialsx
  2. Pingback: techtutorialsx
    1. Hi! This already has the full code. Do you mean putting it all together from the beginning to the end in a section of the post, like I’ve been doing in more recent posts? If so, sure, I will update the post as soon as I can with a section listing all the code.

    1. Hi! This already has the full code. Do you mean putting it all together from the beginning to the end in a section of the post, like I’ve been doing in more recent posts? If so, sure, I will update the post as soon as I can with a section listing all the code.

      1. I would like to upload sensor readings to mysql via esp8266. I want to upload those readings per second. Is it possible? Thank you for your reply!

        1. Hi, I think there is no problem with sending a request per second. Nevertheless, if you need to guarantee that rate, maybe you should go to something a little more optimized, such as direct socket connection. That way, you don’t have the overhead of the HTTP protocol and the need to connect / disconnect each time you are sending a request. With the socket, you can leave it open while you need.

          I don’t know of a way to directly send the data to a SQL database using the ESP. The protocols used to access the DB are different, unless you can find a way to expose the database functionality directly over HTTP.

          So, for using the code shown here, the easiest way is to develop a server application (for example, in Python or Java) that receives the data from the ESP via HTTP and then inserts it on the MySQL database using a suitable protocol.

          Hope it helps

          1. Thank you for your reply!
            I am using php to insert data into mysql. The process of sending a request is about 2 to 3 seconds. The is not what i want. Direct socket connection? How to use it?

            1. Hi!

              Sockets are lower level mechanisms to allow computers (and now, microcontrollers such as the ESP8266) to communicate through a network. So, HTTP is actually a protocol that works on top of socket communication. You can read a lot about sockets around the web, it’s a very well known and documented mechanism.

              I have no tutorial yet on how to use them, but in the libraries for the Arduino IDE there is a very nice example that you can adapt, for the ESP8266:
              https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino

              But please note that in the example they are building the HTTP protocol by hand, on top of the socket connection, which would have the same result. In your case, moving to socket oriented communication with increased performance would need some changes, both in the ESP code and the server, to keep the connection alive. Probably you may not want to do this kind of changes.

              In your case, it seems that you are having performance issues, maybe with the php server, which you can try to optimize in order to achieve the desired rate. Have you tried to make the request to your php page from other tool, just to confirm if you can do a request per second? That way you can confirm if the problem is on the ESP8266 side or on the php server side.

              Do you insert the data on the database synchronously? If so, you leave the ESP8266 waiting for the procedure to conclude, which takes some time. One approach could be returning an immediate response to the ESP and then inserting the data asynchronously.

              You can also try to measure the execution time of the different procedures you have in your php page, to check where it is taking more time and trying to optimize.

      1. I would like to upload sensor readings to mysql via esp8266. I want to upload those readings per second. Is it possible? Thank you for your reply!

        1. Hi, I think there is no problem with sending a request per second. Nevertheless, if you need to guarantee that rate, maybe you should go to something a little more optimized, such as direct socket connection. That way, you don’t have the overhead of the HTTP protocol and the need to connect / disconnect each time you are sending a request. With the socket, you can leave it open while you need.
          I don’t know of a way to directly send the data to a SQL database using the ESP. The protocols used to access the DB are different, unless you can find a way to expose the database functionality directly over HTTP.
          So, for using the code shown here, the easiest way is to develop a server application (for example, in Python or Java) that receives the data from the ESP via HTTP and then inserts it on the MySQL database using a suitable protocol.
          Hope it helps

          1. Thank you for your reply!
            I am using php to insert data into mysql. The process of sending a request is about 2 to 3 seconds. The is not what i want. Direct socket connection? How to use it?

            1. Hi!
              Sockets are lower level mechanisms to allow computers (and now, microcontrollers such as the ESP8266) to communicate through a network. So, HTTP is actually a protocol that works on top of socket communication. You can read a lot about sockets around the web, it’s a very well known and documented mechanism.
              I have no tutorial yet on how to use them, but in the libraries for the Arduino IDE there is a very nice example that you can adapt, for the ESP8266:
              https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino
              But please note that in the example they are building the HTTP protocol by hand, on top of the socket connection, which would have the same result. In your case, moving to socket oriented communication with increased performance would need some changes, both in the ESP code and the server, to keep the connection alive. Probably you may not want to do this kind of changes.
              In your case, it seems that you are having performance issues, maybe with the php server, which you can try to optimize in order to achieve the desired rate. Have you tried to make the request to your php page from other tool, just to confirm if you can do a request per second? That way you can confirm if the problem is on the ESP8266 side or on the php server side.
              Do you insert the data on the database synchronously? If so, you leave the ESP8266 waiting for the procedure to conclude, which takes some time. One approach could be returning an immediate response to the ESP and then inserting the data asynchronously.
              You can also try to measure the execution time of the different procedures you have in your php page, to check where it is taking more time and trying to optimize.

  3. Hey, I have a working http request working, except what you don’t cover in this tutorial is the ability to use specific json data. I have been searching everywhere to achieve this, but without any results. Can you please help me how I would get the value of a json key, such as street name, and save it in a variable. Thanks in advance!

  4. Hey, I have a working http request working, except what you don’t cover in this tutorial is the ability to use specific json data. I have been searching everywhere to achieve this, but without any results. Can you please help me how I would get the value of a json key, such as street name, and save it in a variable. Thanks in advance!

  5. Hey, I have a working http request working, except what you don’t cover in this tutorial is the ability to use specific json data. I have been searching everywhere to achieve this, but without any results. Can you please help me how I would get the value of a json key, such as street name, and save it in a variable. Thanks in advance!

  6. Hey, I have a working http request working, except what you don’t cover in this tutorial is the ability to use specific json data. I have been searching everywhere to achieve this, but without any results. Can you please help me how I would get the value of a json key, such as street name, and save it in a variable. Thanks in advance!

  7. Hi!

    You can check this previous tutorial on how to parse JSON content on the ESP8266. You can use it to parse incoming JSON content, as a result of your request:
    https://techtutorialsx.com/2016/07/30/esp8266-parsing-json/

    Here is a more advanced tutorial, were is explained how to parse JSON arrays:
    https://techtutorialsx.com/2016/08/06/esp8266-parse-json-arrays/

    In case you need it in the future, here is explained how to encode JSON messages in the ESP8266. You can used to POST content in JSON format:
    https://techtutorialsx.com/2016/08/10/esp8266-encoding-json-messages/

    Please let me know if it helps. Also, let me know if you get any compiling errors. I’ve been changing the aspect of the coding sections of those posts and sometimes WordPress removes stuff between less than and greater than signals, which may cause unexpected errors, such as loosing includes.

  8. Hi!
    You can check this previous tutorial on how to parse JSON content on the ESP8266. You can use it to parse incoming JSON content, as a result of your request:
    https://techtutorialsx.com/2016/07/30/esp8266-parsing-json/
    Here is a more advanced tutorial, were is explained how to parse JSON arrays:
    https://techtutorialsx.com/2016/08/06/esp8266-parse-json-arrays/
    In case you need it in the future, here is explained how to encode JSON messages in the ESP8266. You can used to POST content in JSON format:
    https://techtutorialsx.com/2016/08/10/esp8266-encoding-json-messages/
    Please let me know if it helps. Also, let me know if you get any compiling errors. I’ve been changing the aspect of the coding sections of those posts and sometimes WordPress removes stuff between less than and greater than signals, which may cause unexpected errors, such as loosing includes.

    1. Hi!

      I think the error may be related to the fact that the website you are trying to reach uses HTTPS. This is a secure version of the HTTP protocol and it means that the connections are encrypted.

      The example shown in this tutorial works only for HTTP (the not secure version where the data is sent in plain text)

      I haven’t yet played with HTTPS in the ESP8266, but you have here an example, from the ESP libraries:
      https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino

      Note that in that example the fingerprint of the digital certificate of the website you are trying to reach is hardcoded. If the digital certificate of the website expires, then the code will stop working.

      In real world HTTPS, the client validates the digital certificate of the website (typically your browser is the client) to check if that certificate can be trusted. This is done by contacting a chain of certificate authorities.

      This is a heavy process that would not be trivial to implement in a device with limited resources, such as the ESP8266. I haven’t yet found a library that does this efficiently. Maybe with the new ESP32 this will be easier to implement

      Nevertheless, you can follow the thread about SSL support for the ESP8266 on the github page of the ESP libraries for the Arduino IDE. I opened the issue 2 years ago ๐Ÿ™‚
      https://github.com/esp8266/Arduino/issues/43

    1. Hi!
      I think the error may be related to the fact that the website you are trying to reach uses HTTPS. This is a secure version of the HTTP protocol and it means that the connections are encrypted.
      The example shown in this tutorial works only for HTTP (the not secure version where the data is sent in plain text)
      I haven’t yet played with HTTPS in the ESP8266, but you have here an example, from the ESP libraries:
      https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/HTTPSRequest/HTTPSRequest.ino
      Note that in that example the fingerprint of the digital certificate of the website you are trying to reach is hardcoded. If the digital certificate of the website expires, then the code will stop working.
      In real world HTTPS, the client validates the digital certificate of the website (typically your browser is the client) to check if that certificate can be trusted. This is done by contacting a chain of certificate authorities.
      This is a heavy process that would not be trivial to implement in a device with limited resources, such as the ESP8266. I haven’t yet found a library that does this efficiently. Maybe with the new ESP32 this will be easier to implement
      Nevertheless, you can follow the thread about SSL support for the ESP8266 on the github page of the ESP libraries for the Arduino IDE. I opened the issue 2 years ago ๐Ÿ™‚
      https://github.com/esp8266/Arduino/issues/43

  9. Hi, I tried your code, but my monitor show things like this:
    ———————————————————————————–
    Connecting..
    Connecting..
    Connecting..
    200 // httpCode

    ——————————————————————————————
    My payload is strange, do you know what’s the problem?

  10. Hi, I tried your code, but my monitor show things like this:
    ———————————————————————————–
    Connecting..
    Connecting..
    Connecting..
    200 // httpCode
    ——————————————————————————————
    My payload is strange, do you know what’s the problem?

  11. html
    head
    meta http-equiv=’refresh’ content=’1; url=http://jsonplaceholder.typicode.com/users/3&arubalp=91c4147b-74cb-4485-8af4-a79f9975fc’
    /head
    /html

        1. My browser can receive the payload correctly as shown in the tutorial.The problem is my ESP8266, My URL is correct but my ESP8266 still prints the HTML tags as payload(not the texts my browser shows). I don’t know why.

        2. After refreshing to the new URL, my httpCode change to 302(redirect), with no payload, I currently stuck at here, don’t know how to response to httpCode 302

          1. Hi! That’s really weird, I’ve just copied and pasted the code on my IDE and uploaded it to a ESP8266 NodeMCU board and it works just fine.

            Can you please tell me which version of the Arduino libraries are you using? Can you please also paste the main loop function here?

            Maybe there is a problem with the character encoding or something with the Arduino IDE. May I ask in what language is your IDE? I never faced a problem like this but if you are copying and pasting the code without success, there may be something with the URL characters.

            Can you send requests to other websites?

            1. I finally solve the problem… that all because I connected to a network that is not working, I change another available WiFi and everything just works perfectly. Thanks for helping me with this somehow silly problem ๐Ÿ™‚

              1. Hi! I’m glad the problem is solved ๐Ÿ™‚ Let me know if you need anything more.
                Good programming!

                Best regards,
                Nuno Santos

  12. html
    head
    meta http-equiv=’refresh’ content=’1; url=http://jsonplaceholder.typicode.com/users/3&arubalp=91c4147b-74cb-4485-8af4-a79f9975fc’
    /head
    /html

        1. My browser can receive the payload correctly as shown in the tutorial.The problem is my ESP8266, My URL is correct but my ESP8266 still prints the HTML tags as payload(not the texts my browser shows). I don’t know why.

        2. After refreshing to the new URL, my httpCode change to 302(redirect), with no payload, I currently stuck at here, don’t know how to response to httpCode 302

          1. Hi! That’s really weird, I’ve just copied and pasted the code on my IDE and uploaded it to a ESP8266 NodeMCU board and it works just fine.
            Can you please tell me which version of the Arduino libraries are you using? Can you please also paste the main loop function here?
            Maybe there is a problem with the character encoding or something with the Arduino IDE. May I ask in what language is your IDE? I never faced a problem like this but if you are copying and pasting the code without success, there may be something with the URL characters.
            Can you send requests to other websites?

            1. I finally solve the problem… that all because I connected to a network that is not working, I change another available WiFi and everything just works perfectly. Thanks for helping me with this somehow silly problem ๐Ÿ™‚

              1. Hi! I’m glad the problem is solved ๐Ÿ™‚ Let me know if you need anything more.
                Good programming!
                Best regards,
                Nuno Santos

  13. Hi, I would like to control a LED based on the contents of a google document. I need to read that file- which is basically a single line file. I tried your code and it is working fine. Now the problem is how to get that json parsing line (“http://jsonplaceholder.typicode.com/users/1”). I searched a lot, but in vain. Can you please help me..

    1. Hi! Awesome, never tried to make a request to a google document!

      As for the parsing of JSON, check here in another post how to do it with the ESP8266 and a very easy to use library:
      https://techtutorialsx.com/2016/07/30/esp8266-parsing-json/

      If you need more complex parsing, such as arrays:
      https://techtutorialsx.com/2016/08/06/esp8266-parse-json-arrays/

      If you need the other way around, check here how to encode JSON with the ESP8266:
      https://techtutorialsx.com/2016/08/10/esp8266-encoding-json-messages/

      Hope it helps!

  14. Hi, I would like to control a LED based on the contents of a google document. I need to read that file- which is basically a single line file. I tried your code and it is working fine. Now the problem is how to get that json parsing line (“http://jsonplaceholder.typicode.com/users/1”). I searched a lot, but in vain. Can you please help me..

    1. Hi! Awesome, never tried to make a request to a google document!
      As for the parsing of JSON, check here in another post how to do it with the ESP8266 and a very easy to use library:
      https://techtutorialsx.com/2016/07/30/esp8266-parsing-json/
      If you need more complex parsing, such as arrays:
      https://techtutorialsx.com/2016/08/06/esp8266-parse-json-arrays/
      If you need the other way around, check here how to encode JSON with the ESP8266:
      https://techtutorialsx.com/2016/08/10/esp8266-encoding-json-messages/
      Hope it helps!

  15. Hello
    I just have a small question. How will the code identify the connected pins of the ESP8266 to the Arduino board?

    1. Hi!

      I’m sorry I’m not sure if I understood your question. In this code we don’t use any digital pins of the board.

      If you are asking about using the ESP8266 in the Arduino IDE, then it is done for us in the background, we don’t need to worry about the details. Basically, the Arduino IDE will know that the code is being compiled for the ESP8266, and use the correct compiler / set of tools needed.

      This code was designed to work in a ESP8266 board, not in a Arduino one.

      Hope it answers your question. If not, please let me know.

      1. Yes it did thank you a lot. I mixed it up for I am using the ESP8266 WiFi module connected to an Arduino UNO board. Speaking of which, I stumbled trying to send a GET request but I’m out of luck. It doesn’t seem to work and I’ve tried and searched through many links.
        I successfully connected to a website using “AT+CIPSTART” command but after that I can’t figure out exactly what needs to be done. I used the “AT+CIPSEND=” but I can’t really tell the exact structure of the GET request string before getting its length. I am really sorry I know my problem became off topic with respect to your post but I’m desperate for help and would highly appreciate it.

        1. No problem, let me see if I can help ๐Ÿ™‚

          First of all, what you are seeing here is code for the ESP8266 and not for the Arduino boards, as I’ve mentioned.

          Nevertheless, we are using the Arduino development environment because Ivan Grokhotkov started a project to support the ESP8266 development on the Arduino IDE that has grown a lot with the support of the community. So, we don’t need to worry about setting complicated toolchains and we can use the Arduino language / development environment we are used to, in order to program the ESP8266.

          So, if you want to start programming your ESP8266 from scratch instead of using the pre-loaded AT commands firmware you are using, please consult the tutorial bellow. Just take in consideration that if you upload a program to the ESP8266, the AT commands firmware will be erased.
          https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/

          I really recommend that approach since you will have much more control of your programs and you can develop them to suit your needs.

          I used the AT commands firmware a couple of years ago, when the ESP8266 was still being sold as a Arduino serial to Wifi adapter and we didn’t now it was a fully programmable microcontroller. At that time, the firmware was considerably unstable and so I moved to the Arduino environment, as many others did.

          I only recently used the firmware again to do some tests on a board and I think its much more stable, but I still prefer to develop under the Arduino environment or other, such as mycropython.

          Hope I have convinced you to change to another environment ๐Ÿ™‚ but if not, I think the best way is that you build the HTTP request string in a variable and simply get the size with the function bellow:
          https://www.arduino.cc/en/Reference/StringLength

          You will need to figure out the structure of the whole HTTP request for the site you are trying to reach, put it into a string and then getting the length. If your request is dynamic, you will need to build the string with the different parts and then get the size in the end.

          Note that what the firmware is doing with the AT commands is establishing a socket connection with the server, and you are going to need to send the whole HTTP request on top of that.

          Also, take in consideration that we cannot connect to HTTPS websites, because of the encryption.

          1. Hello again and thanks for your effort in replying.
            I am actually trying to send the http GET request manually through writing the AT commands directly onto the serial monitor before moving to code. Testing wise I found it better this way before
            the implementation of the code. All respective commands for ESP8266-01 – Website communication are done successfully except for the GET request part.
            The first link you put is good in case I am using the ESP8266-01 module as a generic board, but I am not. Actually it’s connected to the RX TX of the Arduino UNO board along with other sensors and components.
            I took the length of the GET request now what remains is actually sending it but once I receive the “>” sign on the serial monitor, I enter the request but it disappears (it doesn’t appear on the monitor when I hit enter) and though it shows “SEND OK” it still did not send anything (the path of the link is a tested PHP script that adds a value to a database).
            Thanks for your advice but unfortunately I’m still out of luck. The website I’m trying to the send the request to is also not encrypted.

            1. Hi! As for the link, as long as you can program the ESP8266, you can develop the architecture of the system as you want. The only difference is that instead of using a pre-built firmware, you develop your own program for the ESP. You can still develop a program to receive data from an Arduino Uno, connect to a website, send the data, receive the response and send it back to the Arduino, I did something similar in a smart medication dispenser I built some time ago, where the Arduino Mega was running all the sensors and the ESP was just dealing with the communication. Nevertheless, I developed a custom program from the ESP, for more control.

              As for your problem with the AT command, maybe it is related with the delay between you sending the CIPSEND command, receive the “>” and then sending the actual data. I used the firmware a long ago for socket connections, but I have a slight idea of facing some problems with the command on the serial monitor.

              Other thing, are you sending the \r\n at the end of the actual data? It may be also needed and also accounted in the size of the content passed to the CIPSEND.

              Also, just for debugging, have you tried to send the command to other website, just for checking if something happens?

              I’ve been also searching for some libraries for the Arduino and I think you can use one similar to the shown bellow to interact with the ESP8266. It hides some of the AT commands details and probably guarantees the synchronization of the send command.
              https://github.com/itead/ITEADLIB_Arduino_WeeESP8266

  16. Hello
    I just have a small question. How will the code identify the connected pins of the ESP8266 to the Arduino board?

    1. Hi!
      I’m sorry I’m not sure if I understood your question. In this code we don’t use any digital pins of the board.
      If you are asking about using the ESP8266 in the Arduino IDE, then it is done for us in the background, we don’t need to worry about the details. Basically, the Arduino IDE will know that the code is being compiled for the ESP8266, and use the correct compiler / set of tools needed.
      This code was designed to work in a ESP8266 board, not in a Arduino one.
      Hope it answers your question. If not, please let me know.

      1. Yes it did thank you a lot. I mixed it up for I am using the ESP8266 WiFi module connected to an Arduino UNO board. Speaking of which, I stumbled trying to send a GET request but I’m out of luck. It doesn’t seem to work and I’ve tried and searched through many links.
        I successfully connected to a website using “AT+CIPSTART” command but after that I can’t figure out exactly what needs to be done. I used the “AT+CIPSEND=” but I can’t really tell the exact structure of the GET request string before getting its length. I am really sorry I know my problem became off topic with respect to your post but I’m desperate for help and would highly appreciate it.

        1. No problem, let me see if I can help ๐Ÿ™‚
          First of all, what you are seeing here is code for the ESP8266 and not for the Arduino boards, as I’ve mentioned.
          Nevertheless, we are using the Arduino development environment because Ivan Grokhotkov started a project to support the ESP8266 development on the Arduino IDE that has grown a lot with the support of the community. So, we don’t need to worry about setting complicated toolchains and we can use the Arduino language / development environment we are used to, in order to program the ESP8266.
          So, if you want to start programming your ESP8266 from scratch instead of using the pre-loaded AT commands firmware you are using, please consult the tutorial bellow. Just take in consideration that if you upload a program to the ESP8266, the AT commands firmware will be erased.
          https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/
          I really recommend that approach since you will have much more control of your programs and you can develop them to suit your needs.
          I used the AT commands firmware a couple of years ago, when the ESP8266 was still being sold as a Arduino serial to Wifi adapter and we didn’t now it was a fully programmable microcontroller. At that time, the firmware was considerably unstable and so I moved to the Arduino environment, as many others did.
          I only recently used the firmware again to do some tests on a board and I think its much more stable, but I still prefer to develop under the Arduino environment or other, such as mycropython.
          Hope I have convinced you to change to another environment ๐Ÿ™‚ but if not, I think the best way is that you build the HTTP request string in a variable and simply get the size with the function bellow:
          https://www.arduino.cc/en/Reference/StringLength
          You will need to figure out the structure of the whole HTTP request for the site you are trying to reach, put it into a string and then getting the length. If your request is dynamic, you will need to build the string with the different parts and then get the size in the end.
          Note that what the firmware is doing with the AT commands is establishing a socket connection with the server, and you are going to need to send the whole HTTP request on top of that.
          Also, take in consideration that we cannot connect to HTTPS websites, because of the encryption.

          1. Hello again and thanks for your effort in replying.
            I am actually trying to send the http GET request manually through writing the AT commands directly onto the serial monitor before moving to code. Testing wise I found it better this way before
            the implementation of the code. All respective commands for ESP8266-01 – Website communication are done successfully except for the GET request part.
            The first link you put is good in case I am using the ESP8266-01 module as a generic board, but I am not. Actually it’s connected to the RX TX of the Arduino UNO board along with other sensors and components.
            I took the length of the GET request now what remains is actually sending it but once I receive the “>” sign on the serial monitor, I enter the request but it disappears (it doesn’t appear on the monitor when I hit enter) and though it shows “SEND OK” it still did not send anything (the path of the link is a tested PHP script that adds a value to a database).
            Thanks for your advice but unfortunately I’m still out of luck. The website I’m trying to the send the request to is also not encrypted.

            1. Hi! As for the link, as long as you can program the ESP8266, you can develop the architecture of the system as you want. The only difference is that instead of using a pre-built firmware, you develop your own program for the ESP. You can still develop a program to receive data from an Arduino Uno, connect to a website, send the data, receive the response and send it back to the Arduino, I did something similar in a smart medication dispenser I built some time ago, where the Arduino Mega was running all the sensors and the ESP was just dealing with the communication. Nevertheless, I developed a custom program from the ESP, for more control.
              As for your problem with the AT command, maybe it is related with the delay between you sending the CIPSEND command, receive the “>” and then sending the actual data. I used the firmware a long ago for socket connections, but I have a slight idea of facing some problems with the command on the serial monitor.
              Other thing, are you sending the \r\n at the end of the actual data? It may be also needed and also accounted in the size of the content passed to the CIPSEND.
              Also, just for debugging, have you tried to send the command to other website, just for checking if something happens?
              I’ve been also searching for some libraries for the Arduino and I think you can use one similar to the shown bellow to interact with the ESP8266. It hides some of the AT commands details and probably guarantees the synchronization of the send command.
              https://github.com/itead/ITEADLIB_Arduino_WeeESP8266

  17. I think my problem is with the ESP8266-01 module itself. I bought 2 so far and couldn’t get them to work. I tried literally everything but the modules I have aren’t of good quality they started lagging and not always they were able to recognize the DNS. My friends are working with the ESP8266 NodeMCU board and I’m thinking of getting one. What’s your opinion? And also does this post should (if not must) work with that latter board right?

    Thank you for your time and help. I am probably switching hardware and thanks for convincing me! (if that’s what you meant earlier by changing to another environment).

    1. Just to clarify a thing, both the ESP8266-01 and the NodeMCU use the same microcontroller, the ESP8266. Those are just different boards built around the same microcontroller.

      So, all the code you see in my blog in the “ESP8266” category should work fine in any of them. Naturally, there may be a detail or another, but I try to leave a warning when we need to consider something additional specific to a certain board.

      The NodeMCU is a very good board and I have been using it for most of my posts on the ESP8266. The advantage is that it already has an USB header and you just need an USB cable to interact with it, which includes programming it.

      Besides that, it has all of the pins of the ESP8266 exposed, so you can use it as an “arduino”. Many people use the ESP8266 just as a Serial to WiFi converter, but the ESP8266 is actually much more powerful than most of the microcontrollers used in the Arduino boards. Most of the things you can do with an Arduino board you can do with an ESP8266 board, with the advantage of having WiFi built in.

      I think it is a very good option for you to move to the NodeMCU, but I also recommend you to program it with the Arduino IDE instead of using the AT commands. That’s what I meant when recommending you to change the environment ๐Ÿ™‚ The AT commands have always been a little unstable and you typically end up with a more complex program because you need to be parsing strings from the answers of the commands.

      The link I shared before with the tutorial to move to the Arduino IDE covers the ESP-01 as hardware because at the time I wrote it it was the most used board. Now with the NodeMCU boards, we really just need an USB cable to connect it to a computer.

      Nevertheless, from the Arduino IDE Configuration section bellow are the generic instructions for installing the ESP8266 support for the Arduino IDE. You will only need to choose the correct board on the boards manager (in your case it will be the NodeMCU) because the upload configurations may vary from board to board. But the IDE takes care of everything as long as we choose the correct board.
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/

      I think it’s still working fine, but the latest instructions from the creators of the ESP8266 libraries for the Arduino IDE are bellow, if you prefer:
      https://github.com/esp8266/Arduino#installing-with-boards-manager

      Let me know if you succeed with the NodeMCU ๐Ÿ™‚

      1. Actually I’ve managed to solve my issue thanks to your advice (really this time). Approaching it by code, the http GET request was sent correctly. I thought it’s a ESP8266-01 since I followed several tutorials that used AT commands and successfully sent a request but mine did not. Eventhough I tried several Arduino IDE approaches (by code) I wasn’t successful at first until now

        Still, I will get a NodeMCU just as a backup plan and I will follow your instructions. Thanks a lot if it wasn’t for you I would’ve have gave it another try with the Arduino IDE.

        Here’s the link that I followed http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/#comment-5758

        Now that remains is that I need to get the response from the ESP-01 and type it again just to keep track in front of me of what’s happening. That link was great but it didn’t get back the responses.. I guess this is my next step. Thanks again for your assistance mate.

        1. You’re welcome, I’m glad it is working fine ๐Ÿ™‚ I think you will like the NodeMCU, it’s much more easy to use than the ESP-01.

          Also, if you like Internet of Things, keep an eye for the ESP32 boards, it’s the ESP8266 big brother and it’s much much more powerful. ๐Ÿ™‚

          Good luck with your project!

  18. I think my problem is with the ESP8266-01 module itself. I bought 2 so far and couldn’t get them to work. I tried literally everything but the modules I have aren’t of good quality they started lagging and not always they were able to recognize the DNS. My friends are working with the ESP8266 NodeMCU board and I’m thinking of getting one. What’s your opinion? And also does this post should (if not must) work with that latter board right?
    Thank you for your time and help. I am probably switching hardware and thanks for convincing me! (if that’s what you meant earlier by changing to another environment).

    1. Just to clarify a thing, both the ESP8266-01 and the NodeMCU use the same microcontroller, the ESP8266. Those are just different boards built around the same microcontroller.
      So, all the code you see in my blog in the “ESP8266” category should work fine in any of them. Naturally, there may be a detail or another, but I try to leave a warning when we need to consider something additional specific to a certain board.
      The NodeMCU is a very good board and I have been using it for most of my posts on the ESP8266. The advantage is that it already has an USB header and you just need an USB cable to interact with it, which includes programming it.
      Besides that, it has all of the pins of the ESP8266 exposed, so you can use it as an “arduino”. Many people use the ESP8266 just as a Serial to WiFi converter, but the ESP8266 is actually much more powerful than most of the microcontrollers used in the Arduino boards. Most of the things you can do with an Arduino board you can do with an ESP8266 board, with the advantage of having WiFi built in.
      I think it is a very good option for you to move to the NodeMCU, but I also recommend you to program it with the Arduino IDE instead of using the AT commands. That’s what I meant when recommending you to change the environment ๐Ÿ™‚ The AT commands have always been a little unstable and you typically end up with a more complex program because you need to be parsing strings from the answers of the commands.
      The link I shared before with the tutorial to move to the Arduino IDE covers the ESP-01 as hardware because at the time I wrote it it was the most used board. Now with the NodeMCU boards, we really just need an USB cable to connect it to a computer.
      Nevertheless, from the Arduino IDE Configuration section bellow are the generic instructions for installing the ESP8266 support for the Arduino IDE. You will only need to choose the correct board on the boards manager (in your case it will be the NodeMCU) because the upload configurations may vary from board to board. But the IDE takes care of everything as long as we choose the correct board.
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/
      I think it’s still working fine, but the latest instructions from the creators of the ESP8266 libraries for the Arduino IDE are bellow, if you prefer:
      https://github.com/esp8266/Arduino#installing-with-boards-manager
      Let me know if you succeed with the NodeMCU ๐Ÿ™‚

      1. Actually I’ve managed to solve my issue thanks to your advice (really this time). Approaching it by code, the http GET request was sent correctly. I thought it’s a ESP8266-01 since I followed several tutorials that used AT commands and successfully sent a request but mine did not. Eventhough I tried several Arduino IDE approaches (by code) I wasn’t successful at first until now
        Still, I will get a NodeMCU just as a backup plan and I will follow your instructions. Thanks a lot if it wasn’t for you I would’ve have gave it another try with the Arduino IDE.
        Here’s the link that I followed http://blog.huntgang.com/2015/01/20/arduino-esp8266-tutorial-web-server-monitor-example/#comment-5758
        Now that remains is that I need to get the response from the ESP-01 and type it again just to keep track in front of me of what’s happening. That link was great but it didn’t get back the responses.. I guess this is my next step. Thanks again for your assistance mate.

        1. You’re welcome, I’m glad it is working fine ๐Ÿ™‚ I think you will like the NodeMCU, it’s much more easy to use than the ESP-01.
          Also, if you like Internet of Things, keep an eye for the ESP32 boards, it’s the ESP8266 big brother and it’s much much more powerful. ๐Ÿ™‚
          Good luck with your project!

  19. Thank you so much. It helped me so much to understand this ESP8266 beast. ๐Ÿ™‚ Such blogs really help people like me who try to learn something without programming background and proceed with their enthusiasm.

    Once again. thanks a lot. much appreciated.

    1. Thank you very much for your feedback ๐Ÿ™‚ It’s important for me to know that you are finding the content useful and easy to understand.

      Best regards,
      Nuno Santos

  20. Thank you so much. It helped me so much to understand this ESP8266 beast. ๐Ÿ™‚ Such blogs really help people like me who try to learn something without programming background and proceed with their enthusiasm.
    Once again. thanks a lot. much appreciated.

    1. Thank you very much for your feedback ๐Ÿ™‚ It’s important for me to know that you are finding the content useful and easy to understand.
      Best regards,
      Nuno Santos

  21. Hi,
    Firstly, I wanna commend you on this great post. I’ve been working on something similar but not entirely. I’m using an arduino, esp8266 and some sensors. the aim is to send data from the sensors to a server (not to a jsoplaceholder). I got it right when I’M sending data to a thingspeak channel (through an example code of course!)

    SoftwareSerial SerialS(8, 9) ;
    #include "ESP8266.h"
    ESP8266 wifi(SerialS,9600);
    
    

    The library I used was from ITEAD, and not your prescribed library in your tutorial,

    #define SSID "AndroidAP"      //name of wireless access point to connect to
    #define PASSWORD "*******"  //wifi password
    #define IP "184.106.153.149"                  
    
    
    String GET_start = "GET /update?key=";
    String writeAPIKey = "D*************SJ";    
    String GET_end1 =  "&field1=";                 // field1 for the Celcius
    String GET_end2 =  "&field2=";                 // field2 for the fahrenheit
    String GET_end3 =  "&field3=";
    String GET_end4 =  "&field4=";
    String GET_end5 =  "&field5=";
    
    String GET = GET_start + writeAPIKey + GET_end1 ;
    
    

    Here is one challenge I will be facing, the write API key is what I can’t substitute because heroku doesn’t have one.
    below is the remainder of the code for the sending of strings to the thingspeak server

        char buffer1[10];
        char buffer2[10];
        char buffer3[10];
        char buffer4 [10];
        char buffer5 [10];
        String light = dtostrf(ldr, 4, 1, buffer1);
        String voltBatt = dtostrf(vin1, 4, 1, buffer2);
        String voltSolar = dtostrf(vin2, 4, 1, buffer3);
        String hum = dtostrf(h, 4, 1, buffer4);
        String temp = dtostrf(t, 4, 1, buffer5);
        updateTemp(light,voltBatt,voltSolar,hum,temp);
    }
    
    void updateTemp(String temp1, String temp2, String temp3, String temp4, String temp5 ){
    
       if (wifi.createTCP(IP, (80))) {
         //   Display.print("create tcp ok \r\n");
         //Display.print(field);
        } else {
         //   Display.print("create tcp err\r\n");
        }
    
      String cmd = GET;
      cmd += temp1;
      cmd += GET_end2;
      cmd += temp2;
      cmd += GET_end3;
      cmd += temp3;
      cmd += GET_end4;
      cmd += temp4;
      cmd += GET_end5;
      cmd += temp5;
      cmd += "\r\n";
    
      ESPserial.print("AT+CIPSEND=");
      ESPserial.println(cmd.length());
      if(ESPserial.find(">")){
        Serial.print(">");
        Serial.print(cmd);
        ESPserial.print(cmd);
      }
    
      if(ESPserial.find("OK")){
        Serial.println("RECEIVED: OK");
      }else{
        Serial.println("RECEIVED: Error");
      }
    
      if (wifi.releaseTCP()) {
        //Display.print("release tcp ok \r\n");
      } else {
        //Display.print("release tcp err\r\n");
      }
    }
    
    

    But now, I have deployed on heroku and used firebase as my database server, and I want to make this code send my sensor data to my deployed server. Do you have any solution you can help me with? I’ll be forever grateful. Thanks!

    Kay.

    1. Hi! Thanks for your feedback, I’m glad you found this post useful ๐Ÿ™‚

      As for your application, I haven’t yet worked with Heroku (although I definitely want to take a look as soon as I have some time), but I’m assuming you are deploying an application there with a endpoint that will receive your sensor data, right?

      First of all, you need to confirm that you are not using HTTPS on your application, if so you will not be able to send the data because of the encryption. So, you will need to be using just HTTP.

      I haven’t used ITEAD’s library, so I can’t be of much help in that part, but from what I’m seeing it is using AT commands.

      So, my suggestion is more of on how to approach the problem. Since you’re moving to a new platform, you should start by doing something really simple before sending the sensor data, in order to be able to debug.

      So, my recommendation is that you put a simple route in your Heroku app that just does something as simple as returning a “hello world” string. Thus, you can confirm that you can reach the server and send an HTTP request and get the answer.

      So, your command would be something like
      String GET = “GET /hello\r\n”;

      If you can reach the server like this, then you can build the remaining application on top of it. After that, you could do a route that receives your parameters as query strings, like you are doing in that code in the /update. First you can try if it works with your browser, and then move to the ESP/Arduino code.

      From my understanding of your code / application, if you set a /update route similar to the one you have on ThingsSpeak, you just need to remove the ?key= from the Get_start variable and in the following line of code remove the + writeAPIKey.

      String GET = GET_start + writeAPIKey + GET_end1 ;

      It would then be:
      String GET_start = “GET /update”;
      String GET_end1 = “?field1=”;
      (…)
      String GET = GET_start + GET_end1 ;
      (…)

      But you should test sending the request from a browser first to avoid having complicated debugging on the Arduino / ESP. If the problem is in the server, you need to solve it first.

      So, the bottom line is that if your backend doesn’t need an API key, you just don’t need to send one. Just send the remaining query parameters.

      Also, if you are not familiarized with why you are sending the ?field1=something&field2=something… do a quick search about how query parameters work. It’s very important to understand your code.

      Basically, it is a way for sending parameters to the server on the URL. In the code you are showing me, you can do the same request to ThingsSpeak from a web browser by typing the following on the search bar:

      http://184.106.153.149/update?key=YourAPIKey&field1=YourFieldValue&field2=YourFieldValue

      and so on for the remaining values (all in the same line).

      Hope it helps!

  22. Hi,
    Firstly, I wanna commend you on this great post. I’ve been working on something similar but not entirely. I’m using an arduino, esp8266 and some sensors. the aim is to send data from the sensors to a server (not to a jsoplaceholder). I got it right when I’M sending data to a thingspeak channel (through an example code of course!)

    SoftwareSerial SerialS(8, 9) ;
    #include "ESP8266.h"
    ESP8266 wifi(SerialS,9600);
    

    The library I used was from ITEAD, and not your prescribed library in your tutorial,

    #define SSID "AndroidAP"      //name of wireless access point to connect to
    #define PASSWORD "*******"  //wifi password
    #define IP "184.106.153.149"
    
    String GET_start = "GET /update?key=";
    String writeAPIKey = "D*************SJ";
    String GET_end1 =  "&field1=";                 // field1 for the Celcius
    String GET_end2 =  "&field2=";                 // field2 for the fahrenheit
    String GET_end3 =  "&field3=";
    String GET_end4 =  "&field4=";
    String GET_end5 =  "&field5=";
    String GET = GET_start + writeAPIKey + GET_end1 ;
    

    Here is one challenge I will be facing, the write API key is what I can’t substitute because heroku doesn’t have one.
    below is the remainder of the code for the sending of strings to the thingspeak server

        char buffer1[10];
        char buffer2[10];
        char buffer3[10];
        char buffer4 [10];
        char buffer5 [10];
        String light = dtostrf(ldr, 4, 1, buffer1);
        String voltBatt = dtostrf(vin1, 4, 1, buffer2);
        String voltSolar = dtostrf(vin2, 4, 1, buffer3);
        String hum = dtostrf(h, 4, 1, buffer4);
        String temp = dtostrf(t, 4, 1, buffer5);
        updateTemp(light,voltBatt,voltSolar,hum,temp);
    }
    void updateTemp(String temp1, String temp2, String temp3, String temp4, String temp5 ){
       if (wifi.createTCP(IP, (80))) {
         //   Display.print("create tcp ok \r\n");
         //Display.print(field);
        } else {
         //   Display.print("create tcp err\r\n");
        }
      String cmd = GET;
      cmd += temp1;
      cmd += GET_end2;
      cmd += temp2;
      cmd += GET_end3;
      cmd += temp3;
      cmd += GET_end4;
      cmd += temp4;
      cmd += GET_end5;
      cmd += temp5;
      cmd += "\r\n";
      ESPserial.print("AT+CIPSEND=");
      ESPserial.println(cmd.length());
      if(ESPserial.find(">")){
        Serial.print(">");
        Serial.print(cmd);
        ESPserial.print(cmd);
      }
      if(ESPserial.find("OK")){
        Serial.println("RECEIVED: OK");
      }else{
        Serial.println("RECEIVED: Error");
      }
      if (wifi.releaseTCP()) {
        //Display.print("release tcp ok \r\n");
      } else {
        //Display.print("release tcp err\r\n");
      }
    }
    

    But now, I have deployed on heroku and used firebase as my database server, and I want to make this code send my sensor data to my deployed server. Do you have any solution you can help me with? I’ll be forever grateful. Thanks!
    Kay.

    1. Hi! Thanks for your feedback, I’m glad you found this post useful ๐Ÿ™‚
      As for your application, I haven’t yet worked with Heroku (although I definitely want to take a look as soon as I have some time), but I’m assuming you are deploying an application there with a endpoint that will receive your sensor data, right?
      First of all, you need to confirm that you are not using HTTPS on your application, if so you will not be able to send the data because of the encryption. So, you will need to be using just HTTP.
      I haven’t used ITEAD’s library, so I can’t be of much help in that part, but from what I’m seeing it is using AT commands.
      So, my suggestion is more of on how to approach the problem. Since you’re moving to a new platform, you should start by doing something really simple before sending the sensor data, in order to be able to debug.
      So, my recommendation is that you put a simple route in your Heroku app that just does something as simple as returning a “hello world” string. Thus, you can confirm that you can reach the server and send an HTTP request and get the answer.
      So, your command would be something like
      String GET = “GET /hello\r\n”;
      If you can reach the server like this, then you can build the remaining application on top of it. After that, you could do a route that receives your parameters as query strings, like you are doing in that code in the /update. First you can try if it works with your browser, and then move to the ESP/Arduino code.
      From my understanding of your code / application, if you set a /update route similar to the one you have on ThingsSpeak, you just need to remove the ?key= from the Get_start variable and in the following line of code remove the + writeAPIKey.
      String GET = GET_start + writeAPIKey + GET_end1 ;
      It would then be:
      String GET_start = “GET /update”;
      String GET_end1 = “?field1=”;
      (…)
      String GET = GET_start + GET_end1 ;
      (…)
      But you should test sending the request from a browser first to avoid having complicated debugging on the Arduino / ESP. If the problem is in the server, you need to solve it first.
      So, the bottom line is that if your backend doesn’t need an API key, you just don’t need to send one. Just send the remaining query parameters.
      Also, if you are not familiarized with why you are sending the ?field1=something&field2=something… do a quick search about how query parameters work. It’s very important to understand your code.
      Basically, it is a way for sending parameters to the server on the URL. In the code you are showing me, you can do the same request to ThingsSpeak from a web browser by typing the following on the search bar:
      http://184.106.153.149/update?key=YourAPIKey&field1=YourFieldValue&field2=YourFieldValue
      and so on for the remaining values (all in the same line).
      Hope it helps!

  23. Thank you so much. I didn’t find a solution for my problem in another site, but your post helped me. Best Regards.

    1. Hi! Thank you very much for your feedback ๐Ÿ™‚ I’m glad these posts are being useful, I will try to keep posting more content about the ESP8266.

      Best regards,
      Nuno Santos

  24. Thank you so much. I didn’t find a solution for my problem in another site, but your post helped me. Best Regards.

    1. Hi! Thank you very much for your feedback ๐Ÿ™‚ I’m glad these posts are being useful, I will try to keep posting more content about the ESP8266.
      Best regards,
      Nuno Santos

  25. Hi Antepher, Your post is very useful for newcomers like me……….My problem is I don’t know whether I can use this library with ESP01 serial wifi transceiver SO I wanted to know can I use this library that you prescribed with ESP-01? If yes than How should I connect ESP01 to My arduino Mega? I am trying to send data to mySQL server it’s an RFID based logging system so I need to read the confirmation echo back from the server too….please guide me on that…..thank you

    1. Hi! Thanks for your feedback ๐Ÿ™‚ Yes, you should be able to use it with the ESP01.

      These tutorials are for the ESP8266 microcontroller, which is found at those ESPXX boards (and many others).

      So, in theory, it should work as long as the microcontroller is the ESP8266, as it is the case of the ESP01.

      Naturally, in other programs where we use specific pins of the ESP8266, they may not be exposed in some boards, so you need to be careful with that. In this case we don’t use any GPIO.

      For the ESP01, you can check the tutorial bellow on how to program it with the Arduino IDE:
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/

      It’s a bit old, but I think it still applies to the ESP01. I can’t confirm since I’ve not been using the ESP01’s for a while. Due to the relatively complicated process of uploading code to the ESP01, I’ve started using other simpler to program ESP8266 boards.

      Let me know if it works.

      Best regards,
      Nuno Santos

  26. Hi Antepher, Your post is very useful for newcomers like me……….My problem is I don’t know whether I can use this library with ESP01 serial wifi transceiver SO I wanted to know can I use this library that you prescribed with ESP-01? If yes than How should I connect ESP01 to My arduino Mega? I am trying to send data to mySQL server it’s an RFID based logging system so I need to read the confirmation echo back from the server too….please guide me on that…..thank you

    1. Hi! Thanks for your feedback ๐Ÿ™‚ Yes, you should be able to use it with the ESP01.
      These tutorials are for the ESP8266 microcontroller, which is found at those ESPXX boards (and many others).
      So, in theory, it should work as long as the microcontroller is the ESP8266, as it is the case of the ESP01.
      Naturally, in other programs where we use specific pins of the ESP8266, they may not be exposed in some boards, so you need to be careful with that. In this case we don’t use any GPIO.
      For the ESP01, you can check the tutorial bellow on how to program it with the Arduino IDE:
      https://techtutorialsx.com/2016/02/28/esp8266-uploading-code-from-arduino-ide/
      It’s a bit old, but I think it still applies to the ESP01. I can’t confirm since I’ve not been using the ESP01’s for a while. Due to the relatively complicated process of uploading code to the ESP01, I’ve started using other simpler to program ESP8266 boards.
      Let me know if it works.
      Best regards,
      Nuno Santos

    1. Hi! It’s not a dumb question, many people have the same doubt ๐Ÿ™‚

      This is the ESP8266 on its own, you don’t need an Arduino uno board or any other board.

      Although in the beginning, the ESP8266 was sold as an Arduino WiFi to serial bridge, the community quickly discovered that it was actually a very powerful microcontroller with WiFi support that could be programmed and used without any additional board.

      So, one of the projects that was first initiated was adding support on the Arduino IDE to program it. This was an awesome initiative since the original set of tools was very complicated and the Arduino was a well known environment for the community.

      So, to sum up, you just need an ESP8266 board (there are plenty of them on the market with usb headers for programming without external hardware) and the Arduino IDE and you can follow this tutorial.

      Hope this clarifies your doubt.

      Best regards,
      Nuno Santos

    1. Hi! It’s not a dumb question, many people have the same doubt ๐Ÿ™‚
      This is the ESP8266 on its own, you don’t need an Arduino uno board or any other board.
      Although in the beginning, the ESP8266 was sold as an Arduino WiFi to serial bridge, the community quickly discovered that it was actually a very powerful microcontroller with WiFi support that could be programmed and used without any additional board.
      So, one of the projects that was first initiated was adding support on the Arduino IDE to program it. This was an awesome initiative since the original set of tools was very complicated and the Arduino was a well known environment for the community.
      So, to sum up, you just need an ESP8266 board (there are plenty of them on the market with usb headers for programming without external hardware) and the Arduino IDE and you can follow this tutorial.
      Hope this clarifies your doubt.
      Best regards,
      Nuno Santos

    1. Hi! Can you please share a description of the problem you are experiencing? Due to security reasons, I usually don’t follow links to uploaded files, hope you understand.

      Best regards,
      Nuno Santos

      1. Hi, I have a same problem to. baud rate is ok but I can not see the data correctly. data in serial monitor is some thing like this : #?# . what is the problem ? note that I use the same URL as yours and some other URL but results are the same.

        1. Hi!

          That’s weird, I’ve not been able to reproduce the issue.

          Maybe some problem with your units or cable? Or are you able to do regular serial prints of string without any problem?

          Best regards,
          Nuno Santos

          1. Yes, I am able to print Strings in serial monitor without any problem!! but when I use http.getString() I have problems in printing on serial monitor. note that I found a solution for my problem. I changed my access point and every thing is ok now! That’s so weird, do you have any idea about it? what is the problem of my old Access Point? it was a regular 4G/LTE TP-Link router!

            1. Hi!

              That’s really weird, I’ve never experienced that type of problem. Nonetheless, I’ve always used the ESP with my Thomson router.

              But I’m glad you were able to solve it. ๐Ÿ™‚

              My suggestion is that you post your issue in the Git Hub page of the Arduino core, maybe someone there has faced the same issue and has a solution or ideia about the cause.

              Best regards,
              Nuno Santos

    1. Hi! Can you please share a description of the problem you are experiencing? Due to security reasons, I usually don’t follow links to uploaded files, hope you understand.
      Best regards,
      Nuno Santos

      1. Hi, I have a same problem to. baud rate is ok but I can not see the data correctly. data in serial monitor is some thing like this : #?# . what is the problem ? note that I use the same URL as yours and some other URL but results are the same.

        1. Hi!
          That’s weird, I’ve not been able to reproduce the issue.
          Maybe some problem with your units or cable? Or are you able to do regular serial prints of string without any problem?
          Best regards,
          Nuno Santos

          1. Yes, I am able to print Strings in serial monitor without any problem!! but when I use http.getString() I have problems in printing on serial monitor. note that I found a solution for my problem. I changed my access point and every thing is ok now! That’s so weird, do you have any idea about it? what is the problem of my old Access Point? it was a regular 4G/LTE TP-Link router!

            1. Hi!
              That’s really weird, I’ve never experienced that type of problem. Nonetheless, I’ve always used the ESP with my Thomson router.
              But I’m glad you were able to solve it. ๐Ÿ™‚
              My suggestion is that you post your issue in the Git Hub page of the Arduino core, maybe someone there has faced the same issue and has a solution or ideia about the cause.
              Best regards,
              Nuno Santos

  27. Hello!
    I have tried running your code on a private network, and it works. However, whenever I use a public network, I get the following error.

    Connecting..Connecting..Connecting..
    Stylesheet for Squid Error pages
    Adapted from design by Free CSS Templates
    http://www.freecsstemplates.org
    Released for free under a Creative Commons Attribution 2.5 License
    */

    /* Page basics */
    * {
    font-family: verdana, sans-serif;
    }

    html body {
    margin: 0;
    padding: 0;
    background: #efefef;
    font-size: 12px;
    color: #1e1e1e;
    }

    /* Page displayed title area */
    #titles {
    margin-left: 15px;
    padding: 10px;
    padding-left: 100px;
    background: url(‘http://www.squid-cache.org/Artwork/SN.png’) no-repeat left;
    }

    /* initial title */
    #titles h1 {
    color: #000000;
    }
    #titles h2 {
    color: #000000;
    }

    /* special event: FTP success page titles */
    #titles ftpsuccess {
    background-color:#00ff00;
    width:100%;
    }

    /* Page displayed body content area */
    #content {
    padding: 10px;
    background: #ffffff;
    }

    /* General text */
    p {
    }

    /* error brief description */
    #error p {
    }

    /* some data which may have caused the problem */
    #data {
    }

    /* the error message received from the system or other software */
    #sysmsg {
    }

    pre {
    font-family:sans-serif;
    }

    /* special event: FTP / Gopher directory listing */
    #dirmsg {
    font-family: courier;
    color: black;
    font-size: 10pt;
    }
    #dirlisting {
    margin-left: 2%;
    margin-right: 2%;
    }
    #dirlisting tr.entry td.icon,td.filename,td.size,td.date {
    border-bottom: groove;
    }
    #dirlisting td.size {
    width: 50px;
    text-align: right;
    padding-right: 5px;
    }

    /* horizontal lines */
    hr {
    margin: 0;
    }

    /* page displayed footer area */
    #footer {
    font-size: 9px;
    padding-left: 10px;
    }
    body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; } –> ERROR The requested URL could not be retrieved The following error was encountered while trying to retrieve the URL: http://jsonplaceholder.typicode.com/users/1

    ICAP protocol error.

    The system returned: [No Error] This means that some aspect of the ICAP communication failed. Some possible problems are: The ICAP server is not reachable. An Illegal response was received from the ICAP server. Generated Sun, 24 Sep 2017 01:23:48 GMT by px019.st.vc.core.wifi.inet (squid/3.1.23)
    ERROR: The requested URL could not be retrieved <!– /*

    1. Hi! It seems like the public network you are trying to use has some kind of authentication page that you have to fill, and by trying to connect using the ESP library it is giving you some error. That HTML you are seeing is most likely an error page.

      If you try to connect to that network from your computer, don’t you need to fill a form or something similar on your browser?

      1. Hi! Thank you for the reply!

        Yes, an authentication is required. However, there’s an option to register the devices’ MAC addresses so that there is no need to authenticate. So, is it possible that the devices’ MAC addresses are wrong or not being supported (btw raspberry pi’s seem to work just fine..)? Are there any other suggestions? Is there a way to connect then authenticate using the ESP library without having to register the MAC address for example?

        1. You’re welcome ๐Ÿ™‚

          Unfortunately I never tried to connect the ESP to a public network, so I don’t know an alternative way to solve the issue you are running into.

          But do you have access to the network to add the MAC address? If so you can easily get the MAC address of the ESP8266:
          https://techtutorialsx.com/2017/04/09/esp8266-get-mac-address/

          Another alternative is asking around the GitHub page for the ESP Arduino core:
          https://github.com/esp8266/Arduino

          It is very likely that someone has already tried it and may have a solution.

          Hope this helps.

          Best regards,
          Nuno Santos

  28. Hello!
    I have tried running your code on a private network, and it works. However, whenever I use a public network, I get the following error.
    Connecting..Connecting..Connecting..
    Stylesheet for Squid Error pages
    Adapted from design by Free CSS Templates
    http://www.freecsstemplates.org
    Released for free under a Creative Commons Attribution 2.5 License
    */
    /* Page basics */
    * {
    font-family: verdana, sans-serif;
    }
    html body {
    margin: 0;
    padding: 0;
    background: #efefef;
    font-size: 12px;
    color: #1e1e1e;
    }
    /* Page displayed title area */
    #titles {
    margin-left: 15px;
    padding: 10px;
    padding-left: 100px;
    background: url(‘http://www.squid-cache.org/Artwork/SN.png’) no-repeat left;
    }
    /* initial title */
    #titles h1 {
    color: #000000;
    }
    #titles h2 {
    color: #000000;
    }
    /* special event: FTP success page titles */
    #titles ftpsuccess {
    background-color:#00ff00;
    width:100%;
    }
    /* Page displayed body content area */
    #content {
    padding: 10px;
    background: #ffffff;
    }
    /* General text */
    p {
    }
    /* error brief description */
    #error p {
    }
    /* some data which may have caused the problem */
    #data {
    }
    /* the error message received from the system or other software */
    #sysmsg {
    }
    pre {
    font-family:sans-serif;
    }
    /* special event: FTP / Gopher directory listing */
    #dirmsg {
    font-family: courier;
    color: black;
    font-size: 10pt;
    }
    #dirlisting {
    margin-left: 2%;
    margin-right: 2%;
    }
    #dirlisting tr.entry td.icon,td.filename,td.size,td.date {
    border-bottom: groove;
    }
    #dirlisting td.size {
    width: 50px;
    text-align: right;
    padding-right: 5px;
    }
    /* horizontal lines */
    hr {
    margin: 0;
    }
    /* page displayed footer area */
    #footer {
    font-size: 9px;
    padding-left: 10px;
    }
    body :lang(fa) { direction: rtl; font-size: 100%; font-family: Tahoma, Roya, sans-serif; float: right; } :lang(he) { direction: rtl; } –> ERROR The requested URL could not be retrieved The following error was encountered while trying to retrieve the URL: http://jsonplaceholder.typicode.com/users/1

    ICAP protocol error.

    The system returned: [No Error] This means that some aspect of the ICAP communication failed. Some possible problems are: The ICAP server is not reachable. An Illegal response was received from the ICAP server. Generated Sun, 24 Sep 2017 01:23:48 GMT by px019.st.vc.core.wifi.inet (squid/3.1.23)
    ERROR: The requested URL could not be retrieved <!– /*

    1. Hi! It seems like the public network you are trying to use has some kind of authentication page that you have to fill, and by trying to connect using the ESP library it is giving you some error. That HTML you are seeing is most likely an error page.
      If you try to connect to that network from your computer, don’t you need to fill a form or something similar on your browser?

      1. Hi! Thank you for the reply!
        Yes, an authentication is required. However, there’s an option to register the devices’ MAC addresses so that there is no need to authenticate. So, is it possible that the devices’ MAC addresses are wrong or not being supported (btw raspberry pi’s seem to work just fine..)? Are there any other suggestions? Is there a way to connect then authenticate using the ESP library without having to register the MAC address for example?

        1. You’re welcome ๐Ÿ™‚
          Unfortunately I never tried to connect the ESP to a public network, so I don’t know an alternative way to solve the issue you are running into.
          But do you have access to the network to add the MAC address? If so you can easily get the MAC address of the ESP8266:
          https://techtutorialsx.com/2017/04/09/esp8266-get-mac-address/
          Another alternative is asking around the GitHub page for the ESP Arduino core:
          https://github.com/esp8266/Arduino
          It is very likely that someone has already tried it and may have a solution.
          Hope this helps.
          Best regards,
          Nuno Santos

  29. Hi there,
    first of all, thanks for the tutorial, it is great.
    Based on it, I have my own code and it works flawlessly. The problem is that the code wont start when I power the bord on. I need to connect to it through serial and then it works like a charm, but again, if i powercycle, nothing happens unless I start putty and connect to it again.
    Do you know what to do with it? My code has the same structure. consts, setup and loop as in your tutorial
    My board is https://www.banggood.com/NodeMcu-Lua-WIFI-Internet-Things-Development-Board-Based-ESP8266-CP2102-Wireless-Module-p-1097112.html?rmmds=myorder
    Geekcreitยฎ NodeMcu Lua WIFI Internet Things Development Board Based ESP8266 CP2102 Wireless Module

    Thanks for help!

      1. Hi Martin! Thanks for the feedback ๐Ÿ™‚

        I’m glad you were able to solve the issue.

        Through my time in embedded systems development, I’ve also spent a fair amount of time looking for issues in the software that were actually being caused by hardware problems. ๐Ÿ™‚

        Best regards,
        Nuno Santos

  30. Hi there,
    first of all, thanks for the tutorial, it is great.
    Based on it, I have my own code and it works flawlessly. The problem is that the code wont start when I power the bord on. I need to connect to it through serial and then it works like a charm, but again, if i powercycle, nothing happens unless I start putty and connect to it again.
    Do you know what to do with it? My code has the same structure. consts, setup and loop as in your tutorial
    My board is https://www.banggood.com/NodeMcu-Lua-WIFI-Internet-Things-Development-Board-Based-ESP8266-CP2102-Wireless-Module-p-1097112.html?rmmds=myorder
    Geekcreitยฎ NodeMcu Lua WIFI Internet Things Development Board Based ESP8266 CP2102 Wireless Module
    Thanks for help!

      1. Hi Martin! Thanks for the feedback ๐Ÿ™‚
        I’m glad you were able to solve the issue.
        Through my time in embedded systems development, I’ve also spent a fair amount of time looking for issues in the software that were actually being caused by hardware problems. ๐Ÿ™‚
        Best regards,
        Nuno Santos

  31. Please take a look at my error. I don’t know where the problem is.
    When i uploaded your code, i had this error.
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed
    I used PL2302 to upload code for ESP8266v1.
    And my connections:
    ESP8266 USB UART
    TX RX
    RX TX
    Vcc 3.3V
    GND GND
    CH_PD 3.3V
    Thank you.

  32. Please take a look at my error. I don’t know where the problem is.
    When i uploaded your code, i had this error.
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    error: failed reading byte
    warning: espcomm_send_command: cant receive slip payload data
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed
    error: espcomm_upload_mem failed
    I used PL2302 to upload code for ESP8266v1.
    And my connections:
    ESP8266 USB UART
    TX RX
    RX TX
    Vcc 3.3V
    GND GND
    CH_PD 3.3V
    Thank you.

Leave a Reply