# **Tutorial on how to build a temperature station**
by Henrik Bergsten (hb222xt)
For this project, a temperatur station has been built, using a Pycom LoPy4 together with a MCP9700 temperature sensor from electrokit.
Estimated time for reproduce this kit is 3-4 hours.
### **Objective**
The purpose of this project is to build a temperature station for a babycot that I can access from my computer to monitor that the temperature in the cot is moderate when the baby sleeps.
This is a pretty basic build, hopefuly it will be a good experience and my intention is to move on and build a station that messures moisture in the soil of my plants, telling me when they need to be watered.
### **Material**
List of material
| Material | Quantity | Vendor | Price |
| --------------- | -------- | ---------- | -------- |
| LoPy4 | 1 | Electrokit | ~450 SEK |
| Expansion board | 1 | Electrokit | ~250 SEK |
| Breadboard | 1 | Electrokit | 59 SEK |
| USB 2.0 | 1 | Electrokit | 39 SEK |
| Battery pack | 1 | Electrokit | 29 SEK |
| Power bank | 1 | - | ~150 SEK |
| Wires male+male | 5 | Electrokit | 6 SEK |
| MCP9700 | 1 | Electrokit | 8 SEK |
#### **LoPy4**
LoPy4 is a compact quadruple network micropython development board, with WiFi, bluetooth, LoRa and sigfox enabled. It´s an IoT platform for connecting things. It´s programmable with micropython and Pymakr plugins for IoT application development.

Fig. 1. LoPy4 https://pycom.io/product/lopy4/
#### **Expansion board 3.0**
The expansion board is used to connect the sensors and power, as well as an antenna if neccesary.

Fig. 2. Expansion board 3.0 https://pycom.io/product/expansion-board-3-0/
#### **Sensor**
For this project, a temperature sensor, MCP9700 has been used. It is an analog sensor, used for meassure indoor temperature.

Fig. 3. MCP9700 temperature sensor https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/?gclid=EAIaIQobChMImu6Cs8SN8gIVGhF7Ch0kpgWIEAAYASAAEgJx5PD_BwE
#### **Wiring**
To make the wiring easier, a breadboard has been used.

Fig. 4. Breadboard with 400 connections
https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/

Fig. 5. Cables https://www.electrokit.com/produkt/labbsladdar-100mm-hane-hane-30-pack/
### **Computer setup**
The code for this project has been written in windows 10 using VS code.
Node.js has been installed, along with the PyMakr extension to enable python code to be written.
Firmware update of LoPy4 and expansion board might be needed. I needed to update firmware on the LoPy4, and it´s described here: https://docs.pycom.io/firmwareapi/
Before uploading code, make sure to connect the LoPy4 to the computer using USB cable.
Uploading the code to LoPy4 is simple, just press the upload button as shown in fig. 1. and the code is uploaded.
If using Pybytes as service for signals, the download button can be handy to import a pybytes_config.json file to the project.

Fig. 6.
### **How to connect**
Connect the LoPy4 with the expansion board as in Fig. 7.
For this project I use pin 16, connect green cable from expansion board P16 to breadboard D27. First red cable goes from 3.3V on expansion board to plus pool on the breadboard. Second red cable connects from plus pool on breadboard to A26 on breadboard. For blue cables, first one connects from GND on expansion board to minus pool on breadboard, second cable connects from minus pool to A28 on breadboard. Sensor MCP9700 connects to E26-28 on breadboard. The station can be powered by USB, but for mobility I use a powerbank as source of power. Another solution is to use a batterypack as seen in fig. 7.

Fig. 7. Connections
### **Platform**
Pybytes has been chosen as the platform. Pybytes offers a free option with 5mb cloud storage. For this project, it is more than enough as data is sent every five minutes and each data point corresponds to 8 bytes. Under the pybytes dashboard there are a number of widgets that can be used to present the data being sent.
Describe your choice of platform.
### **The code**
Code below is placed in the main folder of the project.
keys.py
```
# Used for storage of the SSID and password of the network
wifi_ssid = "network id"
wifi_password = "password"
```
boot.py
```
from network import WLAN
import machine
import keys
# Connect to WiFi
wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
# net.ssid is the name of the network to connect to.
if net.ssid == keys.wifi_ssid:
print('Network found!')
# keys.wifi_password, password to the network.
wlan.connect(net.ssid, auth=(net.sec, keys.wifi_password), timeout=5000)
while not wlan.isconnected():
machine.idle() # save power while waiting
print('WLAN connection succeeded!')
print(wlan.ifconfig())
break
```
main.py
```
import pycom
import time
import machine
pycom.heartbeat(False)
adc = machine.ADC()
apin = adc.channel(pin = 'P16') # Pin on expansion board
while True:
millivolts = apin.voltage() # Analog temeperature measured in millivolts
celcius = (millivolts - 500.0) / 10.0 # Converts millivolts to celcius
print(celcius)
pybytes.send_signal(1, celcius) # Send values to pybytes
print ("sending: {}".format(celcius)) # Print values in terminal
time.sleep(60)
```
### **Transmitting the data / connectivity**
For this project I have chosen to work with WiFi and MQTT.
WiFi is used since the device are intended to be in use at home only, and MQTT is the standard protocol used in PyBytes for transmitting data and is suitable for IoT.
The device sends data every 5 minutes.
All data is stored in Pybytes own database, the thing you need to do is to connect to pybytes and publish to your own account/device and pybytes will handle the rest.
### **Presenting the data**
The data is saved in database for a month. Since the stations purpose is to enable live monitoring of temperature, no need for saving data is actually needed.
The dashboard shows the temperare in bars, the signal is sent every 5 minutes and the dashboard is updated.

Fig. 8. Presentation of the data on pybytes.
### **Finalizing the design**

Fig. 9. Final build.
It´s not pretty and is not in a box, but it works.
Think the project went fine, even though I had some initial problems with connectitivity for com ports in VSC.
Don´t know if something could have been done in another way for this simple project. Adding a moisture sensor for a more versatile machine might be one thing, creating a box to hold the station would be another improvement.
It´s been fun and educational and I intend to create some more advance IoT devices in the near future.