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. | Components | Distributor | Link To Buy | |
1 | ESP32 | Amazon | Buy Now |
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:
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:
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.
Fig. 3 IFTTT
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:
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
Enter the following link in the web browser: https://ifttt.com
Fig. 4 Creating an Applet
Fig. 5 ” If This”
Fig. 6 Search and Select Webhooks
Fig. 7 Receive a Web Request
Fig. 8 Create Trigger
Fig. 9 Then that
Fig. 10 Selecting a Service
Fig. 11
Fig. 12 To Trigger an Event
Fig. 13 Event Successfully Triggered
#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();
}
}
Fig. Libraries
Fig. Network Credentials
Fig.
Fig.
Fig.
Fig.
Fig
Fig.
Fig.
Fig.
Fig. 14 Serial Monitor
Fig. 15 View Activity
Fig. 16 Received data.
Fig. 17 Email Received from IFTTT Server
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:
Fig. 18 Getting Started for Free
Fig. 19 Create New Account
Fig. 20 MathWorks Sign in
Fig. 21 New Channel
Fig. 22 Create a New Channel
//-----------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();
}
}
Fig.
Fig.
Fig.
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.