Hello,
I have a cat and a anxious wife, so to prevent checking every window while going outside, I am trying to make 6 circuits with ESP-01, li-po battery and micro switch and place them on windows.
I am sharing my pinouts.
The problem I am facing is, to wake the ESP-01 from the deep sleep, I have to
(I believe) connect RST pin to micro switch's signal leg. It works when the micro switch button is pressed (window is closed) but since I am sending POST requests to my Raspberry PI 4 with opening and closing states of the windows, opening window doesn't trigger RST because it is sending HIGH signal while switch is opened. So my ESP-01 keeps on sleeping when the window is opened.
By the way it's not shown on the diagram but I have soldered GPIO16 pin to RST to use deep-sleep functionality of ESP-01
If you tell me that it's impossible with ESP-01 and I have to use another ESP board, I can buy them too. All I want is being able to wake ESP-01 when micro switch state changes and post a request to my Raspberry PI and let it sleep peacefully.
Here is my code.
#include <Arduino.h>
#include <WiFiManager.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
const int pinWifiLed = 0;
const int pinMicroSwitch = 2;
bool shouldSleep = false;
String windowState;
char endpoint[40] = "http://192.168.1.102:3000/hall";
int state;
WiFiManager wm;
// Runs whenever the micro switch changes state
ICACHE_RAM_ATTR void changeWindowStatus() {
shouldSleep = true;
}
void setup() {
pinMode(pinWifiLed, OUTPUT);
pinMode(pinMicroSwitch, INPUT);
// Set the micro switch pin as interrupt, assign interrupt function and set CHANGE mode
attachInterrupt(digitalPinToInterrupt(pinMicroSwitch), changeWindowStatus, CHANGE);
// Explicitly set Wi-Fi mode
WiFi.mode(WIFI_STA);
bool res;
res = wm.autoConnect("AutoConnectAP", "password");
if (res) {
if (MDNS.begin("esp8266")) {
digitalWrite(pinWifiLed, HIGH);
}
}
}
void loop() {
if (shouldSleep) {
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(pinWifiLed, HIGH);
WiFiClient client;
HTTPClient http;
http.begin(client, endpoint); // HTTP
http.addHeader("Content-Type", "application/json");
if (digitalRead(pinMicroSwitch) == LOW) {
Serial.println("closed");
windowState = "closed";
state = 0;
} else {
Serial.println("open");
windowState = "open";
state = 1;
}
http.POST("{\"window\":\"salon1\",\"state\":\"" + windowState + "\"}");
http.end();
shouldSleep = false;
ESP.deepSleep(0);
}
}
}
If you can tolerate a little delay a more robust (and straighter ahead) solution would be to wake up the microprocessor every minute or three and have it attempt to report the window status until it succeeded or decided it was never going to succeed, attempts separated by a random delay.
This would allow N window sensors to reliably report the state of the window with less possibility of communication collisions resulting in missed brief events, window opening or closing.
possibly an exclusive-OR with one input connecter to the window switch and an RC pair between the window switch and the other input.
when the switch changes the 2 inputs of the exclusive OR will differ causing the output to go high until the capaciitor charges/discharges to equal the input.
is there a way to program the ESP to do this or is an external circuit needed (e.g. 555)?
If you put esp to deepSleep(0), it never wakes up...
You cant use lithium battery like that, when it's full, it has voltage 4.2V, way too high for esp.
There are tiny battery powered wireless window sensors around. Like Shelly Blu Door and Window. Several years of battery life.
Good question. I assume so; other AVRs offer wake up on the watch dog timer, that's 8 seconds maximum on an UNO so you'd have to count those for a longer period.
It would take some figuring to decide between that and some very low power '555 circuit type thing.
Waking up every 8 seconds for a bit of work would use less power, I think.
I didn't include AMS1117 3.3v voltage reducer to the sketch. Since I was afraid of using this li-po directly would burn my ESP-01, I added it between ESP-01 and li-po so it gives 3.3v. After your message I am now sure that it would burn it! =)
If you put esp to deepSleep(0), it never wakes up...
But, isn't pulling RST pin to LOW causes ESP-01 to wake up?
This MrDIY guy has a video on youtube about the very same circuit I am trying to do. I couldn't
understand the ATtiny13a part at first but today I got in touch with him and he told me that it is impossible to achieve this project with bare ESP-01 ot ESP8266. Like him, I should use another micro controller like ATtiny to externally trigger to activate on both LOW->HIGH and HIGH->LOW states.
I wouldn't want to do this task with intervals, waking the ESP-01 externally was a challenge for me and to be honest it is the most precise solution I can think of. If I fail to wake ESP by the ATtiny solution then I would lovely do your solution!
Always.
Difficult to help if real setup doesn't corrisponde with described setup. Anyway don't expect great battery life from your circuit, what's the minimum input of your voltage regulator to output 3.3V...?
I will be watching to see if the solution you have found works well.
It must never miss an event, as it is based on the events at the window. Or is it?
Two identical systems must use some method of collision resolution, or maybe not, and certainly not if you "know" that window events are not performed at the same time.
I like the timer wake up in part because it is software, and no messing with another micro with its own code.
My garage has a microprocessor, the heavies may be able to say exactly which kind when I tell you it wakes up every 256 * 18 milliseconds (2.3 seconds) and when it has counted 25 of those (about a minute) says the door is open or closed.
A base station is always listening. Repeated failure to obtain the door status is a signal that the batteries (4 x AA) have run down. That is an infrequent event.
The loop is open. The garage is just one of a number of similar transmitting entities. It was done with cheap 433 style simple transmitter and receiver modules.
I would prefer you to use a cell or battery with chemistry other than a Lipo. They are great, but have their place. Here I would use something rechargeable but standard like AA or AAA.
So I have asked myself several questions and look forward to looking at why the ATTiny can do something that just cannot be done by another micro.
The OP already has this set up by connecting GPIO16 to RST. In deep sleep mode GPIO16 is connected to a timer (that is the only thing running in deep sleep). It triggers after the number of microseconds specified in the deep sleep call. It is a 32 bit timer so you can sleep up to about 70 minutes.
But you should. In many voltage regulators it's noticeably higher than the output voltage.
You could use Lifepo4 cell without any voltage regulator. Or some wemos board that has built in 18650 battery holder.
But I still recommend Shelly or similar ready made sensor. You wanted to simplify your life with your 6 windows, not to have a second job for charging 6 batteries every other day...