Mailbox Sensor

I'm tired of walking the whole way to my mailbox to see if there is mail in it, so technology comes to the rescue

Similar projects worth following

The purpose of this project is to instrument my mailbox with sensors so mail can be detected when the mailbox is full, and a 3D printed display mail box can raise its flag.

A Tilt Ball Switch is attached to the mailbox door, to detect when the door is closed. Closing the door triggers the VCNL4200 Proximity Sensor. The proximity sensor has an IR transmitter and receiver, and the difference between the IR reflectivity of the empty mailbox, and a mail filled box is quite measurable. The sensor has an I2C interface, and returns a value proportional to the reflected IR signal from the emitter. The STM32L0 uController runs the show, and will read that value from I2C, as well as the position of the flag through the ADC and a rotary sensor. If the flag is up, then the mail in the box is meant for the postman, and should not trigger the display on the wall of the garage. 

Communication to the base station is done over Lora with E32 Lora modules. These modules communicate to the uController over a UART, and contain the firmware handling of the Lora radio. This simplifies the Lora communication. The mailbox instrumentation will send the raw values from the Proximity Sensor, the Rotary Sensor, and the battery voltage to the base station, which can use that information to decide to raise the flag. The raw values are sent to the base station, because it is far easier to update the firmware on the ESP32 module on WiFi to tweak settings, than it is to update the firmware on the remote STM32. 

The base station consists of an ESP32 modules, another E32 Lora module, and a hobbyist servo. On receiving a Lora message, the ESP32 will decide whether to raise the flag.

The ESP32 also connects to an existing MQTT server, which I use to automatically close my garage door, among other things. This MQTT server provides communication for logging, thought, and monitoring with It is also responsible for sending a text message when the batteries are low, as well as monitoring the heartbeats to ensure both the basestation and remote mailbox are running. 

  • 1 × VCNL4200 Proximity Sensor
  • 2 × E32 Lora Module Lora Module 915MHz
  • 1 × Servo 9 Gram
  • 1 × Murata Rotation Sensor 10K
  • 1 × Mercury Switch

View all 6 components

  • Proof Of Concept Successful

    Kevin Kessler07/24/2019 at 01:44 0 comments

    I've successfully tested a taped together version of the mailbox sensors and a pretty complete version of the base station and have documented it in this video:

    My video presentation skills leave a lot to be desired.

  • The VCNL4200 Proximity Sensor

    Kevin Kessler07/24/2019 at 01:42 0 comments

    The VCNL4200 Proximity Sensor is an IR LED and IR receiver in a single, very hobbyist unfriendly, package. It also contains an ambient light sensor, which I do not use in this project. The package requires 2 power supplies; a 3.3v one for the controller, and a 5V power supply to drive the LED. An external MOSFET and current sensing resistor are required for the device to control the current through the IR LED, and this current, along with the duration and number of pulses per sense, is configured through I2C.

    I configure the device to run the sense on command (as opposed to the default continuous mode), and is configured for the max 800mA current for the shortest interval and 2 pulses. I haven't tried to optimize these setting for current yet, but they seem to be a good compromise of current usage and consistent detection of paper in the mailbox. This application note on using the VCNL4200 is very good, and would be required reading if you wis to use this device.

    The schematic and PCB layout are in a github repository.

  • The ESP32 Base Station

    Kevin Kessler07/24/2019 at 01:14 0 comments

    The ESP32 base station receives the Lora transmissions from the mailbox sensors and indicates the state of the mail with a servo attached to a flag. The base station also forwards the state of the sensor to the MQTT server for further processing. The MQTT server logs the sensor values to, and plot the temperature and voltage data on Thingspeak. The base station also periodically sends a heartbeat to the MQTT server (indicated by a sensor value set of all the lowest values). The MQTT server monitors this heartbeat, and will text my phone if the heartbeat goes missing for long enough.

    The base station code is written in the Arduino core. I normally avoid the Arduino programming model, mainly because I despised the Arduino IDE. I've discovered the PlatformIO IDE, and I'm pretty happy with it; so much so that I've written the STM32 CubeMX code for the Mailbox Sensors with it as well, instead of Eclipse that I use for pretty much everything. 

    In order to configure the device, a SPIFFS file system is created with a JSON file that looks like:

        "ssid": "xxxxxxxx",
        "password": "xxxxxxxxxxxx",
        "pskIdent": "xxxxxxxxx",
        "pskKey": "xxxxxxxxxxxxxxx",
        "mqtt_host": "xxxxxxxxxxxxxx",
        "mqtt_port":  8883

    This is filled out with the wifi credentials and the pre-shared key and address of the MQTT server, and saved to the data directory. The github repo contains this file in a data_model directory, but the actual filled out version must go into a data directory at the same level. I do this so I don't accidentally upload my wifi credential to github. The SPIFFS file system is uploaded separately from the code with the command:

    platformio run --target uploadfs

    The PCB is rather large and would have been expensive to get created at OSHPark so I decided to try some Perf+ boards boards I had lying around.  The Perf + PCB uses perpendicular traces and solder bridges to route signals between endpoints. The software Perfy assists in routing traces and deciding where to put the solder bridges. 

    This type of PCB works because the circuit for the base station is so simple. An ESP32 Dev Board drives an E32 Lora Module through the second UART, and a servo runs through PWM of a GPIO.

  • The MQTT Server

    Kevin Kessler06/19/2019 at 01:29 0 comments

    A full discussion of the MQTT server is an entire project itself, but some background is necessary to understand its function in this system.

    The primary purpose of this system is to close my garage door if I leave it open for more than 10 minutes, but it is a victim of scope creep. This MQTT server is a Raspberry Pi Zero W, in a 3D printed case. It contains a ring of WS2812B RBG LEDs, for eye candy, a camera, a PIR motion sensor, an ambient light sensor, temperature sensor and hall sensors to detect the position of the garage door. The software is written in many Python modules which hang off an MQTT server, and MQTT messages provide the interprocess communication. The architecture is like Node Red for Python, without the complicating GUI.

    So the basic application flow looks something as follows: The garage door starts to open, which triggers one of the hall switches. The GPIOInput Module sends an MQTT message indicating that, which the logging module picks up and logs, and the LED controller module picks up and displays the "Door Opening" pattern. When the door completely opens, another hall sensor detects that, and another message is sent, which triggers the controller modules to start a timer, another log message, and a different led pattern. The source code for this project is here:

    In this project, these modules will be used to monitor the ESP32 base station. Log messages will be sent from the ESP32 to the logging module and to The values returned from proximity sensor and the rotary sensor will be sent to through the thingspeak module so I can find the best values indicating mail in the box, and the position of the flag.

    An important function that the this MQTT system provides is heartbeat monitoring. The MQTT server logs a heartbeat to every 10 minutes. In Amazon AWS, I have a lambda function that runs every 20 minutes, and queries loggly for the heartbeat log messages. If it goes 40 minutes without seeing a heartbeat message, it sends a text message notifying me of a problem. This entire process can be run totally free in AWS lambda since it is such a light process. This same system will be used to monitor heartbeats from the basestation and instrumented mailbox.

    The connection between the ESP32 and MQTT server is SSL configured to use a pre-shared key. This is a simpler approach to encryption as opposed to the full SSL stack, at almost no cost, since full certificate validation in a private network system like this doesn't really exist anyway.

View all 4 project logs

Enjoy this project?



Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates