# Measuring Temperature and Light I have taken this course to learn about IoT, how to make them, use them and what to do with the data. This is written by: Name: Anna Arvemo Student credentials: aa225dx ### The Project As a part of this I have created this project tutorial where I explain how you can do what I have done by using a LoPy4 device with connected sensors to measure light and temperature and send the data over Wifi to the web page Pybytes where the data is visualized. ### Time Needed How much time this will take depends on your previous experience with this kind of thing but I would estimate 3-5 hours in total. ## Objective I choose this project because it was not too complicated and I had no previous experience of either building an IoT unit, programming in Python or sending data to the cloud. I therefore did not want it to be too complicated or include too many parts since I had to learn each steps along the way. If you have previous experience this is a trivial project but if you, like me, are new to this, then this can be a place to start. It will include how to connect the parts, the code needed, how to send the data to a web page and how to visualize it. At the end, you have hopefully gained some understanding of the techniques behind the use of IoT. ## Material To build this a few thing are needed as can be seen in the table below. I bought everything at [Electrokit](https://www.electrokit.com/). You will also need a computer and a Wifi connection. | Device | Description | | -------- | -------- | | LoPy4 | LoPy4 is programmable quadruple bearer board, it uses Micro Python | | Expansion board 3.0 | Used to connecting sensors, use GPIO (General Purpose Input/Output)| | Photo resistor | Used to measure light| | Temperature sensor, MCP9700 | Used to measure temperature | | Micro USB cable | Connect the LoPy4 to the computer | | Breadboard | Connect the sensors to the LoPy4 | | Jumper wires | Used to connect the components | I bought all of the material in a prepared bundle and then it costs about 950 SEK. If the components are bought separately then it might cost less since the bundle included other parts as well. The parts that I used can be seen below. ![](https://i.imgur.com/kOb23qY.jpg) ## Hardware setup Before you can connect your LoPy to the computer, you put the LoPy on the expansion board. In the image of the expansion board below you can see the micro USB connection to the middle left. ![](https://i.imgur.com/G1YQ0ZL.png) After you have connected the LoPy on top, it will look like this: ![](https://i.imgur.com/MjeT3HA.png) You can now connect the device to your computer with the USD cable. The next step is to update the firmware on the device. * Go to the [Pybyte web page](https://sso.pycom.io/register?client_id=pycom&redirect_uri=https%3A%2F%2Fpyauth.pybytes.pycom.io%2Fauth_code%2Fcallback&scope=profile&response_type=code&state=pybytes-browser) and create an account This will also be used to receive the data sent from the sensors. * Click on **ADD DEVICE** and follow the instructions * To be able to send the data by Wifi do the following: Click on **Settings** and **ADD WIFI** and add the credentials for your local Wifi. * Update the LoPy4 firmware by following [these instructions](https://docs.pycom.io/pybytes/connect/quick/). ## Software setup The LoPy4 device needs to be programmed and you therefore need an IDE where you can write the code and then upload it to the device. I choose to use Visual Studio Code from Microsoft. There were no special reason why I choose this one but I had used it earlier and felt that it would be easier than to learn a new IDE. The program can be downloaded from [the Visual Studio Code web page](https://code.visualstudio.com/), you just choose the correct download for your operating system. You will also need Node.js that can be downloaded and installed from https://nodejs.org/en/. Since the programming will be done in Micro Python you also need to install Python which can be downloaded from [the Python web page](https://www.python.org/). Once you have installed Visual Studio Code and Node.js, you need to add an extension to be able to connect to your LoPy4 device. These can be found in VSC by clicking on the button shown below: ![](https://i.imgur.com/wTRb7Nj.png) You search for the extension Pymakr and click install. You can now open the IDE and connect the LoPy4. You can also find a [step by step guide here](https://docs.pycom.io/gettingstarted/). As well as some code to make sure that everything works as it should. The upload button can be found at the bottom of VSC: ![](https://i.imgur.com/QgYNnE3.png) ## The Sensor setup How the sensors are connected to the LoPy4 can be seen in the following image. ![](https://i.imgur.com/pztcTUa.png) The photo resistor that measures light (to the left in the image) needs a 10kOhm resistor as seen above. The blue lines are for the ground connection, the red is for the 3.3V connection, the yellow goes to pin P16 and the green goes to pin P14. This is just a basic setup for this learning project and has limited usefulness. ## Platform Since I lacked previous experience and did not at this stage want a local installation I choose the platform [Pybytes](https://pybytes.pycom.io/) for sending data to and visualizing it. This seemed like the easiest choice to start with. The platform is also free which is good when you do not know how much you will use it. The drawback is that the settings are quite limited. However, it is easy to use. You add the different signals that you are sending your data to and then you can add different widgets to visualize the data. Since I have two sensors, I add two signals. ![](https://i.imgur.com/fR2S6AO.png) ## The Python Code The code can be found on [Github](https://github.com/student-forlife/Greenhouse_sensor). Since I have just used two simple sensors, the code is not very long. ```python= import machine # used for the hardware from machine import ADC # convert from analog to digital from machine import Pin # used to configure pins import time # needed for time.sleep() ``` The pins are just used to read the data from the sensors. ```python= lightPin = Pin('P16', mode=Pin.IN) tempPin = Pin('P14') adc = machine.ADC() lpin = adc.channel(attn=ADC.ATTN_11DB, pin=lightPin) tpin = adc.channel(pin=tempPin) ``` The only conversion of data that is needed is the temperature data. The voltage we get from the sensor is converted to Celsius through the following equation: ```python= millivolts = tpin.voltage() celsius = (millivolts - 500.0) / 10.0 ``` I use the signals that I created in Pybytes to send the data. ```python= pybytes.send_signal(3, light) pybytes.send_signal(1, celsius) ``` ## Explanation for the Code Since I did not have any coverage for LoRa or Sigfox I sent my data over my Wifi. The data was sent to the Pybyte server so to the internet. While I tested the code and wanted to see what it looked like on the Pybyte web page, I sent the data quite often, once every 5 minutes. When I will move the sensors to my greenhouse that will be too often. Then I want to see the changes during the day and it will be enough to send data once every 30 minutes. I did not specify any transport protocol since I used a very basic solution. ## Data Presentation and Visualization Pybyte saves your data for one month so if you want to use it longer than that you need another solution. Nevertheless, if you only want to see what is happening now it works just fine. Each time you send a signal the data is stored in the database. The dashboard is rather basic so you need to add extra widgets to visualize the data. The original dashboard: ![](https://i.imgur.com/Vm8wFH2.png) In order to see the data represented in another way you need to add widgets. This can be done by clicking on the signal and clicking on CREATE NEW DISPLAY. The Pybyte documentation have a [detailed instruction on how to do this](https://docs.pycom.io/pybytes/dashboard/#step-3-add-a-widget-for-the-signal). The temperature sensor: ![](https://i.imgur.com/SORODkV.png) The light sensor: ![](https://i.imgur.com/SNBF28t.png) ## The Final Design The sensors are connected to the LoPy4 through the breadboard and it looks like this: ![](https://i.imgur.com/IEeurt9.jpg) My project was very basic since I had never done anything like this before. As such, it did what it was supposed to do and I learned the basics of creating an IoT device and sending the data to the cloud. Some things could have been done better and I have some other sensors that I had planned to connect but did not have the time and I also had some problems with the sensors not connecting as they should. I would also have liked to try out some other way of both sending and visualizing the data but again I have not had the time. Therefore, I will save it for a later time after the course is finished.