Power outage detector (5v relay+NodeMCU) - strange behavior

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:

  1. when first connect my powerbank to my NodeMCU, it should notify with message "Power detector has started"
  2. when I switch off my home power, it should notify with message "Power is outage at home"
  3. when I switch on my home power, it should notify with message "Power is back on at home"

The actual results:

  1. when first connect my powerbank to my NodeMCU, it does notify with message "Power detector has started"
  2. 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.

What are the pin designations of the pins to the left of the relay module?

What happens if you leave the home power ON, but unplug the UPS?

5v from USB cable to DC+ and IN pins. Ground from USB to DC-.
Interfacing with NodeMCU: NC-->D1, COM-->GND

The NodeMCU just stops running the sketch? Not sure I understood your question.

Unplugging the UPS will put the NodeMCU on backup power, wanted to verify there was not a problem with the power adapter you were using to power the NodeMCU.

I see. Actually, for my testing purpose, I feed 5v power to my NodeMCU with a big power bank which has plenty of power. I also tried it with another power bank and it behaves the same.

Hi,
Can you actually measure the voltage, with a DMM, from D1 to GND going from HIGH to LOW, and staying in those states?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

It stays at around 3.21-3.22v when the relay circuit is on, and drops to 0V when off.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.