Deploy a real-time Z-score statistical classification model directly on an ESP32 to detect sensor anomalies.
Domain
Edge AI / Signal Processing
Difficulty
⭐⭐⭐⭐☆ (Advanced)
Course Module
Edge AI & TinyML
Deliverables
Edge Classifier Firmware, Testing Dashboard
1. Edge AI Classification & Buffer Topology
Cloud-based anomaly detection introduces latency and requires continuous network access. To run diagnostics locally, we implement an Edge AI pipeline on the ESP32. The diagram below details the processing flow. The firmware stores a rolling buffer of the last 10 sensor readings in RAM. For each new reading, the CPU calculates the mean and standard deviation of the window, then computes the Z-score. If the value falls outside the threshold (Z > 2.5), the ESP32 flags an anomaly and activates a buzzer alert.
2. Part 1: Step-by-Step Individual Code Implementation & Verification Steps
Follow these detailed steps to set up the Edge AI classification workspace inside your VirtualBox VM, compile the C++ firmware, and verify detection logs.
STEP 1
Launch VM Terminal and Create Edge AI Folder
Boot up your VirtualBox Ubuntu machine. Open the terminal (Ctrl+Alt+T) and create a directory to organize your C++ and Python source files.
ubuntu@iot-vm:~$ mkdir -p ~/workspace/edge_ml && cd ~/workspace/edge_ml
We run `mkdir -p` and `cd` to generate and enter a dedicated workspace directory named `~/workspace/edge_ml` to isolate our Edge AI testing scripts.
STEP 2
Implement Rolling Window Buffer in Firmware
Declare a rolling window array in your C++ sketch to store the latest sensor readings.
C++ Code snippet to write:
#define WINDOW_SIZE 10
float window[WINDOW_SIZE];
int windowIndex = 0;
bool bufferFull = false;
void insertValue(float value) {
window[windowIndex] = value;
windowIndex = (windowIndex + 1) % WINDOW_SIZE;
if (windowIndex == 0) bufferFull = true;
}
We implement a circular buffer in C++ using modulo division to continuously update the array with the latest sensor values.
STEP 3
Implement Mean & Standard Deviation Calculations
Add functions to calculate the statistical mean and standard deviation of the current buffer window.
C++ Code snippet to write:
float getMean() {
float sum = 0;
for (int i = 0; i < WINDOW_SIZE; i++) sum += window[i];
return sum / WINDOW_SIZE;
}
float getStdDev(float mean) {
float sumSqDiff = 0;
for (int i = 0; i < WINDOW_SIZE; i++) {
sumSqDiff += pow(window[i] - mean, 2);
}
return sqrt(sumSqDiff / WINDOW_SIZE);
}
We add functions to compute the statistical mean and standard deviation, which are required to evaluate the Z-score of new readings.
STEP 4
Implement Z-Score Classification Logic
Compare new sensor readings against the rolling window's historical data, flagging anomalies if the Z-score exceeds 2.5.
We implement the classification logic, using a threshold check to flag readings that deviate significantly from the rolling historical average.
STEP 5
Compile and Upload the Firmware
Upload the completed firmware to your ESP32 using the Arduino IDE. Keep the USB serial connection active.
IDE GUI Operations:
1. Open the Arduino IDE.
2. Select your target port ("/dev/ttyUSB0" on Linux VM or "COM3" on Windows).
3. Click the circular Upload arrow icon in the toolbar.
4. Once uploaded, go to "Tools" -> "Serial Monitor". Configure the baud rate to 115200.
We flash the firmware to run the statistical anomaly detection loop on the ESP32, and open the serial monitor to view real-time calculations.
STEP 6
Test Anomaly Detection with a Temperature Spike
Warm the temperature sensor (e.g. by breathing on it or pressing it with your finger) to simulate a sudden environmental spike. The onboard LED should light up immediately.