Building an MQTT letterbox. // Tutorial

Remember that thing in front of your house? The letterbox. In this multi-part video I’ll show you how easy it is to get your letterbox to send you alerts via MQTT when you get new mail. So you won’t have to guess anymore.


Jamie’s quote

For those people who’ve been around the UNIX systems scene for years will know about Jamie Zawinski and his famous law of software envelopment, (aka Feature Creep), which is:

Every program attempts to expand until it can read mail. Those programs which cannot so expand are replaced by ones which can.

Well, with this particular project, this is where I start.

The problem

You know how some weeks you get no letters and other weeks your mailbox is full? This is one problem that I had to solve.
I want to be able to detect every time someone puts a letter into the box and also detect the movement of the rear flap, indicating someone has removed letters.

I want to avoid any micro-switches as they are more exposed to the elements, are messy and can be unreliable. So a sensor was needed that could respond to something moving from a distance.


I didn’t want to use a PIR as I may get false readings, especially if the back flap was left open.
Same story with ultrasonic sensors and most of them run off 5 volts which limits the choice of MCU.
An IR shooting sensor could work, but would rely on some fiddly positioning in the mailbox. 5mA current draw was also a little high.
Likewise Phototransistors would be fiddley to work with and would require more cables that I’d want.

So, I eventually settled on this simple distance sensor, which is capable of responding to an object between 20 and 200mm away. Perfect size for my mailbox and there won’t be any noisy bounce or false triggers.

I also needed something on the flap that was cheap and reliable. This Fast Vibration sensor would do the trick and is dirt cheap.

So, the complete Bill Of Materials ended up being:

On the software side I installed the TSL2561 and DHT22 library from AdaFruit, the Sharp distance sensor library, the PubSubClient MQTT library, and enabled support for the ESP8266 in my Arduino IDE.