Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.
Months on one battery, if you are careful
The reed switch closes a circuit with a magnet and wakes the ESP32 from deep sleep, so the chip does nothing until the door moves. The bare chip sleeps at a few microamps, but a typical dev board does not: its voltage regulator and power LED can eat a milliamp. For a real battery device use a bare module or remove the LED and the regulator.
Where the energy actually goes
The big cost is not the sleep, it is connecting to Wi-Fi after the wake-up, one to three seconds at high current. Shorten it by keeping a static IP and the channel and BSSID of the access point. If you have several sensors, ESP-NOW to a single always-on ESP32 gateway sends the message in tens of milliseconds and extends the life a lot.
Set the wake level to the opposite state after each event, so you catch both opening and closing.
The number of door events per day, not the calendar, determines the life. A front door and a wardrobe are very different.
Use a magnet with a gap of a few millimetres; reed switches are sensitive to misalignment.
Components
ESP32 DevKit V1 (30 pin, WROOM-32)
Reed switch + magnet (door sensor)
18650 Li-ion cell + holder
TP4056 USB-C Li-ion charger with protection
Wiring
Module
Module pin
ESP32 pin
Note
Reed switch
one side
GPIO33 (RTC)
internal pull-up, other side to GND
Code
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN"; // from @BotFather
const char* TG_CHAT = "YOUR_CHAT_ID";
// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
if (WiFi.status() != WL_CONNECTED) return false;
WiFiClientSecure client;
client.setInsecure(); // simple; pin the certificate for production
HTTPClient http;
String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
if (!http.begin(client, url)) return false;
http.addHeader("Content-Type", "application/json");
String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
int code = http.POST(body);
http.end();
return code == 200;
}
#define WAKE_PIN GPIO_NUM_33
void setup() {
Serial.begin(115200);
pinMode(33, INPUT_PULLUP);
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
if (cause == ESP_SLEEP_WAKEUP_EXT0 || cause == ESP_SLEEP_WAKEUP_UNDEFINED) {
if (connectWiFi()) telegramSend(digitalRead(33) ? "Door opened" : "Door closed");
}
// wake on the opposite level so that both open and close are reported
esp_sleep_enable_ext0_wakeup(WAKE_PIN, digitalRead(33) ? 0 : 1);
esp_deep_sleep_start();
}
void loop() {}
Build tips
Use a 1 MΩ external pull-up to keep the idle current in the microamps.
Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.
Comments
No comments yet. Share your experience or ask a question.