ESP32 Devlog 12 — Data Logging
It might help you in a contest
In the previous web project, we make a simple barometric sensor reading with BMP180 and visualize it using highcharts. Here’s the previous link to the old projects related to this devlog:
- ESP32 Devlog 3 — Barometric Sensor
- ESP32 Devlog 8 — Wifi LED Switch
- ESP32 Devlog 9 — Real-Time Weather Station Web
- ESP32 Devlog 10 — SPIFFS Filesystem
- ESP32 Devlog 11 — Data Visualization
In this project, i want to log the data readings from BMP180 sensor using IFTTT service. Like the previous devlog, i want to focus the data logging project. The ESP32 and BMP180 schematic diagram can be found on my previous ESP32 Devlog 3.
If This Then That, also known as IFTTT, is a freeware web-based service that creates chains of simple conditional statements, called applets. An applet is triggered by changes that occur within other web services such as Gmail, Facebook, Telegram, Instagram, or Pinterest. You can visit the IFTTT website here.
In this project, data logging with Google Sheets require integration which requires an HTTPS authentication. The easiest way to integrate with Google Sheets is using IFTTT or other related 3rd party service.
This project is guided by an Random Nerd Tutorials article about ESP32 Publish Sensor Readings to Google Sheets.
- First, the ESP32 connects to the wireless network
- Then, the BMP180 reads the temperature and atmospheric pressure
- The ESP32 communicates with the IFTTT Webhooks service that publishes the reading to a spreadsheet on Google Sheets that is saved in the Google Drive folder
- After publish the readings, the ESP32 delays the reading by specified amount of time and the process repeats.
A. Requirements and To-Do
These are the requirements for this project assuming you have your own personal Google Account:
Software Requirement
- Arduino IDE with ESP32 Board Support
- Modern Browser that can open google sheets without problem
Hardware Requirement
- Breadboard
- Jumper Cable
- ESP32
- BMP180
Service Requirement
- Adafruit BMP180 sensor library
- IFTTT
- Google Sheets
To-Do
- Make an IFTTT Account using Google Account
- Test the IFTTT Webhooks service
- Use the ESP32 and BMP180 to read barometric data and passes the readings to IFTTT Webhook service to log the readings to Google Sheets
Most of the links refer to the Random Nerd Tutorials, check the blog out for more reference and technical detail. This devlog is not a tutorial, rather it keeps me log the troubles i’ve got so far.
B. Make an IFTTT Account and Start Webhooks to Google Sheets Service
Go to ifttt.com, Use your google account, get started, it’s free.
Create an applet by clicking the human head looking icon on the top right.
Click the “This” word
Type “webhook” in the input form and click the big blue rectangle webhooks icon
Click connect
Then there was an blue button element which titled “Receive a web request” in Choose Trigger page. Click on it.
You will be taken to complete trigger fields page. Enter the Event name.
Then, click on the “That” button
Fill the input form with “sheets” keyword and you will get the dark blue Google Sheets button. Click on it.
Connect to the google sheets and select the appropriate account. Allow the IFTTT permissions to Google Services
Then, click the button titled “Add row to spreadsheet”
Fill out the forms, the Spreadsheet name, formatted row, and drive folder path. Leave the drive folder path empty to default. After that, click the Create Action Button
Then, you’ll arrived at Review and Finish page. Turn off the notifications and click the Finish Button.
C. Test the Webhook Service
First, go to the Webhook page in the IFTTT. the click the documentation button on the top left beside the settings button.
The button will take you to settings page. You may test the event by filling out the form and check your corresponding google sheets.
D. Arduino Code
First, include the necessary libraries to the sketch.
#include <WiFi.h>
#include <Adafruit_BMP085.h>
#include "SPIFFS.h"
#include "ESPAsyncWebServer.h"
Then, create an instance to communicate with BMP180 Sensor using I2C:
Adafruit_BMP085 bmp;
The thing you would like to modify is the ssid and the password to connect to the wireless network you use. Then, change the value of resource constant equals to the unique IFTTT URL Resource.
// Replace with your SSID and Password
const char* ssid = "TP-LINK_890E7A";
const char* password = "suryanaga";// Replace with your unique IFTTT URL resource
const char* resource = "/trigger/bmp180_readings/with/key/dO1uekEnRGjVGzi245KCYn";
Then, you may set the sleep time for the deep sleep. The deep sleep method is another way to utilize the delay method in loop, but it makes the ESP32 sleep and thus, you don’t have to worry about the loop() function.
// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// sleep for x / 60 minutes = x seconds
uint64_t TIME_TO_SLEEP = 1800; // x
In the setup(), initialize the serial baud rate, BMP180 sensor and SPIFFS filesystem
Serial.begin(115200);// Hardware Checking
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085 sensor, check wiring!");
while (1) {}
}
Then, connect to WiFi and print the IP Address in the Serial Monitor
// Connect to WiFi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
Serial.println("IP Address: ");// Local IP Address
Serial.println(WiFi.localIP());
You might notice the makeIFTTTRequest function in the setup block. Create said function / procedure in a seperate block. Then, after make an IFTTT request, set the ESP32 to deep sleep
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to sleep now");
esp_deep_sleep_start();
The loop block remains empty.
The makeIFTTTRequest Procedure
The HTTP request to the IFTTT service is similiar to my previous devlog about the HTTP Connection. The different thing in this project is instead of text/html data, we are sending POST (not GET) request to the IFTTT service which consists of an application/json data. We construct the jsonObject
// Temperature in Celsius
String jsonObject = String("{\"value1\":\"") + bmp.readTemperature() + "\",\"value2\":\"" + (bmp.readPressure()/100000.0F)+ "\"}";
client.println(String("POST ") + resource + " HTTP/1.1");
client.println(String("Host: ") + server);
client.println("Connection: close\r\nContent-Type: application/json");
client.print("Content-Length: ");
client.println(jsonObject.length());
client.println();
client.println(jsonObject);
The ESP32 will send the data to the IFTTT and the IFTTT will pass the data to the Google Sheets which can be saved for later use.
E. Conclusion and Next Project
The next project would be an Input Data from HTML Form, which in extension could be passed to an existing database service such as MongoDB Atlas Cluster. So far i haven’t encountered a problem from this project. The data logging could be expanded if you have several sensor reading across the landscape.