# IoT tutorial for temperature sensor with LoPy4 and expansionboard over Wifi.
By Marcus svensson, ms226ym.
# Overview
This is a turorial to setup a temperature monitor connected through a LoPy4 and visualise the data with Pybyted over wifi.
All software used in this tutorial is free with Pybytes being the exception, they do offer a month free use.
The set up is estimated to take between 2-4h.
The general purpose of this project was to gain a insight into IoT.
# Objective
To gain insight into IoT and hopefully also share this insight through a simple tutorial with a single temperature monitor. The data collected in this tutorial showcase the temperature in my apartment the summer of 2021.
# Material
* Lopy4 with headers
* Expansionboard
* USB cable
* Jumper wire x3
* Breadboard
* Temperature sensor MCP9700
* Battery holder 3xAAA
* Batteries
The list of material can be purchase through Elektrokit. For this tutorial the material was bought in bundles.
| Material | Price (SEK) | Where to buy |
| -------- | -------- | -------- |
| LoPy4 basic bundle | 849:- | [LoPy4 basic bundle, Elektrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-basic-bundle/) |
| Sensors only bundle | 129:- | [Sensors only bundle, Elektrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-sensors-only-bundle/) |
| Battery holder 3xAAA | 29:- | [Sensors only bundle, Elektrokit](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare-och-jst-kontakt/) |
# Computer setup
I choosed to program with python since it suits the pycom devices.
To begin with, download [Pycom Firmware Updater](https://docs.pycom.io/updatefirmware/device/) to flash your device with the latest firmware, connect your device through a USB-port and follow the decriptions.
The Pycom Firmware Updater is also required for connecting your device to Pybytes over wifi to visualize your data. Vistit [Pybytes](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 sign up, first month is free. Once you you have signed upp create a new device and choose your wifi for signal. Under provisioning follow the steps to connect your device using the Pycom Firmeare Updater.
I used [Visual Studio Code](https://code.visualstudio.com/Download) as my IDE since I prefeer it and it was easy to install the Pymakr plugin under the extenxion icon.
With the Pymakr plugin you get a REPL terminal through USB to the lopy4 and you can now run and upload code on to it with the pymakr toolbar.

# putting everything together
Follow the circuit diagram below to connect the device.

Begin by mounting the Lopy4 onto the expansionboard with the LED light above the USB port. Then connect the battary holder to the expansionboard. The pins we are going to use are, GND for ground, 3v3 for voltage and p16 for our input. With the temperature sensors flat side facing away from you connect the GND to the left, the p16 to the middle and the 3v3 to the right rod.
Since the MCP9700 temperature sensor operates on a voltage input ranging from 3.1-5.5V no resistor was needed. I used the Lopy4 3v3 pin that has a maximum load of 1.2A. It would also be possible to use the Vin 3.5-5.5v pin as a power output from the Lopy4.
If you desire to place the device quite far away from the wifi a improvment can be to connct a antenna to the wifi/bluetooth port on the lower left corner of the lopy4 if looking at it from the same view as the circuit diagram.
The design is currently to simple and not soldered or protected in any way so for the moment it is only suited for developement setup.
# platform
I decided to go with Pybytes which is a cloud based platform. This was for me a short project and the first month on Pybytes is free of charge. I did like the simplicity of the plattform, they provide a easy way to provision your device and also had pre defined templates for displaying the data. After setting up your device and a template for your signal, upon revisting your device through pybytes you will have a nice dashboard.
In the future for a bigger project or depending on the data collected i might explore a different platform, I felt as Pybytes has its limitations surrounding option of display data when creating charts.
# The code
```
# boot.py
from network import WLAN
# Connect to WiFi
lan = WLAN(mode=WLAN.STA)
lan.connect("Your SSID", auth=(WLAN.WPA2, "Your Password"), timeout=10000)
```
```
# main.py
import machine
import time
import pycom
time.sleep(10) #delay for code upload before program and deepsleep
pycom.heartbeat(False) # turns off heartbeat
ADC = machine.ADC() # convert the analog voltage signal to digital
apin = ADC.channel(pin='P16') # assign the signal from pin 16
mV = apin.voltage()
temp = (mV - 500.0) / 10.0 #calculate temp from analog voltage according to data sheet
# terminal output
print('millivolts: ', mV)
print('Temperature ',temp, 'Celsius\n')
# send data to Pybyte
pybytes.send_signal(1, temp)
pybytes.send_signal(2, mV)
machine.deepsleep(1000 * 60 * 30) # deepsleep for 30min
```
More detail on syntax functionallity can be read about under the [machine](https://docs.pycom.io/firmwareapi/pycom/machine/) module at Pycom.
# Transmitting the data
The data is transmitted from the Lopy4 every 30 min over wifi with the MQTT protocol. With MQTT the Lopy4 is acting as a publicher onto a so called broker that Pybytes runs over, Pybytes in this case also act as subscriber to that topic (the data). Pybytes provide 1MB of data in the cloud.
# presenting the data
The data is presented as charts for the temperature every 30 min and the amount of data sent per hour.


*The figure display the dashboard charts on pybytes. The charts showcase temperature and data recieved by the Lopy4*
The temperature stayed quite consistent over the short amount of days I measured it.
# Finalizing the design
I am happy with the project but there is a ton of space for improvements. To start with I could have chosen a less power demanding transmission like sigfox or loraWAN. When I first tried the device i had no deepsleep, a transmission of every minute and the battaries only lasted for about 4h. With that said being power effiecient is key and really showcase its importans within IoT.

*The project situated on a shelf near the window in my apartment.*
As for the device itself, it would be nice with a case for it and also to solder the temperature sensor onto the expansionboard to be able to exclude the breadboard and get a more compact and light end product.
Also to situate the device outside would have given more interesting data, the current temperature as seen did stay quite consistant even though i hoped to get some fluctuation by placing it clode to the window. I think in the future I will probably expand the device with more sensors add a shell and place it outside.
A throubleshoot I did not manage to resolve was the data not being recivied by pybytes a couple of times. I could observe gaps the chart and longer than 30min reciev notifications from pybytes. It can also be obsereved on the total data sent per hour chart.
If placing the device somwhere else in the future and still using wifi I will probably go wit a antenna aswell.