banner



Mail In A Box Raspberry Pi

Thanks for reading my blog! If you have any questions or need a 2d opinion with anything Microsoft Azure, security or Power Platform related, don't hesitate to contact me.


Does anyone use the term 'snail mail service' anymore? Anyway, I withal use it to refer to regular posts and mail I seem to be getting each day through my door. We have a sticker on the door to avoid getting spam, so usually, it's something we are either expecting or something useful.

I've wanted to build a solution that detects when I get regular mail. There are numerous solutions like this already out at that place, just I had semi-specific requirements: it would have to exist wireless, and it has to piece of work on a Raspberry Pi Cypher. Why this model of the Raspberry Pi? Considering I accept one I'1000 not using, and information technology'due south small enough to exist left in the entryway of our home.

Pattern

The purpose of the solution is clear – take some machinery to detect when paper mail is inserted through the mailbox in my front end door and warning or notify me accordingly. For alerting, I programme on using Azure Logic Apps. Information technology'southward a versatile service, very affordable, and well suited for a task such equally this.

A rough design looks like this:

  1. Mail arrives, and triggers the sensor
  2. Raspberry Pi polls the sensor (or the sensor pings the Raspberry Pi device) in return
  3. Raspberry Pi calls Logic Apps with relevant data
  4. Logic Apps notifies me – perhaps via electronic mail, or equally a message via Microsoft Teams

I already had the Raspberry Pi Nothing, and the verbal model is the Raspberry Pi Zero WH – and I paid nigh 20 € ($25) for it. Information technology's powered with a regular Micro-USB adapter, and information technology connects to my internal network with Wi-Fi. It besides has Bluetooth, which is critical so that it can ping or poll the wireless sensor.

I researched the sensor earlier realizing that the sensors I already utilise – the Ruuvitag beacons – have a built-in accelerometer. I've previously used the Ruuvitag beacons in numerous projects; see the other builds here. It'south a small, bombardment-powered device that can measure humidity, temperature – and acceleration.

Implementation

Before any real project can get-go, you really have to start with the hardware. I taped the Ruuvitag beacon on my mailbox. In Republic of finland, you have a existent mailbox if you live in a house. I live in an apartment circuitous, so we take a mail flap on the door.

I replaced the battery to ensure my solution will run for years. Now that the hard part is washed, I provisioned a fresh Raspbian Os to the Raspberry Pi Null. One time in that location, I installed Python 3 and the necessary libraries for the Ruuvitag. First, I installed the bluez and bluez-hcidump – the Linux Bluetooth Organisation:

sudo apt-get install bluez-hcidump

Once done, I updated pip3, the Python installer thing:

sudo pip3 install --upgrade setuptools

And finally, I installed the Ruuvitag sensor package:

sudo pip3 install --user ruuvitag-sensor

Because the Raspberry Pi Zero is quite poor in performance, it took more than an hr for these commands to complete. I rebooted the device to ensure information technology's still coming back upward.

I then figured I didn't desire to build a script that runs every minute to poll the sensor. Mayhap my forenoon newspaper arrives at 03:23.23 am, and if I'm not polling the sensor at the exact fourth dimension, I volition miss this crucial event. It turns out y'all can use a Reactive wrapper that reacts to events from the Ruuvitag sensor. My Python knowledge is still minimal, and so I hacked together this script:

          import json import requests from ruuvitag_sensor.ruuvi_rx import RuuviTagReactive ruuvi_rx = RuuviTagReactive()  def foo(accvalue):     if (int(accvalue) > 80):         print(accvalue)         url = 'https://URL-TO-LOGIC-APPS'         response = requests.postal service(url, data = { 'move': accvalue });         time.sleep(60)  ruuvi_rx.get_subject().\     filter(lambda ten: x[0] == 'MAC:Address:FOR:SENSOR').\     map(lambda x: x[1]['acceleration_z']).\     distinct_until_changed().\     subscribe(lambda 10: foo(format(x)))        

And information technology works! It took a few tries. What happens, in essence, is that we filter the letters, using a Python Lambda function, and but poll for acceleration_z, which is the z-centrality of the accelerometer. Remember, the sensor is placed on the postal service flap, and once mail service is inserted, the z-axis volition showtime emitting accelerations. It's an STMicroelectronics accelerometer, and the dispatch values are between 0 g and 16 g. As I learned, an accelerometer measures proper acceleration, and the unit of mensurate is M-Force (G). I'one thousand expecting 80 mg, which is equivalent to 0.08 G of acceleration.

Once we get an issue (from acceleration_z), we phone call a custom role accvalue and verify that the value is above our threshold (80). If so, a simple call to our Logic Apps-based REST API is all that's needed. I added a 60-2d sleep to avoid getting numerous alerts if mail is being inserted very slowly.

There is something magical when you run the script and gently tilt the sensor between your fingers – the readings are instant!

The terminal matter is to create the Logic App. This was piece of cake, and information technology looks similar this:

Information technology'south quite simple – out of habit, I'm defining a variable to concur the data I'yard receiving. I then pick the value (with the bodily strength used for inserting mail) and post that to my test Teams channel. I don't worry much near spamming the aqueduct, equally my script on the Raspberry Pi sleeps for a minute after each notification. Rarely do I get mail that takes minutes to insert through my door.

One concluding thing – running the Python script on the Raspberry Pi in the groundwork. One time you lot log out from the SSH session, the script dies. Then I establish this uncomplicated and somewhat lazy way to put information technology in the background:

nohup python3 ./mailbox.py &

And verifying that the script is running in the groundwork with ps ax | grep python:

Testing the solution

I inserted mail through my door. And after just two seconds, Teams notifies me there's a new aqueduct message!

And looking from Logic Apps, I can encounter it completes quite rapidly – in about 0.8 seconds:

You lot can see the G-force was 0.2 G in my test. And I was quite gentle.

Closing thoughts

Projection completed! It'southward a simple solution, utilizing just a Raspberry Pi Naught and a Ruuvitag sensor. The logic in Azure is too straightforward to replace if that'south needed. Evidently, in that location are possible issues in the future: what if mail is stuck in the flap? Well, I will be getting an warning each minute then. I need to build a unproblematic check somewhere that one time postal service arrives, perhaps we don't need to worry almost mail arriving over again any time soon. As well, information technology would be useful to store the data and timestamps for long-term analysis.

In improver, the Raspberry Pi Nix occasionally loses Wi-Fi and requires a power cycle. As a quick set, I disabled power saving on the Wi-Fi adapter of the device, and it seems to take fixed most connectivity issues.

Upon seeing me install the sensor, my family asked me – "why?" just I estimate if you read all the way hither, you know the reply – because you tin can.

Source: https://jussiroine.com/2021/06/building-a-mailbox-sensor-solution-using-raspberry-pi-and-a-wireless-sensor/

0 Response to "Mail In A Box Raspberry Pi"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel