Wireless measurement project

Edge AI Stress Sense

Say “measure my stress level”. An ESP32 hears you and tells an ESP8266 over WiFi to start. For 60 seconds both boards stream your pulse, skin conductance and motion to a laptop, where a model trained on the WESAD stress dataset estimates how stressed you are. The answer is spoken back and shown on a live dashboard.

94.7%
stress vs. no-stress accuracy
(leave-one-subject-out, 15 people)
2 boards
ESP32 + ESP8266,
WiFi only, no wires between them
4 sensors
pulse (PPG), skin conductance (GSR),
accelerometer, microphone
60 s
per measurement, the same window
as the WESAD benchmark
The live dashboard: system diagram with active wires, a stress gauge at 100 % high stress, heart rate 104 bpm, and live PPG, skin-conductance, accelerometer and microphone charts
Overview

From a spoken sentence to a stress estimate

Stress shows up in the body before we notice it: the heart beats faster and less variably, and the skin sweats more. This project measures those signals with low-cost sensors, sends them over WiFi, and classifies them with a model trained on a published lab study, triggered entirely by voice.

Speak

The ESP32's microphone detects speech on the device and streams it to the laptop, where speech-to-text and a command matcher recognise the request.

Coordinate

The laptop tells the ESP32 to start. The ESP32 relays START to the ESP8266 over UDP and retries until it gets an ACK.

Collect

For 60 s the ESP8266's LED is on while it samples pulse and skin conductance at 64 Hz. The ESP32 samples motion at 32 Hz.

Analyse

Lost packets are filled in, units are converted, and 44 physiological features are extracted with the same code used for training.

Answer

The classifier returns a stress probability and level. The laptop speaks it and the dashboard shows every detail.

System architecture

Three devices, one WiFi network

The two microcontrollers are never wired together. Everything they exchange, with each other and with the laptop, travels over WiFi. Dashed blue lines are wireless links.

Components

ComponentHardware / technologyRole
ESP32 nodeESP32-WROOM, INMP441 microphone (I2S), MPU6050 (I2C)Voice capture with on-device speech detection, motion sensing, measurement coordinator
ESP8266 nodeWeMos D1 mini, ADS1115 16-bit ADC, analog PPG, Grove GSR, LEDPhysiological sensing at 64 Hz, "collecting" indicator
Laptop serverPython asyncio, websockets, Starlette / UvicornDevice hub, measurement orchestration, feature extraction, inference
VoiceMoonshine (speech-to-text), Piper (text-to-speech), optional local LLM via OllamaSpoken commands and replies
Machine learningscikit-learn: AdaBoost, LDA, Random Forest, Decision Tree, kNNStress classification trained on WESAD
DashboardHTML / CSS / JavaScript, SVG diagram, canvas charts, WebSocketLive visualisation and control in the browser
Hardware

Sensors, boards and wiring

Every sensor runs at 3.3 V. The ESP8266 has only one analog input, but the pulse sensor and the skin-conductance sensor are both analog, so an ADS1115 16-bit ADC digitises both and hands the values over I2C.

ESP32 DevKitC wired to an INMP441 microphone (WS to GPIO25, SCK to GPIO26, SD to GPIO27) and an MPU6050 GY-521 (SDA to GPIO21, SCL to GPIO32), powered from a 3.3 V breadboard rail
ESP32 node. The INMP441 microphone sends audio over I2S; the MPU6050 sends motion over I2C. Both take 3.3 V and ground from the breadboard rail.
WeMos D1 mini wired to an ADS1115 ADC (SCL to D1, SDA to D2); the PPG pulse sensor goes to ADS1115 A0 and the Grove GSR to A1; an LED with a 220 ohm resistor is on D5
ESP8266 node. The pulse sensor and the GSR feed the ADS1115, which the D1 mini reads over I2C. D5 drives the collect LED through a 220 Ω resistor.

Bill of materials

PartQtyPurpose
ESP32 DevKitC (WROOM)1microphone, motion, coordinator
ESP8266 WeMos D1 mini1vitals node
INMP441 I2S microphone1voice commands
MPU6050 (GY-521)13-axis accelerometer + gyroscope
ADS1115 16-bit ADC1reads both analog sensors
Analog PPG pulse sensor1blood volume pulse → heart rate, HRV
Seeed Grove GSR + electrodes1skin conductance (sweat response)
LED + 220 Ω resistor1lit while collecting
Breadboards, jumpers, 2 USB cables, velcro—assembly and wearing

Wiring

DeviceConnection
ESP32
INMP441SCK→GPIO26 · WS→GPIO25 · SD→GPIO27 · L/R→GND · VDD→3V3
MPU6050SDA→GPIO21 · SCL→GPIO32 · AD0→GND · VCC→3V3
Status LEDGPIO2 (on-board), on while measuring
ESP8266 (D1 mini)
ADS1115SDA→D2 · SCL→D1 · ADDR→GND · VDD→3V3
PPG sensorsignal→ADS1115 A0 · +→3V3 · −→GND
Grove GSRSIG→ADS1115 A1 · VCC→3V3 · GND→GND
LEDD5 → 220 Ω → LED → GND

Why these pins

Flash pins

ESP32 GPIO 6–11 (labelled SD0–SD3, CMD, CLK) are wired to the flash chip inside the module. Anything connected there stops the board from being programmed.

Boot pins

ESP32 GPIO 0/2/5/12/15 and ESP8266 D3/D4/D8 set the boot mode. GPIO12 even selects the flash voltage, so sensors stay off these pins.

Input-only pins

ESP32 GPIO 34–39 can't drive a signal or pull up, so they can't be an I2C clock. SCL sits on GPIO32 instead, next to them on the header.

Sensor placement

WESAD's wrist device was worn on the non-dominant wrist, so we copy that: the PPG sits on a fingertip, the GSR electrodes go on two other fingers of the same hand, and the MPU6050 is strapped to the back of that wrist. During a measurement you sit, keep the hand still and stay silent.

Turning raw GSR readings into microsiemens

The ADS1115 count is converted to the Grove's 10-bit-equivalent reading X. The Grove formula R = (1024 + 2X)·10000 / (Xcal − X) gives the skin resistance, and 106/R gives the conductance in µS. Xcal is the not-worn reading, stored once from the dashboard.

Firmware

What runs on each board

Both sketches build in the Arduino IDE. The ESP32 uses FreeRTOS tasks so that audio is never blocked by WiFi; the ESP8266 runs one non-blocking loop with microsecond-timed sampling.

ESP32: three tasks

TaskCore / priorityJob
audio1 / 10Reads 20 ms I2S frames, removes DC offset, runs an energy-based speech detector with an adaptive noise floor and a 300 ms pre-roll
net0 / 5Owns the WebSocket; sends audio in 60 ms batches; a 1.3 s queue rides out WiFi stalls
coordinator0 / 3Samples the MPU6050 at 32 Hz, finds the ESP8266, relays START with retries, reports status every second

ESP8266: one loop

  • Built-in ADS1115 driver (no library): single-shot conversions at 860 SPS, both channels every 15.6 ms → 64 Hz
  • 8 samples per UDP packet, each tagged with a sample index
  • Broadcasts a heartbeat every second, so it needs no IP configuration
  • Collect LED on D5; the on-board LED blinks once per second to show it's alive
  • Ignores duplicate STARTs, stops on STOP or after the duration, then reports DONE

Credentials stay out of the code

WiFi name, password and laptop IP live in a gitignored .env file. A script generates a secrets.h header for each board, which config.h includes at compile time. If it's missing, the build stops with instructions instead of producing firmware that can't connect.

cp .env.example .env            # WIFI_SSID, WIFI_PASSWORD, LAPTOP_IP
python3 tools/make_secrets.py   # → src/secrets.h, esp8266_vitals/src/secrets.h
Wireless communication

The measurement, message by message

Commands use a reliable relay with acknowledgements; sensor data uses lightweight UDP streams whose sample indices reveal every lost packet. If the relay fails, the laptop reaches the ESP8266 directly.

Ports

PortProtocolContent
8765WebSocketESP32 ⇄ laptop: audio, status, commands
4210UDP→ ESP8266: START, STOP
4211UDPESP8266 → laptop: PPG + GSR
4212UDP broadcastESP8266 heartbeat, ACK, DONE
4213UDPESP32 → laptop: MPU6050
8080HTTP + WebSocketdashboard in the browser

Reliability by design

  • Discovery: the ESP32 learns the ESP8266's IP from its broadcast heartbeat.
  • Acknowledged commands: START is re-sent every 250 ms (up to 6×) until ACK, alternating unicast and broadcast.
  • Fallback path: no ACK within 2 s → the laptop sends START itself.
  • Loss accounting: sample indices expose gaps; loss % is shown live and saved with each session, and gaps are interpolated before analysis.
  • Link monitoring: RSSI of both boards, packet rate and loss per stream appear on the dashboard.
P,<id>,<first_index>,8,<ppg>,<gsr>,…     ESP8266 data, 125 ms
A,<id>,<first_index>,8,<ax>,<ay>,<az>,…  ESP32 motion, 250 ms
HB,esp8266,<rssi>,<collecting>,…         heartbeat, 1 Hz
Voice interface

Hands-free control

Speech detection runs on the ESP32: only actual speech is streamed, starting 300 ms before the detected onset so no word gets cut off. On the laptop, Moonshine turns the audio into text.

Commands are matched with fuzzy rules, not by a language model, so they work every time, even when the speech-to-text hears "measure my dress level". Anything else is answered by an optional local assistant.

While the laptop speaks, it mutes the ESP32's microphone, so the system never hears and answers its own voice.

SayEffect
“measure my stress level”60 s measurement → spoken result
“calibrate my baseline”3 min relaxed recording → personal baseline
“check my signals”30 s live signal check, no prediction
“stop”cancels a running measurement
Machine learning

Learning stress from the WESAD study

WESAD (Schmidt et al., 2018) recorded 15 people during a neutral baseline, a stress test (the Trier Social Stress Test: a public speech plus mental arithmetic) and an amusement condition. Our sensors match its wrist device, the Empatica E4, so the model is trained on those wrist signals, at the same sampling rates.

WESAD wrist signalOur sensorRateUsed for
BVP (photoplethysmogram)analog PPG via ADS111564 Hzheart rate, heart-rate variability
EDA (µS)Grove GSR via ADS111564 → 4 Hzskin conductance level and responses
ACC (g)MPU605032 Hzrecorded and shown, not used for the prediction (see below)
TEMP——no sensor

The pipeline

1 · Window

60 s segments inside each condition, 5 s apart → 6124 windows.

2 · Features

44 features. Heart: HR, SDNN, RMSSD, pNN50, LF/HF power. Skin: level, slope, tonic/phasic split, response peaks. Motion: magnitude, variability.

3 · Personalise

Optionally express each feature relative to the user's relaxed baseline, which cancels person and sensor offsets.

4 · Classify

Five classifiers from the paper. Each is tested on a person it has never seen (leave-one-subject-out).

Built to transfer to different hardware

Results

How well it detects stress

Leave-one-subject-out: train on 14 people, test on the 15th, repeated for everyone. The scores therefore describe performance on a new person. Black ticks mark the best wrist result reported in the WESAD paper.

Stress vs. no stress: accuracy by sensor set

best classifier per bar · hover for details

raw featureswith personal baselineWESAD paper (wrist)

Where it errs

deployed model (AdaBoost, pulse + skin, personal baseline) · share of each true class

Accuracy per person

stress vs. no stress, deployed model · each bar is one held-out subject

What the results taught us

Motion alone "detects" stress too well

The accelerometer alone reached 87.5 % accuracy, against about 70 % in the paper. WESAD participants moved while giving their stress-test speech, so motion partly gives the label away. A seated, still user would then look unstressed. Pulse and skin alone score the same (94.7 % vs. 94.9 %), so the deployed model ignores motion.

Personal calibration pays off

Comparing each person with their own relaxed baseline lifts stress detection from 91.2 % to 94.7 %. Skin conductance gains most (3-class: 60.8 % → 74.0 %), which makes sense: its absolute level differs a lot between people.

Amusement is subtle

As in the paper, amusement is mostly mistaken for baseline: watching funny clips barely changes the body's signals. Stress itself is recognised reliably.

Better than the paper's wrist results, with fewer sensors

94.7 % against 88.3 % for stress detection, without a temperature sensor. The best model was selected on the same evaluation, so the top figures are slightly optimistic; the paper's figures share that bias.

Dashboard

Everything visible, live

The browser dashboard connects to the laptop server over a WebSocket and updates many times per second.

System diagram

Every sensor, board and software stage as a node. Nodes light up when active, wires animate while data flows, and the LED glows during collection.

Stress estimate

Gauge with stress probability and level, the 3-class state, heart rate, HRV, skin conductance, and the full table of 44 features.

Live signals

PPG waveform with running heart rate, skin conductance in µS, 3-axis acceleration and microphone level, with hover read-outs.

Wireless links

RSSI of both boards, sample rate and packet loss per stream, and which path started the measurement (relay or direct).

Voice and log

Transcripts, recognised commands, spoken replies, and a timestamped log of every link event (START, ACK, DONE).

History and data

P(stress) of past sessions. Each session's raw signals, features, prediction and link stats are saved for analysis.

Build it

From parts to first measurement

The complete guide, with the expected serial output at every step, is STEP_BY_STEP.md. In short:

1 · Boards (Arduino IDE)

  • Install the boards esp32 (Espressif) and esp8266, and the libraries WebSockets and MPU6050_light.
  • Create .env and run tools/make_secrets.py.
  • Upload esp8266_vitals.ino (LOLIN D1 mini) and esp32_voice.ino (ESP32 Dev Module).

2 · Laptop

python3 -m venv venv
venv/bin/pip install -r tools/requirements.txt
venv/bin/python tools/stress_server.py --no-llm
# open http://localhost:8080

3 · Model

venv/bin/python -I tools/ml/wesad_extract.py
venv/bin/python -I tools/ml/build_dataset.py
venv/bin/python -I tools/ml/train.py --deploy-features physio

The WESAD dataset is downloaded separately from its authors.

4 · Use

  • Set the GSR zero with the electrodes off.
  • “calibrate my baseline” (3 min, relaxed).
  • “measure my stress level” (60 s, still and silent).
No hardware? Run the simulator.

tools/sim_devices.py impersonates both boards with the exact firmware protocol and replays real WESAD recordings, with optional packet loss. Features computed through the simulated wireless path match the direct computation (heart rate 102.8 vs. 102.9 bpm at 2 % packet loss).

Limitations

What this is, and isn't

  • Not a medical device. The model learned from 15 young, mostly male people in a lab. Its output is an estimate, not a diagnosis.
  • Different hardware: fingertip sensors and a hobby PPG differ from the Empatica E4. Personal calibration narrows the gap, but accuracy on this hardware hasn't been measured against ground truth.
  • Wrist-PPG variability is noisy: heart rate is reliable (r = 0.91 vs. ECG), but beat-to-beat measures such as RMSSD agree poorly with ECG.
  • Credentials are compiled into the firmware and can be read from a board by anyone with physical access.

References

  1. P. Schmidt, A. Reiss, R. Duerichen, C. Marberger, K. Van Laerhoven. Introducing WESAD, a Multimodal Dataset for Wearable Stress and Affect Detection. ICMI 2018. doi:10.1145/3242969.3242985
  2. C. Kirschbaum, K. Pirke, D. Hellhammer. The Trier Social Stress Test. Neuropsychobiology 28, 1993.
  3. S. Kreibig. Autonomic nervous system activity in emotion: A review. Biological Psychology 84(3), 2010.
  4. Task Force of the ESC and NASPE. Heart rate variability: standards of measurement. European Heart Journal 17, 1996.
  5. Seeed Studio. Grove – GSR Sensor.