# A tutorial to create a IoT project to see who opened the mailbox This tutorial created for course 1DT305 Tillämpad IoT at Linnaeus University. This document follows the final project template that can be found [here](https://hackmd.io/@lnu-iot/iot-tutorial#Template) ## Information of creator **Name:** Daniel Persson **Student credential:** dp222iw **Term:** Summer 2020 ## Table of Contents [TOC] ## Introduction In this guide I will guide you throught how to create a digital mailbox that will notify you when its open throught wifi. When the mailbox is open it will also scan for bluetooth devices and try to find the nearest device mac adress to identify the opener. This guide will go throught everything you will need to create a similar project. ### Time The project takes approx 10-15 hours. ## Objective This project is a proof of concept and should be treated as such. This project was created to see if you can track someone throught their bluetooth devices. A digital mailbox was chosen because it was an easiest way to test the proof of concept as there is always a mailman opening my mailbox. Also who doesnt want to track there mailman? ### Purpose The purpose of this project is to see when the post is in the postbox. The other purpose is to see if post comes in at the same time everyday or it depends on which mailman it is. ### Insight This project will give me an insight if there is enough people carrying bluetooth devices that can be scanned to track them. It will also give me insight over when the post comes each day and how it depends on who delivers it. # Material Table below will show all materials used in this project. All material except the powerbank was bought at [electrokit](https://www.electrokit.com/). But there is other alternativs where you can buy the materials like for example [amazon](https://www.amazon.de/) or the offical [pycom website](https://pycom.io/webshop/). The powerban can be bought almost at any electronic store of your choice. | Material | Purpose of material | Price approx | | -------- | ------------------- | ------------ | | 1x LoPy4 with headers | The main hardware that we gonna program in micropython to send our data throught wifi to the cloud. It also handles the bluetooth functionallity of this project | € 35 | | 1x Expansion board | The expansion board make it able to connect the lopy to a computer to program and also connect external sensors to read data | € 16 | | 1x Tilt switch | The sensor that checks if the mailbox is opened | € 1 | | 1x Micro USB cable | To give the device the power and to connect the lopy to a computer | € 5-10 | | 1x Breadboard | To make the connection of the electronic compontens solderless | € 3-6 | | 1x powerbank | To power the device without any poweroutlet | € 4-10| | 1x Resistor 330 ohm | To make the electronic connection have the right resistance. | € <1 | | 4x jumper wire | To connect the sensor to the expansionboard throught the breadbroad | € <1| | 1x double sided tape | To stick the project in the mailbox | € <3| **Total cost approx:** € 60-100 depending on where you buy the stuff. # Computer setup For this project you need a few programs. I will go throught how to install each of them listwise below. 1. I have choosen to use visual studio code as my Integrated Development Environment (IDE). It can be downloaded [here](https://code.visualstudio.com/). This is the plattform we will program in. 1. You need to download node.js. It can be downloaded [here](https://nodejs.org/en/download/). This is needed for a plugin installed later. 2. If the devices need to be upgraded follow this [youtube clip](https://www.youtube.com/watch?v=FkycTZvj-ss). The lopy might be outdated and might need a upgrade to its firmware 3. After that you need to setup the pymakr plugin. Follow this [linked guide](https://docs.pycom.io/pymakr/installation/vscode/) to do this. 4. Add you device to pybytes plattform by following this [linked guide](https://docs.pycom.io/pybytes/connect/). This will act as our dashboard were we see all information sent over wifi. 5. When everything of this is done you can connect your device and upload the code by pressing upload in the left corner in visual studio code. # Putting togheter the project Before we start programming we need to put togheter the project. This instruction will be visual throught a picture of the circuit diagram. ## Diagram of the electronic connections The switch in the diagram is the tiltswitch. ![Diagram of the connections](https://i.imgur.com/4tFIG0N.png) ## Picture of everything connected togheter This is a picture of everything connected togheter. > * Antenna is there from another project > * The green wire is ground > * Red wires are input > * Black wire is the one that reads the output > * Blue tube is resistor > * Black tube is tilt switch ![Picture of the electronic connection](https://i.imgur.com/q7T9QXC.jpg) # Platform The chosen plattform for this project is pybytes. This is the easiest way to save and upload data especially as we gonna do it over wifi, because the setup is so easy. This plattform also offers simple a dashboard to be ready to use. All data is gonna be stored in the cloud and is limited to 5 mb as thats what pybytes offer. This works for our proof of concept but in the futher the plattform need either to be changed, upgraded to fit bigger data or downloaded to a local database. But the free verison will good enough for this project. If you want to see more in find detail what pybytes offer se following [link](https://pycom.io/products/software/pybytes-3/). # The code This is the whole code for the project. The explaintion for it is in the code comments inside the code block. ```python= #Everything that needs to be imported from machine import Pin from network import Bluetooth import ubinascii import time import pycom #GLOBAL VARIABLES BELOW hold_time_sec = 1 #The pin the tiltsensor output is connected to PIN = 'P18' # It either detcted or not (0 or 1) motionDetected = 1 #Bluetooth time it scans BT_SCAN = 10 #START OF CODE #This function search bluetooth devices def search_BT_dev(BT_SCAN): #Rssi is how close the bluetooth device is old_rssi = 0 #Add placeholder if no bluetooth device is found BT_adress = "?" #Start scan bluetooth = Bluetooth() bluetooth.start_scan(BT_SCAN) while bluetooth.isscanning(): #Gets the information from bluetooth devices (adv) adv = bluetooth.get_adv() if adv: #Checks if the connection is closer then the last device scanned if adv.rssi < old_rssi: #If it is set the mac adress to the variable BT_adress BT_adress = adv.mac #Add the old rssi to check with the rest of the devices getting scanned old_rssi = adv.rssi #Returns the bluetooth mac adress nearest. return BT_adress def main(): #Find where tiltsensor is connected pir = Pin(PIN,mode=Pin.IN) #If it detects motion do code below if pir()==motionDetected: #Converts the hexidecimal mac adress and decodes the bytes to string BT = ubinascii.hexlify(search_BT_dev(BT_SCAN)).decode('ascii') #Sends the information to pybytes at signal 10 pybytes.send_signal(10, BT) #Sleeps a second so mailflap has time to go back to normal postition time.sleep(hold_time_sec) if __name__ == "__main__": #Loop all the code so it always check if mailbox is opened while True: main() ``` # Transmitting the data This section will explain why and how I sent the data the way i did. Everytime someone opens my mailbox were transmitting data. This data get sent over wifi over to the cloud throught a message protocol. This message protocol is MQTT as it is was pybytes uses in the send_signal command. MQTT more or less is like a postoffice were we can send data and recive data, it is used to minmize network bandwith.For more information about MQTT see following [video](https://www.youtube.com/watch?v=Kq0OLK08tqQ). Why both wifi and MQQT was chosen was simply as it was the simplest solution for my project. I am in range of the mailbox with wifi and it has and it can send data big data. MQTT was chosen as it was build in to the commando and also is improvment of sending the data faster. # Presenting the data The data is saved on pybytes and goes in to a simple dashboard that shows two things time the mailbox was opened and the nearest bluetooth mac adress. The data is stored up to 50 openings of the mailbox, this to prevent the data from being bigger than 5mb free limit. 50 stored macadresses and times will be enough to see if we can identify diffrent mailmans. To make this even more clear how the dashboard looks a picture has been taken of the dashboards and can be seen below. ![Picture of the dashboard](https://i.imgur.com/igKlre4.png) # Finalizing the design The final design was connected in the mailbox with a powerbank. It does not currently work as the powerbank was faulty so a new needs to be order. The breadboard was connected to the mailbox flap to detect opening. The lopy was connected to the sidewall. It will be intresting to see in the future if it works as intended. I added a pictures of the final setup below. > Mailbox with everything connected ![](https://i.imgur.com/4KlrOJI.jpg) > Breadboard on the mailbox flap ![](https://i.imgur.com/PCj0OQ3.jpg) > Closed mailbox ![](https://i.imgur.com/LaQLLuX.jpg) Feel free to use my code and project for you liking. I would like to see it develop even further. Hope the guide was easy to follow and your project becomes a sucess.