LoRa and ESP8266 Relay Control with Arduino Code

How to make the LoRa Arduino ESP8266 control relay with real-time feedback using the RYLR896 LoRa module. Explained both the transmitter and receiving LoRa circuit and source codes and how to configure the LoRa modules with AT commands.
So if you follow all the steps, you can easily make this Lora Arduino project to control any appliances from 10KM away without Wi-Fi and Bluetooth. So this project is very useful in rural areas.
Transmitter Components:
1. REYAX RYLR896 Lora Module
2. Arduino Nano
3. 220-ohm Resistor
4. 4.7k Resistor
5. 10k Resistor
6. 5-mm LED 3no
7. Push buttons 4no.

Transmitter LoRa Circuit:

Transmitter code:

const int pLED = 13;
const int rLED = 7;
const int gLED = 8;

const int pSwitch_1 = 2;
const int pSwitch_2 = 3;
const int pSwitch_3 = 4;
const int pSwitch_4 = 5;

int i;
String incomingString;
boolean state;

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(pLED, OUTPUT);
  pinMode(rLED, OUTPUT);
  pinMode(gLED, OUTPUT);
  
  pinMode(pSwitch_1, INPUT_PULLUP);
  pinMode(pSwitch_2, INPUT_PULLUP);
  pinMode(pSwitch_3, INPUT_PULLUP);
  pinMode(pSwitch_4, INPUT_PULLUP);

}

void getFeedback(String excpt_str){
  i = 0;
  incomingString = "";
  state = true;

  digitalWrite(gLED, LOW);
  digitalWrite(rLED, LOW);

  //Serial.print("Waiting for feedback");
  while(1)
{
    if(Serial.available()>0)
{
      incomingString = Serial.readString();
      if(incomingString.indexOf(excpt_str) > 0) 
{
        state = true; break;
      }
    }      
    if (i > 60) 
{
      state = false; break;
    }
    delay(100);
    digitalWrite(pLED, !digitalRead(pLED));
    i++;
    }

    if(state){
      digitalWrite(gLED, HIGH);
      digitalWrite(rLED, LOW);
      digitalWrite(pLED, LOW);
      //Serial.println("Received");
    }
    else
{
      digitalWrite(gLED, LOW);
      digitalWrite(rLED, HIGH);
      digitalWrite(pLED, LOW);
      //Serial.println("Not Received");
    }    
}

void loop() {
  // put your main code here, to run repeatedly:

  if (digitalRead(pSwitch_1) == LOW){
      Serial.println("AT+SEND=2,6,R1");
      getFeedback("FR1");
    }
  else if (digitalRead(pSwitch_2) == LOW){
      Serial.println("AT+SEND=2,6,R2");
      getFeedback("FR2");
    }
  else if (digitalRead(pSwitch_3) == LOW){
      Serial.println("AT+SEND=2,6,R3");
      getFeedback("FR3");
    }
  else if (digitalRead(pSwitch_4) == LOW){
      Serial.println("AT+SEND=2,6,R4");
      getFeedback("FR4");
    }
}

In the transmitter LoRa circuit, I have used Arduino Nano. Here I have made a voltage divider using 4.7k and 10k resistors to drop down 5V logic level to 3.3V logic level.
The push buttons are connected with the D2, D3, D4, and D5 digital pins of the Arduino Nano.
And the LEDs are connected with D7, D8, and D13.
On the Arduino board, a resistor is already connected in series with the D13 pin, so I have not used any series resistor with the LED for the D13 pin. You can use any 5V DC supply or 9V battery to supply the circuit.

Receiver Circuit:

Receiver Components:
1. REYAX RYLR896 Lora Module
2. ESP8266 NodeMCU
3. 5v 4-channel Relay Module
4. 4.7k Resistor
5. 10k Resistor

In the receiver LoRa circuit, I have used ESP8266 NodeMCU. Also here I have made a voltage divider using 4.7k and 10k resistors to drop down 5V logic level to 3.3V logic level.

Receiver code:

#define RelayPin1 5  //D1
#define RelayPin2 4  //D2
#define RelayPin3 14 //D5
#define RelayPin4 12 //D6
#define sLed   16   //D0
String incomingString;

void setup()
{
  Serial.begin(115200);
  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(sLed, OUTPUT);

  //During Starting all Relays should TURN OFF
  digitalWrite(RelayPin1, HIGH);
  digitalWrite(RelayPin2, HIGH);
  digitalWrite(RelayPin3, HIGH);
  digitalWrite(RelayPin4, HIGH);

  digitalWrite(sLed, HIGH);
}

void loop()
{ 
  lora_control();
}

void lora_control()
{
  
  if(Serial.available()) 
{
    incomingString = Serial.readString();
    digitalWrite(sLed, LOW);
    if(incomingString.indexOf("R1") >0) 
{
      digitalWrite(RelayPin1, !digitalRead(RelayPin1));
      Serial.println("AT+SEND=1,6,FR1");
    }
    else if(incomingString.indexOf("R2") >0) 
{
      digitalWrite(RelayPin2, !digitalRead(RelayPin2));
      Serial.println("AT+SEND=1,6,FR2");
    }
    else if(incomingString.indexOf("R3") >0) 
{
      digitalWrite(RelayPin3, !digitalRead(RelayPin3));
      Serial.println("AT+SEND=1,6,FR3");
    }
    else if(incomingString.indexOf("R4") >0) 
{
      digitalWrite(RelayPin4, !digitalRead(RelayPin4));
      Serial.println("AT+SEND=1,6,FR4");
    }
   delay(100);
   digitalWrite(sLed, HIGH);
 }
}

I have used D1, D2, D5, D6 GPIO pins to control the 4-channel relay module.
As per the source code, when the control pins of the relay module receive the LOW signal the respective relay will turn on and the relay will turn off for the HIGH signal in the control pin.

The Boot will fail if RX is grounded during the Boot process. So connect the LoRa module after giving the supply to NodeMCU.
I have used a 5V 2Amp power supply to supply the NodeMCU and relay module.
Please take the proper safety precautions while working with high voltage.
After downloading, you will get two codes (Transmitter & Receiver).
Upload the code for the transmitter circuit to Arduino Nano and code for the receiver to ESP8266 NodeMCU.

AT commands used for this Arduino Lora project:
For the Transmitter LoRa module
1. AT+ADDRESS=1
2. AT+NETWORKID=5
3. AT+BAND=865000000.

For the Receiver LoRa module
1. AT+ADDRESS=2
2. AT+NETWORKID=5
3. AT+BAND=865000000.
Now, when you press the pushbutton in the transmitter circuit, the related relay should turn ON.
And after receiving the feedback from the receiver circuit the Green LED (D7) should turn on.
Download Full Code Code_Arduino_TX_NodeMCU_RX_LoRa_feedback_01

Subramanian
Subramanian

Subramanian MK, currently serving as a workshop instructor at Sakthi Polytechnic College, Erode Tamil Nadu. With a career spanning 25 + years, Subramanian MK has dedicated himself to advancing knowledge in Electronics and Communication Engineering (ECE). His passion for exploring new technologies has led to the development of numerous projects, showcasing expertise in IoT and PCB design.

Articles: 514

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

×

Hi, How can I help you?

×