This Raspberry Pi script (Pi_Script.py) acts as a secure MQTT gateway, bridging NodeMCU (ESP8266) sensor devices and a cloud web application (Azure IoT Hub). It enables secure bidirectional communication, handling device identity, message forwarding, and local broker management.
- Dual MQTT Clients: Maintains connections to both a local Mosquitto MQTT broker (for ESP devices) and Azure IoT Hub (cloud).
- Certificate-based TLS Security: Uses client and CA certificates for secure cloud connections.
- Automatic Broker Management: Installs and starts the local Mosquitto broker if needed.
- Warehouse ID Management: Loads, requests, and persists a unique warehouse ID from the cloud for identification.
- ESP Device Onboarding: Forwards ESP MAC address to the cloud for cell assignment; relays assigned cell IDs back to ESPs.
- Bidirectional Data Flow:
- Forwards ESP sensor/consumption data from local broker to the cloud.
- Forwards web control and mode commands from the cloud to ESP devices.
- Robustness: Handles reconnection, signal-based shutdown, and cleans up stale requests.
- Configurable via environment (cert paths, IDs).
- Python 3.x
paho-mqtt,netifaces- Local Mosquitto MQTT broker (auto-installed if missing)
- Certificates for Azure IoT Hub in
/home/pi/pi/certs/
- Start & Setup:
- Installs and starts Mosquitto if not present.
- Verifies required certificates, loads warehouse ID (or requests one).
- MQTT Connections:
- Connects securely to Azure IoT Hub and locally to Mosquitto.
- ESP Communication:
- Listens for ESP ID requests and sensor data locally.
- Forwards IDs and data to the cloud.
- Relays web commands/modes from cloud to ESPs.
- Shutdown:
- Handles graceful shutdown on SIGINT/SIGTERM.
| Topic | Direction | Description |
|---|---|---|
wh/pi/id/request |
Out | Request warehouse ID for Pi (by MAC) |
wh/pi/<MAC>/id/response |
In | Receives warehouse ID assignment |
wh/esp/id/request |
Out | Request ESP cell ID assignment |
wh/<WAREHOUSE_ID>/esp/id/response |
In | Receives assigned ESP cell ID |
wh/<WAREHOUSE_ID>/esp/data |
Out | ESP sensor data forwarded to cloud |
wh/<WAREHOUSE_ID>/esp/consumption |
Out | ESP consumption data forwarded to cloud |
wh/<WAREHOUSE_ID>/esp/command |
In | Control commands from web app to ESPs |
wh/<WAREHOUSE_ID>/esp/mode/change |
In | Mode change commands from web app to ESPs |
| Topic | Direction | Description |
|---|---|---|
esp/id/request |
In | ESP requests cell ID (by MAC) |
esp/<MAC>/id/response |
Out | Assigned cell ID sent to ESP |
wh/esp/data |
In | Sensor data from ESP |
wh/esp/consumption |
In | Consumption data from ESP |
esp/commands |
Out | Control commands relayed to ESPs |
esp/mode/change |
Out | Mode change relayed to ESPs |
- Place certificates in
/home/pi/pi/certs/. - Install Python dependencies:
pip install paho-mqtt netifaces
- Run the script:
The script will auto-install Mosquitto if needed.
python3 Pi_Script.py
- Script:
Pi_Script.py - Warehouse ID file:
/home/pi/warehouse_id.txt - Certificates:
/home/pi/pi/certs/
MIT