Hi Everyone,
Blynk is a wonderful thing, I really like the concept and its interface, however I am still learning Blynk and coding skill. I try searching the similar issue thread through Blynk Forum and Google, maybe my English problem, I did not find the solution yet, I really don’t know is it the sketch or hardware issue, I am struggling with the weird behavior for a week, please kindly help to identify the issue.
History:
I used to build Relay with PIR/RF/Magnetic Door Sensor individually and all of them are working without any issue so far, now I combine all the hardware & sketches into one Blynk Project. I want the light to turn on either by PIR sensing or open the door or press RF button or press Blynk Button.
Problem Statement:
In the begining, let’s say in the first 5 minutes, everything is OK, after that, the relay happened the following situation,
- Turn off light too early before timer reached (Ex: Sketch shows PIR sensed HIGH and then triggered Relay to turned on light for 10 seconds, but it turned off light at 5~10 second, same issue for RF & Door Sensor).
Action:
-
I made 2 hardware sets with same sketch in order to clarify the issue, basically the hardware for these 2 sets are exactly the same, except the PIR/Radar, 1st assembly is NodeMcu+PIR+RF+Relay+Magnetic door Sensor, the 2nd one is WeMos D1 mini Radar+RF+Relay+Magnetic Door Sensor, they all encountered the same issue, after that, I used another 3 different 5V relays and two 5V power adapter to do troubleshooting, but still no joy.
-
I added one 2N2222 Transistor & 4.7K resistor on Relay Out Pin, did not see any improvement.
Hardware:
- NodeMcu Amica (2.4.1) or WeMos D1 mini
- PIR hc-sr501 or RCWL-0516 Radar
- Magnetic Door Sensor
- RF 433 Receiver
- 5V Relay
- Two 5V 2.4A power supply, one is for WeMos D1 mini board, another one is for supplying PIR+RF+Relay+Magnetic Door Sensor.
Blynk Local Server Version: V0.38.3
Blynk Library: 0.5.3
Blynk App Version: V2.26.7
NodeMcu ESP8266 Core Version: 2.4.1
Blynk Widget:
- Button
- LED
Wire Connection:
D1: RF 433 Receiver
D2: PIR or RCWL-0516 Radar
D5: 5V Relay
D7: Magnetic Door Sensor
Sketch:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <SPI.h>
#include <ArduinoOTA.h>
#include <RCSwitch.h>
#define RF_RECEIVER D1
char auth[] = ""; //Local Server
char ssid[] = "";
char pass[] = ""
int relay = D2;
int radar = D5;
int door = D7;
int counter = 0;
int isDoorOpened = 0;
int VButton = 0;
int flagRadar = 0;
int flagRF = 0;
int flagDoor = 0;
int flagVButton = 0;
int staterelay = LOW;
int val;
int previous = LOW;
long xtime = 0;
long debounce = 500;
int stayON = 10000; //stay on for 10000 ms
long lastActivated = 0; //last time display was activated
long myDelay = 15000; //15 seconds in millis
RCSwitch mySwitch = RCSwitch();
WidgetLED led(10);
SimpleTimer timer;
BLYNK_CONNECTED() {
Blynk.syncAll();
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if (Blynk.connect()) {
BLYNK_LOG("Reconnected");
} else {
BLYNK_LOG("Not reconnected");
}
}
}
void setup() {
pinMode(radar, INPUT);
pinMode(relay, OUTPUT);
pinMode(door, INPUT_PULLUP);
digitalWrite(relay, LOW);
digitalWrite(radar, LOW);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
Blynk.begin(auth, ssid, pass, "123.456.com", 8080); //Blynk Local Server
while (Blynk.connect() == false) {}
ArduinoOTA.setHostname("");
ArduinoOTA.setPassword((const char *)"");
ArduinoOTA.begin();
int mytimeout = millis() / 1000;
while (Blynk.connect() == false) { // try to connect to server for 10 seconds
if ((millis() / 1000) > mytimeout + 8) { // try local server if not connected within 9 seconds
break;
}
}
mySwitch.enableReceive(RF_RECEIVER); //RF 433 Receiver
timer.setInterval(1000L, CheckSensor); //PIR or Radar Sensor
timer.setInterval(150L, RFSwitch); //RF433 Wall Mount Switch
timer.setInterval(1000L, CheckDoorStatus); //Magnetic Door Sensor - 3.4M Cable
}
//Radar/PIR Sensor Status Check
void CheckSensor() {
val = digitalRead(radar);
Serial.print("Radar Status: ");
Serial.print(val, DEC);
Serial.println(" ");
Serial.println(" ");
if (flagRF == 0 && flagDoor == 0 && flagVButton == 0) {
if (val == 1 && previous == LOW && millis() - xtime > debounce) {
if (staterelay == HIGH) {
digitalWrite(relay, LOW);
}
//verifies it's been 15 seconds
else if (millis() - lastActivated > myDelay) {
digitalWrite(relay, HIGH); //Turn On Relay
Blynk.virtualWrite(V11, HIGH);
led.on();
flagRadar = 1;
timer.setTimeout(10000L, []() { // LED Light is On for 10 sec
digitalWrite(relay, LOW);
Blynk.virtualWrite(V11, LOW);
led.off();
});
flagRadar = 0;
lastActivated = millis(); //resets lastActivated
}
xtime = millis();
}
previous == val;
}
}
//RF433 Switch Turn On Light Manually for Long Period
void RFSwitch() {
if (mySwitch.available()) {
if (mySwitch.getReceivedValue() == 1234567 || mySwitch.getReceivedValue() == 7654321 && flagRF == 0 && flagRadar == 0 && flagVButton == 0) {
digitalWrite(relay, HIGH); //Turn On Relay
Blynk.virtualWrite(V11, HIGH);
led.on();
flagRF = 1;
timer.setTimeout(300000L, []() { // Run every 5 minutes
digitalWrite(relay, LOW);
Blynk.virtualWrite(V11, LOW);
led.off();
flagRF = 0;
});
}
mySwitch.resetAvailable();
if (mySwitch.getReceivedValue() == 1234567 || mySwitch.getReceivedValue() == 7654321 && flagRF == 1 && flagRadar == 0 && flagVButton == 0) {
digitalWrite(relay, LOW);
Blynk.virtualWrite(V11, LOW);
led.off();
flagRF = 0;
}
}
}
void CheckDoorStatus() {
int isDoorOpened = digitalRead(door);
Serial.print("Door Status: ");
Serial.print(isDoorOpened);
Serial.println(" ");
Serial.println(" ");
if (isDoorOpened == 1 && flagDoor == 0 && flagRadar == 0 && flagRF == 0 && flagVButton == 0)
{
digitalWrite(relay, HIGH); //Turn On Relay
Blynk.virtualWrite(V11, HIGH);
led.on();
flagDoor = 1;
timer.setTimeout(120000L, []() { // Run every 2 minutes
digitalWrite(relay, LOW);
Blynk.virtualWrite(V11, LOW);
led.off();
});
}
if (isDoorOpened == 0 && flagDoor == 1)
{
led.off();
flagDoor = 0;
}
}
//Virtual Button to Turn On/Off Light Manually
BLYNK_WRITE(V11)
{
int VButton = param.asInt(); // Get value as integer
if (VButton == 1)
{
digitalWrite(relay, HIGH);
led.on();
flagVButton = 1;
}
else
{
digitalWrite(relay, LOW);
led.off();
flagVButton = 0;
}
}
void loop() {
ArduinoOTA.handle();
Blynk.run();
timer.run();
}