Skip to content

skyline-greens/pi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Raspberry Pi MQTT Gateway

Overview

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.

Features

  • 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).

Requirements

  • Python 3.x
  • paho-mqtt, netifaces
  • Local Mosquitto MQTT broker (auto-installed if missing)
  • Certificates for Azure IoT Hub in /home/pi/pi/certs/

How It Works

  1. Start & Setup:
    • Installs and starts Mosquitto if not present.
    • Verifies required certificates, loads warehouse ID (or requests one).
  2. MQTT Connections:
    • Connects securely to Azure IoT Hub and locally to Mosquitto.
  3. 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.
  4. Shutdown:
    • Handles graceful shutdown on SIGINT/SIGTERM.

MQTT Topics

Cloud (Azure IoT Hub)

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

Local (Mosquitto, ESP Devices)

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

Setup

  1. Place certificates in /home/pi/pi/certs/.
  2. Install Python dependencies:
    pip install paho-mqtt netifaces
  3. Run the script:
    python3 Pi_Script.py
    The script will auto-install Mosquitto if needed.

File Locations

  • Script: Pi_Script.py
  • Warehouse ID file: /home/pi/warehouse_id.txt
  • Certificates: /home/pi/pi/certs/

License

MIT

About

Code to run on the Raspberry PI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages