---
# System prepended metadata

title: 'Tutorial on how to build a temperature, humidity and pressure sensor'
tags: [IoT, Sense HAT, Raspberry Pi, LNU, Ubidots]

---

Tutorial on how to build a temperature, humidity and pressure sensor
===
By Hugo Bäckman Ulmgren, hb223bn
###### 24-06-21
###### tags: `IoT` `Raspberry Pi` `Ubidots` `Sense HAT` `LNU`
___
- Table of content
[ToC]
___
Project overview
---
This tutorial will describe how to build an IoT-device that uses sensors to detect temperature, humidity and pressure.

Following this guide will have you set up in 2-3 hours. If you are an experienced user you will complete the project in ~1 hour. 

Objective
---
The main thought behind the project at first was that it would function as a thermometer. After my mother-in-law heard about my project she wanted a thermometer that can be placed in the near by lake. I haven't figured out some hardware problems yet though but all the software functions is completed for both water and land since it's the same. 

For now I can read the temperature, pressure and humidity in my home. This will help me with knowing if the temperature gets to high for my dog. The pressure and humidity serves just as a fun add-on because with only the temperature it looks kind of bland.

I think this small project will give me a basic insight on what IoT is and how it works and that I will learn the basics behind IoT.

Material
---
* Raspberry Pi 3 model b+ - a single card computer
* Raspberry Pi Sense HAT - an expension card that contains various sensors such as:
    *  Gyroscope
    *  Accelerometer
    *  Magnetometer
    *  Temperature
    *  Humidity
    *  Barometric pressure

| Hardware |Cost(KR) |Where to buy | 
| -------- |----- |--------     | 
|Raspberry Pi 3 model b+|449|[Kjell.com](https://www.kjell.com/se/produkter/dator/raspberry-pi/raspberry-pi-3-model-b-enkortsdator-p88100)|
Raspberry Pi Sense HAT|395|[Kjell.com](https://www.kjell.com/se/produkter/dator/raspberry-pi/raspberry-pi-sense-hat-sensorkort-p87985)|
|SD-card|159.9|[Kjell.com](https://www.kjell.com/se/produkter/dator/minneskort/sandisk-ultra-micro-sd-kort-32-gb-p90992)|
|Memory card reader SD USB|179.9|[Kjell.com](https://www.kjell.com/se/produkter/dator/minneskort/minneskortlasare/plexgear-minneskortlasare-sd-usb-3.0-p61482)

The Raspberry Pi wasn't one of the recommended boards for this course, but it functions very well as an IoT-device. Since it is basically a small computer you can achive lots of great stuff with it.

Computer setup
---
1. First you need to set up your Raspberry Pi. You'll need to install a operating system. To do this you will need to install a program called "Raspberry Pi Imager". You can find it here: [:link:][RaspberryPi]. 

2. After the program is installed you can go ahead and put your SD-card into the USB and then plug your USB in your computer. You will now choose the first option among the available operative systems which is a Ubuntu OS. After the download is complete you can unplug the USB and insert the SD-card into your Raspberry Pi. It should start booting by itself. After everything is set up you will use the terminal to write:
````
sudo apt-get update
sudo apt-get install sense-hat
sudo reboot
````
This will install the library for the Sense HAT.

3. You will also need to install another library called Requests. To do this open your terminal and write:
````
pip install requests
````
The IDE that was used is Visual Studio Code since it's what I've used for earlier projects. To be able to write code in python you need to install the python extension in Visual Studio Code. The code doesn't have to be uploaded to the Raspberry Pi since we write the code on the device.

Now you're ready to go.

[RaspberryPi]: https://www.raspberrypi.org/software/
[GIF]: https://projects-static.raspberrypi.org/projects/rpi-sensehat-attach/809d620144ae61428d998d0711443583e5e19355/en/images/animated_sense_hat.gif

Putting everything together
---
Plugging in the sense HAT into the Raspberry Pi is very easy since there is no wires or some specific pins that needs to go to a specific place. Just follow the GIF that is linked here: [:link:][GIF]

Since we attach the Sense HAT directly on the Raspberry Pi's GPIO-pins it's quite hard to write a circuit diagram since no wires is being used but this picture might help: ![](https://i.imgur.com/7u6xJNT.jpg)


[GIF]: https://projects-static.raspberrypi.org/projects/rpi-sensehat-attach/809d620144ae61428d998d0711443583e5e19355/en/images/animated_sense_hat.gif

Platform
---
The platform I've chosen for this project is Ubidots. This platform is cloud-based and is free to use as long as it's not used for commercial use. Ubidots also provides a nice dashboard that can be customized by the user. 

When I was deciding on which platform to use I also wanted a platform which uses triggers which will be used to send e-mails when the project is going into the next phase, a underwater thermometer. The downside with Ubidots is that data is only held for 30 days, but that is manageble since I only want real-time data.

The code
---
As I mentioned earlier I'm using the Sense HAT and the Requests libraries for this project. The code to connect and send requests is from their website. 

```python=1
from sense_hat import SenseHat
import requests

sense = SenseHat()
sense.clear()

TOKEN = "XXXXXXX" #Change the x's to your token
DEVICE = "RaspberryPi" #Name your device
VARIABLE_1 = "Temperature"
VARIABLE_2 = "Humidity"
VARIABLE_3 = "Pressure"

#To assign the values our sensors collect to the right variable we use this method
def build_payload(variable_1, variable_2, variable_3):
    value_1 = sense.get_temperature() #sense.get_temperature() is the method our sensor uses to collect the temperature
    value_2 = sense.get_humidity()
    value_3 = sense.get_pressure()
    
    payload = {variable_1: value_1, variable_2: value_2, variable_3: value_3} #Assigns data collected to right variable
    
    return payload
    
#To make a post request towards Ubidots we'll be using this method
def post_request(payload):
    url = "http://industrial.ubidots.com"
    url = "{}/api/v1.6/devices/{}".format(url, DEVICE) #This will make Ubidots know our device name
    headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
    
    status = 400 #Error code
    attempts = 0 #Number of attempts
    
    #Loop makes sure that everything runs smoothly and that it doesn't continue 
    #to try to send data if something is wrong
    while status >=400 and attempts <= 5:
        req = requests.post(url=url, headers=headers, json=payload)
        staus = req.status_code
        attempts += 1
        time.sleep(1)
    
#This method is the core of the code and this is what will be executed
def main():
    payload = build_payload(VARIABLE_1, VARIABLE_2, VARIABLE_3)
    post_request(payload)

if __name__ == '__main__':
    while (True): #Loop being used so our device runs the program forever or until stopped
        main()
        time.sleep(10)
        
```
To run the code simply write this in your terminal (change the x's to your file name):
```
python xxxx.py
```
Connectivity
---
The data is being sent every 10th second through the wireless protocol: Wi-Fi. To transport the data we're using a weebhook that is a Hypertext Transport Protocol (HTTP) push API. This is because it's a fast and efficient way of transport data and you get the data in real-time.

Presenting data
---
![](https://i.imgur.com/eoOHbzy.png)

The data is saved until new data is provided to the platform. If no new data is provided the last known data is saved for 30 days.

I've used an e-mail trigger to send me an e-mail once a day to tell me the temperature data. This is something I want to have when I'm going to read the temperature in the water so it's nothing I need for the state of my IoT-device today.

Finalizing the design
---
The project went well and as expected. I think the project would be more fun if I would have thought of the hardware problems earlier and had the will to buy expensive add-ons for my device so I could make the under water thermometer. The hardware solutions that I was missing was: a LTE-solution, a power supply strong enough and a waterproof case. This will be done soon though!

![](https://i.imgur.com/CjibFPh.jpg)
This is my set up for now since I don't have a water proof case yet. 

![](https://i.imgur.com/Kv9kdop.png)
This is how my dashboard is set up. You can customize your own at Ubidots.

Sources
---
For connecting to Ubidots:
https://help.ubidots.com/en/articles/513309-connect-the-raspberry-pi-with-ubidots