# Tutorial on how to build a mailbox notifier **Dennis Demir - dd222gc** In this project we're going to build a system/sensor that is going to sense the light when the mailbox opens up and than sends a notification to your personal discord channel. The data is going to be sent with sigfox to pybytes and from pybytes as a webhook to discord. Estimated time: **2 hours** --- ## Objective I live on the fourth floor of my apartment building, and the mailbox is on the first floor. Since I only leave the house to go to the gym and I always forget to check my mailbox, I wanted to build something that alert me whenever I get mail in the box. There is probably ready-built devices for this, but I wanted to build my own device to send me notifications. Following this tutorial will hopefully make you interested in IoT more, I started out knowing nothing about hardware, coming from a software development background. I think after this, you are very well off with creating your own devices. ## Material ### Hardware Material * **LoPy4 + expansion board**. You can get this PyCom device from PyComs offical website for around 500-600 SEK or you can get it from elctrokit (swedish website). Either as a separate device or a bundle with different useful sensors. Included when buying the LoPy4, is a LoRa antenna, WAN network, this is used when not connecting with Wi-Fi. * **Wires and resistors**. Most electronic shops sells these, Kjell & Company or Electrokit is to be recommended. If you get a bundle kit, these are included. * **Photoresistor sensor**. This sensor measures light intensity. When light is not present the sensors resistance will decrease and give a higher output value, when light hits, it will the opposite. This sensor is used to sense the light when opening the mailbox. * **Battery**. Luxorparts Li-Po-batteri 3,7 V with 1200 mAh to be able to have the device wireless. ### Links to the hardware | Hardware | Link | | -------- | -------- | | LoPy Bundle | [electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-basic-bundle/) | | Wires and sensors | [electrokit](https://www.electrokit.com/produkt/sensor-kit-26-moduler/)| | Photoresistor (Included in the bundles) | [electrokit](https://www.electrokit.com/produkt/sensor-kit-26-moduler/)| | Luxorparts Li-Po-batteri 3,7 V with 1200 mAh | [Kjell&Company](https://www.kjell.com/se/produkter/el-verktyg/arduino/arduino-tillbehor/luxorparts-li-po-batteri-37-v-med-kontakt-1200-mah-p87924)| ## Computer Setup Assuming your are using a LoPy4 with expansion board 3.0/3.1. This tutorial is based on LoPy4. ### Steps: 1. Pycom advice to flash (update) the firmware on the hardware. I did not do this, I'm using an Macbook and did not have to do this. But incase you need to this than [follow this guide](https://docs.pycom.io/index.html) and [this guide](https://docs.pycom.io/gettingstarted/#step-2-setting-up-your-computer) on pycoms official website. 2. After following this guide. You should connect your LoPy4 to the expansion board. Simply just align them and press those "spikes going down" into the hole until they click together. After this step, insert the USB cable that got included in the bundle, now your hardware is ready, now it's time to setup the software needed on your computer to write, and run the code. 3. Depending on the operating system you use, you might need to install additional drivers to make sure the LoPy4 works with your computer. [Follow this guide from pycom depending on your operating system](https://docs.pycom.io/updatefirmware/device/). 4. The IDE I use and prefer, and would recommend every time, is Visual Studio Code (VSCode). VSCode is a open-source editor made by Microsoft. This is a very flexible editor with bunch of plugins for every use you can imagine. You will need to install the PyMakr plugin on VSCode, [here you can find the plugin for VSCode](https://marketplace.visualstudio.com/items?itemName=pycom.Pymakr). If you are using Atom, [follow this link](https://atom.io/packages/pymakr). PyMakr requires you to have Node.js installed to be able to work, install it following [this link](https://nodejs.org/en/). PyMakr is a nice tool/plugin that makes it easy to upload, run and download code on to your device, with just a simple press, you are running or uploading your code to your Pycom device. 5. When you have installed everything, it's time to try out everything to make sure it works properly. Follow [this](https://docs.pycom.io/gettingstarted/) guide to get started with coding for your PyCom device. 6. Now it's time to connect your device with [SigFox](https://www.sigfox.com/en) and send data to visualize on Pybytes. If you wan to use wifi to connect to pybytes, follow [this link](https://hackmd.io/@lnu-iot/r1bGPUOhu),but if you want to use SigFox like I do in this tutorial, follow [this guide](https://hackmd.io/@lnu-iot/SyUxJU7pu). Make sure you have the coverage to use SigFox, if you don't, then you can use [helium](https://explorer.helium.com/coverage#2.9/57.36/31.54) or [TTN](https://www.thethingsnetwork.org/map). For me Sigfox had the best coverage in my area. You will need to also connect your device to Pybytes, [here is a good tutorial](https://docs.pycom.io/pybytes/dashboard/) 7. To use SigFox you will need to connect your Antenna that is included in your bundle or if you did not get the bundle you will need to get an antenna. ![](https://i.imgur.com/I56rlVz.png) ## Putting everything together Building time! If you use any other sensor or another device (I recommend using the same hardware I'm using), than you will have to read up on documentation. It is a very easy process to connect the wires with the LoPy, I assume you have connected the antenna as shown on the image above. First we add the photoresistor, one leg inside of 23 and one inside of 36. Then we need one wire in 3V3 and the other end should face the leg of the photo resistor in hole 26. Then one wire should go into GND (Ground), one wire inside of PIN16, those 2 wires should face eachother and between those wires we need a resistor. To make this work without having the USB connected to the computer, you need to connect the battery i recommended, there is only one spot where you can add the battery, so the battery should go into that spot. ![](https://i.imgur.com/WZeFtiM.png) This is how the setup should look like. ## Platform The platform we will be using is [SigFox](https://www.sigfox.com/en). It's a service provider from IoT. A global network to connect your device with the digital world. Sigfox offers a software based communication solution, all the networking is managed in the cloud. This reduce the energy consumption, instead of having all the computing on the devices itself. I used Sigfox because it had the best coverage in my area, so make sure it have good coverage in your area, and also because it were easy to connect with Pybytes and sending data to visualize it on Pybytes and from pybytes setup a webhook with Discord, so it triggers a notification every hour if my mailbox opens up. To make sure your device connects with Sigfox, [follow this tutorial](https://hackmd.io/@lnu-iot/SyUxJU7pu). ## The code The code being used in this project were really simple. You could write your own code as you want to customize it for your desire. ```python= from machine import ADC from machine import Pin import time LightSensorPin = 'P16' # sensor connected to P16. Valid pins are P13 to P20. lightPin = Pin(LightSensorPin, mode=Pin.IN) # set up pin mode to input # # create an ADC object bits=10 means range 0-1024 the lower value the less light detected adc = ADC(bits=10) # create an analog pin on P16; attn=ADC.ATTN_11DB measures voltage from 0.1 to 3.3v apin = adc.channel(attn=ADC.ATTN_11DB, pin=LightSensorPin) while True: val = apin() # read an analog value if val > 40: # if the value is greater than 40, than send notification pybytes.send_signal(1, val) print("Value", val) time.sleep(60 * 60) # send data every hour ``` Don't forget to upload the code to your device whenever you finish writing the code. ![](https://i.imgur.com/VJJeCFE.png) ## Transmitting the data / connectivity We are using Sigfox to send data, and with Pybytes we send data through a webhook every hour once the value of the Photoresistor is greater than 40, the value is dependent on the light hitting the sensor. You will need to find the perfect treshold for yourself, I found this by testing the output I got when I had the device in my mailbox. We are connected with a battery so I'm sending data every hour, I don't believe it's necessary to send data more frequent than that. ## Presenting the data Here you can see a dashboard of the data being presented and stored in Pybytes. ![](https://i.imgur.com/M4m7UDO.png) ![](https://i.imgur.com/PKNbb7X.png) And here you can see the Discord bot sending me notifications every hour when the sensor value is > 40. (In this image I have the device place on my desk so it's frequent light hiting it). ![](https://i.imgur.com/MCKTEpI.png) ## Finalizing the design Here is images of the final design of the project. This works without the USB cable inserted into the LoPy. In my opinion once you started understand more about the hardware, I think the project went really well and it were pretty easy to assemble the device and all the wiring and sensors. You can take this project much further and keep on building on it, the imgatination are your only limits. ![](https://i.imgur.com/7TJdSeB.jpg) ![](https://i.imgur.com/Ay0TCLA.jpg) ![](https://i.imgur.com/xZUoIFB.jpg)