# Temperature and humidity
#### Herman Hårdänge (hh223fm)
## Project description
As a part of the coure examining parts I did a project with the purpose och measuring temperature and humidity over one night outside my bedroom. To do this project you need a esp32, three (3) female to female wires, micro-usb data transfer cable and a dht22 sensor.
Collected data is stored in two different graphs for eight hours which is perfect for the night project. Two additional visualizationinstruments shows current temperature and humidity.
To do the project without any previous knowledge about programming, sensors and microcontrollers will take about 20 hours. Afterwards you will wonder how it could take so long – but you will learn a lot. The project can probably be done ff you have previous knowledge about building IoT devices and systems in a matter of hours, this is however a beginner’s tutorial.
## Objective
I chose this project based on two reasons. First of all I have no previous experience about IoT. This is not an advanced project which means that I would learn the basics and manage to complete the project. Second of all, my items did not deliver for a few weeks and was then cancelled. I had to order again from a different supplier and I could start the practical part of the course (basically the important and fun part of the course) very late, therefore I aimed for a lower level than I wanted. The purpose of the project is to gain basic knowledge about internet of things and also to see how the temperature and humidity changes over night. This will give me insight in what internet of things is, aswell as showing me the climate outside my bedroom windom.
## Material
| Product | Description |
| -------- | -------- |
| Esp32, generic (wifi) | Microcontroller |
| DHT22 or DHT11 | Sensor for temperature and humidity
| Female to female, three (3) | Wires to connect the DHT
| Micro-usb wire (with datatransfer) | Gives power to the esp32 and uploads/downloads data
|USB-a adapter |For connecting the esp32 without a computer
Esp32 is a cheap microcontroller. By uploading software to the device from a computer, you can use it for lots of different things. In this case, it is programmed to be able to connect to WI-FI and forward collected data to the internet. The data collected is made using a connected sensor.
Esp32 price: 94kr (https://sizable.se/P.CE9S1/ESP32)
The DHT sensor I used is DHT22. It is a sensor that measures humidity and temperature and is connected to Esp32 to receive power and transmit data. For the project, you can also use DHT11, the difference is poorer precision in some of the measurements and not as large a range in measuring temperature and humidity. To do this particular project it does not matter which one to choose - the purpose is to learn how to connect everything.
DHT22 pris: 61kr (https://sizable.se/P.JN41E/DHT22-AM2302-Temperatur-och-luftfuktighetssensor)
Female to female wires are needed to connect the DHT sensor directly into the circuit board on the ESP32, they are included with the purchase of DHT22. Usually you use a "breadboard" for this, but since I did not have much time left on the course and wanted to make it as cheap as possible, this way worked great.
Wires price: included in dht22
Micro-usb cable is needed to be able to connect esp32 to the computer, the cable provides power and enables programming of the device. The cord should handle file transfer (which most do). Try all the cords at home and one of them will work.
Micro-usb price: 99kr
(https://www.kjell.com/se/produkter/mobilt/ladda-koppla/kablar-adaptrar/micro-usb-kablar/micro-usb-kabel-1-m-p68687
I also used a power adapter (USB-a) to be able to power it without the computer. The one for my iPhone worked great.
## Computer setup
To be able to go through with the project the esp32 need to be configured. This will be done by downloading software to the computer and uploading software to the esp32. Atom is an IDE where the code for the project will be written. Pymakr is a plugin which enables writing code in micropython. These are a few steps, and they will take a while – but its for initial configuration and therefore most of it is not needed to do again later in the project. More drivers, libraries and plugins won’t be necessary since the dht sensor import already exists in micropython. This makes the project more simple for a beginner.
1. Download Atom, an IDE for micropython (where we configure the esp32)
2. Download Node js
3. Open atom and go to preferences -> install
4. Download Pymakr (this is to program the device in micropython)
5. Connect esp32 directly to your macbook (if you use adapter/dongle the computer might not identify the esp)
6. Download
https://micropython.org/resources/firmware/esp32-20220117-v1.18.bin
7. Open
https://nabucasa.github.io/esp-web-flasher/
- Select 460800 Baud
- In the popup, choose the serial port connectet to the esp32
- Click connect and hold the boot button on the esp32
8. Hold the boot button and click erase, click ok when popup message shows. Holding the boot button down is necessary on the esp32 when flashing (programming) the device
9. Next to “Offset: 0x” write 1000 and upload the file downloaded on step 6
10. Open Atom, go to Pymakr settings, unbox “Auto connect” and “Safe sss.boot before upload”
11. Enter the port the esp is connected to. The name of the port is visible after typing ls /dev/ in the terminal
12. Click on connect device and write the name of the port, confirm if needed
13. See if it works by writing >>>print(“Hello from Lena Hallengren”). It should print below the print row
## Putting everything together
As previously mentioned, everything needed to make this work is the esp32, wires, dht sensor and a micro-usb cable. Starting with the connection between the esp32 and DHT22. Connect according to the picture below.

Since this is not an advanced project, the most important thing is that the wires goes according to the picture. No calculations need to be done regarding power or anything else. The red wire goes from the 3v3 pin on the esp32 to the left pin on the DHT22. The 3v3 pin is what gives power to the sensor (or other external sensors).
The blue wire goes from D2 (pin 2) on the esp32 to the middle pin on the DHT22. This wire transfers data and enables us to collect temperature and humidity measurements.
The black wire goes from the GND (ground) on the esp32 to the right pin on the DHT22. This wire is an additional path for electricity to make sure to not get an electric shock in case of a short circuit.
## Platform
So far the necessary software for the esp32 and the computer is installed. The wiring is also done. Soon the project code will enable the esp32 and the DHT22 to submit data, but where will it show up, and how will it be presented?
Adafruit is perfect for this project. It is an internet of things platform that receives data through some simple code. Adafruit supports a couple of triggers to receive the data from a programmed esp32. In this case MQTT (Message Queuing Telematry Transport) is used. MQTT is a messaging protocol and enables machine to machine messaging.
With Adafruit a dashboard will be created to visualize the data through a couple of diagrams. All the data from the latest eight (8) hours will be presented at the dashboard. The current temperature and humidity is also shown.
## The code
1. Import modules and from other script, for example DHT and Adafruit

2. Declare wifi info, Adafruit url and username, active key. This is needed for the rest of the program

3. Create a method to connect the esp32 to wifi, there is another way in a workshop but this works. The method tries to connect the esp32 to wifi by using the ssid and password. Wifi.connect searches for this. The method is looking for wifi for 10 seconds, since it sometimes takes a while when using the phone as a hotspot. If it connects it will print “ansluten”, otherwise it will say that it could not connect and reminds the user to make sure the wifi hotspot is on.

4. Connect to the MQTT server by typing cliend id, URL for adafruit, username, password. This is possible by the import of MQTT client. Also connecting to adafruit.

5. Measure values of temperature and humidity with this methos. This code activates measurement through sensor.temperature and sensor.humitity. Furtheron it connects to client and publishes temperature_feed with “temperature” which is collected from sensor.temperature. Same thing for humidity. The collected values is searched for and collected every 9 seconds, this is done by a timer. The dht22 can measure every two seconds as a minimum which can be good to know.

6. Create a timer which performs a callback to the sens_data method. The metod will go again after at least two seconds (with the dht22) Gör en timer som utför en callback till sens_data metoden. Då börjar metoden om efter ett visst antal sekunder.
## Transmitting the data / connectivity
The humidity and temperature data from the esp32 and DHT22 is sent every 9th second. To get results about seeing how the temperature and humidity is over night, it will be just fine with every few minutes otherwise (and you will still collect more data than needed). The transport protocol as mentioned previousley and shown in the code is through mqtt. This is because it was a simple solution and worked great. The data is transmitted through wifi. I was not able to connect to my homerouter, but using the iPhone 11 as a hotspot (or earlier non 5g phones) works great.
## Presenting the data
This is the dashboard for the project. The top two are live measurements of temperature and humidity, lower ones are the graphs that filled the purpose of the project. The data is collected for eight (8) hours in the graphs and that was enough for this one night project.

## Finalizing the design
Other than the dashboard shown above, the picture below is my beautiful weatherstation. The DHT22 is ofcourse somewhat waterresistant but the esp32 is not, therefore it was good that it was covered by a roof, I recommend keeping it under a roof or getting a waterproof case. The data collected shows a connection between the rise/fall of the humidity and temperature. If the temperature lowers, the humidity increases - and the opposite way around. Pretty cool to see!

I think the project and the course went well considering the circumstances. I have definately learned a lot and also generated an interest for internet of things. It will be fun to do more projects during the summer and in the future and now I have a solid foundation to go from.
Thank you to the teachers and my classmates for all the help, and thank you for reading.