ESP8266: HTTP GET Requests

Introduction

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

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

The setup

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

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

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

To be able to connect to the WiFi network, we will need its credentials. So, we will define two global variables containing the network name (SSID) and password. Note that you should replace the placeholders in the code snippet below by the credentials of your network.

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

On the setup function, we will connect to the WiFi Network. This is done with a call to the begin method on the WiFi extern variable and providing the previously defined network credentials. More details about how to connect to a WiFi network using the ESP8266 are explained in this previous post.

We will also start a Serial connection to print the results of our program.

The complete Arduino setup function code can be seen below.

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 Arduino 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 will call the begin method on the http object and pass the URL that we want to connect to and make the HTTP GET request. The destination website specified here implements a dummy REST API for testing and prototyping.

Note that the particular endpoint we are reaching will return a JSON payload as response, simulating a possible “user” data structure. You can directly access the URL and check the result in a web browser. Later, when testing the ESP8266 code, we should receive the same response.

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 is a standard HTTP code. If the value is lesser than 0, then it is 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 the response payload by calling the getString method on the http object. The response is returned as a String, which we can directly print to the serial port.

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

Finally, we will 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. Note that we have added a small delay of 30 seconds between each iteration of the Arduino main loop, to avoid constantly polling the server.

#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
}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE.

After the upload procedure finishes, open the IDE serial monitor and wait for the WiFi connection to be established. Once this procedure is done, the ESP8266 should start sending the HTTP GET requests to the server periodically.

The expected output of the program is shown in figure 1. The response printed to the serial monitor corresponds to the JSON payload returned by the server. The ESP8266 should keep doing a HTTP GET request every 30 seconds.

Output of the ESP8266 GET Request program, on the Arduino IDE serial monitor
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.

Related Posts

Technical details

  • ESP8266 libraries: v2.3.0
  • Operating system: Windows 8.1

315 thoughts on “ESP8266: HTTP GET Requests”

      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.

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

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

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

  6. 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 rizqivCancel reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading