# Tutorial for IoT-project with LoPy4 and a temperature sensor Student Credentials: - Robin Klint - rk222ps - robinklint@live.se # **Introduction** Tutorial on how to setup a temperature sensor with a Pycom device and send it to Ubidots! This tutorial shows the way I have chosen to send data from a small, low-power temperature sensor with a LoPy4 device programmed in Atom with Pymakr plugin. This tutorial can be used as a template and expanded or personalized to better fit your needs. Essentially what happens in the end is that the temperature measurements are sent to an online dashboard and from there the data can be visualized in a variety of different ways. If you follow this tutorial, and basically just copy & paste all the code to your own computer, and setup the sensor and pycom devices as shown in the following pictures, I don't think it would take more than around 3 hours to replicate it. For me the most time spent has been for trouble shooting, researching and learning about how sensors and different communication protocols work. For me from start to finish, including writing this tutorial, i expect to have spent around 50 hours. # **Objective** I chose to put togheter a device that can measure temperature and send its data via WiFi to an online dashboard. I am a beginner to the subject of IoT, sensors, Pycom devices etc. so it therefore seemed to be a good starting point to setup this, fairly basic, sensor. And the online material to use for guidance was pretty substantial. I believe this project connects physical work (such as connecting wires, sensors etc.) with more software-based work in a good way. Thus, giving a good understanding of how to make physical devices interact with software applications and services. # **Material** All the material used in this project was bought from "electrokit.com" and the specific kit can be found via the following link: https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/ Cost of this kit was around 1 000 sek. However, all items in this kit were not put to use. So, specified below are the devices actually used for this project: # **List of material** - LoPy4 with headers ![](https://i.imgur.com/usuDzZg.png) **Fig. 1** - Expansion board ![](https://i.imgur.com/c0S5lqE.png) **Fig. 2** - Micro USB cable - Breadboard ![](https://i.imgur.com/c6lZZns.png) **Fig. 3** - Jumper wires - Temperature sensor MCP9700 ![](https://i.imgur.com/Dy3RtPl.png) **Fig. 4** The LoPy4 (**Fig. 1**) is a Pycom device programmed by MicroPython which offers alot of different features such as WiFi, Bluetooth, LoRa, Sigfox and more. It also has both analog and digital in/outputs which gives the possibility to connect several sensors at once if you want. The LoPy4 is connected via its pins directly on the expansion board from Pycom (**Fig. 2**), which can be powered via micro-usb cable or by using an external battery, thus also powering the LoPy4. In this project a micro-usb cabel was used for power and direct connection to my computer. The Breadboard, as seen in Fig. 3, provides an easy way to connect sensors and there corresponding cables. It is not necessarily needed but, as said, gives a good overview of how wiring is used and how everything is connected. The jumper wires are used to transfer power between the pycom device and breadboard, thus giving the possibility to connect a sensor to the breadboard instead of directly to the pycom device. The temperature sensor MCP9700 (**Fig. 4**) is a low power sensor that can measure temperature from 0°C to +70°C with an accuracy of ±4°C. # **Computer setup** For this project a laptop with Windows 10 was used. Atom was used as the program/text editor for writing code and interacting with the devices, also to send temperature data to Ubidots which is the online dashboard application I selected for creating graphical views of the measurements. This chapter will walk you through some the steps that are needed to do to start interacting with your devices. 1. **Update firmware** To start with, its best practice to update your Pycom device to the latest update. To do this, download the program Pycom Firmware Update from this link: (https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true) Plugin your pycom device via usb-cable and start the program. ![](https://i.imgur.com/sN2dNG4.png) (**Fig. 5**) Make sure to mark the same options as in the picture above. The port option might differ on your computer though. In the next page you will be asked for activation code from Pybytes. Log into pybytes.pycom.io, or register if you don't have an account, add your LoPy4 device and also add your WiFi credentials. Head to the "provisioning" tab and copy your activation code from there. Now paste it into the firmware update program and press continue. ![](https://i.imgur.com/K2x6cdT.png) (**Fig. 6**) Make sure to mark the same options as in the picture above, since this will also delete any possible existing files on your device. Let the installer run and save your device info, which will be shown when done, in some text file. On the pybytes website the device should now be shown as connected and that it was "last connected x seconds ago". This means the device has successfully connected via WiFi. 2. **Install Atom** Enter atom.io and download the program for your computer with the right OS-version. Once inside atom, navigate to "packages" and install Pymakr. Pymakr plugin lets you program your pycom devices in atom with MicroPython. Now head to this link https://help.ubidots.com/en/articles/961994-connect-any-pycom-board-to-ubidots-using-wi-fi-over-http and follow the steps given to setup your project code files (main.py, boot.py, urequests.py) ubidots account and be able to send data to ubidots.com. BUT don't run your scripts yet, since the main.py file needs some changes and the device itself is not yet physically configured with sensor and cables etc. 3. **Main.py changes** Remove any code from the main.py file and copy paste the main.py code from the following link into it: https://gist.github.com/a18robkl/92759f5c78305f2c9b5b4df6f43b72f8 Now you have the exact same main.py files as I have and of course you have to change things like ubidots-token, WiFi ssid, passwords etc. Then save all the files and upload them to the device from the pymakr terminal. 4. **Physical setup of sensor and cables** ![](https://i.imgur.com/sBDk9WC.png) (**Fig. 6**) Unplug your pycom device so it has no power. In the picture above, you see that three jumper wires are in use, blue, red and black. Put the blue cable in the GND outlet, red cable in 3V3 outlet and the black cable in P16 outlet. ![](https://i.imgur.com/UVGWkqN.png) (**Fig. 7**) Refer to the picture above and connect the cables to the breadbord in the following way. Blue cable to a15, black cable to a14 and red cable to a13. By doing this we run the ground (GND), 3 volt (3V3) to the breadbord and use P16 as the pin on the pycom device to take in the measurements. ![](https://i.imgur.com/rvV2qBG.png) (**Fig. 8**) Insert the MCP9700 sensor as in the picture above, with the flat side facing the jumper wires. In detail the left pin sits in e13, middle pin e14 and right pin e15. This corresponds with how the official datasheet (https://www.mouser.com/datasheet/2/268/20001942F-461622.pdf) says you should connect it. This way of doing it should not be considered to be used in a production state, since I don't have expert knowledge on electrical knowledge, and have only followed guides i found online, it should be seen as a development setup. ![](https://i.imgur.com/yofz4dJ.png) Hopefully this picture above helps to better see how everything should be connected... 5. **Run the scripts!** Open atom, connect your device with your usb-cable and run the script. The output should look like in the following picture: ![](https://i.imgur.com/udNKKt7.png) (**Fig. 9**) Now when you log into ubidots you should see the data "Temp" on your devices dashboard. # **Platform** Firstly I started of by using the pybytes platform, mainly to see that the device actually had connection via WiFi and that it could send data. But since the dashboard options there were a bit limited, i moved on to Ubidots instead since it's free (30 day trial) and has alot of neat ways to setup dashboards. It also allows for showing averages of the data that is sent which was convenient since the sensor used has an accuracy of ±4°C. Ubidots is an online platform which saves your data in their own servers, meaning you don't need your own server at home. As mentioned before, it's also free (30 day trial) and still offers alot of great functionalities, granted they offer more stuff if you pay. But these are the reasons for why I chose ubidots as the platform for displaying data. I tried VSCode at first but just found Atom to be easier to navigate in and that it also had all the functions i needed, while also being free. # The Code The main.py file is where it all happens... As seen in the comments in the top of main.py file the code has in general been taken from Ubidots tutorial for how to setup the pycom device and send data to Ubidots. Some things have been imported to the main.py from an added library called urequests.py. This library was taken from Ubidots as well as the code for boot.py. The lines that i have changed/personalized or added are explained with comments in the code. Link to Github gist containing all code files: https://gist.github.com/a18robkl/92759f5c78305f2c9b5b4df6f43b72f8 # **Transmitting the data / connectivity** The data is transmitted via WiFi, in more detail the WiFi is a mobile hotspot created by my own personal mobile phone. I chose to do it this way since I seemed to be out of range to connect via LoRa, and since i sometimes took all the devices and my laptop outside to try some measurements. The main.py file contains neccessary information to be able to connect to ubidots, such as a personal ubidots-token, WiFi credentials etc. When run, it first makes sure the device connects to the wifi and then builds a "json" file containing a variable-value pair called "data". The variable in this case is "Temp" and the value is the celsius measured from the sensor. Next, the website for which to send the data to is defined with its url. These steps are esentially preparations for sending the data, which is done in the last part of the code. This last part contains a while-loop which reads the data from P16 on the pycom device and does calculations based on the voltage, computes it to celsius, prints it in the terminal, and sends it to ubidots with the "post_var" function which contains the path to my personal ubidots page and registered device. ![](https://i.imgur.com/zajDMVi.png) (**Fig. 10**) When the data is sent to ubidots it appears as seen in the picture above. A graph can be seen in the middle. This graph can either show raw values of data or, as in the picture, average value of the data received over the last 30 minutes. It can also show average values for a specific number of seconds, days or weeks. This dashboard is automatically created on the "Devices" tab and within it, you can do numerous changes to the graph. The following video explains how to create more personalized dashboards with different variables and graphical options: https://www.youtube.com/watch?v=IK_ykCk_Os8&t=56s # **Answers to general questions:** **How often is the data sent?** - The data is sent every 60 seconds. **Which wireless protocols did you use (WiFi, LoRa, etc …)?** - I used WiFi. **Which transport protocols were used (MQTT, webhook, etc …)?** - MQTT was used. # Presentation of dashboard The dashboard is built in Ubidots based on the information given in the code in Main.py. I have written the code so that it creates a variable in Ubidots named "temp" which can be seen in the following picture: ![](https://i.imgur.com/cqEfyPu.png) (**Fig. 11**) Here you can see the last received data (23.60) and when pressing the variable temp, the following page is shown: ![](https://i.imgur.com/MM5AIgh.png) (**Fig. 12**) In this page you can see a graph that shows the average values within the last hour. I have not been able to find information on exactly how long the data is preserved in Ubidots database for this free trial version. However, I started sending data to this website in June and I can still see that data. When running the code it sends data every minute and that is how often Ubidots saves it as well. # Summary of the project The final results are essentially what can be seen in the figures in this tutorial, but in the following picture you can see how the physical devices look when all put together: ![](https://i.imgur.com/AnfhfoC.png) Since I am a beginner to IoT in general, this project was a bit challenging for me, for the most part to understand how all the physical parts were to be connected with each other. But since i have some previous experience with Python-programming it balanced the whole projects level of difficulty. In the end i got the physical parts to work as intended and the data to be measured and sent to a dashboard that I found interesting to work with. Of course things could have been done in other ways, like using a battery to power the pycom device so I would not need to have my laptop right next to it, or sending the data via LoRa which is less "power hungry" than WiFi. Sending the data via LoRa is something I would have wanted to do but since I could not get it to work from the place i did the project, and I don't have a car to drive myself and all the devices to an area within range of a LoRa gateway, WiFi felt like an pretty good alternative. Since the sensor I used is not 100% accurate, I thought about writing the code so that it would take 10 measurements, calculate the average and send that final result. But since Ubidots had an implemented function of displaying an average of the raw measurements, I chose not to do that.