Hello everyone, I’m currently working on a home automation project using LoRa. I am using Blynk which is connected to an esp8266 on the sender side.
i)Sender Circuit:
- Microcontroller: ESP8266 (Wi-Fi module)
- Wireless Communication: LoRa RA-02 module for long-range communication
- Integration: Connected to a home automation app for remote operations
ii)Receiver Circuit:
- Microcontroller: Arduino Uno
- Sensors and Actuators:
- DHT11 (Temperature and Humidity Sensor)
- PIR Motion Sensor
- 2-Channel Relay (to control a bulb and a fan)
Wireless Communication:* LoRa RA-02 module receiving commands from the sender
However, I’ve encountered a problem with the relay control on the receiver side. It seems like the bulb and fan are not being switched on/off at all. I’ll be grateful for any pointers on where the problem could be
Sender:
#include <ESP8266WiFi.h>
#include <LoRa.h>
// WiFi network details
const char* ssid = " ";
const char* password = " ";
// Blynk authentication token
#define BLYNK_TEMPLATE_ID "TMPL2eJutAnKH"
#define BLYNK_TEMPLATE_NAME "LoRa Ra 02 with ESP8266"
#define BLYNK_AUTH_TOKEN "ZHais_e9FkAFjqzTVa-gX37mCt8dYfeU"
#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);
WiFi.begin(ssid, password);
while (WiFi.status()!= WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Initialize LoRa
LoRa.setPins(csPin, resetPin, irqPin);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
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");
}
}
BLYNK_WRITE(V2) {
if (param.asInt() == 1) {
Serial.println("Button 2 pressed");
sendLoRaCommand("R2");
}
}
void sendLoRaCommand(String command) {
LoRa.beginPacket();
LoRa.print(command);
LoRa.endPacket();
Serial.println("Sent LoRa command: " + command);
}
Receiver:
#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
const int relayPinFan = 5; // Relay for fan connected to GPIO 5
const int relayPinBulb = 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
void setup() {
Serial.begin(9600);
pinMode(relayPinFan, OUTPUT);
pinMode(relayPinBulb, OUTPUT);
// Ensure relays are OFF by default if using Normally Open
digitalWrite(relayPinFan, HIGH);
digitalWrite(relayPinBulb, HIGH);
// Initialize LoRa
LoRa.setPins(csPin, resetPin, irqPin);
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
Serial.println("LoRa Receiver Ready");
// Initialize DHT sensor
dht.begin();
// Initialize PIR motion sensor
pinMode(pirPin, INPUT);
}
void loop() {
// Read sensor data
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
int 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");
delay(1000);
// 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 command = "";
while (LoRa.available()) {
command += (char)LoRa.read();
}
Serial.print("Received command via LoRa: ");
Serial.println(command);
relayControl(command);
}
}
void relayControl(String command) {
if (command == "R1") {
// Handle command for Relay 1 (Fan)
digitalWrite(relayPinFan, !digitalRead(relayPinFan));
Serial.println("Received command for Relay 1 (Fan)");
} else if (command == "R2") {
// Handle command for Relay 2 (Bulb)
digitalWrite(relayPinBulb, !digitalRead(relayPinBulb));
Serial.println("Received command for Relay 2 (Bulb)");
}
}