MQTT Mailbox Notification (battery-powered)

how i built mine for less than 10$

requirements:

  • mailbox is in wifi range
  • mailbox is not surrounded by metal (otherwise antenna to the outside is needed)

parts list:

  • Wemos D1 mini
  • reed switch (i used the MC-31B) and magnet
  • triple AA battery holder
  • project box (i used on of those cheap black 100x60x25mm enclosures)
  • wires

construction:

  1. flash the wemos with code that does the following in order:
    a. connect to wifi (ideally with static IP for less power consumption)
    b. connect to mqtt broker
    c. send mqtt message, e.g. wemos/mailbox/openlid = true
    d. ESP.deepSleep(0) which means deepsleep (power consumption ~ 0.1 mA) until it is reset
  2. install reed switch and magnet to mailbox so that opening the lid toggles the switch
  3. connect the reed switch to the RST and GND pin of the wemos d1 mini
  4. connect the battery holder to 5V and GND pin of the wemos d1 mini
  5. put batteries in and all the stuff in the enclosure and then in your mailbox
  6. configure corresponding mqtt sensor and a automation of your choice in home assistant

similar projects


1 Like

How long do the batteries last with this configuration?

Installed it two weeks ago. With those 2000 mAh batteries my (very rough) estimation is that they will last for one year. Depends on temperature and how often it is opened of course.

Let us know how you actually get on. Powering ESP8266 with batteries is always problematic.

Can you share the code?

#define SENSORNAME "mailboxsensor"

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define wifi_ssid "wifi_ssid"
#define wifi_password "wifi_password"
IPAddress ip(?, ?, ?, ?);
IPAddress dns(?, ?, ?, ?);
IPAddress gateway(?, ?, ?, ?);
IPAddress subnet(255, 255, 255, 0);
IPAddress mqtt_server(?, ?, ?, ?);
#define mqtt_user "mqtt_user" 
#define mqtt_password "mqtt_password"
#define mqtt_port 1883

WiFiClient espClient;
PubSubClient client(espClient);

int wificonnectcounter = 10;
int mqttconnectcounter = 10;

void setup() {
  
  Serial.begin(115200);
  
  WiFi.config(ip, dns, gateway, subnet);
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi_ssid, wifi_password);
  client.setServer(mqtt_server, mqtt_port);
  while (WiFi.status() != WL_CONNECTED and wificonnectcounter > 0) {
    wificonnectcounter = wificonnectcounter - 1;
    delay(250);
  }
  while (!client.connected() and mqttconnectcounter > 0) {
    mqttconnectcounter = mqttconnectcounter - 1;
    client.connect(SENSORNAME, mqtt_user, mqtt_password);
    delay(250);
  }

  client.publish("wemos/mailboxsensor/openlid", "true");

  delay(100);
  ESP.deepSleep(0);
}

void loop() {
  ESP.deepSleep(0);
}
1 Like

I had to replace the batteries two times since June.
I’m now testing with three rechargeable NiMH batteries (eneloops) connected to the 3V pin of the d1 mini.

1 Like

I’m pretty new to this, but your example has been really helpful. Thank you! I have things working (reed switch changes the state to “true” in Home Assistant) but I’m wondering how you reset the state after the mailbox door has been closed. At that point, the ESP is in deep sleep and isn’t communicating the closure. Are you doing that through an Automation? If so, mind sharing the details? I’m not sure how to manually change the state back to anything other than true, once it’s been triggered. Thanks!

1 Like

Really nice to hear that my description was helpful for you. :blush:

To be honest I don’t even use a sensor for that anymore.
You can just use the mqtt trigger for your automation and you should not need a sensor at all.

If you absolutely want to have a sensor you could add the off_delay to the mqtt binary sensor configuration you already have, I assume. Then you possibly also have to change the payload in the arduino code from “True” to “On”, but could also work without that change. I haven’t tested that.

Thanks for the reply! I guess the mqtt trigger makes more sense. I ended up getting the state changed back manually with mqtt.publish and that seems to do the job but maybe I should just scrap it and do it the better way you’ve suggested. Either way, thanks a bunch for your help. Really appreciate it.

…oh, one other question…I’m assuming you need GPIO 16 connected up to RST as well in order to enable Deep Sleep, is that correct? Or does the presence of the reed switch somehow take the place of that connection?

ESP.deepSleep(0); works without connecting GPIO16 to RST because the module does not have to wake itself after a certain time span.
The reed switch will wake up the wemos d1 mini just like the little physical reset button which does not need the GPIO16-RST connection either.

tested the code, code it working. But somehow i cannot get it Home Assistant, any suggestion?

got it to work