Hi, I'm having a weird problem. I made a mains power failure device that uses a NodeMCU, a voltage divider reading the voltage from power in side of a diode connected to Vin, and reading on pin D2. And a large capacitor connected between 3.3v and ground pin.
While using a desktop power supply, when i physically disconnect the 5v power, the capacitor holds enough charge to allow the NodeMCU to detect the voltage change and fire off a message to whatsapp. Problem is when I connect it to a dedicated 5v adaptor it doesn't work. All I can think is that the adaptor isn't shutting off quick enough which allows the measured voltage to drop lower before the NodeMCU detects it, but by that time the capacitor has run out of power.
Are there power supplies that instantly shutdown when mains is cut?
I am using digitalRead so just HIGH or LOW. Would I be better using analogRead and setting a threshold? If that would even work.
Or is there any sort of latching circuit that would instantly disconnect the 5v to NodeMCU when mains is cut?
Hi,
Can you please post your code?
Can you please post your circuit diagram, NOT a Fritzy image?
Include, component labels, pin names and power supplies.
Yes, the internal capacitor of the plug-in power supply must be discharged, e.g. via a resistor/LED combination.
Or simply use an opto coupler instead.
The "large cap" must be large enough to supply the arduino until the cap in "the dedicated 5v adaptor " has fully discharged.
However when the voltage from the adaptor drops below about 4V the "large cap" will be supplying current to the arduino and the PSU will have no current drain.
Perhaps you should use an 8266 that includes a lithium battery - like this
and load the PSU so when the mains fails the voltage drops fairly quickly?
Or, does the NodeMCU have a voltage regulator? If so, putting the diode & capacitor on the higher-voltage side of the regulator will help.
For example, with a 12V supply you can use a voltage divider so 10 or 11V is read as a power failure. Then, even with the capacitor dropping down to 6V or less you still have plenty of voltage into the microcontroller.
Another option is a relay with an AC coil so the relay switches within a few milliseconds of a power failure.
I tried the led but no joy, so I then tried reading the voltage with A0 instead of D2 and saying if it dropped below 900 to send message, but for some strange reason when voltage drops below 900 which is equivalent to 4.9volts I get the wifi disconnected message. If I use all the same code but read either 0 or 1 on D2 it works but only when voltage drops below 2.7volts.
Totally confused why the analogRead aint working.
Super capacitor might be an easier option cos I dont want to use a battery that would need changed over time.
Here is code. Sorry about the read wording, I was playing between D2 and A0
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
const int analogInPin = D2; // ESP8266 Analog Pin ADC0 = A0
int sensorValue = 0;
const char* ssid = "########";
const char* password = "##########";
//Your Domain name with URL path or IP address with path
String serverName = "http://api.callmebot.com/whatsapp.php?phone=+00000000000&text=Lakehouse+power+failure&apikey=000000";
String serverNamesetup = "http://api.callmebot.com/whatsapp.php?phone=+00000000000&text=Lakehouse+power+restored&apikey=000000";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
pinMode(D2, INPUT);
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverNamesetup);
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
delay(5000);
}
}
void loop() {
sensorValue = digitalRead(analogInPin);
Serial.println(sensorValue);
if(sensorValue == 0){
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
delay(5000);
}
else {
Serial.println("WiFi Disconnected");
}
}
}
You could continuously send out messages that say the opposite, "power is okay" and even maybe the voltage. Then on the server side, if the message stream times out, either the network is down or the power has failed. Just to consider. Oops I guess you would need a middle server to negotiate the whatsapp.
Which reminds me too, where I am which is not unique, the wifi server goes down fairly instantly when mains power goes down. I wonder if you'll have time to negotiate over the local network before it goes down?
The Nodemcu is fully power so thats not it. This is while testing with NodeMCU hooked upto computer and feeding 5v through diode and divider to sensor pin. While using analogRead on pin A0, if I set the threshold to anything below 900 (4.9v) I get wifi disconnected on Serial monitor instead of the confirmation of message sent. But if i set the threshold to below 800 (4.6v) I get the exact same thing when I get below that threshold. Its acting as if wifi is not connected. The nodeMCU is still powered through usb from computer.
But if I do a digital read on pin A2, I can drop the voltage down to 2.7v before the Serial reading goes low and the nodeMCU sends the message to watsapp.
Its as if the analog read is breaking something.
Why would the analogRead be breaking the wifi, and the digitalRead be working fine?
I just tried a supercapacitor and it worked using the digitalRead code The nodeMCU stays on longer than the adaptor, so kinda sorted, but I would still really like to know why the analogRead wasnt working. If anyone knows, thanks
One possibility, an analog read is slower than a digital read. Nobody should believe that analogRead() somehow directly breaks wifi... it's likely the way it affects the timing in this case (the way you've set it up, whatever that is).
I watched this video last night https://www.youtube.com/watch?v=xPlN_Tk3VLQ&t=792s in it the guy talks about the ADC pins conflicting with wifi. So apparently you cant do analog reads while connected to wifi.