Not an idea of mine: one day a friend asked me for a way to remotely check if any post-mail was laying into his mailbox. The mailbox is not on the walkpath to his door so, as he is a lazy boy, he wondered if a technology gadget should be able to warn him of any letter into the mailbox. I had a look at the market and I couldn’t find any ready-made device suitable to his needs, so I raised a challenge to myself: why not design and build it?
The constraints were:
- battery-powered with a reasonable life between battery changes;
- WiFi communication;
- check just once a day if there was mail or not;
The main question was: which kind of sensor could fit my requirements? A proximity sensor couldn’t work, as the check had to be done just once a day and not in real-time; a weight sensor neither, as this would have added complexity and sensitivity problems (a sheet of paper could be very light). My choice landed on a Time-of-Flight (a micro laser) sensor. Once calibrated it for the mailbox size, anything laying in the middle would have triggered the sensor!
Considering the 3 constraints, I decided to use an ESP8266 (running the software and linking to WiFi), a VL6180 Time-of-Flight sensor for the measurement and a DS3231 Real Time Clock to trigger all the circuitry once a day: that was how Postino was born.
Parts and Components
- ESP8266-01 (or ESP-12E NodeMCU)
- VL6108 Time-of-Flight sensor
- DS3231 Real Time Clock
- IRLZ44 N-channel MosFET
- BC547 Transistor
- CR123 Battery
The heart of the system is VL6180 sensor. This is a ground-breaking technology allowing absolute distance to be measured independent of target reflectance. Instead of estimating the distance by measuring the amount of light reflected back from the object (which is significantly influenced by color and surface), the VL6180X precisely measures the time the light takes to travel to the nearest object and reflect back to the sensor (Time-of-Flight). Combining an IR emitter, a range sensor and an ambient light sensor in a three-in-one ready-to-use reflowable package, the VL6180X is easy to integrate and saves the end-product maker long and costly optical and mechanical design optimizations.
The module is designed for low power operation.
I used the Pololu breakout board which has voltage regulators on board that allow it to work over an input voltage range of 2.7 V to 5.5 V.
The sensor allows 3 valid scaling factors which set the maximum range of the measurement from 20 to 60 cm, with different sensitivities. By configuring a range scaling factor, the potential maximum range of the sensor can be increased at the cost of lower resolution. Setting the scaling factor to 2 provides up to 40 cm range with 2 mm resolution, while a scaling factor of 3 provides up to 60 cm range with 3 mm resolution.
You have to test the 3 scales with your mailbox dimensions. As mine was 25 cm (H) I used scale factor=1.
Real Time Clock customization
For the RTC I used a DS3231 breakout board which includes a EEPROM (useless for my purpose) and a coin size battery. As I decided to power the RTC through main device battery (a 3v CR123), I removed the coin battery; to save power I also removed the EEPROM (by carefully cutting its pins) and the onboard led.
The coin battery was not useful to me because I didn’t need to keep real time date/hour/minute/second, but the RTC had only to count for 24 hours and then trigger the alarm to power on the device.
Other miscellaneous on board
Powering on of the device is accomplished by a transistor and MosFET circuit, triggered by the RTC alarm. Once the alarm is reset, the circuit cuts the power to the device for another 24 hour cycle. When alarm is reached, the DS3231 switches a pin from high to low: in normal conditions the transistor is saturated and shorts to ground MosFET’s gate. Once the alarm brings transistor’s base to ground, it opens and allows the MosFET to close the circuit and give power to the rest of the components.
In addition, I added a “test-1M” jumper. The purpose of this switch is – if activated – to change the cycle from once a day to once a minute, in order to run deployment tests. In order to change the interval from one day to one minute, you first need to close jumper “Test-C” for about 15 seconds, to bypass the clock alarm activation period and to switch on the device. When tests are done, open the jumpers and reset the device (cycle power).
Software and logic
During the tests I used (for practical reasons) a NodeMCU controller, so the software takes care of this by setting the CHIP variale to “NodeMCU” or “esp8266”.
The sketch implements the WiFiManager library to allow the device to connect to a valid WiFi AP during the very first run. In such a case, the device goes into AP mode, allowing you to connect to it and choose the right WiFi network to join. After that, the network configuration is saved in the EPROM for subsequent cycles.
The variable REST_MSG contains the http message to send when the sensor finds an object in the mailbox. In my case, it sends a message to a domotic REST server, but you can change it as you prefer: a Telegram BOT message, an IFTTT WebHook event, etc.
The rest of the sketch is all in the setup() function, as the loop is never reached. After the configurations needed for the several libraries, the software sets the clock time to 00:00:01 and the alarm to once a day (or once a minute if the “test-1M” jumper is activated). Then it makes the measure, sends the notification (if any object is found in the mailbox) and resets the alarm pin, shutting down the device. At the end of the cycle, only the RTC is powered on, counting for 24 hours. The jumper Test-1M is connected to RX pin of the ESP8266, used as GPIO-3 by means of the setting: setMode(PIN, FUNCTION_3). Due to this, you cannot use the Serial monitor while running the ESP8266: the “#define DEBUG” line (which allows all the serial prints in the sketch) is used only when a NodeMCU is installed in place of an ESP8266.
The ESP8266 handles the I2C communication with the RTC and the sensor through its pins GPIO-0 and GPIO-2, initialized in the Wire library.
The full code can be downloaded from this link.