# IoT door signal button tutorial By Emil Lendrén (el223gr) **Short description** This project aims to deliver a solution for a door signal button which sends user data over the cloud when the button is pushed, and presents it on an online dashboard. As it's a button with the function of either being pushed or not, the data in this case is limited to a 0 or 1. It's a *very* simple solution including minimal amount of material and expertise. After acquiring the required material, I estimate a 30-45 minute time interval to get the project up and running with the code provided. **Objective** With experience as a warehouse worker, I know *roughly* how many times a day the door rings and a mailman either wants to make a delivery or collect goods to be sent. It may vary from day to day how many or what times someone is at the door, but it would be neat to know in mean numbers how many times a day, week or over different months that I have to answer the door. It would also be neat to get a notification in my phone or arm watch if I wouldn't happen to hear the ringing signal at any point when I'm not in the proximity. So, for both statistics and convenience would this project help. The statistics would help to give insights in how busy the in and out transport point of goods are at any given time and trend data will be shown with time stamps. **Material** The following material was used for the project: | Material | Purpose | Est. cost | | -------- | -------- | -------- | | Pycom lopy4 | Controls the coded function | 56 EUR | | Pycom expansion board | Allows USB connection to computer | 27 EUR | | Antenna | For better wireless communication connectivity | 10 EUR | | Micro USB cable | Connects the Pycom device with computer and power | 2 EUR | | Breadboard | Easier to use for connection between pins on Pycom device and sensor | 2 EUR | | Push button | The sensor to physically push to activate the coded function. It will output a high signal when pressed and low signal when not pushed. | 2 EUR | | 3 Jumper cables | Connects power, ground and data signal between Pycom device and sensor | 2 EUR | Everything from this bill of material was purchased at electrokit.com and was bought as a bundle at the time. The Pycom LoPy4 device was used for this project due to it's simplicity for beginners like myself and has four different wireless communication protocols (WiFi, LoRa, Sigfox and Bluetooth). **Computer setup** In this tutorial, the integrated development enviroment (IDE) used for coding the device was the free Microsoft sofware of Visual Studio Code (VSC) which can be downloaded at https://code.visualstudio.com. Any other IDE would be fine to use such as Atom.io, as long as the micropython programming language is supported. It is also recommended to atleast update the firmware on the LoPy4 device. This can be done at https://pycom.io/downloads/ by grabbing the latest version to your computer (pycom_firmware_update_1.16.3 in this project example). Install it and run. After downloading and installing VSC on your local computer (running Windows 10 in this example), it's time to install the pymakr plugin needed to program in the micropython language. Simple go to the Extentions tab to the left in the VSC window (or press Ctrl + Shift + X) and search for the pymakr plugin. Install it and you're almost ready to go. Before you can start, the VSC software needs to know what device to interact with. When the pycom expansion board is connected via USB to your computer, start the Device Manager and see what port is listed to your device (Figure 2). Now go to your VSC window and press the All commands button (in the bottom of the window) and then Pymakr -> Global Setting. In the "address" line change to correct port number as found in Device Manager. Now you need to create a project folder and a file for the plugin to run. To do this, on the left hand side in VSC right click and select Add Folder to Workspace to choose where to place the folder. Now right click on the new project folder and select New File to be named main.py. This will be the file where to put the code and later to be uploaded to your device by pressing the Upload button in the bottom of the VSC window (Figure 1). ![](https://i.imgur.com/peYzP8m.jpg) Figure 1. ![](https://i.imgur.com/1LrKPPs.jpg) Figure 2. **Putting everything together** Figure 3 shows a circuit diagram for the wiring and how everything is connected. ![](https://i.imgur.com/cHPCAsI.jpg) Figure 3. In this case, a breadboard would not be needed as only one sensor/button is connected, but it was rather used for convenience. As shown in this tutorial, this setup would only be used assumed to be used on a home prototype level. Specific connection flow, from -> to: | Pycom expansion board ->| Breadboard ->| Push button | | -------- | -------- | -------- | | Pin 3v3 | A15 -> D15 | Middle pin | | Pin GND | A14 -> D14 | - pin | | Pin 10 | A13 -> D13 | S pin | As the pin explains, the red wire connects the 3 volt outlet from the pycom expansion board, via the breadboard, to the middle pin on the push button. The black wire connects the ground on the expansion board to the minus (-) pin on the push button. And lastly, the yellow wire connects the data signal from pin 10 on the expansionboard to the S pin on the push button. As previously mentioned, the breadboard would not be needed for a project with only one sensor, like this, connected as the wires could instead be directly put to their designated pin. **Platform** The cloud based platform pybytes.pycom.io was used to send data and present it over the Internet. To get started, first simply sign up for an account and start a project on the left hand side. Further follow the instructions to connect your data sending device with your project by providing a project name, desciption, network type and information regarding (if chosen) your WiFi network creditentials. Instead of WiFi, you could choose a different network protocol like LoRa or Sigfox. By choosing to activate your device with Pymakr plugin, Pybytes then provides a code to put in the VSC REPL interface for a virtual handshake to get the connection going between your device and the platform. Pybytes then help to store the data sent from your sensor and also allow you in a simple way to present it in a chosen model of graph. **The code** These are the code lines needed to be put in a main.py file in VSC and later executed to get the door signal button working. ```python= from machine import Pin # Importing pin library import pycom # Importing pycom library import time # Importing time library p_in = Pin('P10', mode = Pin.IN) # Button sensor from Electrokit pycom.heartbeat(False) # Turning off LED heartbeat while(1): # The loop conditions val = p_in() # Get value, 0 or 1 if val == 1: # If the value is 1 (button not pushed), then the following happens pycom.rgbled(0xFF0000) # LED on pycome device constant red while button is not pushed else: # If the button is NOT not pushed, then the following: pycom.rgbled(0x0000FF) # LED on pycome device constant blue while button is pushed pybytes.send_signal(1, int(1)) # Sending value 1 over signal 1 to Pybytes while button is pushed print("SOMEONE AT THE DOOR!") # Print out "SOMEONE AT THE DOOR!" on screen while the button is pushed time.sleep(1) # Loop restart time = 1 second ``` **Transmitting the data / connectivity** By previously having configured a WiFi connectivity in the Pybytes platform to your Pycom device, this network communication protocol is chosen and used to transmit the data. As the last line in the code explains, once every second the loop restarts and checks if the button is pushed. If so, then also once every second the data will be sent to Pybytes as signal 1. In the project page it's also declared that it should listen for data over signal 1, otherwise no data would be found for Pybytes. What's practical about using the Pybytes solution, is that it includes a transport protocol such as MQTT. **Presenting the data** As shown in Figure 4, a dashboard is used to show when a signal is sent from the push button connected to the LoPy4 device and collected via Pybytes. ![](https://i.imgur.com/fj6LtKa.jpg) Figure 4. Figure 5 shows hovering over an instance example connected to date 30 June, time 16:43:39, that someone pushed the button. This data is sent to Pybytes once every second if the button is pushed. ![](https://i.imgur.com/bvezfXG.jpg) Figure 5. **Finalizing the design** As an absolute newbie and beginner in the IoT and MicroPython field, I feel that from the point where you only have a LoPy4 device and a sensor in front of you, until you have a small program running and some data presented online from a sensor, it went very fast and easy to get started. This is in the simplest way possible a way to use small programmable object to actually help you in an everyday situation. The project could with more time put into it be developed further and with a neatly designed box for the button to sit inside, it could be placed as a real door button. Here follows some final pictures of the project when the button is and isn't pushed, aswell as the output string in the IDE. ![](https://i.imgur.com/RTNSeHf.jpg) ![](https://i.imgur.com/J6r4GMz.jpg) ![](https://i.imgur.com/7lqE2fi.jpg)