Back to All Blog Posts
Tutorial β€’EltroNerd Engineering

LoRa-based smart agriculture system

let’s create a simple LoRa-based smart agriculture system with one master node collecting data from two slave nodes.

🌾 LoRa Smart Agriculture: 1 Master & 2 Slave Nodes


πŸ“Œ 1. System Overview

  • Master Node: Central hub that collects data from all slaves and updates the IoT dashboard.
  • Slave Node 1: Measures soil moisture and temperature in Field A.
  • Slave Node 2: Measures soil moisture and humidity in Field B.
  • Communication: LoRa (long-range, low-power wireless)
  • Data Flow: Slave β†’ Master β†’ Dashboard

πŸ”Ž 2. Working Principle

  1. Master Node Polls Slaves

    • Master periodically sends a request message to Slave 1 and Slave 2.
  2. Slaves Respond

    • Each slave reads its sensors and sends a JSON packet containing sensor data.
  3. Data Aggregation at Master

    • Master receives data from both slaves.
    • Aggregates data into a single structured packet.
  4. Dashboard Update / Action

    • Master sends aggregated data to IoT dashboard for visualization.
    • Optional: If soil moisture is low, master sends a command to turn on irrigation at that node.

πŸ“œ 3. Example Arduino/ESP32 Code

Slave Node 1 (Field A)

c
#include <SPI.h>
#include <LoRa.h>
#define MOISTURE_PIN 34
#define TEMP_PIN 35

void setup() {
  Serial.begin(115200);
  LoRa.begin(915E6);
}

void loop() {
  // Wait for request from master
  int packetSize = LoRa.parsePacket();
  if(packetSize){
    String request = LoRa.readString();
    if(request == "REQUEST_DATA_1"){
      int soilMoisture = analogRead(MOISTURE_PIN);
      int temperature = analogRead(TEMP_PIN);

      String payload = "{";
      payload += "\"node\":\"slave1\",";
      payload += "\"soil\":" + String(soilMoisture) + ",";
      payload += "\"temp\":" + String(temperature);
      payload += "}";

      LoRa.beginPacket();
      LoRa.print(payload);
      LoRa.endPacket();
      Serial.println("Sent data: " + payload);
    }
  }
  delay(1000);
}

Slave Node 2 (Field B)

c
#include <SPI.h>
#include <LoRa.h>
#define MOISTURE_PIN 32
#define HUMIDITY_PIN 33

void setup() {
  Serial.begin(115200);
  LoRa.begin(915E6);
}

void loop() {
  int packetSize = LoRa.parsePacket();
  if(packetSize){
    String request = LoRa.readString();
    if(request == "REQUEST_DATA_2"){
      int soilMoisture = analogRead(MOISTURE_PIN);
      int humidity = analogRead(HUMIDITY_PIN);

      String payload = "{";
      payload += "\"node\":\"slave2\",";
      payload += "\"soil\":" + String(soilMoisture) + ",";
      payload += "\"humidity\":" + String(humidity);
      payload += "}";

      LoRa.beginPacket();
      LoRa.print(payload);
      LoRa.endPacket();
      Serial.println("Sent data: " + payload);
    }
  }
  delay(1000);
}

Master Node

c
#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(115200);
  LoRa.begin(915E6);
}

void loop() {
  // Request data from Slave 1
  LoRa.beginPacket();
  LoRa.print("REQUEST_DATA_1");
  LoRa.endPacket();
  delay(500);
  receiveData();

  // Request data from Slave 2
  LoRa.beginPacket();
  LoRa.print("REQUEST_DATA_2");
  LoRa.endPacket();
  delay(500);
  receiveData();

  delay(60000); // Poll every minute
}

void receiveData() {
  int packetSize = LoRa.parsePacket();
  if(packetSize){
    String payload = LoRa.readString();
    Serial.println("Received: " + payload);
    // Aggregate data and send to dashboard here
  }
}

πŸ”Ž Working Principle

The LoRa-based smart agriculture system with one master and two slave nodes works as follows:

  1. Master Node Initialization

    • The master node acts as the central hub.
    • It periodically sends a request message to each slave node to collect sensor data.
  2. Slave Node Response

    • Each slave node reads its sensors (soil moisture, temperature, humidity, etc.).
    • It packages the readings into a JSON payload and transmits it via LoRa back to the master node.
  3. Data Aggregation

    • The master node receives data from both slave nodes.
    • It aggregates all field data into a single structure for monitoring and further processing.
  4. Dashboard Update & Decision Making

    • The aggregated data is sent to an IoT dashboard for real-time visualization.
    • If soil moisture is below a threshold, the master can send a command back to the slave to activate irrigation.
  5. Continuous Loop

    • This process repeats at defined intervals, enabling real-time monitoring and control of the farm.

🏁 Conclusion

The master-slave LoRa-based architecture enables:

  • Centralized monitoring of multiple field nodes.
  • Reliable, long-range, low-power communication suitable for large farms.
  • Automated decision-making, such as triggering irrigation based on soil conditions.
  • Scalability, allowing easy addition of more slave nodes as the farm expands.

This system demonstrates a practical approach to IoT-enabled smart agriculture, integrating embedded sensors, wireless communication, and real-time data aggregation for efficient farm management.

Try this project in your browser

Compile code and simulate hardware output with EltroNerd Cloud IDE.

Launch Cloud IDE