Publish telemetry directly from ESP32 to the ThingSpeak Cloud database and write a Python analyzer to process history logs.
Domain
Cloud Databases & Visualization
Difficulty
⭐⭐⭐⭐☆ (Advanced)
Course Module
Cloud & Data Management
Deliverables
Cloud Dashboard Graphs, Python Query Script
1. Cloud Telemetry Data Pipeline Architecture
To enable historical data analysis and remote visualization, we establish a direct cloud data pipeline. The architecture below details the end-to-end telemetry path. The ESP32 reads physical environmental sensors (DHT22 and LDR) and initiates an HTTP connection to the ThingSpeak cloud REST API. Data is structured as parameters in an HTTP GET request. The ThingSpeak database stores this time-series data, updating its dashboard line graphs. A Python script running in VirtualBox queries the Read API via JSON to analyze peak temperature values.
Follow these detailed steps to build the telemetry dashboard in ThingSpeak, set up your Python virtual environment inside the Ubuntu VM, install HTTP dependencies, and execute the analysis query.
STEP 1
Configure Dashboard Widgets in ThingSpeak Cloud
Open your ThingSpeak channel and configure the visual dashboard layout to display incoming sensor telemetry data.
ThingSpeak GUI Configuration:
1. Open your web browser and navigate to https://thingspeak.com
2. Sign in to your account, click "Channels" -> "My Channels" in the top menu, and click on your "IoT Telemetry" channel.
3. Click the "Add Widget" button in the Private View tab.
4. Select "Gauge" from the list of widgets, and click "Next".
5. In the configuration dialog, set:
- Name: "Ambient Temperature"
- Field: Field 1 (Temperature)
- Min: 0, Max: 50
- Display Units: °C
6. Click "Create".
7. Click the "Add Widget" button again, select "Numeric Display", and click "Next".
8. Set:
- Name: "Light Intensity"
- Field: Field 2 (Light)
9. Click "Create". Drag and drop the widgets to organize your dashboard layout.
We configure visual widgets in the ThingSpeak dashboard to display real-time sensor metrics alongside historical line charts for ambient temperature and light intensity.
STEP 2
Launch VM Terminal and Create Dashboard Workspace
Boot up your VirtualBox Ubuntu machine. Open the terminal (Ctrl+Alt+T) and create a directory to organize your telemetry query scripts.
ubuntu@iot-vm:~$ mkdir -p ~/workspace/cloud_dashboard && cd ~/workspace/cloud_dashboard
We run `mkdir -p` and `cd` to generate and enter a dedicated workspace directory named `~/workspace/cloud_dashboard` to isolate our Python telemetry query script.
STEP 3
Install Python Requests Module inside the Virtual Environment
Create an isolated virtual environment and install the Requests library, which you will use to fetch JSON data from the ThingSpeak API.
We initialize the Python virtual environment and install the `requests` library. This module simplifies making HTTP calls to fetch raw JSON telemetry data from ThingSpeak's feeds API.
STEP 4
Create the Python Telemetry Query Script
Open the graphical text editor in the background to write the serial listener script.
We invoke `gedit` alongside the `&` symbol to open the graphical text editor in a background thread, leaving our terminal terminal prompt active for running commands.
STEP 5
Configure API Keys in the ESP32 Firmware
Edit the firmware variables in the Arduino IDE to match your home Wi-Fi credentials and your ThingSpeak Channel Write API Key.
Firmware Settings (Arduino IDE):
1. In the Arduino IDE sketch editor, locate the variables "ssid" and "password". Replace them with your local router credentials.
2. Locate the "apiKey" string variable.
3. Paste your ThingSpeak channel's "Write API Key" (found under the API Keys tab in ThingSpeak) inside the quotes.
4. Click the circular Upload arrow icon in the toolbar to upload the code to the ESP32.
We configure Wi-Fi credentials and the Write API Key in the firmware to ensure the ESP32 can connect to your local network and post sensor telemetry data to your ThingSpeak channel.
STEP 6
Run the Python Query Script to Analyze Telemetry Data
Run the query script in the VM terminal. The script will fetch the last 100 entries from the API and calculate the peak temperature value.
We execute the Python query script, starting the loop that fetches the channel's JSON data feeds from ThingSpeak and calculates the maximum temperature value.
3. ThingSpeak Dashboard Widget Layout Grid
The visual arrangement of dashboard widgets in the web interface is organized in a responsive grid. The layout diagram below represents the grid organization showing how gauges, displays, and historical line charts are positioned.
4. Part 2: Complete Codebases & Line-by-Line Breakdowns
Below is the complete C++ firmware code to run on the ESP32, followed by the complete Python query script to execute inside your VirtualBox Linux VM.
#include <HTTPClient.h>: Imports the ESP32 HTTP client library, enabling the code to format HTTP headers and execute GET/POST request methods.
const char* host = "api.thingspeak.com";: Sets the target host server domain name to resolve the destination IP address.
String apiKey = "YOUR_WRITE_API_KEY";: Defines your channel's Write API Key, which authorizes data updates.
HTTPClient http;: Instantiates an HTTP client object to manage HTTP connection states.
String url = ... + "&field1=" + String(dummyTemp) + ...: Constructs the API endpoint URL by appending sensor readings to the corresponding query parameters.
http.begin(client, url);: Initializes the HTTP connection, target URL, and TCP socket wrapper.
int httpResponseCode = http.GET();: Executes an HTTP GET request and returns the HTTP status code (e.g. 200 for success, negative values for connection errors).
http.getString(): Retrieves the HTTP response payload returned by the server (typically the total count of entries on success).
http.end();: Closes the TCP connection and frees memory allocated for the HTTP client session.
delay(15000);: Pauses the execution loop for 15 seconds. ThingSpeak's free tier enforces a minimum 15-second delay between updates.