Clear, device-specific steps to get everything running. Two setups are provided:
- PC-only with simulator (fastest)
- Raspberry Pi with real IMU + PC for broker/cloud/web
- PC (Windows/macOS/Linux) with Docker Desktop:
- Mosquitto MQTT broker (container)
- Cloud forwarder (container)
- Web dashboard (container)
- Raspberry Pi (optional):
- Device publisher reading the IMU (native Python on Pi)
- On PC: install and start Docker Desktop.
- On PC: open a terminal in the project folder (e.g.,
E:\hw4iot). - On PC (optional): create
.envin repo root to tweak defaults:
MQTT_BROKER=broker
DEVICE_ID=door001
SAMPLING_RATE_HZ=50
GYRO_START_THRESHOLD_DPS=30
GYRO_STOP_THRESHOLD_DPS=10
MIN_EVENT_DURATION_S=0.3
MAX_EVENT_DURATION_S=5
QUIET_TIME_AFTER_EVENT_S=1
# MODEL_PATH=/app/model.joblib # if you add a trained model
- On PC: start the full stack (broker + cloud + device [sim] + web):
docker compose up --build- On PC: open the dashboard:
http://localhost:8080.- You should see simulated open/close events updating the UI.
- On PC (optional): view MQTT traffic from another terminal:
docker run --rm -it --network host eclipse-mosquitto:2 mosquitto_sub -h localhost -t 'door/#' -v- On PC: stop everything when done:
docker compose downPrerequisites (hardware on Pi):
- Enable I2C:
sudo raspi-config→ Interface Options → I2C → Enable → reboot. - Wire MPU6050 to Pi:
VCC→3.3V,GND→GND,SCL→GPIO3,SDA→GPIO2.
Steps:
- On PC: start Docker stack (broker + cloud + web; device will still run in simulator in Docker but we’ll ignore it):
docker compose up --build- On PC: find your PC IP address (for Pi to connect to broker):
- Windows:
ipconfig→ use your LAN IPv4 (e.g.,192.168.1.50).
- Windows:
- On Raspberry Pi: get the code onto the Pi (copy the repo or sync only
device/,common/,requirements.txt). - On Raspberry Pi: install Python deps and run the device publisher against the PC broker:
python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
export MQTT_BROKER=YOUR_PC_IP # e.g., 192.168.1.50
export DEVICE_ID=door001
python device/publisher.py- The device will auto-detect the MPU6050 (if present) and stop using the simulator.
- On PC: open
http://localhost:8080to see live updates from the Pi. - Keep the Pi process running (or supervise via
screen,tmux, or a systemd service).
You can train locally on the PC or inside a container. Collection should run where the IMU is (typically on the Pi).
Collect data (on Raspberry Pi with sensor attached):
python tools/collect.py open 120 data_open.csv
python tools/collect.py close 120 data_close.csv
python tools/collect.py none 60 data_none.csvTrain the model (on PC):
python -m venv .venv
. .venv/Scripts/activate # Windows PowerShell: .venv\Scripts\Activate.ps1
pip install -r requirements.txt
python tools/train.py data_open.csv data_close.csv data_none.csv --out model.joblibUse the trained model:
- If using the Docker device (simulator or future hardware passthrough): place
model.joblibin repo root and rebuild:
docker compose build device
docker compose up -d deviceOr mount without rebuild via docker-compose.override.yml:
services:
device:
volumes:
- ./model.joblib:/app/model.joblib:ro
environment:
- MODEL_PATH=/app/model.joblib- If running the device on the Raspberry Pi natively: copy
model.joblibto the Pi and either place it next todevice/publisher.pyor set:
export MODEL_PATH=/full/path/to/model.joblib
python device/publisher.py- MQTT topics:
- Device decisions:
door/device/{DEVICE_ID}/decision - Cloud events (retained):
door/events
- Device decisions:
- JSON payload example:
{"device_id":"door001","label":"open","confidence":0.93,"timestamp":"2025-10-27T12:34:56","duration_s":0.84}- Logs (PC):
docker compose logs -f broker cloud device web- Tuning thresholds: edit
.env(for Docker) or set env vars (for Pi). See keys incommon/config.py.
- No dashboard updates: confirm
cloudanddeviceare running; checkdocker compose psand logs. - No messages on
door/events: ensure broker is healthy andcloudsubscribed todoor/device/+/decision. - Over/under detection: adjust
GYRO_START_THRESHOLD_DPS,GYRO_STOP_THRESHOLD_DPS, and timing windows; retrain model if needed. - Wrong labels: collect more representative data and retrain; ensure consistent IMU mounting orientation.