The ESP32 microcontroller with Wi-Fi and Bluetooth capabilities is commonly used for Internet of Things (IoT) projects. When it comes to automation, many developers rely on the IFTTT platform. In this article, we’ll explore how to use the ESP32 with ESP-IDF framework to send emails through IFTTT. We’ll cover the steps to set up the ESP32 and IFTTT accounts, create a webhook, and use the Arduino IDE to trigger email notifications. With this guide, you can enhance your IoT projects and automate email alerts effortlessly.
How to Use IFTTT API with HTTP Post Request from EPS32
In this article, we will learn how to send data from an ESP32 to IFTTT using HTTP POST requests. IFTTT, which stands for “If This, Then That,” is a popular service that allows users to automate tasks based on certain conditions. We will explore how to set up the ESP32 to send data to IFTTT, which will then trigger email notifications based on the received data. By following the steps outlined in this article, you can easily create a system that sends automatic email alerts using your ESP32 and IFTTT.
IFTTT is a platform that lets you connect different services together. They have an API (a way for software to communicate with each other) that allows you to interact with their service. One of the ways to do this is by using a HTTP POST request, which is a common way to send information to a website or service. This request can trigger (start) an action or event in IFTTT.
To use the HTTP POST endpoint, you need to create an applet on IFTTT and get a unique webhook URL. This URL can trigger the applet by sending a JSON payload to the IFTTT server using an HTTP POST request.
The JSON payload should contain three fields: “value1”, “value2”, and “value3”. These fields are used to pass data to the applet. You can use them to send different types of information, like sensor readings or device status updates.
To accomplish the task of sending an HTTP POST request to the IFTTT API, we shall utilize the HTTP client library on ESP32 for initiating an HTTP POST request towards a distinct webhook URL.
Creating an IFTTT Account
To use IFTTT, you need to create an account on their website: https://ifttt.com/. It’s free to sign up!
Click the ‘Get Started’ button on the window that appears.
You a window where you can choose to connect with Apple, Google, or Facebook. Alternatively, you can sign up using your email.
To get started, simply click on the ‘sign up’ option. You’ll then see a window appear where you can enter your email address and create a password. This process is completely free for the first three applets.
Creating an Applet:
Once you’ve signed up, you’ll be taken to the app creation page. Click on ‘Create’ to proceed.
Click the “Add” button in the “If This” section that appears when you open the window.
In the next page, you will see various services to choose from. Look for the “webhooks” option by typing it in the search bar. Once you find it, click on its icon.
To set up the trigger, select ‘Receive a web request’. This trigger will activate whenever a web request is received. In the ‘THAT’ section, you can define the action that will take place.
After clicking the “Receive a web request” button, a window will open up. In this window, we will write “sensor_readings” as the event name for the web request. Of course, you can use any other name of your choice. Once you’ve written the event name, click the “Create Trigger” button.
Next, we go back to the webpage where we first added the service for the ‘IF THIS’ section. Then, we click the ‘ADD’ button for the ‘THEN THAT’ section.
Setting up Service
Let’s choose the “email” service as the action when a web request is received. This means that we will send an email whenever a POST request is received.
The following window opens up. We will click on ‘Send me an email’ as highlighted in the red box.
Click on the ‘Connect’ button as shown below.
Next, write down your email address and click ‘Send Pin’ as shown below:
After you successfully enter the PIN, a new window will open up. Fill in all the entries as given below and then click on the ‘Create Action’ button.
After we have created the action, we will be guided towards the initial web page of IFTTT. Click ‘Continue’ to proceed.
After this click the Finish button. Make sure to turn ON the notifications when the applet is running.
You have successfully created the applet as shown below.
Testing the Applet
Go to your and select “My Services” or open a webpage with the link: ifttt.com/my_services. The following windows will appear. Afterward, click on Webhooks.
This will take you to the following web page. Click on ‘Documentation.’
You will receive a key which should be secure with you. Next, enter the details as shown to trigger the event. We will give hypothetical values for the three variables for now. At the end click, ‘Test it.’
If you also received a message with the same readings that you entered then it means that your applet is running successfully.
Create ESP32 ESP-IDF Project to Send Email with IFTTT
In this section, let’s create an ESP-IDF project to send email using the IFTTT service.
Open your VS Code and head over to View > Command Palette. Type ESP-IDF: New Project in the search bar and press enter.
Specify the project name and directory. We have named our project ‘esp32_email_ifttt.’ For the ESP-IDF board, we have chosen the custom board option. For ESP-IDF target, choose ESP32 module. Click ‘Choose Template’ button to proceed forward.
In the Extension, select the ESP-IDF option:
We will click the ‘sample_project’ under the get-started tab. Now click ‘Create a project using template sample_project.’
You will get a notification that the project has been created. To open the project in a new window, click ‘Yes.’
This opens the project that we created inside the EXPLORER tab. There are several folders inside our project folder. This is the same for every project which you will create through ESP-IDF Explorer.
ESP32 ESP-IDF Code to Send Email with IFTTT
The ESP-IDF code implements a function to send email using the IFTTT API. It requires an IFTTT account, which provides an account API key for authentication.
The IFTTT API URL and the data to be sent are constructed from these variables and the auth information. The esp_http_client library is used to send an HTTP POST request to the IFTTT API with the constructed data. The result of the request is checked and, if successful, a message is logged indicating that the message was sent successfully. If an error occurs, a message indicating failure is logged. The function is invoked as a task created by the main function after connecting to a Wi-Fi network.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_http_client.h"
#include "protocol_examples_common.h"
static const char *TAG = "HTTP_CLIENT";
// Your Account authentication key and even name
char ifttt_api_key[] = "enter_iftt_api_key_here";
char ifttt_event_name[] = "BME280";
void ifttt_send_email(void *pvParameters)
{
char ifttt_url[200];
snprintf(ifttt_url,
sizeof(ifttt_url),
"%s%s%s%s",
"https://maker.ifttt.com/trigger/",
ifttt_event_name,
"/with/key/",
ifttt_api_key);
char post_data[200];
snprintf(post_data, sizeof(post_data), "{\"value1\":\"%d\",\"value2\":\"%d\",\"value3\":\"%d\"}", 5, 5, 5);
esp_http_client_config_t config = {
.url = ifttt_url,
.method = HTTP_METHOD_POST,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_header(client, "Content-Type", "application/json");
ESP_LOGI(TAG, "post = %s", post_data);
esp_http_client_set_post_field(client, post_data, strlen(post_data));
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
int status_code = esp_http_client_get_status_code(client);
if (status_code == 201)
{
ESP_LOGI(TAG, "Email sent Successfully");
}
else
{
ESP_LOGI(TAG, "Email sent Failed");
}
}
else
{
ESP_LOGI(TAG, "Email sent Failed");
}
esp_http_client_cleanup(client);
vTaskDelete(NULL);
}
void app_main(void)
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(example_connect());
ESP_LOGI(TAG, "Connected to AP, begin http example");
xTaskCreate(&ifttt_send_email, "ifttt_send_email", 8192, NULL, 6, NULL);
}
How Code Works?
These lines of code include several headers such as “stdio.h”, “string.h” and “stdlib.h” which provide basic input/output operations, string manipulation, and memory allocation functions respectively.
It also includes headers specific to the ESP32 microcontroller such as “esp_log.h”, “nvs_flash.h”, “esp_event.h” and “esp_netif.h”. These headers provide functions for logging, non-volatile storage, event handling, and network interface management.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_http_client.h"
Define two character arrays to store the authentication key and event name. These are string literals that are used to send an email message through the IFTTT API.
// Your Account authentication key and even name
char ifttt_api_key[] = "enter_iftt_api_key_here";
char ifttt_event_name[] = "BME280";
IFTTT Send Email Function
This function is responsible for sending email using IFTTT API and ESP-IDF HTTP client library. The function ifttt_send_email() takes in a void pointer as a parameter.
void ifttt_send_email(void *pvParameters)
Declares a character array ifttt_url of size 200, which is used to store the URL for the IFTTT API endpoint for sending email messages.
char ifttt_url[200];
Constructs the URL for the IFTTT API endpoint by concatenating the various string literals and the values of the ifttt_event_name and ifttt_api_key variables. The result is stored in the ifttt_url array.
snprintf(ifttt_url,
sizeof(ifttt_url),
"%s%s%s%s",
"https://maker.ifttt.com/trigger/",
ifttt_event_name,
"/with/key/",
ifttt_api_key);
Declares a character array post_data of size 200, which is used to store the data that will be posted to the IFTTT API endpoint.
char post_data[200];
Constructs the data that will be posted to the IFTTT API endpoint by concatenating the various string literals and the values of the variables such as value1, value2, and value3. The result is stored in the post_data array.
snprintf(post_data, sizeof(post_data), "{\"value1\":\"%d\",\"value2\":\"%d\",\"value3\":\"%d\"}", 5, 5, 5);
Declares a structure config of type esp_http_client_config_t and initializes it with the values of the ifttt_url variable and HTTP_METHOD_POST constants.
esp_http_client_config_t config = {
.url = ifttt_url,
.method = HTTP_METHOD_POST,
};
Call the esp_http_client_init function, pass it a pointer to the config structure, and store the resulting client handle in the client variable.
esp_http_client_handle_t client = esp_http_client_init(&config);
Calls the esp_http_client_set_header function, passing it the client handle and a string literal, to set the value of the Content-Type header for the request.
esp_http_client_set_header(client, "Content-Type", "application/x-www-form-urlencoded");
Next set the post field of the client to the post_data array and its length. Finally, performs the HTTP request using the esp_http_client_perform() function, and assigns the result to the err variable.
esp_http_client_set_post_field(client, post_data, strlen(post_data));
esp_err_t err = esp_http_client_perform(client);
At the end code check whether the err variable equals ESP_OK or not which checks if HTTP request to IFTTT API was successful or not. If err equals ESP_OK, the code checks the status code of the response. If the status code is 200, print a message indicating that the SMS was sent successfully. If the status code is not 200, print a message indicating that the message was sent failed.
If err does not equal ESP_OK, that means the HTTP request was not successful and prints a message indicating that the message was sent failed.
The esp_http_client_cleanup function is called to free the resources associated with the client variable. The vTaskDelete(NULL) function is called to delete the current task.
if (err == ESP_OK)
{
int status_code = esp_http_client_get_status_code(client);
if (status_code == 200)
{
ESP_LOGI(TAG, "Message sent Successfully");
}
else
{
ESP_LOGI(TAG, "Message sent Failed");
}
}
else
{
ESP_LOGI(TAG, "Message sent Failed");
}
esp_http_client_cleanup(client);
vTaskDelete(NULL);
In summary, this function sends emails using IFTTT API and ESP32 ESP-IDF. It uses HTTP protocol to communicate with the IFTTT API to send a email.
This function prepares the URL and the post data required for the HTTP request. It initializes an HTTP client, sets the header and username/password for the authentication, sets the post field with the message body, performs the HTTP request, and checks the response status. If the status code is 200, it logs a success message, otherwise, it logs a failure message. Finally, it cleans up the HTTP client and deletes the task.
Main App Function
The “app_main” is the entry point of the application. It initializes the Non-Volatile Storage (NVS) flash on the ESP32 microcontroller, which is used to store data that needs to survive a power cycle. After that, it calls “nvs_flash_init” to initialize the NVS flash and checks the return value. If the return value is “ESP_ERR_NVS_NO_FREE_PAGES” or “ESP_ERR_NVS_NEW_VERSION_FOUND”, it means that the NVS flash needs to be erased and initialized again. Then calls “ESP_ERROR_CHECK” to check if there is an error with the NVS flash initialization.
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
Connects to a wifi network by calling the “example_connect()” function.
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_ERROR_CHECK(example_connect());
ESP_LOGI(TAG, "Connected to AP, begin http example");
If the Wi-Fi connection is successful, it creates a task “ifttt_send_email” which sends email using IFTTT API.
xTaskCreate(&ifttt_send_email, "ifttt_send_email", 8192, NULL, 6, NULL);
ESP-IDF Send Email with IFTTT Demo
In this section, we will see a demo of sending email. First of all, download the complete project from the following link and build the project.
Configure Wi-Fi Credentials
Now head over to the menuconfig. Click the icon shown below. It opens the ESP-IDF SDK Configuration Editor.
Scroll down and open the Example Configuration. Here we can set the configuration parameters for this example according to our needs. This includes the Wi-Fi SSID, Wi-Fi password, and Maximum retry number. Specify the Wi-Fi SSID and password of your router that the ESP32 board will connect to. By default, the maximum retries are set to 5. You can change it according to your preference. After specifying the configuration parameters, click the Save button on the top.
Disable ESP TLS
We are using HTTP instead of HTTPS therefore we need to disable the ESP-TLS component from settings->Components Config>ESP-TL.
To flash your chip, type the following command in the serial terminal. Remember to replace the COM port with the one through which your board is connected.
idf.py -p COMX flash monitor
After the code flashes successfully, you can view all the informational logs. First, the station Wi-Fi is initialized. Then we can view the esp_netif_handlers which includes the IP, mask, and gw addresses. Then we get the log “got IP” followed by the IP address which shows that ESP32 has successfully connected with your Wi-Fi network.
Now if everything works fine, you will get messages on the console that “Email sent Successfully” as shown below.
Next, open your email and check for a new email from IFTTT. Inside it, you will be able to view three random values.
You may also like to read:
- ESP32 ESP-IDF Send Messages to WhatsApp Number
- ESP32 ESP-IDF Send Sensor Readings to ThingSpeak Cloud Platform
- ESP32 ESP-IDF SPIFFS Web Server
- ESP32-CAM ESP-IDF Live Streaming Web Server
- ESP32-CAM ESP-IDF Take Photos and Save into SD Card
- ESP32 ESP-IDF MQTT Publish BME680 Sensor Readings
- ESP32 BME680 Web Server ESP-IDF
We are a team of experienced Embedded Software Developers with skills in developing Embedded Linux, RTOS, and IoT products from scratch to deployment with a demonstrated history of working in the embedded software industry. Contact us for your projects: admin@esp32tutorials.com
hello Mr. Bilal. your projects that “thingspeak” and ” send with ifttt ” with esp-idf are not working. all things missing in project folder. Sorry but i think you should work to much than now.