Skip to content

An IoT-based sensor monitoring and anomaly detection system using ESP8266 and MicroPython. Real-time temperature, humidity, and light intensity data is sent to the ThingSpeak cloud and analyzed in MATLAB using Z-score based statistical techniques to identify abnormal sensor behavior.

License

Notifications You must be signed in to change notification settings

kritishmohapatra/IoT_Environment_Monitoring_With_Anomaly_Detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📡 IoT Sensor Anomaly Detection using ESP8266, ThingSpeak & MATLAB

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.

ESP8266 MicroPython ThingSpeak MATLAB IoT Anomaly Detection Sensors Status License


🚀 Features

✔ Real-time IoT Cloud Upload

The ESP8266 collects:

  • Temperature (°C)
  • Humidity (%)
  • Light Intensity (LDR value)

And uploads them to ThingSpeak using an API key.

✔ Statistical Anomaly Detection

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

✔ Visualization

A single figure is generated showing:

  • LDR Z-score graph
  • Temperature Z-score graph
  • Humidity Z-score graph

✔ Outlier Classification

The script prints:

  • Normal data points
  • Anomalous data points
  • Total number of anomalies per sensor

🔧 Hardware Required

  • ESP8266 NodeMCU
  • DHT11 Sensor
  • LDR + 10kΩ resistor
  • Jumper wires
  • USB cable

🔌 Circuit Connections

Circuit Diagram

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

LDR Voltage Divider

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

🧪 MATLAB Script Overview

The MATLAB script performs:

  1. Fetch last 60 minutes of sensor data from ThingSpeak
  2. Calculate mean & standard deviation
  3. Compute Z-score
  4. Classify:
    • Normal values
    • Outlier/anomalies
  5. Print summary
  6. Generate graphs

🧠 Why Z-Score for Anomaly Detection?

Z-score helps detect sudden abnormal deviations by comparing how far a value lies from the statistical mean:

Values outside the threshold → anomalies


📊 Output Example

  • LDR Anomaly Count: 6
  • Temperature Anomaly Count: 2
  • Humidity Anomaly Count: 4

Graphs include:

  • LDR Z-score plot
  • Temperature Z-score plot
  • Humidity Z-score plot
    code output Thingspeak 1 Thingspeak 2 Thingspeak 3 Matlab

🛠 MicroPython Code (ESP8266)

Includes:

  • WiFi connection
  • Read DHT11 sensor
  • Read LDR through ADC
  • Upload to ThingSpeak every 20 seconds

📌 Future Enhancements

  • Email/SMS alert when anomaly detected
  • Live dashboard (React / Node.js)
  • Automated ML model for forecasting
  • Multi-sensor fusion anomaly detection

⭐ Support This Project

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 🙌

👤 Author

Kritish Mohapatra
Electrical Engineering | IoT | Embedded Systems
GitHub: kritishmohapatra
LinkedIn: Kritish Mohapatra


📬 License

This project is open-source under the MIT License.

About

An IoT-based sensor monitoring and anomaly detection system using ESP8266 and MicroPython. Real-time temperature, humidity, and light intensity data is sent to the ThingSpeak cloud and analyzed in MATLAB using Z-score based statistical techniques to identify abnormal sensor behavior.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published