---
# System prepended metadata

title: Untitled

---







## Tutorial on how to build a smart postbox

#### By Anton Kohlbacher, ak224jx.

In this tutorial it is explained how monitor physical mail incoming into your postbox. Also alerting the postbox owner over the internet. To complete a project like this for proof-of-concept stage approximately two days are needed given that all hardware is in place.




### Objective


I have chosen to make this device to:
- Learn more about IoT.
- Let my dad know when he gets mail so he doesn't have to run out and check in the cold and get dissapointed. 

I think this product might be useful in rural areas where the postbox is far away, but mostly it's been made for fun and learning. Completing this project will certainly give you insight on the basics concepts of IoT and development within this area.



### Material

- LoPy4: Micropython-programmable microcontroller
- Expansion Board: Allows the LoPy4 to connect with USB and pins 
- Breadboard: Base for connections
- Jumpwire: Connect different items/pins
- Reed Sensor NC (Normally Closed). To detect postbox lid opening/closing.

| Material            | Where to buy            |Price|
| -----------------   |:----------------------- |:--------|
| LNU IoT bundle      | https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/   | 1024 SEK|
|Reed Sensor NC|https://www.alibaba.com/| 10 SEK |





![LoPy!](https://pycom.io/wp-content/uploads/2018/08/lopySide-1.png =360x)
Fig. 1. LoPy4 with headers. Pycom.io


### Computer setup


Installing latest firmware is always recommended. Guides to do this for the pycom device and the expension board are available here:
- https://docs.pycom.io/pytrackpysense/installation/firmware/
- https://docs.pycom.io/gettingstarted/installation/firmwaretool/


The IDE that's used in the project is Visual Studio Code. Plugins needed are Pymakr and Node.js. A detailed guide for installation and setup of the IDE is available here: https://docs.pycom.io/pymakr/installation/vscode/
The device can then be flashed using the Pymakr plugin in VS Code.



### Putting everything together

Wiring diagram shown below. Reed switch connected as input to the pycom device. LoRa antenna connected to send data over the air to internet.
The reed swich is connected to GND and Pin 22. 
When Pin 22 goes HIGH (reed switch Open Circuit) the device will wake up.

![Wiring](https://imgur.com/SJJy3KY.png)



### Platform

The platform used is Pybytes to easily visualize the collected data in the cloud. This platform also allows integration with a mobile APP, device management over the air and device monitoring.

- https://pycom.io/products/software/pybytes-3/



### The code

This code sets pin P22 as an external wakeup source. 
If the device wakes up by the reed sensor (connected to P22) it will connect to LoRa. Then it will send the status that the mailbox was opened which is indicated by the fact that the device woke up.

When first booting the device a proper setup is made handled by the else statement. 


```python=
from network import LoRa
from network import Bluetooth
import socket
import utime
import pycom
import machine
import ubinascii
from machine import Pin

# Disable bluetooth to save power
bluetooth = Bluetooth()
bluetooth.deinit()

#Set pin 22 to wake up from deepsleep if it goes high
machine.pin_deepsleep_wakeup(P22, machine.WAKEUP_ANY_HIGH, True)

# Disable LED heartbeat (so we can control the LED)
#pycom.heartbeat(False)

if machine.reset_cause() == machine.PIN_WAKE:
    print('Woke from a Deep Sleep by reed sensor')
    lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
    lora.nvram_restore()
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
    s.setblocking(False)
    s.bind(1)
    status=1
else:
    print('Power on or Hard reset') # Join LoRaWAN with OTAA
    # lora config
    lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)
    # TTN access info
    app_eui = ubinascii.unhexlify('****************')
    app_key = ubinascii.unhexlify('yyyyyyyyyyyyyyyyyyyyyyyyy')

    # attempt join - continues attempts background
    lora.join(activation=LoRa.OTAA, auth=(app_eui, app_key), timeout=0)

    # wait for a connection
    print('Waiting for LoRaWAN network connection...')
    while not lora.has_joined():
        # if no connection in a few seconds, then reboot
        if utime.time() > 500:
            print("possible timeout")
            machine.reset()
        pass
    print('Network joined!')
    # setup the socket
    s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
    s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
    s.setblocking(False)
    s.bind(1)
    status=0

#send the packet via LoRa and pybytes
s.send(1)
pybytes.send_signal(22, status)

#print('Going to sleep now...')
lora.nvram_save()
machine.deepsleep(3600000000)
# End code
```



### Transmitting the data / connectivity

The data is transmitted to the internet via LoRa and is sent every time the device wakes up. The device wakes up every time the mailbox lid is opened. The transport protocls used are LoRaWAN (TTN) and Pybytes for visualization. 



### Presenting the data

Describe the presentation part. How is the dashboard built? How long is the data preserved in the database?

Using Pybytes platform for visualizing the data is free of charge (with a 30 day limit). The data is as previously stated sent and saved on external interrupt.

You can easily make and configure many dashboards in Pybytes. For now only a received signal of value 1 will look like the image below, indicating that the postbox was opened by the mailman.

![Dashboard](https://imgur.com/jcZgJdo.png)



### Finalizing the design

Show the final results of your project. Give your final thoughts on how you think the project went. What could have been done in an other way, or even better? Pictures are nice!

I think this project gave me a great overview on the world of IoT. I'm really looking forward to produce more complex and useful devices as well. A lot can be improved in the code for the future. For example a dampening oscillations filter of the mailbox lid, alert via SMS, 3D-printing a housing and attaching it nicely outside. 
The final prototype looks like the image below.

![Setup](https://imgur.com/GIEW7s0.jpg)
