# Weather Station with switch to control the AC
###### Author: `Sandu Crucerescu` `sc22tf`
Weather station that sends the data through MQTT to Ubidots and displays the temperature humidity and has the ability to turn on the AC.
It took me a few hours to make this project mostly because there was a problem with my board and i could not upload anything.
### Objective
We all know that the mercury thermometers are the most accurate but they also come with a big disadvantage, the mercury is toxic, I have been in need for a thermometer for a while and instead of buying one I decide to make one, to keep trak of the temperature in my apartment. The weather station gives us information about the temperaure and the humidity in my apartment and also has a button to turn on the AC.
### Material
| Hardware | Link |
| ----------------- |:----------------------- |
| ESP32 | [:link:][ESP32] |
| DHT11 | [:link:][Sensor] |
| 5v relay | [:link:][5v-relay] |
[ESP32]: https://www.amazon.se/AZDelivery-NodeMCU-modul-Development-efterf%C3%B6ljande-ESP8266/dp/B071P98VTG/ref=asc_df_B071P98VTG/?tag=shpngadsglede-21&linkCode=df0&hvadid=476583465599&hvpos=&hvnetw=g&hvrand=5098049184017308087&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1012372&hvtargid=pla-367709801435&psc=1
[Sensor]: https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/
[5v-relay]: https://www.amazon.se/ARCELI-stycken-KY-019-rel%C3%A4modul-arduino/dp/B07BVXT1ZK/ref=asc_df_B07BVXT1ZK/?tag=shpngadsglede-21&linkCode=df0&hvadid=476437602690&hvpos=&hvnetw=g&hvrand=16546541740986888662&hvpone=&hvptwo=&hvqmt=&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1012372&hvtargid=pla-683082997822&psc=1
For this project I decided to use an ESP32 (Fig.1) this is a very cheap board that has both BlueTooth and WiFi it is very versatile and it is good for any projects, DHT11 (Fig.2) this is temperature and humidity sensor it is also inexpensive and gives relatively accurate rezults, and a 5V relay(Fig.3) this is used to control the AC.

Fig.1

Fig.2

Fig.3
### Computer Setup
#### Uploading the code
For this project I used the PlatformIO expention for VSCode. PlatformIO allows us to work with multiple platforms and also it is very easy to customize. And it is more advanced than Arduino IDE.
#### Instalation
<ul>
<li>Download <a href="https://code.visualstudio.com/">VSCode</a> </li>
<li>Add PlatformIO extension</li>
<li>Once installed restart VSCode and you will see PlatformIO symbol in the left once you click it it will redirect you to the main page where you can create projects and add libraries</li>
<li>Once yu create a project go to the exporer tab and access the scr directory here you will add the code which will be uploaded on the board.</li>
</ul>
### Putting everything together

### Platform
First I tried Node-Red but for some reason it didn't work so after a few hours wasted I decided to use [UbiDots](https://ubidots.com/stem/). Ubidots gives us a variety of widgets to choose from and is very user friendly and in my opinion more clean.
### The code
For this project I used C++ instead of MicroPython just because I prefer C++ more. To get the data from the sensor 2 libraries were needed: "Adafruit Unified Sensor" and "DHT sensor library" for the ubidots I had to install a extenal library called "ESP32_MQTT".

Here we have to input all of he constants in order for out code to work. We can get the token and the variable_label for Ubidots.

This part of the code recives data from Ubidots for example if the button is pushed or not.

This part is very important this is where we connect the board to wifi and to Ubidots and also subscribe to a variable. If anything is missing the code would not work as intended.

Here the board loops through the code and subscribes to the switch, and then reads the data from the sensor int sotores it in a variable, and then it published it to Ubidots and loops again
### Transmitting the data / connectivity
Transmission of data illustrated in Putting everything together and is done with WiFI from the sensors to a gateway, and MQTT message formatting protocol from the gateway to the server.
Wi-Fi is the wireless technology used to connect computers, tablets, smartphones and other devices to the internet. I decided to use WiFi because I coud not use LoRa, but since it is a home device it won't be a problem.
MQTT, Message Queuing Telemetry Transport, is an IoT protocol often used to send small amounts of data very efficiently.
### Presenting the data

This is how the dashboard looks it has the switch and the temperature and the humidity widgets. The data is send and stored every 5sec.
### Final Design
Here is the final design, all that remains is to solter the board,cables and sensors together and find a housing for it.
