ESP32 & ThingSpeak Analytics

Building Automated Email Alerts for IoT Sensor Data

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.

Real-world scenario: In industrial or environmental monitoring systems, you often need to be notified immediately when a sensor value (temperature, humidity, pressure, etc.) crosses a critical threshold. Instead of manually watching a dashboard, ThingSpeak can automatically send an email alert the moment a limit is exceeded.

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.

Focus of this guide: We will combine MATLAB Analysis (to compose and send the email) with React (to detect the condition and trigger the analysis).

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.

  1. Log in to your ThingSpeak account
  2. Click on your Profile icon/name (not the channel page)
  3. Locate the section labeled Alerts API Key
  4. Copy the key — it always begins with the letters TAK followed by a string of characters
  5. Keep this key ready to paste into your MATLAB Analysis code
Important: This key is different from your regular Channel API Key found on the channel's API Keys tab. The Alert API Key is account-level and specifically used for triggering notifications.

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

  1. Go to Apps → MATLAB Analysis
  2. Click New
  3. Select the Custom template
  4. Delete the default placeholder code
  5. Paste the alert script shown below into the editor
  6. Replace the alert_api_key value with your own copied Alert API Key
  7. Edit the alert_subject and alert_body text to match your use case
  8. Name the analysis (e.g., "My MATLAB Email Code") so it's easy to identify later
  9. 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);
How it works: The script assembles a small JSON payload containing the 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.
Security tip: Never share your 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

  1. Go to Apps → React
  2. Click New React
  3. Give the Reaction a name, e.g. "Temperature Exceed Alert"
  4. Set Condition Type to Numeric
  5. Select Test Frequency — choose on data insertion for instant testing, or every 10 minutes for production use
  6. Set the condition: if Channel [YourChannel], Field [FieldNumber] Temperature is greater than [ThresholdValue]
  7. Choose the Action type: select MATLAB Analysis
  8. Select the specific analysis you created earlier (e.g., "My MATLAB Email Code")
  9. Set Run Action to only the first time the condition is met
  10. Click Save
Why "only the first time"? On a free ThingSpeak account, choosing "each time the condition is met" can rapidly generate excessive requests, potentially causing your account to be temporarily restricted. For testing and low-traffic applications, "first time only" is safer.

Visual Flow of the Alert System

ESP32 Sensor
(DHT11/DHT22)
ThingSpeak Channel
(Data Upload)
React
(Condition Check)
MATLAB Analysis
(Compose Email)
Email Alert
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.

  1. Open your email inbox in a separate window/tab
  2. Note the current temperature reading shown on the ThingSpeak channel view
  3. Apply gentle heat near the sensor (e.g., warm air) to raise the temperature reading
  4. Watch the ThingSpeak dashboard update in near real-time as temperature rises
  5. Once the value exceeds the threshold, ThingSpeak triggers the React condition
  6. The MATLAB Analysis executes and sends the email
  7. Check your inbox for the alert message with subject and body as defined earlier
ParameterExample Value
Baseline Temperature~29–30 °C
Threshold SetGreater than 31 °C
Trigger Achieved31.2 °C
ResultEmail received instantly with alert subject and message
Success Indicator: A working system will deliver an email almost immediately after the sensor crosses the defined threshold, containing the subject and body text you configured in the MATLAB code.

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.

  1. Make your ThingSpeak channel public (via Channel Settings → Sharing)
  2. Note your Channel ID number (visible on the channel's public view page)
  3. Install the ThingView app from your phone's app store
  4. Open the app and enter your Channel ID
  5. View live sensor charts directly on your smartphone
This is extremely useful when demonstrating a prototype to a client before final hardware installation — you can show live, real-time data visualization on a mobile device instantly.

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.

Licensing Reminder: ThingSpeak's free account is intended for personal, educational, and demonstration use only. For commercial deployment, a paid license from MathWorks is required.
Key Takeaway: Even on a free account, you can build a fully functional proof-of-concept — live dashboards, mobile visualization via ThingView, and automated email alerts — making it an excellent tool for client demos before committing to full-scale hardware deployment.

Continue experimenting with different condition types, multiple Reactions, and combining ThingHTTP or TalkBack apps to build increasingly sophisticated IoT automation pipelines.