HTTP requests with ESP8266

In this article I will explain how to send HTTP requests with ESP8266 module. We will see how to send an HTTP GET and a POST request. I will also introduce a new library for processing JSON. Lets start.

Setup wifi

Include the following header. This library has all the required APIs for connecting to a WiFi network.

#include <ESP8266WiFi.h>

Define the your network SSID and password as constants.


const char *SERVER_WIFI_SSID = "SSID";
const char *SERVER_WIFI_PASS = "mypassword";

Connect to WiFi using WiFi.begin . We need to keep checking the status to make sure that the device is connected to network. Here is the method that I have created.

void setupWiFi()
{
   Serial.print("Connecting to WiFi ");
   WiFi.begin(SERVER_WIFI_SSID,SERVER_WIFI_PASS);
   while(WiFi.status() != WL_CONNECTED)
   {
     delay(500);
     Serial.print(".");
   }

   Serial.println("Connected");
}

HTTP Server

I have created a simple servlet application. It has just two end points. Details of the end points will be explained in the following sections. But you can use any other as you wish. Servlet code will also be attached with this post.

HTTP Requests – Get

ESP8266 for Arduino also comes with a HTTP client library ESP8266HTTPClient. Add the following include in your code

#include &lt;ESP8266HTTPClient.h&gt;

HTTP requests should b including within http.begin and http.end calls. Here it goes

HTTPClient http;
http.begin("http://192.168.0.104:8080/webappfordemo/Version");

Above URL will return a simple JSON message mentioned at end of this section. HTTP GET request does not have a payload. All the parameters will be specified in the URL itself. So, the GET request is as simple as

int httpCode = http.GET();

Return value of the GET function will be the HTTP response code. You can find details of response code at https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. But all we are interested is in response code 200. It is possible the httpCode will have value -1. This can happen if GET API itself is failed, for example when the server is not reachable. So, you can handle that as well. HTTP codes are defined as constants in this library. For response code 200, we have the constant as HTTP_CODE_OK.

To read the response message, we call http.getString(). This will return a string object.

{
   Serial.print("HTTP response code ");
   Serial.println(httpCode);
   String response = http.getString();
   Serial.println(response);
}

Finally the code for GET will be as follows.

http.begin("http://192.168.0.104:8080/webappfordemo/Version");

int httpCode = http.GET();
if(httpCode == HTTP_CODE_OK)
{
   Serial.print("HTTP response code ");
   Serial.println(httpCode);
   String response = http.getString();
   Serial.println(response);
}
else
{
   Serial.println("Error in HTTP request");
}

http.end();

When this code is executed, we will expect the following response. Response might changed based on the end point you will be using

HTTP response code 200
{"server":"Demo Servlet","version":"1.0"}

HTTP Requests – POST

The only change we will do compared to GET request is the use of POST payload. This is where we use a library aJSON. You can get this library from https://github.com/interactive-matter/aJson. The project provides detailed documentation on usage.

For this example, we will be using an end point PingPong, hosted on the same server. This end point just returns the JSON message sent my client in another container JSON object.

Let us prepare the JSON object.

aJsonObject *root;
root=aJson.createObject(); 
aJson.addStringToObject(root,"message", "Hello from ESP8266");
char *payload=aJson.print(root);

Now it is time to send the message. Call the POST method with the payload and payload length.

httpCode = http.POST((uint8_t *)payload,strlen(payload));

We expect the response as

HTTP response code 200
{"server":"Demo Servlet","version":"1.0"}

Source code

You can find the Arduino source files and server side code if you want to use the one used in this example.

Demo files for HTTP Requests with ESP8266

4 Comments

  1. Thank you for posting this! This is the first Arduino ESP8266 HTTP GET Request example I found.

    I’m trying to get my ESP to send a GET request to IFTTT. So, I commented out all of the POST code and am currently left with the GET code. However, I only get “Error in HTTP request” after having tried to send a request to both IFTTT.com and google. What else do I need to do in order to simply send a GET request?

      • I just tried connecting to IFTTT. It works fine. I used the Maker channel. All I did in the code was change the URL of GET request and used the URL provided by the Make channel page. I changed the event parameter to my own value, say ‘temp’. I could get the proper response. Can you please provide the httpCode yo re getting. You can print the value of httpCode of GET request.

  2. Note the standard Serial baud rate is set to 1 so be sure to select that in your terminal, we want our debugging terminal as fast as possible so that we can keep up with the ESP8266 on the SoftwareSerial connection and avoid buffer overflows.

Leave a Reply

Your email address will not be published. Required fields are marked *