Hello everyone, I am trying to design a home automation system using LoRa. However, from my setup it's either I am able to get the sensor data or control the relay. I can't do both. Any help will be appreciated.
SENDER CIRCUIT:
~Microcontroller: ESP8266 (Wi-Fi module)
~Wireless Communication: LoRa RA-02 module for long-range communication
Integration: Blynk App
RECEIVER CIRCUIT:
~Microcontroller: Arduino Uno
~Wireless Communication: LoRa RA-02 module for long-range communication
~DHT11 (Temperature and Humidity Sensor)
~PIR Motion Sensor
~2-Channel Relay (to control a bulb and a fan)
#include <ESP8266WiFi.h>
#include <LoRa.h>
// WiFi network details
const char* ssid = " ";
const char* password = " ";
// Blynk authentication token
#define BLYNK_TEMPLATE_ID " "
#define BLYNK_TEMPLATE_NAME " "
#define BLYNK_AUTH_TOKEN " "
#include <BlynkSimpleEsp8266.h>
const int csPin = D8;
const int resetPin = D0;
const int irqPin = D2;
String temperature = "N/A";
String humidity = "N/A";
String motionState = "N/A";
void setup() {
Serial.begin(9600);
// Initialize LoRa
LoRa.setPins(csPin, resetPin, irqPin);
while(!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
delay(500);
}
Serial.println("LoRa Receiver Ready");
Serial.println("LoRa Initializing OK!");
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, password);
}
void loop() {
Blynk.run(); // Run Blynk
int packetSize = LoRa.parsePacket();
if (packetSize) {
String received = "";
while (LoRa.available()) {
received += (char)LoRa.read();
}
Serial.print("Received from LoRa: "+received);
parseLoRaMessage(received);
}
}
void parseLoRaMessage(String received) {
Serial.println("Received data from LoRa: " + received);
if (received.startsWith("Temp:") && received.indexOf(",Humidity:")!= -1 && received.indexOf(",Motion:")!= -1) {
int tempIndex = received.indexOf("Temp:") + 5;
int humIndex = received.indexOf(",Humidity:");
int motionIndex = received.indexOf(",Motion:");
if (tempIndex!= -1 && humIndex!= -1 && motionIndex!= -1) {
temperature = received.substring(tempIndex, humIndex);
humidity = received.substring(humIndex + 10, motionIndex);
int motionStateNum = received.substring(motionIndex + 8).toInt();
motionState = (motionStateNum == 1)? "Yes" : "No";
Serial.println("Temperature: " + temperature);
Serial.println("Humidity: " + humidity);
Serial.println("Motion State: " + motionState);
} else {
Serial.println("Error: Invalid format");
}
} else {
Serial.println("Error: Invalid data format");
}
// Send sensor data to Blynk
Blynk.virtualWrite(V0, temperature.toFloat()); // Virtual pin V0 for temperature
Blynk.virtualWrite(V4, humidity.toFloat()); // Virtual pin V4 for humidity
Blynk.virtualWrite(V3, motionState == "Yes"? 1 : 0); // Virtual pin V3 for motion state
}
BLYNK_WRITE(V1) {
if (param.asInt() == 1) {
Serial.println("Button 1 pressed");
sendLoRaCommand("R1_ON");
}
else{
sendLoRaCommand("R1_OFF");
}
}
BLYNK_WRITE(V2) {
if (param.asInt() == 1) {
Serial.println("Button 2 pressed");
sendLoRaCommand("R2_ON");
}
else{
sendLoRaCommand("R2_OFF");
}
}
void sendLoRaCommand(String command) {
LoRa.beginPacket();
LoRa.print(command);
LoRa.endPacket();
Serial.println("Sent LoRa command: " + command);
}
Receiver code:
#include <SPI.h>
#include <LoRa.h>
#include <DHT.h>
// Define LoRa module pins
const int csPin = 10; // LoRa radio chip select (NSS)
const int resetPin = 9; // LoRa radio reset
const int irqPin = 2; // Interrupt request pin (DIO0)
// Define relay pins
#define relayPin1 5 // Relay for fan connected to GPIO 5
#define relayPin2 6 // Relay for bulb connected to GPIO 6
// Define DHT sensor setup
#define DHTPIN 7 // DHT11 data pin
#define DHTTYPE DHT11 // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);
// Define PIR Motion Sensor pin
const int pirPin = 8; // PIR motion sensor pin
//Define the parameters
float humidity = 0.0;
float temperature = 0.0;
int motionDetected = 0.0;
unsigned long int lastSend = 0;
int interval = 5000;
void setup() {
Serial.begin(9600);
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
// Ensure relays are OFF by default if using Normally Open
digitalWrite(relayPin1, HIGH);
digitalWrite(relayPin2, HIGH);
// Initialize LoRa
LoRa.setPins(csPin, resetPin, irqPin);
while(!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
delay(500);
}
Serial.println("LoRa Receiver Ready");
// Initialize DHT sensor
dht.begin();
// Initialize PIR motion sensor
pinMode(pirPin, INPUT);
}
void loop() {
// Read sensor data
humidity = dht.readHumidity();
temperature = dht.readTemperature();
motionDetected = digitalRead(pirPin);
// Print sensor readings
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.print("%, Motion Detected: ");
Serial.println(motionDetected ? "Yes" : "No");
// Send sensor data via LoRa to transmitter
sendSensorData(temperature, humidity, motionDetected);
// Check for LoRa commands
receiveLoRaCommand();
}
void sendSensorData(float temperature, float humidity, int motionDetected) {
LoRa.beginPacket();
LoRa.print("Temp:");
LoRa.print(temperature);
LoRa.print(",Humidity:");
LoRa.print(humidity);
LoRa.print(",Motion:");
LoRa.println(motionDetected ? "1" : "0");
LoRa.endPacket();
Serial.println("Sent sensor data via LoRa");
}
void receiveLoRaCommand() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
String receivedCommand = LoRa.readString();
Serial.println("Received command: " + receivedCommand);
// Process the received command
if (receivedCommand == "R1_ON") {
digitalWrite(relayPin1, LOW); // Turn on relay 1
} else if (receivedCommand == "R1_OFF") {
digitalWrite(relayPin1, HIGH); // Turn off relay 1
} else if (receivedCommand == "R2_ON") {
digitalWrite(relayPin2, LOW); // Turn on relay 2
} else if (receivedCommand == "R2_OFF") {
digitalWrite(relayPin2, HIGH); // Turn off relay 2
}
}
}