A comprehensive IoT alarm system using BBC micro:bit devices for movement detection, Flask web interface for remote control, and real-time monitoring with InfluxDB and Grafana.
This project implements a distributed alarm system where:
- Two BBC micro:bit devices communicate via radio
- A PIR sensor detects movement
- IR remote provides local control
- Flask web app enables remote control via browser
- InfluxDB stores time-series sensor data
- Grafana provides real-time visualization dashboards
Micro:bit (Controller) ←→ Radio ←→ Micro:bit (Serial Bridge)
↓ ↓
PIR Sensor Serial Port
IR Remote ↓
LCD Display lettura.py ←→ InfluxDB
LEDs ↑ ↓
Flask App ←→ Grafana
- Movement Detection: PIR sensor connected to pin 0
- IR Remote Control: IR receiver on pin 1 for alarm control
- Visual Feedback:
- LCD display showing alarm status
- LED indicators (Green=OFF, Yellow=READY, Red=ALARM)
- Radio Communication: Sends status updates to second micro:bit
- Radio Receiver: Receives status from controller micro:bit
- Serial Output: Forwards data to computer via USB
Reads serial data from micro:bit and stores in InfluxDB:
- Monitors
/dev/serial/by-id/...for micro:bit connection - Parses alarm status messages (ON/OFF/SILENCED/MOVEMENT)
- Writes timestamped data to InfluxDB with authentication token
- Application Factory (
Iot_Flask/flaskr/__init__.py): Creates Flask app with blueprint registration - Dashboard Blueprint (
Iot_Flask/flaskr/Dashboard.py):/dashboard- Main control interface/get_last_status- API endpoint for current alarm state- Serial communication to control alarm remotely
- Toggle switch for alarm activation/deactivation
- Embedded Grafana dashboards for real-time monitoring
- AJAX calls to Flask API for status updates
- InfluxDB: Time-series database for sensor data storage
- Grafana: Real-time dashboards and data visualization
- Docker Compose (
docker-compose.yml): Orchestrates InfluxDB and Grafana containers
- Connect PIR sensor to micro:bit pin 0
- Connect IR receiver to micro:bit pin 1
- Connect LCD display to micro:bit I2C pins
- Flash
python_movimento.pyto controller micro:bit - Flash
python_caricamento.pyto bridge micro:bit
pip install flask influxdb-client pyserial# Start InfluxDB and Grafana
docker-compose up -d
# Configure InfluxDB authentication token in 'token' fileStart data collection (requires sudo for serial access):
sudo python3 lettura.pyStart Flask web interface (requires sudo for serial control):
cd Iot_Flask
sudo python3 -m flask --app flaskr run --host=0.0.0.0 --port=5000- IR Remote:
- OK: Silences the alarm
- #: Cleans the inserted numbers on the LCD
- 8086: Is used to switch the alarm status (ON/OFF)
- Access web interface at
http://localhost:5000/dashboard - Use toggle switch to activate/deactivate alarm
- Monitor real-time status and historical data via Grafana dashboards
- ON (Green LED): Armed, waiting for movement
- ALARM (Yellow LED): Movement detected, alarm triggered
- OFF (Red LED): System inactive
- SILENCED: Alarm acknowledged
Both lettura.py and the Flask app require sudo privileges because they access serial ports:
/dev/serial/by-id/...(micro:bit USB connection)/dev/ttyACM1(alternative serial device)
Serial port access requires elevated permissions for direct hardware communication.
- Flask app:
http://localhost:5000 - InfluxDB:
http://localhost:8086 - Grafana:
http://localhost:3000
- PIR sensor detects movement → Micro:bit controller
- Controller processes event → Updates LCD/LEDs → Sends radio message
- Bridge micro:bit receives radio → Outputs to serial
lettura.pyreads serial → Writes to InfluxDB- Grafana queries InfluxDB → Displays real-time data
- Flask app reads serial + controls system via web interface
- Serial Permission Denied: Run with
sudo - Micro:bit Not Found: Check USB connection and device path in code
- Database Connection: Verify InfluxDB container is running
- Radio Communication: Ensure both micro:bits use same radio group
- LCD library: By shaoziyang (https://github.com/shaoziyang/microbit-lib.git)
- IR Receiver: By inexglobal (https://github.com/inexglobal/irm.git)
This project is licensed under the MIT License. See the LICENSE file for details.