i have a code to control 4 relays with a node mcu which can also be controlled by an RF remote . when the module is powered on it is working jus fine but after sometime it just disconnects (idk if it only disconnects from blynk or it loses connection to the wifi also) and seemingly never connects again it is a constant problem it occurs everytime. Can anyone solve this problem form me? IDK what is wrong with the code or with my Nodemcu Module itself
The Code:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <RCSwitch.h>
BlynkTimer clocktime;
#define relayfan 4
#define relaylight 5
#define relaylightb 14
#define relayrandom 12
#define VPIN_BUTTON_1 V12
#define VPIN_BUTTON_2 V13
#define VPIN_BUTTON_3 V14
#define VPIN_BUTTON_4 V15
RCSwitch mySwitch = RCSwitch();
int relayfanstate = HIGH;
int relaylightstate = HIGH;
int relaylightbstate = HIGH;
int relayrandomstate = HIGH;
int mainstate = HIGH;
long code;
char auth[] = "7k6FzFfP541FKDV-9jwppRFNvR-hDr0n";
char ssid[] = "Nag";
char pass[] = "87654321";
BLYNK_CONNECTED()
{
Serial.println("blynk connected first function");
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
Blynk.syncVirtual(VPIN_BUTTON_3);
Blynk.syncVirtual(VPIN_BUTTON_4);
}
BLYNK_WRITE(VPIN_BUTTON_1)
{
Serial.println("blynk write");
relayfanstate = param.asInt();
digitalWrite(relayfan, relayfanstate);
}
BLYNK_WRITE(VPIN_BUTTON_2)
{
Serial.println("blynk write");
relaylightstate = param.asInt();
digitalWrite(relaylight, relaylightstate);
}
BLYNK_WRITE(VPIN_BUTTON_3)
{
Serial.println("blynk write");
relaylightbstate = param.asInt();
digitalWrite(relaylightb, relaylightbstate);
}
BLYNK_WRITE(VPIN_BUTTON_4)
{
Serial.println("blynk write");
relayrandomstate = param.asInt();
digitalWrite(relayrandom, relayrandomstate);
}
void setup()
{
Serial.begin(9600);
mySwitch.enableReceive(0);
pinMode(relayfan, OUTPUT);
pinMode(relaylight, OUTPUT);
pinMode(relaylightb, OUTPUT);
pinMode(relayrandom, OUTPUT);
digitalWrite(relayfan, HIGH);
digitalWrite(relaylight, HIGH);
digitalWrite(relaylightb, HIGH);
digitalWrite(relayrandom, HIGH);
Blynk.begin(auth, ssid, pass);
Serial.println("Blynk config done");
}
void loop()
{
if (Blynk.connected() == false)
{
Serial.println("Not Connected");
Serial.println("Without internet rf code recieving started");
delay(500);
if (mySwitch.available())
{
Serial.println("Switch available");
code = mySwitch.getReceivedValue();
if (mySwitch.getReceivedValue() == 8594433)
{
Serial.println("code matched without internet");
mainstate = !mainstate;
digitalWrite(relayfan, mainstate);
digitalWrite(relaylight, mainstate);
digitalWrite(relaylightb, mainstate);
digitalWrite(relayrandom, mainstate);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594447)
{
digitalWrite(relayfan, LOW);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594445)
{
digitalWrite(relayfan, HIGH);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594436)
{
relaylightstate = !relaylightstate;
digitalWrite(relaylight, relaylightstate);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594437)
{
relaylightbstate = !relaylightbstate;
digitalWrite(relaylightb, relaylightbstate);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594439)
{
relayrandomstate = !relayrandomstate;
digitalWrite(relayrandom, relayrandomstate);
delay(1000);
mySwitch.resetAvailable();
}
}
}
else
{
Serial.println(" Connected");
Blynk.run();
with_internet();
}
clocktime.run();
}
void with_internet()
{
Serial.println("With internet rf code recieving started");
delay(500);
if (mySwitch.available())
{
Serial.println("Switch available");
code = mySwitch.getReceivedValue();
if (mySwitch.getReceivedValue() == 8594433)
{
Serial.println("code matched without internet");
mainstate = !mainstate;
digitalWrite(relayfan, mainstate);
digitalWrite(relaylight, mainstate);
digitalWrite(relaylightb, mainstate);
digitalWrite(relayrandom, mainstate);
Blynk.virtualWrite(VPIN_BUTTON_1, mainstate);
Blynk.virtualWrite(VPIN_BUTTON_2, mainstate);
Blynk.virtualWrite(VPIN_BUTTON_3, mainstate);
Blynk.virtualWrite(VPIN_BUTTON_4, mainstate);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594447)
{
digitalWrite(relayfan, LOW);
Blynk.virtualWrite(VPIN_BUTTON_1, LOW);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594445)
{
digitalWrite(relayfan, HIGH);
Blynk.virtualWrite(VPIN_BUTTON_1, HIGH);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594436)
{
relaylightstate = !relaylightstate;
digitalWrite(relaylight, relaylightstate);
Blynk.virtualWrite(VPIN_BUTTON_2, relaylightstate);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594437)
{
relaylightbstate = !relaylightbstate;
digitalWrite(relaylightb, relaylightbstate);
Blynk.virtualWrite(VPIN_BUTTON_3, relaylightbstate);
delay(1000);
mySwitch.resetAvailable();
}
else if (mySwitch.getReceivedValue() == 8594439)
{
relayrandomstate = !relayrandomstate;
digitalWrite(relayrandom, relayrandomstate);
Blynk.virtualWrite(VPIN_BUTTON_4, relayrandomstate);
delay(1000);
mySwitch.resetAvailable();
}
}
}