A Robust, Peer-to-Peer IoT Irrigation System Built on Heterogeneous Hardware (STM32 & ESP8266)
Sugarcane IoT is an advanced, decentralized irrigation system built to operate reliably in agricultural environments without relying on centralized cloud-control loops. By leveraging peer-to-peer consensus across edge nodes, the system ensures consistent and fault-tolerant irrigation logic.
This repository contains the complete firmware and documentation for the entire hardware stack, encompassing STM32 microcontrollers for low-level sensor integration/actuation and NodeMCU (ESP8266/ESP32) for lightweight telemetry and cloud bridging.
Why is this different from existing IoT projects? Most traditional IoT irrigation systems rely on a Centralized Cloud Architecture: sensors blindly stream data to a cloud server (or a central gateway), and the server runs the logic to send back an "open valve" command.
- The Problem: If the internet connection drops, or the central gateway fails, the crops die.
- The Sugarcane IoT Solution: We push the intelligence to the edge. The STM32 microcontrollers run a Peer-to-Peer Consensus Protocol. They don't just collect data; they cross-validate environmental states and actively negotiate the irrigation plan directly over a local hardware mesh. The Wi-Fi bridge (NodeMCU) is strictly used for observability (telemetry forwarding), not control. If the Wi-Fi drops, the local STM32 mesh seamlessly degrades to an autonomous consensus mode, continuing to water the sugarcane precisely without skipping a beat.
- Decentralized Decision Making: STM32 nodes share state and execute a unified state machine for irrigation, ensuring reliability even if the main gateway drops.
- Heterogeneous Hardware Mesh: Seamless interoperability between high-performance ARM Cortex-M4/M4F processors (STM32F401RE/STM32F303RE) and low-cost Wi-Fi SoCs (NodeMCU).
- Fault-Tolerant Fallbacks: Built-in safeguards to gracefully handle sensor faults, link drops, or node failures.
- Real-time Telemetry Pipeline: Aggregated telemetry parsing (Temperature, Humidity, Soil/Water metrics) formatted for immediate Grafana/Prometheus ingestion via a Python bridge.
The codebase is organized by hardware node and functional domain:
sugarcane_iot/
├── 303re/ # Firmware for STM32F303RE Node (Sensor & Actuator Logic)
├── 401re_final/ # Firmware for STM32F401RE Node (Core processing & control)
├── NodeMCU/ # ESP8266/ESP32 Firmware (UART bridge & Telemetry forwarder)
├── docs/ # Project documentation
│ └── architecture/ # System architecture, consensus design, and diagrams
├── results/ # Output telemetry, evaluation metrics, and test logs
└── README.md # You are here!
Note: Additional developmental folders (e.g.,
sugarcaneiot,STM32F303_Node) are retained for historical reference and subsystem testing.
| Node Type | Microcontroller | Primary Responsibilities |
|---|---|---|
| Node A (Edge) | STM32F303RE |
Reads DHT11/Soil Sensors, local edge compute, shares state via UART mesh. |
| Node B (Core) | STM32F401RE |
Aggregates local metrics, validates consensus constraints, triggers water relays. |
| Gateway | NodeMCU |
Serial monitor on D7 (GPIO13), formats JSON telemetry, bridges offline mesh to the cloud. |
- STM32CubeIDE: For compiling and flashing the
303reand401re_finalprojects. - PlatformIO / Arduino IDE: For compiling the
NodeMCUbridge firmware. - ST-Link v2: For flashing the Nucleo boards.
- Clone the Repository:
git clone https://github.com/viiavi/sugarcane_iot.git cd sugarcane_iot - Flash the STM32 Nodes:
- Open
STM32CubeIDE. - Import the
303reand401re_finalprojects from the root directory. - Build and run to flash the respective Nucleo-64 boards.
- Open
- Flash the NodeMCU:
- Open
NodeMCU/sugarcane_node.inoin Arduino IDE or PlatformIO. - Connect the NodeMCU via USB and flash.
- Open
- Wiring the Mesh:
- Connect
STM32 TXtoNodeMCU D7 (GPIO13). - Ensure common Ground (
GND) across all microcontrollers. - Power up the system; the NodeMCU will begin broadcasting formatted telemetry at
115200baud.
- Connect
The real-time observability dashboard provides a live look at the decentralized consensus engine in action, alongside comparative analysis of prototype hardware versus ideal agricultural thresholds.
Test logs, performance evaluations, and graphical representations of the consensus protocol in action can be found in the results/ directory. We actively monitor:
- Packet drop rates across the UART mesh.
- State-convergence latency between the STM32 nodes.
- Environmental data trends (Temperature, Humidity, Moisture).
Curious about how the decentralized consensus works under the hood? Check out the detailed System Architecture Documentation.

