---
title: 'IoT Project'
disqus: hackmd
author: Khaled Alkai
---
# IoT temperature monitoring - Using a humidity and temperature sensor for the purpose of verbreduce temperature
By @KhaledAlkai (ka223hg at Linneaus University)

## Table of Contents
[TOC]
## Project overview
This project is a tutorial on how to control a programmable quadruple bearer board LoPy4 together wtih a digital temperature humidity sensor MCP9700, which will be used as a thermometer that measures temperature or a temperature gradient.
The estimate time the project has took is approximately around 4 hours in total.
## Objective
This project touches on the fundamentals and the basics of the internet of things which is the purpose of this course in general. The main aim of this project is to improve living standards by giving control and stability which will be obtained by transforming the physical objects into intelligent or smart virtual devices, more specifically a temperature measurement device will be able to collect the temperature of the actual atmosphere and send information to the collector about the temperature statues (low or high temperature). The data gathered by the sensor will be saved mainly to be shown in a dashboard, but it can also be helpful for further analysis and studies.
My insights on this project are that temperature monitoring systems will be used in more complex contexts providing for IoT applications across many industries, including real estate, manufacturing, and healthcare.
## Material
This project requires the following materials:
| Item | Cost | supplier |
| ---- | ---- | ---------|
| Expansion Board 3.0 | 17.60€ | [Expansion Board 3.0](https://pycom.io/product/expansion-board-3-0/)|
| External WiFi Antenna | 8.00€ | [External WiFi Antenna](https://pycom.io/product/external-wifi-antenna/) |
| Micro-USB-cable | 31 SEK | [DELTACO Micro USB](https://www.teknikdelar.se/mobiltillbehor/sony/xperia-ovriga-modeller/kablar-laddare/deltaco-micro-usb-kabel-2-m-svart/?dfw_tracker=35448-USB-302S&shopping_id_click=USB-302S&gclid=EAIaIQobChMI__HN8qzW-AIVCcgYCh3tVg8dEAQYAiABEgIg-_D_BwE) |
| MCP9700 | 8 SEK | [Digital temperature and humidity sensor](https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/) |
| lopy4 | 38.45€ | [LoPy4](https://pycom.io/product/lopy4/) |
|Jumper Wires | 49 SEK | [Breadboarding Premium Male/Male Jumper Wires](https://www.m.nu/breadboarding/breadboarding-premium-male-male-jumper-wires-40-x-6-150mm)
|Breadboard | 4.80€ | [Breadboard 30X10](https://tweakable-parts.com/en/pcbprotoboards/313-breadboard-30x10.html)
### Material specifications
<img src="https://pycom.io/wp-content/uploads/2020/03/Website-Product-Shots-ExpB-Front-100x100.png" width="100" height="100">
**Expansion Board:** Expansion cards enable a computer system's capabilities and interfaces to be enhanced or augmented in a way that is appropriate for the tasks it will carry out.
<img src="https://i.imgur.com/EE2oEZi.png)
g" width="100" height="100">
**External WiFi Antenna:** It's used to give the quadruple bearer board LoPy4 the capability to detect the Wifi network signal.
<img src="https://img-teknikdelar.cdn.imgeng.in/upload/normal/usb-301s-1.jpg" width="100" height="100">
**Micro-USB-cable:** Used as a power source to the LoPy4 and to upload the program from the computer.
<img src="https://i.imgur.com/dajyPFV.png)
" width="100" height="50">
**Digital temperature and humidity sensor:** This sensor leaves a serial data stream that can be read with a digital input on a microcontroller.
<img src="https://pycom.io/wp-content/uploads/2018/08/lopy4TopN.png" width="100" height="100">
**LoPy4:** LoPy4 is Micropython-programmable quadruple bearer board with ultra-low power usage, Powerful CPU, BLE and state of the art Wi-Fi radio and MicroPython compatible.
<img src="https://images.m.nu/data/product/1076f860/ID758_MED.jpg" width="100" height="100">
**Jumper Wires:** Effective for making wire harnesses or jumpering between headers on PCBs
<img src="https://i.imgur.com/HFMayMi.png)
" width="100" height="100">
**Breadboard 30X10:** Fundamental when building circuits, used to connect wires to the expansion board and the sensor.
## Computer setup
To start this IoT project, we need to configure our computer with a suitable IDE. This tutorial will use Visual Studio Code which can be downloaded from [here](https://code.visualstudio.com/docs/setup/setup-overview), the reason being that VS Code is a comprehensive platform that can handle simple to complicated tasks for users of all skill levels.
Additionally, we need to install a plugin that will help us upload and run the code into the LoPy4 device by clicking on the left sidebar and choosing *extensions* <img src="https://i.imgur.com/cbGSII6.png)
" width=20 height="20"> then type *pymakr* in the search bar and click on download.
## Putting everything together
The assembly process is not complex due to the limitation of the companions used. As I mentioned earlier MCP9700 sensor leaves a serial data stream that can be read with a digital input on a microcontroller which will be connected together using the jumper wires. In our case the data series is the outcome of measuring humidity and temperature data. The information gathered will then be sent to the to LoPy4. With the help of the LoPy4 and the program applied on the card, the system is now able to analyze the given data.

* Make sure that the yellow wire is connected to GND, orange to 3v3 and red to P16.
* The sensor can be placed randomly on the breadboard.
## Platform
Pybytes is the platform I personally chose to transmit the data to the internet and IFTTT. Pybytes is Pycom’s officially released platform giving the user of Pycom's products full control over them. With Pybytes the user will have control over the device’s data stream visually of the sensor. It will function as our database and dashboard. With Pybytes you get a much easier MQ Telemetry Transport Client and it will be ready to use with a single line of code in comparison to other platforms like Grafana. And since we will use IFTTT for temperature measurement, we can use to send notifications to the phone too.
For more detailed information about the device management platform check out Pycom's home page linked [here](https://docs.pycom.io/pybytes/)
## The code
Here is the main function that runs the process on this project . If you wish to change something for example the temperature limit value, it is this file you need to modify.
```
#The imported libraries used.
import machine
import time
import urequests
from network import WLAN
#Make a url request using IFTTT.
webhook = "https://maker.ifttt.com/trigger/Event1/with/key/d-FPf2Itm-iYJ8XyTQBg8t"
#Check lopy4 internet connectivity.
wlan = WLAN()
if wlan.isconnected():
print("connected")
else:
print("not connected")
adc = machine.ADC()
apin = adc.channel(pin='P16')
while True:
#Collecting sensor data.
mV = apin.voltage()
temp = (mV - 500.0) / 10.0
#Check if the tempeture is higher than or equel to 50°C the temp will be uppdated every 30 minutes.
if temp >= 50:
print("The current temperature is high (above 50°C): " , temp)
req = urequests.post(url=webhook, json={'value1': temp})
time.sleep(1800)
#Check if the temperture is lower that 50°C the temp will be uppdated evrey 30 minutes.
else:
print("The current temperature is : " , temp)
pybytes.send_signal(1,temp)
time.sleep(1800)
```
## Transmitting the data / connectivity
As I mentioned earlier in the *platform* section that since we will be using IFTTT for temperature measurement, with Pybytes we will get a much easier MQ Telemetry Transport Client.
The data transmitted via Wi-Fi due to the fact that the data will be used by me at home. Considering having a gateway near by. It can acually be smart to use LoRa because it requires less power and have a much larger range than Wi-Fi.
The data is sent to pybytes using the pycom library which will be accessable by creating an acount on pybytes. I found this [page](https://docs.pycom.io/pybytes/gettingstarted/) being useful on how to connect the device to pybytes and connecting the Wi-Fi to the hardware afterwards.
## Presenting the data
After the assembly process, we need now to connect loPy4 to the computer. The dashboard used is pybytes dashboard to represent the collected data.
The same data can also be sent to a smart phone as a notification via IFTTT every 30 minutes as explained earlier.

## Finalizing the design

I was not able to get the design as I wanted it to be in time due to delivery issues, so I thought it will be enough to just take a picture of the hardware alone and work on the design later on. This project has made me better as a problem solver and I refined my ability to think outside the box. Further the project was really fun to work on and made my summer much more fun.
I hope you enjoyed reading my tutorial!