How to Perform GET Requests using ESP8266

ESP8266 has amazing IOT capabilities which allows you to send or receive data to a remote server that can be displayed through LCD. The HTTP GET request allows you to pull information from a website. So let’s take a look how to perform GET Requests using the ESP8266.

First, we need do include some libraries. We will 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.

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..");

   }}

In this example, we are going to make a request to a fake REST API website called jasonplacefolder.

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 below. You just need to put your network name and password.

#include <ESP8266WiFi.h>

#include <ESP8266HTTPClient.h>

const char* ssid = "NetworkName";

const char* password = "Password";

 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();   //End connection

   }

   delay(50000);    //Send a request every 50 seconds

 }

In this case, we are going to do the request for the user with ID number 1. The output of the GET Request is shown below:

Note: The ESP8266 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.

sarah ali

sarah ali

Sarah is a passionate writer and blogger. As an early adopter, she enjoys trying out new social media and Internet tools along with WordPress plugins and Web apps.
sarah ali

Leave a Reply

Your email address will not be published.