Long-Range LoRa-Based Remote Actuator Control using ESP32

This project is ideal for controlling devices over long distances using LoRa (Long Range) technology, which is great for applications in rural areas, agriculture, and IoT networks where internet access is limited.


In this project, you'll use two ESP32 boards with LoRa modules to control an actuator remotely. One ESP32 with a LoRa module will act as the transmitter, sending control commands. The other ESP32 with a LoRa module will be the receiver, controlling the actuator based on the received commands.


Key components:

- ESP32 boards (2x)

- LoRa modules (e.g., SX1278 or RFM95)

- Actuator (e.g., relay module, servo motor)

- 3.7V LiPo batteries (optional for portability)

- Breadboard, jumper wires, and a 5V power supply


Software requirements:

- Arduino IDE with LoRa library (`arduino-lora` library)


Wiring the Components


#1. Transmitter Setup

ESP32 to LoRa Module (SX1278):

  - `MISO` (LoRa) to `GPIO19` (ESP32)

  - `MOSI` (LoRa) to `GPIO23` (ESP32)

  - `SCK` (LoRa) to `GPIO18` (ESP32)

  - `NSS/CS` (LoRa) to `GPIO5` (ESP32)

  - `RST` (LoRa) to `GPIO14` (ESP32)

  - `DIO0` (LoRa) to `GPIO26` (ESP32)

  - `3.3V` (LoRa) to `3.3V` (ESP32)

  - `GND` (LoRa) to `GND` (ESP32)


#2. Receiver Setup

ESP32 to LoRa Module (SX1278):

  - Same connections as the transmitter.


Connecting the Actuator (e.g., Relay) to ESP32:

  - `Signal` pin of the relay to `GPIO15` (ESP32)

  - `VCC` of the relay to `3.3V` or `5V`

  - `GND` of the relay to `GND` (ESP32)


Coding the Transmitter (ESP32)


#include <SPI.h>

#include <LoRa.h>


#define LORA_CS 5

#define LORA_RST 14

#define LORA_IRQ 26


void setup() {

  Serial.begin(115200);

  LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);


  if (!LoRa.begin(433E6)) {

    Serial.println("Starting LoRa failed!");

    while (1);

  }


  Serial.println("LoRa Transmitter");

}


void loop() {

  // Example: Sending "ON" or "OFF" command to control the actuator

  String message;

  if (digitalRead(2) == HIGH) {  // Replace with your input logic

    message = "ON";

  } else {

    message = "OFF";

  }


  LoRa.beginPacket();

  LoRa.print(message);

  LoRa.endPacket();


  Serial.print("Sent message: ");

  Serial.println(message);


  delay(2000);  // Adjust delay as needed

}


Coding the Receiver (ESP32)


#include <SPI.h>

#include <LoRa.h>


#define LORA_CS 5

#define LORA_RST 14

#define LORA_IRQ 26

#define RELAY_PIN 15


void setup() {

  Serial.begin(115200);

  pinMode(RELAY_PIN, OUTPUT);

  LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);


  if (!LoRa.begin(433E6)) {

    Serial.println("Starting LoRa failed!");

    while (1);

  }


  Serial.println("LoRa Receiver");

}


void loop() {

  int packetSize = LoRa.parsePacket();

  if (packetSize) {

    String receivedMessage = "";

    while (LoRa.available()) {

      receivedMessage += (char)LoRa.read();

    }


    Serial.print("Received: ");

    Serial.println(receivedMessage);


    // Control the actuator based on received message

    if (receivedMessage == "ON") {

      digitalWrite(RELAY_PIN, HIGH);

      Serial.println("Relay turned ON");

    } else if (receivedMessage == "OFF") {

      digitalWrite(RELAY_PIN, LOW);

      Serial.println("Relay turned OFF");

    }

  }

}


Explanation of the Code


Transmitter Code:

  - Initializes the LoRa module to operate at 433 MHz (you may need to adjust the frequency based on your region).

  - Sends "ON" or "OFF" messages based on input (e.g., a button press).

  - Uses `LoRa.beginPacket()` and `LoRa.endPacket()` to send data over the air.


Receiver Code:

  - Waits for incoming LoRa packets and reads the message.

  - Controls the relay based on the message: "ON" activates the relay, "OFF" deactivates it.

  - Uses `LoRa.parsePacket()` to check for new incoming packets.


Testing and Deployment


1. Upload the transmitter code to one ESP32 and the receiver code to the other ESP32.

2. Power up both ESP32s, ensuring that the LoRa modules are properly connected.

3. Monitor the Serial Monitor of both ESP32s to check the status of sent and received messages.

4. Test the control functionality by simulating the input on the transmitter side (e.g., pressing a button or sending an "ON" command).

5. Observe the actuator's behavior connected to the receiver to ensure it reacts accordingly.


Tips for Optimizing Range


- Use antennas for both LoRa modules to maximize the range.

- Ensure there are minimal obstructions between the transmitter and receiver.

- Adjust the spreading factor (SF) in the LoRa library for better range or faster transmission.


Applications


- Remote farm irrigation control.

- Controlling remote lighting systems.

- Industrial automation in remote areas.

- Home automation across large properties.


This guide offers a comprehensive approach to building a Long-Range LoRa-Based Remote Actuator Control system, combining LoRa communication with the power of ESP32. By using this setup, you can achieve reliable control over distances, ideal for IoT and automation projects in areas with limited infrastructure.