I am using ESP8266 nodemcu with MCU Base. There is a 1 Channel 5V Relay Module With Optocoupler SRD-5VDC connected to GPIO4. There is also Liquid Water Level Sensor conected to GPIO2.
When the water in the tank raises, the level sensor send a signal to GPIO2. Then the water valve conected to relay switches to open.
The problem is that everything is working fine from the beginning, but after few hours the relay always stops working - there is no clicking sound coming from the relay but I have checked that the water level sensor works fine - the activity shows on a SuperChart in BlynkIot. Then when I restart nodemcu everything again works fine. Then the relay stops working again after few hours.
code:
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN "" //blynk token
#define BLYNK_PRINT Serial //blynk
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define pinZAPVYP D2 //rele PIN
char auth[] = BLYNK_AUTH_TOKEN; //blynk token
char ssid[] = "";
char password[] = "";
int FloatSensor = 2; //D4 pin of NodeMCU
int buttonState ; //reads pushbutton status
int statBUTTON=0; //relay
void setup() {
Blynk.begin(auth, ssid, password);
pinMode(FloatSensor, INPUT_PULLUP);
pinMode(pinZAPVYP, OUTPUT); //relay
digitalWrite(pinZAPVYP, LOW);
Blynk.virtualWrite(V2, 0);
delay(1000);
}
void loop() {
Blynk.run();
delay(1000);
buttonState = digitalRead(FloatSensor); // read the value of float sensor
if (buttonState == HIGH) // if the value is HIGH
{ // the level is high
Blynk.virtualWrite(V3, 0);
}
else
{
Blynk.virtualWrite(V3, 255);
}
if(statBUTTON == 1) //rele
{
digitalWrite(pinZAPVYP, HIGH);
}
else
{
digitalWrite(pinZAPVYP, LOW);
}
}
BLYNK_WRITE(V2)
{
statBUTTON = param.asInt();
}