This project implements real-time IoT sensing, cloud data logging, and statistical anomaly detection using:
- ESP8266 (NodeMCU)
- DHT11 Sensor (Temperature & Humidity)
- LDR Sensor (Light Intensity)
- ThingSpeak Cloud
- MATLAB Z-Score Analysis
The system uploads live sensor values to ThingSpeak, and a MATLAB script performs anomaly detection on the last 60 minutes of data.
The ESP8266 collects:
- Temperature (°C)
- Humidity (%)
- Light Intensity (LDR value)
And uploads them to ThingSpeak using an API key.
The MATLAB script analyzes:
- LDR
- Temperature
- Humidity
Using Z-score-based anomaly detection, with thresholds:
| Sensor | Threshold (±Z-score) |
|---|---|
| LDR | 1.4 |
| Temperature | 2.5 |
| Humidity | 2.5 |
A single figure is generated showing:
- LDR Z-score graph
- Temperature Z-score graph
- Humidity Z-score graph
The script prints:
- Normal data points
- Anomalous data points
- Total number of anomalies per sensor
- ESP8266 NodeMCU
- DHT11 Sensor
- LDR + 10kΩ resistor
- Jumper wires
- USB cable
| Component | Pin on Sensor | ESP8266 (NodeMCU) Pin | Description |
|---|---|---|---|
| DHT11 | VCC | 3V3 | Power supply |
| GND | GND | Ground | |
| DATA | D4 (GPIO2) | Temperature & Humidity data | |
| LDR | One end | A0 | Analog light intensity input |
| Other end | 3V3 | Voltage divider | |
| 10kΩ Resistor | One end | A0 | Forms voltage divider |
| Other end | GND | Ground | |
| ESP8266 | VIN / USB | USB | Power supply |
| GND | GND | Common ground |
The LDR is connected in a voltage divider configuration with a 10kΩ resistor.
3.3V ── LDR ── A0 ── 10kΩ ── GND
- Bright light → Low resistance → Higher ADC value
- Low light → High resistance → Lower ADC value
The MATLAB script performs:
- Fetch last 60 minutes of sensor data from ThingSpeak
- Calculate mean & standard deviation
- Compute Z-score
- Classify:
- Normal values
- Outlier/anomalies
- Print summary
- Generate graphs
Z-score helps detect sudden abnormal deviations by comparing how far a value lies from the statistical mean:
Values outside the threshold → anomalies
- LDR Anomaly Count: 6
- Temperature Anomaly Count: 2
- Humidity Anomaly Count: 4
Graphs include:
Includes:
- WiFi connection
- Read DHT11 sensor
- Read LDR through ADC
- Upload to ThingSpeak every 20 seconds
- Email/SMS alert when anomaly detected
- Live dashboard (React / Node.js)
- Automated ML model for forecasting
- Multi-sensor fusion anomaly detection
If you found this project useful or learned something new from it,
please consider giving it a ⭐ on GitHub — it really helps and motivates me to build more IoT projects like this!
Thank you for your support 🙌
Kritish Mohapatra
Electrical Engineering | IoT | Embedded Systems
GitHub: kritishmohapatra
LinkedIn: Kritish Mohapatra
This project is open-source under the MIT License.





