I'm using NodeMCU and 5v Relay control for notification messages in case of the power outage and power recovery, using Naver Line Notify API call. This is the diagram:
Connection:
- the NodeMCD will connect to UPS via a 5v adapter to its microusb port
- the relay is connected to my home power outlet through a 5v adapter
- when home power is on, the D1 and GND on NodeMCU are open
- when home power is off, the D1 and GND on NodeMCU are closed or short, and a power outage notification message will be sent to my mobile phone
- when home power is back on, D1 and GND is opened again, the power recovery notification message will be sent to my mobile phone
Please find below my coding:
#include <ESP8266WiFi.h>
#include <TridentTD_LineNotify.h>
int Relay = D1;
#define LINE_TOKEN "5eCw...Line Notify service token......VSm"
char ssid[] = "myssid";
char pass[] = "mywifipass";
void setup()
{
Serial.begin(9600);
pinMode(Relay,INPUT_PULLUP);
WiFi.begin(ssid, pass);
LINE.setToken(LINE_TOKEN);
delay(10000);
LINE.notify("Power detector has restarted");
}
void loop()
{
int RelayState = digitalRead(Relay);
if(RelayState == LOW){
LINE.notify("Power is outage at home");
delay(3000);
while(digitalRead(Relay)== 0){
}
delay(3000);
LINE.notify("Power is back on at home");
}
else
{
}
}
Based on the code above, and home power is already on (and relay is on), I expect:
- when first connect my powerbank to my NodeMCU, it should notify with message "Power detector has started"
- when I switch off my home power, it should notify with message "Power is outage at home"
- when I switch on my home power, it should notify with message "Power is back on at home"
The actual results:
- when first connect my powerbank to my NodeMCU, it does notify with message "Power detector has started"
- when I switch off my home power, it does notify with message "Power is outage at home"
3.1 after switching off a few seconds, then I switch on my home power again, it does notify with message "Power is back on at home"
3.2 however, if I switch off my home power a bit longer, then I switch it on again, it notifies with "Power detector has started", even the power bank is feeding my NodeMCU all along.
3.3 however, if I switch off my home power, during its power off (relay off), it keeps on notifying both "Power detector has started" and "Power is outage at home", alternately. In this case, eventually I switch on my home power, it just notifies "Power detector has started"
I'm not sure whether my coding is somehow wrong, or the NodeMCU reboots itself regularly while pin D1 and GND are short.
Your advice would be highly appreciated.