# Temperature and humidity monitoring
My name is Taofik Arnouk (student credential: ta223dm). In this tutorial, I will go through how to create a temperature and humidity monitoring system that connects to wifi via a microcontroller and displays the data on Ubidots. This tutorial will take 2-3 hours to complete.
## Objective
* I chose to start with this project as being an eye-opener for IoT devices. This project may be simple as it uses one sensor, but the data that is received from that sensor can be used to study the conditions of the area that it is placed within.
* The purpose of this project is to create a device that can gather live temperature and humidity data when placed in an area. This is beneficial if you require constant monitoring of an area to see whether there might be a sudden spike in temperature and humidity.
* The insight that can be gained from this project is not only to gain data on temperature and humidity data for both indoor and outdoor use. This data can be used to give insight into the air quality indoors and could be used to prevent food decay in storage rooms.
## Material
Here is the list of materials that are used to complete the project
| Item | Description | Purchased from | Price | Picture |
|:---------------- |:--------------------------------------------------------------------------:| --------------:| ----- | ------- |
| Micro controller | Waveshare Pi Pico W Microcontroller Board used for data collection, transfer, analysis, more.| Amazon | 165Kr ||
| Jumper cables | Elegoo 120 st Flerfärgad Duplex Kabel used to connect componnents together |Amazon | 90Kr| |
| Breadboard | AZDelivery MB-102 Breadboard used to place componnents on | Amazon | 70Kr | |
| Powerbank | A ADDTOP Powerbank 26 800 mAh used to power the project when not connected to computer | Amazon | 440Kr | |
| DHT11 | A sensor used to measure temperature and humidity in the area | Amazon | 48Kr |  |
## Computer setup
In this tutorial, a Mac OS system has been used. Similar steps can be followed for Windows machines.
The first step is to download and install Node js on your machine. This can be done by pressing [**here**](https://nodejs.org/en/download) and selecting the installer for your machine.
After Node js is installed, an IDE should be chosen to write code into. I chose to use Visual Studio code (VS Code). You can download VS Code by pressing [**here**](https://code.visualstudio.com/Download) and selecting the installer for your machine.
Once VS Code has been installed, you need to download the **Pymakr** extension. This is done by finding the extension tab on the left-hand side of your VS Code window or by pressing **cmd + shift + x** to reach the extension tab. When you are in the extension tab, type Pymakr in the search bar and download the first extension. After the installation is completed you should be able to see the Pymakr plugin icon in the menu to the left of the screen. You should be seeing a **Projects** and **Devices sections** when you press the Pymakr plugin.
Once the above steps are completed, you should follow these to flash your micro controller.
Vist this [link](https://micropython.org/download/rp2-pico-w/) and download the latest Micropython release to your desktop. It would be in a uf2 file format.
Connect the micro-usb end of the cable to the micro controller carefully to not break the port.
Before plugging in the USB type A end to the computer, hold the BOOTSEL button on your board and release it once you plug it in the computer.
A new drive will pop up on your desktop called RPI-RP2. You should **copy and paste** the **uf2** file to it.
The drive should then automatically disconnect and reconnect to your computer.
## Putting everything together
Here is the circuit diagram for this project.

In the diagram, the black wire coming from micro controller is the ground (-) while the red wire is the power (+). The blue wire is connected from GP27 to the out pin of the DHT11 sensor. Both the ground and positive wires are connected to the corresponding sensor pins.
The micro controller can be powered from the computer's USB A port or the use of the powerbank.
Press [here](https://datasheets.raspberrypi.com/picow/PicoW-A4-Pinout.pdf) to see the micro controller (Raspberry Pi Pico W) pin layout.
## Platform
The platform that was chosen for this project was [Ubidots](https://ubidots.com/). Ubidots offer a variety of ways to collect and receive data, display data through a dashboard, and notifications and alerts when specific terms are met. The dashboard can be easily customizable for any use case that you may have. I chose to go with Ubidots due to its popularity, customizability, and ease of use when it came to displaying my data.
Ubidots is a cloud IoT platform, so our data would be stored and maintained by Ubidots servers. Ubidots offer a wide variety of subscriptions to choose from for your use case. They offer a 30-day free trial to use for educational purposes called the [STEM](https://ubidots.com/stem) account which is used in this project. The limitation with this account is that you are limited to 3 devices, so scalability is limited. This can be solved by subscribing to one of many of the subscriptions that they offer.
## The code
The code that will be mentioned and needed to run this project is called the [main.py](https://github.com/TaofikArnouk/Temperature-and-humidity-monitoring/blob/main/main.py) file. It can be found on [Github](https://github.com/TaofikArnouk/Temperature-and-humidity-monitoring/blob/main/main.py). The code includes how to connect to WiFi, send http requests, and gather sensor data to send to the Ubidot server. To get the [WiFi](https://hackmd.io/@lnu-iot/rJVQizwUh) and [requests](https://hackmd.io/@lnu-iot/r1k63jjwo) working these two guides were followed and used.
Before getting the project running on your device, a few alterations in the code should be made. See the code snippet below to see what needs to be changed.
```
TOKEN = "Placeholder" #Put here your TOKEN from Ubidots
DEVICE_LABEL = "Placeholder" # Assign the device label desire to be send
TEMPERATURE_LABEL = "temperature_sensor" # Assign the variable label desire to be send
HUMIDITY_LABEL = "humidity_sensor" # Assign the variable label desire to be send
WIFI_SSID = "Placeholder" # Assign your the SSID of your network
WIFI_PASS = "Placeholder" # Assign your the password of your network
```
The things that need to be changed and replaced are the placeholder text. Replace the Placeholder text with your information. The Ubidot token can be found at profile -> API Credentials -> then copying and pasting your token code. Then write the device name that you entered when registering your device in Ubidots. Then write your WiFi name in WIFI_SSID and write the password for your WiFi in WIFI_PASS.
These should be the only things that you need to alter to get your device working.
The code snippet below is responsible to measure, collect, and send the sensor data to Ubidots every 30 seconds.
```
tempSensor = dht.DHT11(machine.Pin(27)) # DHT11 Constructor
# The code within the while loop would be executed repeatedly due to the condition being set as true
while True:
tempSensor.measure()
temperature = tempSensor.temperature()
humidity = tempSensor.humidity()
value = humidity
returnValue = sendData(DEVICE_LABEL, HUMIDITY_LABEL, value)
value = temperature
returnValue = sendData(DEVICE_LABEL, TEMPERATURE_LABEL, value)
# Sends sensor data every 30 seconds
sleep(30)
```
## Transmitting the data / connectivity
I have chosen to use WiFi as the wireless protocol for this project. It was chosen since this project's use case would be in a house environment with a constant WiFi connection. The data from the sensor is being sent by HTTP requests due to Ubidots offering a Rest API to handle them.
The data gathered from the sensor is sent every 30 seconds to the Ubidots server. This can be seen in the sleep function in the [main.py](https://github.com/TaofikArnouk/Temperature-and-humidity-monitoring/blob/main/main.py) file.
#Sends sensor data every 30 seconds
sleep(30)
To change the rate that the data is being sent. You need to modify the value in the sleep function.
## Presenting the data

The Ubidots dashboard that I created includes five widgets. Two gauges and three graphs. The gauges consist of one temperature gauge that displays the temperature in degrees Celcius and one humidity gauge. The graphs consist of one temperature/date, humidity/date, and temperature and humidity/date graph. The temperature and humidity/date graphs are used to see whether there is a relationship between the two data points. The data is seen on the dashboard as soon as the requests are sent to the Ubidots API. They are sent every 30 seconds to the ubidot server and stored.
## Finalizing the design

The project ended up being a success since I was able to collect data from the DHT11 sensor and display them on Ubidots. As you can see in the image the sensor is directly connected with the raspberry pico w with female to-female connecter when being compared to how the sensor was connected in the circuit diagram, this was done for a couple of reasons. When testing out whether the project was working as it should, I received mixed outcomes. These outcomes were that the pico was not picking up the sensor at all times. Troubleshooting methods were taken, such as changing the location of the hardware in the breadboard, using different jumper cables, and retesting and rerunning the code. The fear that the sensor may have been fried was a real concern but luckily after all the troubleshooting, the issue was the breadboard. The breadboard that I have been using was defective which resulted in mixed results of the functionality of the sensor.
As mentioned above this project may be a fast and simple project to complete due to it only using one sensor. This is not to stop us from expanding and adding more to the project. More sensors can be added to collect more data and be implemented to act once a threshold is reached. You can say that there is no limit to what you can do.