---
# System prepended metadata

title: 'How to: Read temperature using analog sensor and transmit data over Wifi to online dashboard'

---

# How to: Read temperature using analog sensor and transmit data over Wifi to online dashboard
This tutorial was created by: Oliver Östensen (oo222ex) and Therése Berg (tb222va).

The project consists of wiring an analog temperature sensor to a Pycom Makr and Lopy4 using a breadboard and programming the device to present and transmit the data gathered about the ambient temperature from the sensor. The data is transmitted from the Lopy4 through Wifi to the pybytes platform where it is presented in graphs and tables to make it easy to read and understand.

By following this guide, you should have the sensor with code and transmission of data set up within 4 hours.

# Objective
An apartment can get very warm in the summer when the sun is constantly heating it up and the ventilation is limited. We thought it would be fun to measure the temperature inside of the apartment to see what the average temperature is on a typical summer day. It would be a good project to pursue to learn the basics of programming, IoT and also get an interesting result. 
Therefore, the project was chosen for two reasons.
    1. To measure temperature inside of an apartment
    2. It is a simpler project that is suitable for people learning the basics of programming and IoT
    
The purpose of the project is to simply measure ambient temperature and transmit the data to an easily readable dashboard. The data collected will be used for analyzing the ambient temperature and illustrate the readings in a graph.

By following this guide, you will learn-by-doing how to set up hardware and code using MicroPython and reach a result with maybe your first basic IoT project. The guide is therefore a good choice for a beginner who is just starting to learn.

# Material
Below follows a table of all the necessary materials needed to complete this tutorial.
The material was purchased as a package from electrokit.com. The package was called "LNU-1DT305 Tillämpad IoT - Lopy4 and sensors bundle". The cost of the package was 796 SEK excluding shipping and VAT. However, all the material in the package was not used.

Below the table is a list of pictures showing the material that was used in the project including cost.

### Table of materials

| Item | What it does | Cost (SEK) |
| -------- | -------- | -------- |
| PyCom ExpansionBoard 3.1    | Enables the use of different microcontroller devices and the ability to create an IoT sensor environment & projects.   | 160   |
| PyCom Lopy4  | A high-capacity, MicroPython enabled development board. It is suitable for WiFi, LoRa, LTE, BLE, and Sigfox networks.   |   350   |
| MCP9700 TO-92 Temperature Sensor | Analog sensor that gathers data on ambient temperature. The data is then transmitted through wiring to the Lopy4 in the unit mV.   | 6,40   |
| Breadboard    | A simple plastic board that is used to test out development projects and electrical circuits.  | 47,20    |
| Micro USB-cable  | A simple USB-cable that is used to initially connect the device to pybytes and then, in this case, used as a power source.    | 31,20 |
| Electrical Wires   | Simply electrical wires that are adapted to fit well into the Expansionboard as well as the breadboard, for easy connection of electical components.   | 28,80   |

Total cost of material for the project was therefore: 624 SEK.


### Pictures:
![](https://i.imgur.com/UE9o80j.jpg) 
*PyCom ExpansionBoard 3.1.*

![](https://i.imgur.com/jTnVZSg.jpg)
*PyCom Lopy4.*

![](https://i.imgur.com/75BLD72.jpg)
*MCP9700 TO-92 Temperature Sensor.*

![](https://i.imgur.com/Dw6A74r.jpg)
*Breadboard.*

![](https://i.imgur.com/68nqepj.jpg)
*Micro USB-cable.*

![](https://i.imgur.com/UR2kxfB.jpg)
*Electrical Wires.*

# Computer Setup
The computer that was used was a MacBook Pro from early 2015. The OS used was macOS High Sierra v.10.13.3
Flashing the firmware of the Lopy4 was not necessary as it came with a factory standard firmware updated to v.1.20.1.r2. This was enough to be able to complete the project.

Below follows the steps of setup that were taken when creating this project.

### Steps of setup:
1. Downloaded the IDE, Atom from their official website Atom.io. The reason this IDE was chosen is that it had been recommended as easy to use.
2. Installed the Pymakr plugin into Atom by following the steps as they are described in https://docs.pycom.io/pymakr/installation/atom/
3. Created an account for pybytes at pybytes.pycom.io
4. Created a project on the pybytes platform and added the expansionboard+Lopy4 as the device via USB
5. Connected Lopy4 to be using the Wifi network
6. To activate Lopy4, the Pymakr plugin was used through the Pybytes platform over Wifi. This was done by having the device connected to Atom, then finding an activation string under the "provisioning" tab in the current Pybytes project. The activation string was then copy-pasted directly into Atom which activated the device.
7. In order to test that everything was connected correctly and was working, the following code was used in Atom:
        >>>import pycom
        >>>pycom.send_signal(1,10)

If the signal shows up in the "signals" tab in your pybytes project, everything is as it should and you are ready to go on to setting up the hardware and begin the project.
The programming language used in this guide is MicroPython.

# Hardware Setup
The following setup is only a development setup and could not be used in production as it is not compact enough.
However, since this is a beginner's guide to a simple first project, there is no need for a production-ready hardware setup.

What is important when first setting up your hardware is knowing which pins do what. This applies to the Lopy4, ExpansionBoard and the sensor that is used.
Finding which pins to use on the Lopy4 was done by following the pin-out sheet available in the Pycom documentation.
Finding which pins do what on the sensor was done by using the data sheet associated with the sensor. The sheet was found on Electrokit.com 


### Pictures of hardware setup: 
![](https://i.imgur.com/vyoYLDb.jpg)

![](https://i.imgur.com/UTaNPCL.jpg)

![](https://i.imgur.com/FedjDpb.png)
* *Yellow wire is connected between output on sensor and pin 20 on Lopy4*.
* *Red wire is connected between 3v3 outlet on Lopy4 and power leg on sensor*.
* *Green wire is connected between GND on Lopy4 and GND leg on sensor*.

# Platform
The platform that was used for this project is the Pybytes platform. It is an easy to use platform that has everything you'll need to complete this basic project. It is easy to connect to with little needed effort to be able to transmit data and present it nicely.

Pybytes is a free, cloud-based platform that works only for Pycom devices. It offers ways of managing and monitoring multiple devices and networks. It also has an app so that you can do these things from your phone as well, if you'd like.

# The code
Below is the code used to first measure the output from the sensor, translate it from mV to Celsius then send the data as a signal to the Pybytes platform. The code specifies that the pin used on the expansionboard to measure the output from the sensor is pin number 20. The code also specifies that the defined signal over which the code is sent to Pybytes is signal 4.
```python=
import machine                  #Imports the code from the "machine" module in python to the device.
import time                     #Imports the code from the "time" module in python to the device.
#V_out=T_C*T_A+V0C              #Formula used to translate the output voltage to degrees celcius.
T_C=10                          #Temperature coefficient is equal to 10.
T_0C=500#mV                     #Output voltage of the sensor.
adc=machine.ADC()               #Here you create a Analog to Digital Conversion (ADC) object.
apin=adc.channel(pin='P20')     #Here you specify which pin on your Lopy4 you are going to read the data output from the sensor. In this case, pin 20.
val=apin()                      #This block tells the device to read the value from the specified pin.
while True:                     #Starts the non-stop loop of gathering, converting and sending the data.
    val=apin.voltage()          #Tells the device to read the voltage on the specified pin.
    temp=(val-T_0C)/T_C         #Converts the voltage into degrees Celcius.
    print(temp)
    pybytes.send_signal(4,temp) #Transmits the data from the device to the Pybytes platform through WiFi.
    time.sleep(10)              #Tells the device to wait for 10 seconds before running the loop again.
````

# Transmitting the Data
The data was gathered and sent every ten seconds through WiFi to the Pybytes platform. This means that the wireless protocol that was used was WiFi. This was chosen because it is a simple to handle protocol and it is the most widely used protocol for private persons. That means that if this project will be further developed in the future, it is already prepared to fit for everyday use.

The transport protocol that was used was MQTT which is integrated into the Pybytes platform.

# Presenting the Data
The dashboard that was used was the integrated dashboard on the Pybytes platform. It provides a simple and easy to use customizable dashboard for presenting data. For this project, the data presented on the dashboard was illustrated in two graphs, one showing amount of data received and the other one showing the temperature readings from the sensor.

The data is stored on the platform for up to 100Mb which means that the data can be stored for a very long time giving the user a good historic overview of what the device has been transmitting.

For this project, the temperature data was presented in a graph using the last 20 readings as can be seen below.
### Pictures showing the dashboard
![](https://i.imgur.com/aJlqXRI.png)
*Temperature- and bits received graphs on the platform.*

![](https://i.imgur.com/JotW2aU.png)
*Complete dashboard.* 

# Results of Project

The project was successful but some things could be done differently in order to increase the complexity of the project and at the same time create a more comprehensive analysis. To get a smoother curve of temperatures in the graph, the code could be updated and constructed in a way where it measures the mean of several separate points for example. Furthermore, the project was limited to the area of one apartment. The project could have been taken one step further through a more specific application. For instance, placing the sensor in a pot next to a plant to measure the temperature the plant is exposed in order to draw conclusions around the plant's well-being. 

The objective of the project was met, meaning that ambient temperature was recorded, transmitted over WiFi and illustrated for simple analysis on a conveniant dashboard. The average temperature in the apartment was approximately 25 celcius on a typical summer day.