Hello guys, how are you all doing? I will try to explain this in the best possible way. If you don’t get anything, just ask me. So let’s go.
I’m working on a simple home automation project for my house using ESP32 with WiFi connection and Blynk Server, not local server. So far the logic is very simple. The ideia so far is to use relays to control lights. For now, nothing more. The lights will be controlled by the Blynk App, Wall Switches and Google Assistant. I will be controlling just 8 lights. Over time I will add more functions to the project, but I’m trying to keep it simple as I’m developing it, so it will not be a mess so soon.
I’m using a 16 Relay Module (Low Level Trigger), remember that I’m using just 8 relays out of those 16. I will be using 8 AC Voltage Sensors to know when the lights are turned on or off using wall switches so I can have button feedback within the Blynk App. I’m still coding and implementing stuff. For example the AC Voltage sensors are NOT done yet, but the issue I’m having has nothing to do with it. This circuit is powered by a 5V 1A power supply.
I’ve tested the button feedback using Google Assistant and IFTTT and I know that the button feedback is working fine. If I turn on or off any relay using Google Assistant and IFTTT I have the response within Blynk App. This part of the code is fine.
So I started the real world tests. I turned on all the 8 relays and I left it on to know if they wouldn’t turn off for some reason. And they did. After about 30 minutes all the relays turned off, with no feedback within the app. So I tried again. This time all the relays were off within 5 minutes. One more time and all the relays were off immediately after I turned them on. Finally the app was saying “project is offline” and stayed like this until I reboot the ESP. It doesn’t matter if I use the button ALL ON or if I turn them on one by one or even using Google Home and IFTTT. After some time they all always turn off and I have no idea why.
On my app (iOS version) I’m using 10 buttons. All off them are virtual. V1 to V8 are buttons the control one relay each. V9 Button will turn all the relays on and V10 button will turn all the relays off. V1 to V8 are switch button. V9 and V10 are push buttons.
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
//#include <TimeLib.h> //not using it yet
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // auth token
char ssid[] = "xxxxxxxxxxxx"; //Wifi name
char pass[] = "xxxxxxxxxxx"; //Wifi password
// relays pins
int relay1 = 2;
int relay2 = 4;
int relay3 = 5;
int relay4 = 18;
int relay5 = 19;
int relay6 = 21;
int relay7 = 22;
int relay8 = 23;
// using Boolean variables to declare the button state
boolean switch1State;
boolean switch2State;
boolean switch3State;
boolean switch4State;
boolean switch5State;
boolean switch6State;
boolean switch7State;
boolean switch8State;
//
BLYNK_WRITE(V1) // every time the button is pressed, this will run
{
switch1State = param.asInt(); // get the switch value
if (switch1State == HIGH) {
digitalWrite(relay1, HIGH); // set the pin to HIGH
} else {
digitalWrite(relay1, LOW); // set the pin to LOW
}
}
BLYNK_WRITE(V2)
{
switch2State = param.asInt();
if (switch2State == HIGH) {
digitalWrite(relay2, HIGH);
} else {
digitalWrite(relay2, LOW);
}
}
BLYNK_WRITE(V3)
{
switch3State = param.asInt();
if (switch3State == HIGH) {
digitalWrite(relay3, HIGH);
} else {
digitalWrite(relay3, LOW);
}
}
BLYNK_WRITE(V4) //
{
switch4State = param.asInt();
if (switch4State == HIGH) {
digitalWrite(relay4, HIGH);
} else {
digitalWrite(relay4, LOW);
}
}
BLYNK_WRITE(V5)
{
switch5State = param.asInt();
if (switch5State == HIGH) {
digitalWrite(relay5, HIGH);
} else {
digitalWrite(relay5, LOW);
}
}
BLYNK_WRITE(V6)
{
switch6State = param.asInt();
if (switch6State == HIGH) {
digitalWrite(relay6, HIGH);
} else {
digitalWrite(relay6, LOW);
}
}
BLYNK_WRITE(V7)
{
switch7State = param.asInt();
if (switch7State == HIGH) {
digitalWrite(relay7, HIGH);
} else {
digitalWrite(relay7, LOW);
}
}
BLYNK_WRITE(V8)
{
switch8State = param.asInt();
if (switch8State == HIGH) {
digitalWrite(relay8, HIGH);
} else {
digitalWrite(relay8, LOW);
}
}
BLYNK_WRITE(V9){ // all on function
digitalWrite(relay1, LOW);
Blynk.virtualWrite(V1, LOW);
digitalWrite(relay2, LOW);
Blynk.virtualWrite(V2, LOW);
digitalWrite(relay3, LOW);
Blynk.virtualWrite(V3, LOW);
digitalWrite(relay4, LOW);
Blynk.virtualWrite(V4, LOW);
digitalWrite(relay5, LOW);
Blynk.virtualWrite(V5, LOW);
digitalWrite(relay6, LOW);
Blynk.virtualWrite(V6, LOW);
digitalWrite(relay7, LOW);
Blynk.virtualWrite(V7, LOW);
digitalWrite(relay8, LOW);
Blynk.virtualWrite(V8, LOW);
}
BLYNK_WRITE(V10){ // all off function
digitalWrite(relay1, HIGH);
Blynk.virtualWrite(V1, HIGH);
digitalWrite(relay2, HIGH);
Blynk.virtualWrite(V2, HIGH);
digitalWrite(relay3, HIGH);
Blynk.virtualWrite(V3, HIGH);
digitalWrite(relay4, HIGH);
Blynk.virtualWrite(V4, HIGH);
digitalWrite(relay5, HIGH);
Blynk.virtualWrite(V5, HIGH);
digitalWrite(relay6, HIGH);
Blynk.virtualWrite(V6, HIGH);
digitalWrite(relay7, HIGH);
Blynk.virtualWrite(V7, HIGH);
digitalWrite(relay8, HIGH);
Blynk.virtualWrite(V8, HIGH);
}
void setup()
{
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
digitalWrite(relay1, HIGH); // Set HIGH (off) when BOOT
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
digitalWrite(relay7, HIGH);
digitalWrite(relay8, HIGH);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
I will upload a picture of the system in the comments. Please, any advice on how to make this code better will be appreciated. Thanks guys.