# Simple mailbox notifier through Discord **Nils Erlands (ne222hk)** The aim of the projekt is to create device that notifies the user when they have mail in their mailbox, when refering to mail we mean packages and not letters and newsletters. Since this is small projekt it probably wont rake more than 3 hours. ## Objektive Since I recive alot of packages I dont want the inconvenience of having to check the mailbox multipulte times during a day due to a expected deliverly. I use Discord as my main way of communication and because of this I want to be notified through this platfrom. ## Material [HC-SR04 - Ultrasonic Sensor](https://www.electrokit.com/produkt/avstandsmatare-ultraljud-hc-sr04-2-400cm/). Price €5.78. A sensor that uses ultrasonic sound to messure distance 400 cm infront if it. <img src="https://www.electrokit.com/uploads/productimage/41013/41013207.jpg" width="100"> <br /> [Labsladd 4-pin 100mm](https://www.electrokit.com/produkt/labsladd-4-pin-100mm/). Price €1.47. A calbe to connect the the Ultrasonic Sensor to the Fipy. <img src="https://www.electrokit.com/uploads/productimage/41010/41010142.jpg" width="100"> <br /> <br /><br /> [Labsladd 1-pin hane-hane 150mm 10-pack](https://www.electrokit.com/produkt/labsladd-1-pin-hane-hane-150mm-10-pack/). Price €2.85. Some jumper wires to connect the the Ultrasonic Sensor to the Fipy. <img src="https://www.electrokit.com/uploads/productimage/41015/41015693-600x453.jpg" width="100"> <br /> [Batterihållare 3xAAA med strömbrytare och JST-kontakt](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare-och-jst-kontakt/). Price €2.85. Just a batterycase. <img src="https://www.electrokit.com/uploads/productimage/41017/41017851-2-600x450.jpg" width="100"> <br /> [FiPy Multipack](https://pycom.io/product/fipy-multipack/). Price €90.97. A pack that includes a Fipy module, Expansion Board, LTE-M Antenna and LoRa/Sigfox Antenna. <img src="https://pycom.io/wp-content/uploads/2018/08/fipyMultiPack1.1.png" width="100"> <br /> Optional: [PyCase Blue](https://pycom.io/product/pycase-blue/). (There are other colors too) Price €8.00. A case to protect your Fipy with. <img src="https://pycom.io/wp-content/uploads/2018/08/pyCaseBlueNew3X2.png" width="100"> ## Computer setup First of all we need to make sure that the pycom devices are up to date, follow this link and follow its intstruktions: [Link for updating firmware](https://docs.pycom.io/index.html). To communicate with the pycom device through USB I used [Vscode](https://code.visualstudio.com/download) with the extension [pymkr](https://marketplace.visualstudio.com/items?itemName=pycom.Pymakr). If you are having problems with communicating with the device you might need to insall [node.js](https://nodejs.org/en/) as well. You can also check out this [Tutorial](https://pycom.io/wp-content/uploads/2020/04/Lesson-2-Pymakr-Set-Up-FiPy.pdf) by pycom. ## Putting everything together ![](https://i.imgur.com/X0xPuKR.png) To put everthing together you connect the "HC-SR04" sensor to the expansionboard as shown in the figure above. Since the "Labsladd 4-pin 100mm" is female to female we are going to have have to connect it with some jump wires. <img src="https://i.imgur.com/cMRLWv7.jpg" width="500"> <br /> ## Platform I've chosen the [Pybytes](https://pybytes.pycom.io/) platform provided by pycom, its free and it can be used to update you "Fipy". Pybytes works well for this projekt since Im only going to be sending messages to discord. ## The code ```python= import Dist import machine import time import pycom def send(): print("recived") post = "You got mail" pybytes.send_signal(1, post) print("sending: {}".format(post)) temp2 = 30 while True: temp1 = Dist.distance_measure() print(temp1) print(temp2) if temp1 < 27 and (round(temp1) - round(temp2) not in range(-1,1,1)): temp2 = temp1 send(); time.sleep(5) time.sleep(0.1) ``` There is not much happening here except the if definition that will only send a signal to pybytes if the distance is less than 27 (My mailbox size) and if has changed since the last time it checked. It is also important to point out that the ```time.sleep(0.1)``` on row 24 should be changed to something like 600 (10 minutes) instead of 0.1. Dist is what I called the file for the library code that can be found below. Use the "TAKING MEASUREMENTS" code from the following link [Necessary Libraries](https://core-electronics.com.au/tutorials/hc-sr04-ultrasonic-sensor-with-pycom-tutorial.html) to get the needed Libraries code to run the “HC-SR04” sensor. ## Transmitting the data / connectivity For the Fipy device to send data to Pybytes we first need to make it so it connects to the internet. In this case Im going to be useing wifi to cennet to the internet. To do this you must first use ***"add an device"*** that can be found on ***"Getting started"*** page on the pybytes website. After adding the device we are going to connect the device with the ***"pymakr plugin"*** found under ***"Devices" -> "Provisioning" -> "With Pymakr plugin"*** copy the ***"Activation over Wi-Fi"*** key and paste it in to the terminal inside Vscode. Now you should be connected. To communitacte to discord we are going to be useing a webhook that sends a json with commands to discord to follow. To do this we need to create a [webhook](https://discordjs.guide/popular-topics/webhooks.html) on a discord server. After we have created the weebhook we now need to cennect it to pybytes. To do this we go to ***"Intergrations" -> "Create new integration" -> "Webhook"*** on the pybytes website. Now we paste the URL that we recived from discrods weebhook and into the URL box. ![](https://i.imgur.com/WBPFYZX.png) Since discord uses it's own command we need to use command "content" for the server to print in the chat (Here is a link to diffrent discord weebhook [funktions](https://birdie0.github.io/discord-webhooks-guide/discord_webhook.html)). As show in the picture above we need to pair the command "content" with "MASSAGE_PAYLOAD". The "MASSAGE_PAYLOAD" is what we send from our Fipy device which can be found in the code on row 8 as ```"post = "You got mail"```. After this you chose the device that you created erlier and press create and you are done. It should look like this: ``` POST URL(your discord webhook URL) Host: discord.com Content-Type: application/json Accept: application/json Message Payload: {"content":"MESSAGE_PAYLOAD"} ``` ## Presenting the data So when testing the setup via USB it should look like this in the terminal: ``` Connecting to COM3... WMAC: E915D1CD14F7 Firmware: 1.20.2.r4 Pybytes: 1.6.1 Initialized watchdog for WiFi and LTE connection with timeout 1260000 WiFi connection established Connected to MQTT mqtt.pybytes.pycom.io Pybytes connected successfully (using the built-in pybytes library) Pybytes configuration read from /flash/pybytes_config.json ``` When putting you hand infront of the sensor you should get this: ``` 69.19 30 5.984 30 recived sending: You got mail ``` After sending the massage you should get the a notification on discord saying "You got mail!". ![](https://i.imgur.com/bzZ2hP2.png) ## Finalizing the design <img src="https://i.imgur.com/tMiL0M3.jpg" width="300"> <img src="https://i.imgur.com/aBBzPw7.jpg" width="300"> To house the Fipy I modified the package that the expansionboard came in, I also made 3 holes in the antistatic bag the HC-SR04 senor came in so it could house it too. It would be rather wastefull to not use resorces that can be modified for use. The look dosent matter anyway since it not going to be seen by anyone. There are some things I want to thurther develop with this device. To make it more efficient I would like to make it so it saves the most common times during the day it recives a package so it can save power by not checking the other times.