By Karin Ekenberg / ke222yr ## Description: For some parents out there it is a struggle to keep your kids safe, especially if they are very active and want to explore the world on their own. For me personally I have two extra locks on the front door, but still do not feel 100% safe that the kids will not escape. That is why I wanted to make a security system notifying me whenever the front door is opened. A notification on my phone whenever the front door is opened, to remind me to check if the person that opened the door will also lock it or if god forbid it was the toddler being able to open the door somehow. ## Amount of time: Estimated time for this project is about five hours depending on your knowledge around IoT and the devices used in the project. ## Why I chose this project: This project gives a lot of meaning to me. With this project I am able to mix different components such as learning to use different programs with something that would be really useful in my everyday life. Having active kids with no sense of danger is a real struggle and adding an extra safety net will help ease my own mind. ## What purpose does it serve: The purpose is to keep my kids safe, making sure the front door is always getting locked after it has been opened. ## What insights I think it will give: I think it will give a lot of insight in developing different kinds of smart tools for everyday life. How to code and build things. Even though I have a specific project, it will still be very educational and help in learning how IoT works as even different projects still have a lot of the same steps to get them done. ## List of materials: | Item |Description | Price | | -------- | ---------- |--------------| | Cables (male to male) | Connecting all the parts together | 49 SEK Electrokit.com | | Breadboard | Putting everything neatly together | 49 SEK Electrokit.com | | Reed switch (sensor) | Detecting if there is a magnet field | Part of a sensor kit at Electrokit.com | | Raspberry Pi Pico WH | Microcontroller, which will control and monitor devices and systems | Part of starter kit at Electrokit.com | | USB cable | Providing power to the Pico | Part of starter kit at Electrokit.com | | Magnet | Used to detect the magnetic field with the reed switch | Had mine at home already, can be bought in many stores | ## Installing chosen IDE: The IDE I chose to use is *VS Code*, since it is a very simple program for beginners. I am mixing *VS Code* with *Adafruit* since it is a good match and both platforms are beginner friendly. It works well with my operating system *Windows 10* as well and I can use *MicroPython* as a programming language. ### What to download: 1) I started by downloading Node js (https://nodejs.org/en), following the simple steps on the link. 2) Followed by downloading *VS Code* (https://code.visualstudio.com/Download), same here follow the steps in the link as it is a simple download. 3) When *VS Code* is downloaded it has to be opened and then go to *View* > *Extensions* > search for *Pymkr* and install it. 4) Download this uf2 file (https://micropython.org/download/rp2-pico-w/), the latest version is required. 5) Create an account on https://www.adafruit.com/. ### Getting started with *VS Code*: When *VS Code* is downloaded you will have to get the code provided further down, but first see these steps: 1) Create a new folder with a fitting name, i chose Project IoT: ![](https://hackmd.io/_uploads/Hk21TSid3.png) 2) Open *VS Code* > press file explorer: ![](https://hackmd.io/_uploads/SkqxxLjuh.png) 3) In the project IoT folder add three folders named boot.py, main.py and secrets.py. 4) Create a new folder called *lib* inside of the project *IoT folder*, inside the *lib* folder make two folders called *library.py* and *mqtt.py*. 5) Put in the correct code from further down in each folder. 6) Press on Pymkr: ![](https://hackmd.io/_uploads/rkh6TrjO3.png) 7) Hover over the Seriell USB-enhet and press the little flash and then open a new terminal a little bit to the left of the flash. ![](https://hackmd.io/_uploads/rk23CSs_3.png) 8) Then hover over the Empty project folder above and start Developer mode. 9) When saving code in *VS Code* press *ctrl + s* where the code is, this will also run the code in the terminal below. 10) Code is put in the folders (shown as empty in this picture): ![](https://hackmd.io/_uploads/r1AfHL-Yh.png) ### Getting started with *Adafruit*: After creating the account and putting together the *Pico* with sensors and wiring in the correct way follow these steps to get started with *Adafruit*: 1) Press the *Feeds* > *New Feed* button. 2) Name it and describe it (I chose the name magnet with no description). 3) Press the *Dashboard* > *New Dashboard*. 4) Name it and describe it (I chose magnetdashboard with no description). 5) Press the wheel and pick *create new block* > *status indicator*: ![](https://hackmd.io/_uploads/rkyNgjYu3.png) 6) Choose these options for the indicator: ![](https://hackmd.io/_uploads/BJJq01sd2.png) 7) Get the *Adafruit* IO key in the upper right corner (this will later be put into the *VS Code*): ![](https://hackmd.io/_uploads/BygBhHj_2.png) ### Getting started with webhooks: To get a notification when the door is opened we will use webhooks via *Adafruit* and *Discord*. Follow these steps to get started: 1) Download *Discord* if you do not already have it https://discord.com/download. 2) Open *Discord* and press the *+* sign in the down left corner to add a server > press *create my own*: ![](https://hackmd.io/_uploads/BJ5FEfn_3.png) 3) Pick a fitting name for the server and press create: ![](https://hackmd.io/_uploads/S113NM3_h.png) 4) Press the wheel on the text channel: ![](https://hackmd.io/_uploads/r1j4SM3O2.png) 5) Pick interaction: ![](https://hackmd.io/_uploads/r1eFSznOn.png) 6) Pick create webhook: ![](https://hackmd.io/_uploads/B19eLM2On.png) 7) Copy the link: ![](https://hackmd.io/_uploads/r1oDLMnuh.png) 8) Now log into *Adafruit* and go to *actions* > *new action*: ![](https://hackmd.io/_uploads/Byrywf3u3.png) 9) Pick the reactive action type: ![](https://hackmd.io/_uploads/rkEsDfhd3.png) 10) Pick the same values as shown in the picture below: ![](https://hackmd.io/_uploads/ry9hDMh_h.png) 11) Check that *Notify on action Reset* is checked: ![](https://hackmd.io/_uploads/Sy0O_fn_n.png) ### Updating the firmware: 1) Start with plugging in the *USB* cable to the pico. 2) While pressing down the bootsel, connect the other part of the usb into your computer:![](https://hackmd.io/_uploads/ryY78Sh_h.png) 3) A folder will pop up now, drag in the uf2 file from earlier. Wait while the pico loads. Unplug it and connect the pico again, this time do not press down the bootsel button. ## Putting everything together: The picture below demonstrates how to connect all the parts with the *Raspberry Pico W* (microcontroller), *Reed switch* and cables: ![](https://hackmd.io/_uploads/r1TuiMgFn.png) To follow this example above the following is needed: 1) *Breadboard*. 2) *Raspberry Pi Pico WH*, is the *microcontroller* that will control the *Reed switch*. If there are any changes detected from the *Reed switch* we will code it to take certain actions, in our case a notification that the door is opened. 3) Two black cables, two red cables and one blue cable. 4) *Reed switch* is a magnetic sensor that will detect if there is any magnetic field around it. ### Then connect the wires as in the pictures provided: 1) Connect the first black cable to GND and a - rail on the breadboard. 2) Connect the first red cable to 3V3 out and a + rail on the breadboard. 3) Put the *Reed switch* into the breadboard, it does not matter where. 4) Connect the second black cable to a - rail on the breadboard and the first pin of the *Reed switch*. 5) Connect the second red cable to a + rail on the breadboard and the second pin in front of the *Reed switch*. 6) Connect the blue cable to GP27 and the third pin on the *Reed switch*. When I put this together it looks like this: ![](https://hackmd.io/_uploads/Bka4K9KO3.jpg) This circuit setup is made for development only, it demonstrates how to connect the components with the microcontroller and is for trying out and experimenting with different codes and applications. ## Platforms: As mentioned before this tutorial is using *VS Code* and *Adafruit*. Both of these programs are beginner friendly and are easy to use and getting comfortable with. They are both free to use, with options for premium features on *Adafruit*. *VS Code* is an open-source code editor that is very user friendly and supports a lot of extensions and programming languages such as *MicroPython* which is used in the tutorial and is commonly used in IoT development. It also has a lot of available extensions like *Pymkr* which is also used in the tutorial. *Adafruit* is cloud-based and works well together with *VS Code* since it receives data, logs it and also offers ways of sending notifications when the sensor is triggered. The layout of *Adafruit* is very simple and easy to understand which is perfect for a beginner in IoT. ## The code: All code used I got from guides made of Linnéuniversitetet or teacher assistants from the Linnéuniversitetet. All of the code can be found in the following link: https://github.com/karinEkenberg/IoT-Project.git Copy the code into your own folders with the same name (in *VS Code*), it is important that the folders have the same name for the code to work properly. However, a few things have to be changed in the code for it to be able to work. In the code for *main.py* folder you have to put in your own *Adafruit* username and key and also change the username with the magnet field to your username: ![](https://hackmd.io/_uploads/Hy7Vn72un.png) You also need to change the code in the folder *secrets.py* to your own *wifi name* and *wifi* *password*: ![](https://hackmd.io/_uploads/rk8ohm3_2.png) The code below is detecting magnet with Pin GP27, the loop monitors state of Reed switch, Value 0 means that the magnet is detected, value 1 means there is no magnet detected. Loop demonstrates real-time monitoring capabilities of the IoT device. Data is published to Adafruit IO feed. MQTT client uses the publish method. Published values represent the status of the door. ![](https://hackmd.io/_uploads/S11w8Xlth.png) ## Connectivity: In this tutorial *Wifi* is used as an wireless protocol, *MQTT* as an transport protocol and *webhooks* to get notifications on *Discord*. The data is sent every 5 seconds, but notifications are only triggered when the *reed switch* is not detecting the magnet anymore and when it senses the magnet again. Some issues occured and sometimes the notification for the magnet coming back to the sensor did not trigger an alert. It was the same on *Adafruits* dashboards. To fix it I needed to pull the magnet away for a few seconds and then put it back. ## Presenting the data: The data in the dashboard on *Adafruit* looks like this when the magnet is close to the sensor: ![](https://hackmd.io/_uploads/r14PJB2u2.png) And like this when it is not detected: ![](https://hackmd.io/_uploads/Hkoukrndh.png) As seen in the pictures above the data is uploaded every five seconds, this can be changed in the code in *VS Code* if another amount of time is more desirable for you. The notifications from *Discord* chat, 1 means that the door has been opened, and 0 means it is closed again: ![](https://hackmd.io/_uploads/HkRcyBnu2.png) And *Discord* popups: ![](https://hackmd.io/_uploads/ry-klB3_h.png) ## Final results: Seen below is the final result, although it would need to be put together better for it to work well on the door, in the current state the cables could easily loosen up for example. The entire project would need to be a bit more compact and easy to install on the door for sure, perhaps by using a 3D printer. This photo only demonstrates where the *Pico* and sensor could be placed. ![](https://hackmd.io/_uploads/rJU0xrndh.jpg) Overall I am satisfied with the results of the project, the end result was what I wanted it to be. There are a few things to do now to improve the end result tho. It would be to make everything more compact and neat, fix a few bugs with the triggering of the sensor to make sure the notifications will be on point. Getting notifications as a text message on the phone would also be more desirable, as it would most likely not be missed as easily as a *Discord* notification.