The ESP32 is a powerful microcontroller with built-in Wi-Fi, making it perfect for IoT applications. One useful feature is its ability to send HTTP requests, allowing it to interact with web services, APIs, and automation platforms like IFTTT (If This Then That).
In this guide, you’ll learn how to send HTTP GET and POST requests with the ESP32 using the Arduino IDE to trigger IFTTT webhooks and automate tasks.
What Are HTTP Requests?
The Hypertext Transfer Protocol (HTTP) is the foundation of data communication on the web. When your ESP32 connects to the internet, it can interact with web services and APIs using HTTP requests.
This guide will deal with the two most common types of HTTP requests: HTTP GET and HTTP POST.
However, keep in mind that there are more request methods, like PUT and DELETE.
HTTP GET requests are used to retrieve data from a server, for example for getting weather data from an API. Also, your browser always performs a GET request when visiting a website.
Using POST requests, we can send data to a server, for example, to submit sensor data to a cloud database or trigger an event.
Setting UP IFTTT Webhooks
Important: IFTTT moved the Webhook trigger to their Pro tier and isn’t available for free anymore!
Step 1: Create an IFTTT Account
Before you can automate tasks using webhooks and an ESP32, you need to create an IFTTT (If This Then That) account. You can do so on the IFTTT website.
Step 2: Configure the Webhook Trigger
After creating your account, you can start implementing automation tasks by configuring a webhook trigger.
Click on Create → If This → search for “webhooks” → select Receive a web request. Then, give a name to your event and hit Create trigger.
#1
#2
#3
Step 3: Choose an Action
Next, it’s time to choose what should happen if the webhook is triggered by an HTTP request from our ESP32. IFTTT provides plenty of services to perform various actions like sending an E-Mail, sending a push message to your phone, and a lot more.
To choose your action, hit “Then That” and connect your service as described by IFTTT.
Step 4: Get Your Webhook URL
We need a URL to send an HTTP request to trigger the webhook. The URL for the webhook you set up can be found in the webhook documentation.
This is the URL, you are looking for:
https://maker.ifttt.com/trigger/YOUR_EVENT_NAME/json/with/key/YOUR_KEY
Always specify your key at the end of the URL. Also, keep that key private!
How to Send an HTTP POST Request with ESP32
If you need help setting up the ESP32 in Arduino IDE, check out this helpful guide.
You can use the following sketch to send an HTTP POST request to an API or IFTTT webhook trigger.
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* url = "https://maker.ifttt.com/trigger/esp32_event/with/key/YOUR_IFTTT_KEY";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Connect to Wi-Fi
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Send a POST request with JSON data
String jsonData = "{\"value1\":\"ESP32\",\"value2\":\"Temperature: 25C\"}";
int httpResponseCode = http.POST(jsonData);
Serial.println(httpResponseCode);
http.end();
}
}
void loop() {
}
How To Send an HTTP GET Request with ESP32
If you need help setting up the ESP32 in Arduino IDE, check out this helpful guide.
Use the following sketch to send a GET request. For instance, to trigger an IFTTT webhook.
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* url = "https://maker.ifttt.com/trigger/esp32_event/with/key/YOUR_IFTTT_KEY";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
// Connect to Wi-Fi
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
// Send HTTP GET request
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url);
int httpResponseCode = http.GET();
Serial.println(httpResponseCode);
http.end();
}
}
void loop() {
}
Troubleshooting Common Issues
Failed to connect to Wi-Fi
If your ESP32 cannot connect to a Wi-Fi network, check whether the SSID and Wi-Fi password is correct and the Wi-Fi network you are trying to connect to is within the range of your ESP32 device.
HTTP request fails
If your HTTP GET or POST request fails to send or doesn’t return with a 200 (OK) response code, check whether you are connected to Wi-Fi properly and whether the URL with all data and API keys is correct.
Conclusion
In this guide, you’ve successfully learned how to send ESP32 HTTP requests to trigger IFTTT webhooks and other web services. With these techniques, you can automate various tasks and build useful IoT devices.
Next, check out how to host a web server on your ESP32!
What are you using HTTP requests for with your ESP32? Share your experiences in the comments!
Thanks for reading, happy making!
Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.