ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

ESP32 is a powerful chip for Internet of Things applications. This tutorial is also based on another ESP32 application in the field of IoT.

Hello readers, I hope you all are doing great. In the previous tutorial, we learned how to send sensor readings from ESP32 to the cloud (ThingSpeak webserver).

In this tutorial, we will learn to send HTTP POST requests from the ESP32 board to ThingSpeak and IFTTT APIs.

Where To Buy?
No.ComponentsDistributorLink To Buy
1ESP32AmazonBuy Now

What is HTTP?

ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 1 Hypertext Transfer Protocol

HTTP stands for hypertext transfer control and it is a protocol for transferring data between a web client and a web server. Hyper text transfer protocol was invented alongside HTML (Hypertext markup language) to create the first interactive, text-based web browser: the original www or World Wide Web.

Server and client communication process over HTTP:

  • The ESP32 (client) sends an HTTP request to a server ( for example ThingSpeak or IFTTT.com)
  • The server responds to the ESP32 ( client ).
  • Finally, the response contains request status information as well as the requested content.

HTTP POST Request

ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 2 HTTP POST Request

Hypertext transfer protocol uses particular request methods to execute various tasks. Two mostly used HTTP request methods are: HTTP GET request and HTTP POST request.

HTTP GET request is generated to request data from a specific resource and the HTTP POST request method is used to send data from the client device to the server to create or update resources.

In this tutorial, we will demonstrate only the HTTP POST method with ThingSpeak and IFTTT web services.

Features of the HTTP POST request:

  • Unlimited data length: Data is submitted through the body of HTTP so there is no limit/restriction on data length.
  • Secure: Data does not get saved on the web browser hence, this method of data communication is secure.
  • Allows different data types.
  • Data privacy.

What is IFTTT?

IFTT stands for If This Then That. It is a free web service for making different services like email, weather services, Twitter etc to connect.

IFTTT means if a service is triggered, other IFTTT services will take action.

 
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 3 IFTTT

IFTTT and ESP32

IFTTT acts as a bridge between ESP32 and other web services. Some of the tasks the ESP32 board can perform with the IFTTT API service are:

  • Sending Emails and SMSs
  • Controlling ESP32 with Google Assistant
  • Communicating data or information with smartphones.
  • Scheduling events for ESP32.

IFTTT comprises Applets and Applets further contains two IFTTT services namely trigger and action.

You can use the applets created by a company or can also create your own applet. To use the IFTTT applet with ESP32, we need to create an applet by ourselves. Such applet will contain Webhooks service to interact directly with ESP32 and other services that you want to use like email, Twitter service etc.

There are cases while using ESP32 with the IFTTT: either ESP32 will trigger the IFTTT to do some task or the IFTTT triggers ESP32 to do some task.

Steps to trigger IFTTT via ESP32

  • Create an IFTTT account
  • Create an Applet to connect Webhooks to the desired service.
  • Sending HTTP POST request from ESP32 board to IFTTT
  1. Creating an IFTTT account:

Enter the following link in the web browser: https://ifttt.com

  1. Login with your Gmail or Facebook accounts for free.
  2. Click on Create icon (top left menu) to create an Applet.
 
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 4 Creating an Applet

 
  1. Click on the ”if This” icon.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 5 ” If This”

 
  • Select a service. Search for the Webhooks service and select the respective icon.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 6 Search and Select Webhooks

 
  • Click on the Receive a web request option to select a trigger option. The trigger will fire every time the maker service receives a web request to notify it to an event.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 7 Receive a Web Request

 
  • Assign a name to the trigger event and click on Create trigger We have assigned ESP32_test.
 
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 8 Create Trigger

  • Next, click on the “Then That”
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 9 Then that

  • Select a service. We are selecting an Email service.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 10 Selecting a Service

  • Next, define what will happen whenever the event is triggered (the event that we have created earlier) and click on the Finish
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 11

  • Testing the Applet
  1. Open the following link: https://ifttt.com/maker_webhooks
  2. Click on the Documentation A new window will open containing your key (API).
  3. Enter the details in To trigger an Event and click on Test it.
 
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 12 To Trigger an Event

ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 13 Event Successfully Triggered

 
  • Open the email account, you have used while creating an IFTTT account.
  • You should receive an email from IFTTT.

Arduino Code

#include <WiFi.h>

#include <HTTPClient.h>

//---------Netwrok Credentials

const char* ssid = "SSID";

const char* password = "Password";

const char* serverName = "http://maker.ifttt.com/trigger/ESP32_test/with/key/Enter you API key";

unsigned long lastTime = 0;

unsigned long timerDelay = 15000;

void setup()

{

Serial.begin(115200);

WiFi.begin(ssid, password);

Serial.println("Connecting");

while(WiFi.status() != WL_CONNECTED)

{

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.print("Connected to WiFi network with IP Address: ");

Serial.println(WiFi.localIP());

// Random seed is a number used to initialize a pseudorandom number generator

randomSeed(hallRead());

}

Void Loop()

//Send an HTTP POST request after every 15 seconds

if ((millis() - lastTime) > timerDelay)

{

//Check WiFi connection status

if(WiFi.status()== WL_CONNECTED)

{

WiFiClient client;

HTTPClient http;

// Your Domain name with URL path or IP address with path

http.begin(client, serverName);

// Specify content-type header

http.addHeader("Content-Type", "application/x-www-form-urlencoded");

// Data to send with HTTP POST

String httpRequestData = "value1=" + String(random(25)) + "&value2=" + String(random(25))+ "&value3=" + String(random(25));

// Send HTTP POST request

int httpResponseCode = http.POST(httpRequestData);

/*

// If you need an HTTP request with a content type: application/json, use the following:

http.addHeader("Content-Type", "application/json");

// JSON data to send with HTTP POST

String httpRequestData = "{\"value1\":\"" + String(random(40)) + "\",\"value2\":\"" + String(random(40)) + "\",\"value3\":\"" + String(random(40)) + "\"}";

// Send HTTP POST request

int httpResponseCode = http.POST(httpRequestData);

*/

Serial.print("HTTP Response code: ");

Serial.println(httpResponseCode);

Serial.println("successfully conected to host");

// Free resources

http.end();

}

else

{

Serial.println("WiFi Disconnected");

}

lastTime = millis();

}

}

Code Description

  • Add the required header files.
  • WiFi.h header file is used to enable the Wi-Fi module and its respective functions.
  • HTTPClient.h header file is used to let the server and client pass information with HTTP response or request.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. Libraries

  • Enter the network credentials, SSID and Password.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. Network Credentials

  • Add the IFTT domain name, the event name (you have created) and the API key. The event name we have created is ESP32_test.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

Setup()

  • Initialize the Serial monitor with a 115200 baud rate for debugging purposes.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

  • Enable ESP32’s Wi-Fi module using begin() function which is using SSID and password as arguments.
  • Wait until the ESP32 is not connected to the Wi-Fi network.
  • Fetch the IP address using WiFi.localIP() function.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

  • randomSeed() function is used to generated a pseudorandom number. We are using Hall sensor to take hall readings and share them to IFTTT server (host).
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

Loop()

  • If the ESP32 board is successfully connected to the Wi-Fi network, HTTP POST requests will be generated automatically after every 15 seconds.
  • Some random values (hall readings) will be sent through value1, value1, value3
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig

  • Send HTTP POST request.
  • Print the HTTP POST response with the response code.
  • Response code 200 is for successful communication and 402 code will be printed if some error is detected during HTTP post request.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

  • Following lines are used when you want to make a request with some JSON
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

  • End the HTTP request.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

Testing

  • Select the right development board in Tools >> Boards >> DOIT ESP32 DevKit V1 in Arduino IDE.
  • Compile and upload the code into ESP32 using Arduino IDE.
  • Make sure that you have entered the right Wi-Fi credentials, API key and event name before uploading the code.
  • Open the serial monitor with a 115200 baud rate as defined in the Arduino code.
  • Press the EN button from the ESP32 development board.
  • On the serial monitor, we can check whether ESP32 is successfully connected to the network or not and whether the HTTP POST request is generated successfully or not.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 14 Serial Monitor

  • Open your IFTTT account and click on My
  • Next, click on View Activity.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 15 View Activity

  • A screenshot of the latest activity is shown below:
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 16 Received data.

 
  • Check your registered email. You should receive an email from IFTTT.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 17 Email Received from IFTTT Server

 

Making an HTTP POST Request (JSON data) from ESP32 to ThingSpeak with Arduino IDE

We have already posted an article on sending sensor readings from ESP32 to ThingSpeak. In this article, we will learn how to send HTTP POST requests from ESP32 to send JSON data to the ThigSpeak server.

ThingSpeak is a web service operated by MathWorks where we can send sensor readings/data to the cloud. We can also visualize and act on the data (calculate the data) posted by the devices to ThingSpeak. The data can be stored in either private or public channels.

Steps to be followed to access ThingSpeak API:

  • First, you need to create a MathWorks Account.
  • To create an account or log in to ThingSpeak (operated by MathWorks) server follow the link: https://thingspeak.com/
  • Click on Get Started for free.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 18 Getting Started for Free

  • Enter your details to create a MathWorks account as shown below:
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 19 Create New Account

  • If you have already created a MathWorks account, then click on Sign in.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 20 MathWorks Sign in

  • Create a channel by clicking on the New Channel
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 21 New Channel

  • Enter the respective details in the channel.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. 22 Create a New Channel

 

Arduino Code

//-----------Libraries

#include <WiFi.h>

#include <HTTPClient.h>

//-----------Network Credentials

const char* ssid = "replace with your network SSID";

const char* password = "replace with netwrok password";

// Domain Name with full URL Path for HTTP POST Request

const char* serverName = "http://api.thingspeak.com/update";

// Service API Key

String apiKey = "Write API Key";

unsigned long lastTime = 0;

unsigned long timerDelay = 5000; //to add delay of 5sec

void setup()

{

Serial.begin(115200);

WiFi.begin(ssid, password); //initialize ESP32 wifi module

Serial.println("Connecting");

while(WiFi.status() != WL_CONNECTED)

{

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.print("Connected to WiFi network with IP Address: ");

Serial.println(WiFi.localIP());

Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");

// Random seed is a number used to initialize a pseudorandom number generator

randomSeed(analogRead(25));

}

void loop()

{

//Send an HTTP POST request after every 5 seconds

if ((millis() - lastTime) > timerDelay)

{

//Check the WiFi connection status

if(WiFi.status()== WL_CONNECTED)

{

WiFiClient client;

HTTPClient http;

http.begin( client, serverName );

http.addHeader("Content-Type", "application/json");

String httpRequestData = "{\"api_key\":\"" + apiKey +

"\",\"field1\":\"" +

String(random(30)) + "\"}";

int httpResponseCode = http.POST(httpRequestData);

Serial.print("HTTP Response code: ");

Serial.println(httpResponseCode);

// Free resources

http.end();

}

else {

Serial.println("WiFi Disconnected");

}

lastTime = millis();

}

}

Code Description

  • Add the server address and API (Write) Key.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

Setup()

  • Inside setup() function, initialize the serial monitor with a 115200 baud rate for debugging purposes. Also initialize the Wi-Fi module using WiFi.begin() function.
  • randomSeed() function is used to generate pseudorandom numbers.
  • Inside the randomSeed() function, the data you want to share will be passed as an argument.
  • The data could be a sensor reading or some analog values.

Loop()

    • Inside the loop function, once the ESP32 board is successfully connected with Wi-Fi, ESP32 will make an HTTP POST request for JSON data.
    • The request will be made after every 5 seconds.
    • In this code, we will share JSON data.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

  • Print the HTTP POST response with the response code.
  • Response code 200 is for successful communication and 402 code will be printed if some error is detected during HTTP post request.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig.

Testing

  • Select the right development board in Tools >> Boards >> DOIT ESP32 DevKit V1 in Arduino IDE.
  • Compile and upload the code into ESP32 using Arduino IDE.
  • Make sure that you have entered the right Wi-Fi credentials, and write the API key before uploading the code.
  • Open the serial monitor with a 115200 baud rate to check whether ESP32 is connected to Wi-Fi or not.
  • Open the ThingSpeak account and check the Channel Stats.
ESP32 HTTP Post with ThingSpeak and IFTTT, ESP32 IFTTT ThingSpeak, HTTP Post in ESP32, ESP32 Post request with Thingspeak and IFTTT, IFTTT and Thingspeak with ESP32

Fig. : data (JSON) Chart on ThingSpeak

 

This concludes the tutorial. I hope you found this of some help and also to see you soon with the new tutorial on ESP32.