# How to build a simple system to measure outdoor temperature
Andreas Goldmann | ag223ih
This is a tutorial on how to build a system to measure the outdoor temperature. The project is using the Micropython-programmable microcontroller Lopy4 to connect a temperature sensor through wi-fi, and the data is visualized via PyBytes.
:rocket: Estimated time: 1-2 hours
## Objective
I wanted to build a simple system which could measure outdoor temperature without having to spend a lot of money (student life hehe.). I am new to IoT and programming, and I've also been working full-time the summer - hence this project. I wanted to do something simple in order for me to learn the basics but not take on on too much.
The main purpose with this project is for me to learn the basics of IoT and micropython, and is designed in its simplest form. Even though it's simple, the hope is to give a insight on IoT and programming for beginner, like myself.
After this project and course I learnt a lot of IoT and plan to do something more advanced in the future with the new knowledge I have now!
## Material
I bought the Lopy4 and sensor bundle - kit from Electrokit.com. The hardware I used from it was:
**LoPy4 + Expansion board**
The LoPy4 is a tiny development device as a Micropython-programmable microcontroller. It offers 4 different network connectivity options as LoRA, Sigfox, Wi-Fi, and Bluetooth. The expansion board is used together with the LoPy4 device, in order to conenct sensors and create IoT projects.
**Breadboard**
A breadboard is a construction base for prototyping of electronics. It is easy to use for creating temporary prototypes and experimenting with circuit designs.
**Wires**
Used in order to connect your sensor(s) from the breadboard to the development device (LoPy4 in this project). In this project only three will be used.
**Temperature sensor MCP9700**
A sensor which measures temperature.
**USB - Cable**
A cable which gives your device power and is connect either to your computer or an external battery. In this project I will not use an external battery.
**Antennae**
Connects to your device if you wish to send your data through wifi.
:exclamation: Total cost: 949 SEK
## Computer setup
### Firmware
To start you might need to update the firmware on your expansionboard, if you bought it second hand. There's no need to do it if you just bought it brand-new. Here's a [guide](https://docs.pycom.io/updatefirmware/expansionboard/) on how to upgrade your expansionboard (if needed).
The next step is to download the Pybytes firmware update tool and install it. Follow this [guide](https://docs.pycom.io/pybytes/gettingstarted/) on how to get started with Pycom.
### Software & IDE
Before you can start to write code you'll need somewhere to actually write it. Below are the steps and website with guides on how to download the softwares.
**Step 1:** You'll need to download NodeJS which can be done from this [website](https://nodejs.org/en/). Download the latest LTS version and then follow the installation steps. Once you're finished the installation you'll need to restart your computer.
**Step 2:** The next step is to download an IDE (This is where you'll write the actual code) and for this project I chose to go with VSCode. In order to download it, just follow [this link](https://code.visualstudio.com/Download) and choose according to the operative system you're using. There are different IDE that you can choose from like VSCode (used in this project) and Atom for example.
Once the installation of VSCode is finished, you can run VSCode. Before we are able to start programing we need to install the Pymakr plugin. On the left sidebar in VSCode press the Extensions icon and in the search field that pops search for **Pymakr**. Download it, and restart the programme.
The programming language you will be using is Micropython.
**Step 3:** Check that the software and firmware have been installed correctly by connecting the LoPy4 to the computer port using the USB-cable.
**Step 4:** If you're new to programming and micropython I'd suggest that you start off by creating a smaller project. For example you can follow this [guide](https://docs.pycom.io/gettingstarted/) on how to activate different colours on your device.
### Pybytes
I chose to visualize my data using the online platform Pybytes and send the data through WifI. In order to connect to Pybytes using WiFi (like I did in this project) you can follow this useful [guide](https://hackmd.io/@lnu-iot/r1bGPUOhu).
## Hardware setup
It's time to start putting your hardware together! YEY! 😃
:exclamation: **IMPORTANT** :exclamation:
Disconnect your device from your computer before you start connecting sensors and wires to it!
Below is a picture of how to connect the sensor with the wires and your device. (Picture used from https://hackmd.io/@lnu-iot/BJnuGmJh_)
Using the wires you should connect the positive voltage leg to 3V3, the ground leg to GND and the output leg to any pin with ADC capabilities (in this projecy, P16).
## Platform
In this project I chose to use the online platform called [PyBytes](https://pybytes.pycom.io/) which is free to use. What it does is to send and store data in to the cloud using WiFi connection.
The platform itself is built for Pycom devices and by following this [guide](https://docs.pycom.io/pybytes/gettingstarted/) you'll learn how to connect your own. This platform is working very well with visualizing the data on different charts, depending on your project.
## The code
The code for this project is really simple. First you'll need to create to files called main.py and boot.py. See below for the code in each one of them.
```
# Code in main.py
import machine
import time
adc = machine.ADC()
apin = adc.channel(pin='P16')
while True:
millivolts = apin.voltage()
celsius = (millivolts - 500.0) / 10.0
print(celsius)
pybytes.send_signal(1, celsius)
print("Tempature sent = {}".format(celsius))
# Send signal hourly
time.sleep(60*60)
```
This is the main code that will transform the millivolts into celsius and then send a signal to pybytes. It will send data every hour.
```
#Code in boot.py
# boot.py -- run on boot-up
from network import WLAN
import machine
wlan = WLAN(mode=WLAN.STA)
nets = wlan.scan()
for net in nets:
if net.ssid == "YOUR WIFI":
print('Network found!')
wlan.connect(net.ssid, auth=(net.sec, "YOUR PASSWORD"), timeout=5000)
while not wlan.isconnected():
machine.idle() # save power while waiting
print('WLAN connection succeeded!')
print(wlan.ifconfig())
break
```
This is the code needed in order to connect your device to your own WiFi.
:exclamation: Note that you need to change "YOUR WIFI" and "YOUR PASSWORD" to your own.
## Visualization
As mentioned earlier I used Pybytes in order to visualize my data and as I wanted to measure temperature, I thought a simple line chart would be usefull. Here's a [guide](https://docs.pycom.io/pybytes/dashboard/) of how to visualize your data.

Visualization of data, measured in celsius via Pybytes.
## Finalizing the design
Here are two images of the final design.

The breadboard with sensor being outside to measure the outdoor temperature.

LoPy4 and expansion board on the inside, connected with USB to my computer.
### Thoughts
I could have done a lot more and more advanced things with this project, but in the end I felt a little overwhelmed being a beginner in programming and IoT overall, together with working full-time. Hence I decided to go simple just to learn and not take on too much.
An option could be more sensors, like sensing humidity, light (to sense the sunrise perhaps?) and much more. Overall I'm happy with the end result and that it is actually working . :thumbsup: