ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (2023)

In this guide, you’ll learn how to make HTTP POST requests using the ESP32 board with Arduino IDE. We’ll demonstrate how to post JSON data or URL encoded values to two web APIs (ThingSpeak and IFTTT.com).

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (1)

Recommended: ESP32 HTTP GET with Arduino IDE (OpenWeatherMap.org and ThingSpeak)

HTTP POST Request Method

The Hypertext Transfer Protocol (HTTP) works as a request-response protocol between a client and server. Here’s an example:

  • The ESP32 (client) submits an HTTP request to a Server (for example: ThingSpeak or IFTTT.com);
  • The server returns a response to the ESP32 (client);
  • Finally, the response contains status information about the request and may also contain the requested content.

HTTP POST

POST is used to send data to a server to create/update a resource. For example, publish sensor readings to a server.

The data sent to the server with POST is stored in the request body of the HTTP request:

POST /update HTTP/1.1Host: example.comapi_key=api&field1=value1Content-Type: application/x-www-form-urlencoded

In the body request, you can also send a JSON object:

POST /update HTTP/1.1Host: example.com{api_key: "api", field1: value1}Content-Type: application/json

(With HTTP POST, data is not visible in the URL request. However, if it’s not encrypted, it’s still visible in the request body.)

Prerequisites

Before proceeding with this tutorial, make sure you complete the following prerequisites.

Arduino IDE

We’ll program the ESP32 using Arduino IDE, so make sure you have the ESP32 add-on installed.

  • Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)

Other Web Services or APIs

In this guide, you’ll learn how to setup your ESP32 board to perform HTTP requests to ThingSpeak and IFTTT.com. If you prefer to learn with a local solution you can use HTTP with Node-RED. All examples presented in this guide also work with other APIs.

In summary, to make this guide compatible with any service, you need to search for the service API documentation. Then, you need the server name (URL or IP address), and parameters to send in the request (URL path or request body). Finally, modify our examples to integrate with any API you want to use.

1. ESP32 HTTP POST Data (ThingSpeak)

In this example, the ESP32 makes an HTTP POST request to send a new value to ThingSpeak.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (2)

Using ThingSpeak API

ThingSpeak has a free API that allows you to store and retrieve data using HTTP. In this tutorial, you’ll use the ThingSpeak API to publish and visualize data in charts from anywhere. As an example, we’ll publish random values, but in a real application you would use real sensor readings.

(Video) Control ESP32 and ESP8266 GPIOs from Anywhere in the World

To use ThingSpeak API, you need an API key. Follow the next steps:

  1. Go to ThingSpeak.com and create a free account.
  2. Then, open the Channels tab.
  3. Create a New Channel.
ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (3)
  1. Open your newly created channel and select the API Keys tab to copy your API Key.
ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (4)

Code ESP32 HTTP POST ThingSpeak

Copy the next sketch to your Arduino IDE:

/* Rui Santos Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-post-ifttt-thingspeak-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.*/#include <WiFi.h>#include <HTTPClient.h>const char* ssid = "REPLACE_WITH_YOUR_SSID";const char* password = "REPLACE_WITH_YOUR_PASSWORD";// Domain Name with full URL Path for HTTP POST Requestconst char* serverName = "http://api.thingspeak.com/update";// Service API KeyString apiKey = "REPLACE_WITH_YOUR_API_KEY";// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES// For a final application, check the API call limits per hour/minute to avoid getting blocked/bannedunsigned long lastTime = 0;// Set timer to 10 minutes (600000)//unsigned long timerDelay = 600000;// Timer set to 10 seconds (10000)unsigned long timerDelay = 10000;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()); 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(33));}void loop() { //Send an HTTP POST request every 10 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 = "api_key=" + apiKey + "&field1=" + String(random(40)); // 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 = "{\"api_key\":\"" + apiKey + "\",\"field1\":\"" + String(random(40)) + "\"}"; // Send HTTP POST request int httpResponseCode = http.POST(httpRequestData);*/ Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); // Free resources http.end(); } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); }}

View raw code

Setting your network credentials

Modify the next lines with your network credentials: SSID and password. The code is well commented on where you should make the changes.

// Replace with your network credentialsconst char* ssid = "REPLACE_WITH_YOUR_SSID";const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Setting your API Key

Modify the apiKey variable to include your ThingSpeak API key.

String apiKey = "REPLACE_WITH_YOUR_API_KEY";

Now, upload the code to your board and it should work straight away. Read the next section, if you want to learn how to make the HTTP POST request.

HTTP POST Request

In the loop() is where you make the HTTP POST request with URL encoded data every 10 seconds with random data:

// Specify content-type headerhttp.addHeader("Content-Type", "application/x-www-form-urlencoded");// Data to send with HTTP POSTString httpRequestData = "api_key=" + apiKey + "&field1=" + String(random(40));// Send HTTP POST requestint httpResponseCode = http.POST(httpRequestData);

For example, the ESP32 makes a URL encoded request to publish a new value (30) to field1.

POST /update HTTP/1.1Host: api.thingspeak.comapi_key=api&field1=30Content-Type: application/x-www-form-urlencoded

Or you can uncomment these next lines to make a request with JSON data (instead of URL-encoded request):

// 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 POSTString httpRequestData = "{\"api_key\":\"" + apiKey + "\",\"field1\":\"" + String(random(40)) + "\"}";// Send HTTP POST requestint httpResponseCode = http.POST(httpRequestData);

Here’s a sample HTTP POST request with JSON data:

POST /update HTTP/1.1Host: api.thingspeak.com{api_key: "api", field1: 30}Content-Type: application/json

Then, the following lines print the server response code.

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

In the Arduino IDE serial monitor, you should see an HTTP response code of 200 (this means that the request has succeeded).

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (5)
(Video) ESP32 DHT11/DHT22 Asynchronous Web Server (auto updates Temperature and Humidity)

Your ThingSpeak Dashboard should be receiving new random readings every 10 seconds.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (6)

For a final application, you might need to increase the timer or check the API call limits per hour/minute to avoid getting blocked/banned.

2. ESP32 HTTP POST (IFTTT.com)

In this example you’ll learn how to trigger a web API to send email notifications. As an example, we’ll use the IFTTT.com API. IFTTT has has a free plan with lots of useful automations.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (7)

Using IFTTT.com Webhooks API

IFTTT stands for “If This Than That”, and it is a free web-based service to create chains of simple conditional statements called applets.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (8)

This means you can trigger an event when something happens. In this example, the applet sends three random values to your email when the ESP32 makes a request. You can replace those random values with useful sensor readings.

Creating an IFTTT Account

If you don’t have an IFTTT account, go the IFTTT website: ifttt.com and enter your email to create an account and get started. Creating an account on IFTTT is free!

Next, you need to create a new applet. Follow the next steps to create a new
applet:

1. Open the left menu and click the “Create” button.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (9)

2. Click on the “this” word. Search for the “Webhooks” service and select the Webhooks icon.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (10)

3. Choose the “Receive a web request” trigger and give a name to the event. In this case, I’ve typed “test_event”. Then, click the “Create trigger” button.

(Video) Build an ESP8266 Web Server with Arduino IDE - Code and Schematics

4. Click the “that” word to proceed. Now, define what happens when the event you’ve defined is triggered. Search for the “Email” service and select it. You can leave the default options.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (11)

5. Press the “Finish” button to create your Applet.

Testing Your Applet

Before proceeding with the project, it’s important to test your Applet first. Follow the next steps to test it:

1. Search for Webhooks service or open this link: https://ifttt.com/maker_webhooks

2. Click the “Documentation” button.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (12)

A page as shown in the following figure shows up. The page shows your unique API key. You should not share your unique API key with anyone.

3. Fill the “To trigger an Event” section. Then, click the “Test it” button.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (13)

4. The event should be successfully triggered, and you’ll get a green message saying “Event has been triggered”.

5. Go to your Email account. You should have a new email in your inbox from the IFTTT service with the values you’ve defined in the previous step.

If you’ve received an email with the data entered in the test request, it means your Applet is working as expected. Now, we need to program the ESP32 to send an HTTP POST request to the IFTTT service with the sensor readings.

Code ESP32 HTTP POST Webhooks IFTTT.com

After installing the necessary board add-ons and libraries, copy the following code to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for you.

/* Rui Santos Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-post-ifttt-thingspeak-arduino/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.*/#include <WiFi.h>#include <HTTPClient.h>const char* ssid = "REPLACE_WITH_YOUR_SSID";const char* password = "REPLACE_WITH_YOUR_PASSWORD";// Domain Name with full URL Path for HTTP POST Request// REPLACE WITH YOUR EVENT NAME AND API KEY - open the documentation: https://ifttt.com/maker_webhooksconst char* serverName = "http://maker.ifttt.com/trigger/REPLACE_WITH_YOUR_EVENT/with/key/REPLACE_WITH_YOUR_API_KEY";// Example://const char* serverName = "http://maker.ifttt.com/trigger/test_event/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3tC";// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES// For a final application, check the API call limits per hour/minute to avoid getting blocked/bannedunsigned long lastTime = 0;// Set timer to 10 minutes (600000)//unsigned long timerDelay = 600000;// Timer set to 10 seconds (10000)unsigned long timerDelay = 10000;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()); 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(33));}void loop() { //Send an HTTP POST request every 10 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(40)) + "&value2=" + String(random(40))+ "&value3=" + String(random(40)); // 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); // Free resources http.end(); } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); }}

View raw code

(Video) ESP32/ESP8266 Plot Sensor Readings in Real Time Charts - Web Server

Setting your network credentials

Modify the next lines with your network credentials: SSID and password. The code is well commented on where you should make the changes.

// Replace with your network credentialsconst char* ssid = "REPLACE_WITH_YOUR_SSID";const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Setting your IFTTT.com API Key

Insert your event name and API key in the following line:

const char* serverName = "http://maker.ifttt.com/trigger/REPLACE_WITH_YOUR_EVENT/with/key/REPLACE_WITH_YOUR_API_KEY";

Example URL:

const char* serverName = "http://maker.ifttt.com/trigger/test_event/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3t";

HTTP POST Request

In the loop() is where you make the HTTP POST request every 10 seconds with sample data:

// Specify content-type headerhttp.addHeader("Content-Type", "application/x-www-form-urlencoded");// Data to send with HTTP POSTString httpRequestData = "value1=" + String(random(40)) + "&value2=" + String(random(40))+ "&value3=" + String(random(40));// Send HTTP POST requestint httpResponseCode = http.POST(httpRequestData);

The ESP32 makes a new URL encoded request to publish some random values in the value1, value2 and value3 fields. For example:

POST /trigger/test_event/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3tC HTTP/1.1Host: maker.ifttt.comvalue1=15&value2=11&value3=30Content-Type: application/x-www-form-urlencoded

Alternatively, you can uncomment these next lines to make a request with JSON data:

// 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 POSTString httpRequestData = "{\"value1\":\"" + String(random(40)) + "\",\"value2\":\"" + String(random(40)) + "\",\"value3\":\"" + String(random(40)) + "\"}";// Send HTTP POST requestint httpResponseCode = http.POST(httpRequestData);

Here’s an example of HTTP POST request with a JSON data object.

POST /trigger/test_event/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3tC HTTP/1.1Host: maker.ifttt.com{value1: 15, value2: 11, value3: 30}Content-Type: application/json

Then, the following lines of code print the HTTP response from the server.

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

HTTP POST Demonstration

After uploading the code, open the Serial Monitor and you’ll see a message printing the HTTP response code 200 indicating that the request has succeeded.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (14)

Go to your email account, and you should get a new email from IFTTT with three random values. In this case: 38, 20 and 13.

ESP32 HTTP POST with Arduino IDE (ThingSpeak and IFTTT.com) | Random Nerd Tutorials (15)

For demonstration purposes, we’re publishing new data every 10 seconds. However, for a long term project you should increase the timer or check the API call limits per hour/minute to avoid getting blocked/banned.

Wrapping Up

In this tutorial you’ve learned how to integrate your ESP32 with web services using HTTP POST requests. You can also make HTTP GET requests with the ESP32.

If you’re using an ESP8266 board, read:

  • Guide for ESP8266 NodeMCU HTTP GET Request
  • Guide for ESP8266 NodeMCU HTTP POST Request

You might also like reading:

(Video) sensor data upload in cloud | Thingspeak

  • [Course] Learn ESP32 with Arduino IDE
  • ESP32/ESP8266 Send Email Notification using PHP Script
  • Visualize Your Sensor Readings from Anywhere in the World
  • ESP32 Relay Module Web Server

I hope you liked this project. If you have any questions, post a comment below and we’ll try to get back to you.

If you like ESP32, you might consider enrolling in our course “Learn ESP32 with Arduino IDE“. You can also access our free ESP32 resources here.

Thank you for reading.

FAQs

How do I use Ifttt with ESP32? ›

How do I use Ifttt with ESP32?

How do I send data from ESP32 to my website? ›

How do I send data from ESP32 to my website?

Why is my ESP32 not working? ›

Why is my ESP32 not working?

How do you use ESP32 with ThingSpeak? ›

How do you use ESP32 with ThingSpeak?

How do I transfer data from Ifttt to ThingSpeak? ›

How do I transfer data from Ifttt to ThingSpeak?

What can I do with an ESP32? ›

What can I do with an ESP32?

How do I transfer data from ESP32 to WiFi? ›

How do I transfer data from ESP32 to WiFi?

How do I connect my ESP32 to WiFi? ›

How do I connect my ESP32 to WiFi?

How do I transfer data from ESP32 to ESP32? ›

How do I transfer data from ESP32 to ESP32?

How do I connect my ESP32 to my laptop? ›

How do I connect my ESP32 to my laptop?

Where can I find ESP32 port? ›

Where can I find ESP32 port?

What is ESP32 camera? ›

What is ESP32 camera?

How does ESP32 read data from ThingSpeak? ›

How does ESP32 read data from ThingSpeak?

How do I transfer data from ESP32? ›

How do I transfer data from ESP32?

How do I transfer data from ESP32 to AWS? ›

How do I transfer data from ESP32 to AWS?

Can ESP32 connect to phone hotspot? ›

Can ESP32 connect to phone hotspot?

Is ESP32 a microcontroller? ›

Is ESP32 a microcontroller?

Does ESP32 have Bluetooth? ›

Does ESP32 have Bluetooth?

How do I connect Google home to ESP32? ›

How do I connect Google home to ESP32?

What is Webhook URL? ›

What is Webhook URL?

How do I send data to ESP8266 from a website? ›

How do I send data to ESP8266 from a website?

What is ESP32 camera? ›

What is ESP32 camera?

Is ESP32 a microcontroller? ›

Is ESP32 a microcontroller?

Does ESP32-Cam need Wi-Fi? ›

Does ESP32-Cam need Wi-Fi?

What is the difference between ESP32 and ESP32-Cam? ›

What is the difference between ESP32 and ESP32-Cam?

Videos

1. Send Data From ESP to MySQL Database! Updated 2019 | 100% working.
(Gledis Qose)
2. ESP32 Publish Data to Cloud without Wi-Fi (TTGO T-Call ESP32 SIM800L)
(Rui Santos)
3. IOT: Send Data to Thingsboard in five Minutes using Arduino
(Innovation With Mbari)
4. ESP32: ESP-NOW Web Server Sensor Dashboard (ESP-NOW + Wi-Fi)
(Rui Santos)
5. ESP8266 NodeMCU IoT Example (Tagalog)
(Anything_Info-torial)
6. ESP32-CAM with Telegram: Take Photos, Control Outputs, Sensor Readings and Motion Notifications
(Rui Santos)
Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated: 11/11/2022

Views: 6504

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.