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

314 thoughts on “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

  3. Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | techtutorialsx

  4. Pingback: ESP8266: Posting JSON data to a Flask server on the cloud | 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.

  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. 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!

  8. 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!

  9. 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.

  10. 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

Leave a Reply to gogo Cancel reply

%d bloggers like this: