1. Introduction: Why Analytics Matter
Once your ESP32 project is successfully sending sensor data to ThingSpeak and you can view it as charts and numeric displays, the natural next step is analytics — turning raw data into automated, actionable responses.
This tutorial walks through the exact process of configuring ThingSpeak's analytics tools — MATLAB Analysis and React (Reactions) — to trigger an automatic email whenever a sensor reading crosses a defined threshold.
2. Overview of ThingSpeak Apps
Inside the Apps menu on ThingSpeak, several tools are available for extending the basic dashboard into a fully automated analytics system. Here is a breakdown of each:
MATLAB Analysis
Allows you to write and run a block of MATLAB code against your channel data. This is the core tool used in this tutorial to check sensor values and trigger an email.
MATLAB Visualization
Lets you write MATLAB code purely to generate custom visualizations/plots of your channel data (not used in this tutorial).
Plugins
Includes ready-made plugins such as multi-series charts and Google Gauge visuals. The Custom plugin option lets you write your own HTML/JavaScript to build fully customized visual widgets.
TimeControl
Used to trigger scheduled actions at specific times — for example, running a task automatically at the end of a factory shift.
React (ThingSpeak Reactions) Used in this tutorial
Monitors your channel data and automatically performs an action — such as sending an email — when a defined condition becomes true.
ThingHTTP / ThingTweet / TalkBack
Additional integrations for sending HTTP requests, posting to Twitter, or queuing commands back to your device.
3. Step 1 — Retrieve Your Alert API Key
Before writing any code, ThingSpeak requires an Alert API Key to permit sending automated email alerts from your account.
- Log in to your ThingSpeak account
- Click on your Profile icon/name (not the channel page)
- Locate the section labeled Alerts API Key
- Copy the key — it always begins with the letters
TAKfollowed by a string of characters - Keep this key ready to paste into your MATLAB Analysis code
4. Step 2 — Create the MATLAB Analysis Code
The MATLAB Analysis app is where you define what happens when an alert condition is met — in this case, composing and sending an email through the ThingSpeak Alerts REST API.
Navigation Steps
- Go to Apps → MATLAB Analysis
- Click New
- Select the Custom template
- Delete the default placeholder code
- Paste the alert script shown below into the editor
- Replace the
alert_api_keyvalue with your own copied Alert API Key - Edit the
alert_subjectandalert_bodytext to match your use case - Name the analysis (e.g., "My MATLAB Email Code") so it's easy to identify later
- Click Save
MATLAB Analysis Code
This script builds a JSON message and posts it to the ThingSpeak Alerts endpoint using your Alert API Key.
alert_body = 'This is the text that will be emailed'; alert_subject = 'This will be the subject of the email'; alert_api_key = 'TAK5Q7V3N5EEH07FXD658'; alert_url = "https://api.thingspeak.com/alerts/send"; jsonmessage = sprintf(['{"subject": "%s", "body": "%s"}'], alert_subject, alert_body); options = weboptions("HeaderFields", {'Thingspeak-Alerts-API-Key', alert_api_key; 'Content-Type', 'application/json'}); result = webwrite(alert_url, jsonmessage, options);
subject and body of the email, attaches your Alert API Key as a custom HTTP header, and sends it via webwrite to ThingSpeak's /alerts/send endpoint — which then dispatches the email to the address linked to your account.
alert_api_key publicly (e.g., in public repositories or forums). Anyone with this key can send alerts through your account.
5. Step 3 — Configure the React (Reaction)
The React app is the "trigger" that watches your channel data continuously and decides when to run the MATLAB Analysis you just created.
Navigation Steps
- Go to Apps → React
- Click New React
- Give the Reaction a name, e.g. "Temperature Exceed Alert"
- Set Condition Type to Numeric
- Select Test Frequency — choose on data insertion for instant testing, or every 10 minutes for production use
- Set the condition:
if Channel [YourChannel], Field [FieldNumber] Temperature is greater than [ThresholdValue] - Choose the Action type: select MATLAB Analysis
- Select the specific analysis you created earlier (e.g., "My MATLAB Email Code")
- Set Run Action to only the first time the condition is met
- Click Save
Visual Flow of the Alert System
(DHT11/DHT22)
(Data Upload)
(Condition Check)
(Compose Email)
Sent to User
6. Testing the Alert System
To verify the setup works correctly, the sensor's real-world reading needs to actually cross the threshold defined in the React condition.
- Open your email inbox in a separate window/tab
- Note the current temperature reading shown on the ThingSpeak channel view
- Apply gentle heat near the sensor (e.g., warm air) to raise the temperature reading
- Watch the ThingSpeak dashboard update in near real-time as temperature rises
- Once the value exceeds the threshold, ThingSpeak triggers the React condition
- The MATLAB Analysis executes and sends the email
- Check your inbox for the alert message with subject and body as defined earlier
| Parameter | Example Value |
|---|---|
| Baseline Temperature | ~29–30 °C |
| Threshold Set | Greater than 31 °C |
| Trigger Achieved | 31.2 °C |
| Result | Email received instantly with alert subject and message |
7. Bonus — ThingView Mobile App
For quick client demonstrations without deploying a full mobile application, ThingSpeak offers a companion app called ThingView, available for smartphones.
- Make your ThingSpeak channel public (via Channel Settings → Sharing)
- Note your Channel ID number (visible on the channel's public view page)
- Install the ThingView app from your phone's app store
- Open the app and enter your Channel ID
- View live sensor charts directly on your smartphone
8. Conclusion & Best Practices
With MATLAB Analysis and React combined, ThingSpeak's free tier already provides powerful automation capabilities suitable for prototyping and demonstration purposes.
Continue experimenting with different condition types, multiple Reactions, and combining ThingHTTP or TalkBack apps to build increasingly sophisticated IoT automation pipelines.