# Another way to utilize a DHT11 sensor
Artem Gazarov (ag224fz)
## Overview
As the header states, this project takes into consideration the usage of temperature and humidity sensor DHT11 with additional visual and auditory representation through the piezo buzzer and two LED lights. For a beginner this work may take up a few days. Primarily to understand the wiring, connections to the service and some code of course.
## Objective
The reason behind this project was to come up with the solution to the 'dry eyes' problem. Since the beginning of this summer, I have been waking up with very dry eyes and really sore throat which obviously had to do something with humidity (potentially temperature as well). I also use the computing devices almost all the time so this project can also help to measure up the heat level while the computer is running near. In terms of insights, this project can be taken to another level if there was more time, for instance, better data visualization added or potentially more sensors (tilt, motion). However, this is still a nice way to delve into the IoT concept.
## Materials
| Name | Description | Price |
| ----------- | ----------- |--------- |
| Temperature and humidity sensor DHT11| Measures temperature in Celcius and humidity in percentage | 50 SEK |
| Piezo Buzzer | Makes a sound used for indicating when something went wrong | 5 SEK |
| LED lamp Red | Lights up when the temperature is too high (tested with 25 degrees Celcius) | 1.50 SEK |
| LED lamp Blue | Lights up when the humidity level is either too high or too low (below 40 or above 60 %) | 1.50 SEK |
| 10 male jumper wires | Used to connect the components to pico itself | 20 SEK |
2 resistors of 270 Ohm 2W | Help to work with the lights | 10 SEK |
Breadboard | Playground for placing everything | 100 SEK |
Raspberry Pi Pico WH | Microcontroller | 150 SEK
Most of the components were bought as a part of the starter kit from Electrokit. However, a specific piezzo buzzer with some wires and LED's were bought from local Kjell & Company. Total price was about 400 SEK.

**Figure 1**
Components
## Computer setup
This whole project is done on Windows as that is the machine used. The device is programmed through the use of MicroPython which is written in Visual Studio Code. The needed extension called PyMakr was installed for an easier integration between the devices.
So in order to implement this project:
1. Download python first.
2. Install Visual Studio Code.
3. Add PyMakr plugin to VSCode.
4. Node.js is also required for the plugin, install it!
5. Ready to code now, add your connected device in the PyMakr plugin and run it in the development mode.

**Figure 2**
Regular VSCode look
## Putting everything together
Diagram describing the components and wiring for only the purposes of development:

**Figure 3**
It is important to notice that the DHT11 sensor represented here is with 3 pins only, meaning that it does not need a resistor. The LED lights however were connected as intended. The application called Fritzing was used in the making, although additional parts were imported from the official Raspberry Pi documentation.
## Platform
For the purposes of visual data representation and connectivity within this course, the Ubidots REST API were used. There are two panels, one for humidity and one for the temperature. A free version was used and not the paid subscription. It is a cloud-based solution rather than stored locally. In terms of alternatives, one can go more into self-hosted or even Big Data solutions such as Google Cloud, for instance. That is, however, only needed if there is some larger amounts of data to be displayed. In this particular case, not much was required, therefore a low-code solution was used.
## The Code
Some main code snippets are going to be shown below. The connection functions together with json builders were borrowed from the Roadmap guides.
Here the data is sent to the Ubidots website by importing another api.py file which contained json builder together with keys.py that had my Wi-Fi credentials.
```
tempSensor.measure()
temperature = tempSensor.temperature()
humidity = tempSensor.humidity()
print("Temperature is {} degrees Celsius and Humidity is {}%".format(temperature, humidity))
value = temperature
returnValue = api.sendData("picowboard", "temperature", value)
value = humidity
updateValue = api.sendData("picowboard", "humidity", value)
```
In this small part, the main check is happening, so that whenever the levels of measurements are off, the piezo buzzer will make a sound in a loop and the lights will flash.
```
while True:
try:
if temperature > 25 and humidity > 60 or < 40:
blue.value(1)
red.value(1)
buzzer.value(1)
time.sleep(1)
buzzer.value(0)
print("Levels of temperature and humidity are poor.")
blue.value(0)
red.value(0)
except Exception as error:
print("Exception occurred", error)
```
So in general, only one light should light up depending on the situation (humidity or temperature being in a risky position). In this case, it is an if-statement that specifies a scenario where both measurements are not ideal; the red and blue lights flash and the buzzer makes a distinct sound.
Data being sent over as json to the Ubidots is shown in this last code bit.
```
def sendData(device, variable, value):
try:
url = "https://industrial.api.ubidots.com/"
url = url + "api/v1.6/devices/" + device
headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"}
data = build_json(variable, value)
if data is not None:
req = requests.post(url=url, headers=headers, json=data)
return req.json()
else:
pass
except:
pass
```
## Transmitting data / connectivity
The data is updated quite often, only with a couple of seconds delay. The wireless protocol for the main connectivity used was Wi-Fi. Data is transmitted from Pico to Ubidots through json, making an object assigned with a value. The transport protocols were HTTP since it is quite a familiar solution in my case. There exists a communication between the server and device with the usage of built json files. When the humidity and temperature change quickly, for example. Another cool feature about the use of Ubidots was that it provided the notification system either through calls or messages. The choice here was emails and the event was set to be whenever something is wrong with temperature or humidity.

**Figure 4**
Example of email notifications
## Presenting the data
As already mentioned, the Ubidots provides a nice data presentation which can be seen in the figure below. The data is saved almost immediately which is really neat considering that the information passed should be available almost at instant. The notifications then take place if something unusual happens. They are implemented in the form of sending an email from Ubidots to my university account as mentioned earlier. The dashboard is built of a thermometer measuring temperature and a humidity level shown as a value.

**Figure 5**
Output represented on a Ubidots dashboard
## Finalizing the design
Final result of my little project.

**Figure 6**
Such small mechanism can be put near a bed to check for humidity or near any other device to check for temperature if needed. Personally I think adding more sensors or figuring out a nicer way to represent data could have been very beneficial. Although, with such a simple idea, there is only room for imagination.