# Indoor temperature and humidity monitor
###### Author: Hampus Victorén (hv222dm)
###### tags: `IoT`
This tutorial will explain the steps to create a temperature and humidity monitor, how the sensors works and the logic when data is sent to the cloud.
The data gathered from the sensors are sent to the cloud where it can be visualized and presented in the web or to a smartphone. It does also show the data in a small screen on the microcontroller.
The software used for the microcontroller are Arduino IDE and Blynk IoT (send data through wifi).
The time to finish this project is around 5 hours.
## Objective
This project was chosen because one week into the applied IoT course, my son was born. I'm a beginner with IoT and I found this project to be fun, usable, educational and manageable with a newborn at home.
The purpose of this project is to monitor the temerature and humidity, but also to easily access the gathered data in the cloud from a computer or smartphone.
In the summer the indoor temperature and humidity can easily be too high to be healthy, especially for a newborn.
The most valuable insight it will give is probably which combination of temperature and humidity the baby sleeps at his best. His naps are documented and the data gathered from this controller will be a good improvement for the value of the documentation.
## Material
**List of materials used**
| Hardware pic | Name & description | Price & link |
| -------- | -------- | -------- |
|  | TTGO ESP-32 Microcontroller. programmed with C++ in Arduino IDE to manage sensor data. | [170 kr](https://www.amazon.se/gp/product/B07VNG9D52/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1) |
| DHT22: Temperature & humidity sensor. Sensor that gathers data |[125 kr](https://www.amazon.se/gp/product/B078SVZB1X/ref=ppx_yo_dt_b_asin_title_o04_s00?ie=UTF8&psc=1)
 | USB-C to USB-A cable. Used as power supply and upload for code/flashing | [25 kr](https://www.amazon.se/PremiumCord-anslutningskabel-snabbladdning-laddningskabel-ku31cf3w/dp/B07NSQJQ5N/ref=sr_1_8?keywords=usb%2Bc%2Bkabel&qid=1656962742&sprefix=usb%2Bc%2Caps%2C80&sr=8-8&th=1)
 | Jumper wires female to male. For connecting sensor pins to microcontroller | [60 kr](https://www.amazon.se/AZDelivery-kompatibel-Raspberry-Breadboard-inklusive/dp/B07K8PVKBP/ref=pd_bxgy_img_sccl_2/259-7207367-5739743?pd_rd_w=EDGOC&content-id=amzn1.sym.ac31a142-07ec-4ec2-a56a-32921a0c41c2&pf_rd_p=ac31a142-07ec-4ec2-a56a-32921a0c41c2&pf_rd_r=FDR844G22SSC8F5ATSYS&pd_rd_wg=LWLGy&pd_rd_r=b7bc724f-2cc4-4560-993f-7a3d0a05f2de&pd_rd_i=B07K8PVKBP&psc=1)
## Computer setup
* Windows 10
#### Chosen IDE
* Arduino IDE
#### Libraries for IDE
* DHT Sensor Library
* Adafruit Unified Sensor library
* Adafruit GFX Library
* Adafruit SSD1305
* Blynk
* https://dl.espressif.com/dl/package_esp32_index.json
### How the code is uploaded
The code is uploaded through usb, be sure to use right port. In this case it's COM3. 

### Steps for computer
1. Install [Arduino IDE](https://www.arduino.cc/en/software)
2. Find your board. To add the board used in this tutorial, go to "Preferences" and add this link at Additional Boards Manager: https://dl.espressif.com/dl/package_esp32_index.json

3. Install required libraries, see "Libraries for IDE"
## Putting everything together
At this early stage the monitor only need three jumper wires (female to male), microcontroller and a DHT22 sensor.
##### **Diagram**



* VCC (Yellow) is connected to 5V
* DATA (Brown) is connected to pin 17
* GND (White) is connected to G (Ground)
## Platform
The platform used is [Blynk IoT](https://blynk.io/en/developers). This plaform offers free cloud software which include what was needed for this project.
#### Why Blynk IoT?
* Ease of use (setup, dashboard etc)
* Easy documentation
* Free subscription
* Android app
The platform connects through wifi and the setup was really easy to get up and running. Each virtul pin is created and easily set up in Blynk, for example pin, data type, unit min and max values.



## The code
The monitor is connected trough wifi and defined with an authentication token provides by Blynk.
```
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "wifi-name";
char pass[] = "wifi-password";
```
The pins used are defined below.
```
#define DHTPIN 17
#define DHTTYPE DHT22
#define BUTTON1PIN 35
#define BUTTON2PIN 0
```
Blynk library writes to virtual pins created in the Blynk software.
```
Blynk.virtualWrite(V17, temperature); // send all the values to their respective virtual pins
Blynk.virtualWrite(V15, humidity);
```
The code below defines t and h (temperature and humidity) and sets the background color and text. Functionality to switch between Farenheight and Celsius is shown in the if else statement.
```
float t = dht.readTemperature(useFahrenheit);
float h = dht.readHumidity();
Serial.println(t);
Serial.println(h);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setCursor(0, 30);
tft.setFreeFont(&Orbitron_Light_24);
tft.println("Temp Humidity");
tft.drawLine(0, 35, 250, 35, TFT_BLUE);
tft.setCursor(0, 60);
tft.print(t);
if (useFahrenheit){tft.print(F("f"));}
else {tft.print(F("c"));}
tft.setCursor(130, 60);
tft.print(h);tft.print(F("%"));
```
Code below runs the Blynk software in a loop with a delay.
```
void loop() {
delay(3000);
Blynk.run();
```
## Transmitting the data / connectivity
Transmission of the data is done with wifi and is done every three seconds.
The transport protocol used is [TLS](https://www.cloudflare.com/learning/ssl/transport-layer-security-tls/) which encrypts, authenticates and verifies data.
This connectivity seemed easy and safe enough for this project.
## Presenting the data
There are multiple ways to present data with Blynk. The first one below is probably most useful because for this project purpose the air quality at the moment is very important.

The gathered data can also be presented in charts which provides a good overview. It's easy to get updates when temperature or humidity are at abnormal values at certain times of the day.
**Temperature chart**

**Humidity chart**

---
### **On smartphone**


## Finalizing the design
This project was very fun and educational. In the future I want to make it portable. To use it in a stroller. In order to do that I need to buy JST (batteries as power supply), a customized PCB and solder it together. A neat way to protect it would be to 3d print a case as well.




