Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Subhajit
Published © GPL3+

LoRa Project With ESP8266 Arduino Control Relay

In this Lora project, I have shown how to make the LoRa Arduino ESP8266 control relay using the RYLR896 LoRa module with real-time feedback.

BeginnerFull instructions provided6 hours6,381
LoRa Project With ESP8266 Arduino Control Relay

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Grove - 4-Channel SPDT Relay
Seeed Studio Grove - 4-Channel SPDT Relay
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Transmitter Lora Circuit Using Arduino

Transmitter Lora Circuit Using Arduino Nano

Receiver Lora Circuit Using Arduino

Receiver Lora Circuit Using ESP8266 NodeMCU

Code

Code for the Transmitter Lora Circuit

Arduino
Code for the Transmitter Lora Circuit
/*************************************************************************************************************************************************
 *  TITLE: This is transmitter LoRa Arduino sketch to send and receive signal
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/uWCY1CkvhR8
 *  Related Blog : https://iotcircuithub.com/esp8266-projects/
 *  by Tech StudyCell
 *************************************************************************************************************************************************/

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");
    }
}
  

Code for the Receiver Lora Circuit

Arduino
Code for the Receiver Lora Circuit
/**********************************************************************************
 *  TITLE: LoRa control 4 Relays using NodeMCU ESP8266 with real-time feedback (Receiving end)
 *  Click on the following links to learn more. 
 *  YouTube Video: https://youtu.be/uWCY1CkvhR8
 *  Related Blog : https://iotcircuithub.com/esp8266-projects/
 *  by Tech StudyCell
 *  Preferences--> Aditional boards Manager URLs : 
 *  https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json
 *  
 *  Download Board ESP8266 NodeMCU : https://github.com/esp8266/Arduino
 *  
 **********************************************************************************/

// define the GPIO connected with Relays and switches
#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);
 }
}

Credits

Subhajit
47 projects • 126 followers
I have studied Electrical Engineering. I do love to make different DIY electronics projects & share it on my YouTube channel Tech StudyCell.

Comments