---
# System prepended metadata

title: Untitled

---

**IoT-Tutorial - Temperature and humidity device for home or greenhouse**
-----------------
**Filip Johansson - FJ222TU**
This is a beginners tutorial on how to build a temperature and humidity IoT device using an ESP32 devkit. If you are new to IoT and want to start out with an easy and inexpensive temperature and humidity device this is the guide for you!



**Estimated time to complete project:** 10h
**Estimated price:** $60

## Project Objective
My grandpa has a greenhouse in his yard which he enjoy spending his retirement time in to tend to his various plants. He turned 70 this may and I thought I could give him a cool and smart gift that measures the air temperature in the greenhouse. 

The purpose of this project is to:
1. Learn the basics of IoT and IoT-Protocols
2. Get a reliable system to measure the humidity and temperature in your home or greenhouse
3. Develop an understadning on how to send and receive data from an IoT-device either locally or using the cloud
4. Get a cool birthday present for my grandpa

## Materials
The kit I've used for this course is a MakePython ESP32 Dev Kit which contains the following components:


| Name | Use Case |
| -------- | -------- | 
| 1 x MakePython ESP32    | An ESP32 based module with an 128*63 OLED screen.| 
| 1 x Ultrasonic ranging module     | Sensor that works smiliar to a radar. Converts electrical energy to soundways in order to measure distance.     | 
| 1 x Temperature and humidity sensor (DHT11)    | As the name implies this sensor measure the temeprature and humidity in a room.     | 
| 1 x Buzzer module     | Module that sends out a fixed frequency sound. The higher the frequence, the higher sound/pitch it will omit.     | 
| 1 x DS18B20 module     | Low-cost digital temperature sensor. Range is between -55C - 125C    | 
| 1 x Infrared module     | Can be used to measure heat or detect movement.     | 
| 1 x Potentiometer     | An adjustable resistor with three ends that alters resistence with a dial. (Like an volume dial)     | 
| 1 x WS2812 module    | LED controller shaped as a circle.     | 
| 1 x Sound sensor     | Sensor that detects sounds in the nearby area.     | 
| 1x Vibration sensor    | Sensor that detects vibrations. Could be used to measure acceleration or pressure     | 
| 1 x Photosensitive resistance module    | Used to measure the amount of light (or lack of) in a room.      | 
| 1 x Pulse sensor     | Can be used to measure diffrent pulses, like your heartbeat for an example.     | 
| 1 x Servo motor     | This can spin an object around at diffrent speeds, almost like a helicopters roto-blade.     | 
| 1 x USB cable     | Used to connect the device (ESP32 Module) to your computer.     |
| 2 x Button    | Plain old button. Various use cases, on/off switch for example.     |
| 2 x Breadboard    | Used to attach the sensors, LEDs and what not to the ESP32 module. Works as a temporary circuit.     |
| 45 x Jump wire     | Electric wire that connects remote electric circuits.     |
| 10 x Resistance 330R     | Limits the current through the LED lights which prevents excess current from burning them out.    |
| 10 x Red LED     | Basic red LED-"bulb"     |
| 10 x Green LED    | Basic green LED-"bulb"      |



I ordered it from this website: https://hitechchain.se/makerfabs/29-iot/makepython-esp32-dev-kit for a total of $60.

I choose this kit because it was within my budget and seemed to contain all the necessary components need for this project. It also contains other components that could be used in the future in other builds and projects.


![](https://i.imgur.com/cnzFewq.png)

![](https://i.imgur.com/lDqbgCU.jpg)



## Computer setup

In order to get started and flash your device firmware, follow the instructions for your OS and download the IDE uPyCraft from this link:
https://randomnerdtutorials.com/install-upycraft-ide-windows-pc-instructions/

When uPyCraft is downloaded, you need to download the drivers for the USB to UART VCP (fancy words for your USB-cable) from this link: https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers

![](https://i.imgur.com/NnF1wts.png)

Click downloads and choose the driver version you need. I used the *CP210x VCP Windows* version. 

You should end up with something like this:

![](https://i.imgur.com/IOzDyFo.png)

Click the file and extract it to your folder of choice, i chose a folder called "Tutorial".

![](https://i.imgur.com/C9vRDZ4.png)

Open the folder and follow the installation guide in the windows version your system use:

![](https://i.imgur.com/31Kc4K4.png)

When this steps are done, plug in your device into your PC and you should be able to find your device on your PCs Device Manager.

![](https://i.imgur.com/LLV7wcj.png)

Now you can open up the uPyCraft IDE and go to Tools -> Board and select your board-type.

![](https://i.imgur.com/YQtxPuP.png)

 
Then you want to connect you device. Go to Tools -> Serial and choose your device, mine was called COM4.

Next step is to download following link:
https://micropython.org/resources/firmware/esp32-idf3-20191220-v1.12.bin

When the firmware is downloaded, go to Tools -> BurnFirmware
and follow these settings:

Board - esp32
burn_addr - 0x1000
erase_flash - yes
com - *Your device name*
select the Users option and click "choose", select the file you just downloaded. It should look like this:

![](https://i.imgur.com/xprw6Of.png)

Then hit OK and wait for everthing to complete. ITS IMPORTANT THAT YOU DO NOT DISCONNECT OR UNPLUGG YOUR DEVICE AT THIS STAGE.

![](https://i.imgur.com/9TP4qKV.png)

After everything is done, disconnect you device from your PC and connect it again to make sure its updated.



For the development I choose the IDE Atom. It's free to use and it's not as "clunky", it also comes with more features than the uPyCraft IDE.

To download follow the steps in this article: 
https://flight-manual.atom.io/getting-started/sections/installing-atom/

When Atom is installed, we need to install the plug-in pymakr. Follow along the steps in this link to download pymakr correctly:
https://docs.pycom.io/gettingstarted/software/atom/

Once installed, open up pymakr by clicking this icon in the right corner of the Atom IDE:
![](https://i.imgur.com/D9gfWWm.png)

![](https://i.imgur.com/X029UQN.png)


Click on Connect device and choose your device, when you se >>> in the terminal, that means your device is connected. Try out "print(7+9)" to see if its working :)

After this step you should be able to create a new project in Atom and start working on your project!

To download the code to your device first select your project (or else you'll get my error), and secondly press the "Upload" button:

![](https://i.imgur.com/WLwCpcm.png)

## Putting everything together

Behold my magnum opus, brought to you by Paint:

![](https://i.imgur.com/6pfzBwb.png)

Real life solution:
![](https://i.imgur.com/CcqHFaD.jpg)

The setup is really easy and straight forward. Im using the DHT11 sensor which measure the temperature and humidity. It has three pins, a GND pin (ground), DATA pin, and a VCC pin (voltage). Each of the pins "powers" the holes marked in green, so you could put your jumper cables in any of them, I chose the hole closest to the sensor. 

Before starting it's important to have your device disconnected from your powersource, this also stands if you are going to move sensors/cables etc. Otherwise you risk losning your hardware!

If you are colorblind, Im truly sorry. I just wanted some consistensy with the jumper cables in real life. 

The red cable is connected to the boards 3V3 (current) output of the ESP32 board and the VCC ("power supply") pin of the DHT11.

The orange cable is connected to the ground output (GND) of the ESP32 board and the ground (GND) pin of the DHT11.

The brown cable is connected to the 18th pin output of the ESP32 board and the DATA/serial pin of the DHT11.

Since this is a development set-up, I have not done any electrical calculations. It should be noted that the DHT11 [datasheet](https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf) recommends using a 5k pull-up resistor for the DATA/serial pin. Since I didnt have the need to limit the current in this development set-up I choose to not use a resistor, but you should consider if this is needed in your project.


## Platform
I chose the platform https://io.adafruit.com/ to send and host my data.
It's easy to register and to set up your end-point so you could send your data to the cloud. It offers a free version which works fine if you want a simple solution. In the future I would consider DataCake or ThingSpeak, as I think they would offer more features than the free verion of AdaFruit.

AdaFruit lets you create different dashboards depending on your needs. 

![](https://i.imgur.com/4uEoCEn.png)

![](https://i.imgur.com/ZKEmhYc.png)


In order to connect with your dashboards your offered a variety of options:

![](https://i.imgur.com/BvGT65h.png)

The main reason I choose AdaFruit is because it offers a free version, have great customization options and is pretty easy to use and set up/ connect to. 


## The Code
Link to my GitHub repository with all the code could be found here: https://github.com/filipj96/IoT






## Transmitting the data / connectivity

The data is transmitted every minute and hosted on AdaFruit. I chose to use WiFi to connect to the internet since I used a generic ESP32 board, and there are lots of tutorials online if you get stuck connecting your device to the internet.

![](https://i.imgur.com/xTsSfcu.png)

First you must import the MTTQClient to your device. Then you must declare some variables that contains your SSID, WiFi-Password, and info to your AdaFruit account.

Then you'll have to create a solution to connect to your local WiFi:

![](https://i.imgur.com/kiMKWWQ.png)

The function activate the station interface, then it enters your WiFi credentials, and if it doesnt connect, the terminal starts a countdown from 5 down to 1, then it sleeps and tries again. If the WiFi connects to the device, it prints the "connected" message, otherwise youll get the "not connected" message.

Then you have to establish the connection to your AdaFruit Account:

![](https://i.imgur.com/91Hj3uU.png)

 Set the client variable with all the necessary information the MTTQClient needs. If this doesnt work, the terminal will print an error message with some information revolving the issue.
 
 At the end we have to pass our information to our feed variables with our AdaFruit username and the correct feed id. 
 
I choose to use Wifi because it is easy to set up and since I power my device with the USB-cable connected to my laptop, I dont have to worry about the high power consumtion comparing to LoRa for example. Since my grandpa live in a small town up in the inland of Västerbotten, Im not sure that LoRa will be available, hence Wifi is the best solution for my project. 


## Presenting the data
AdaFruit dashboard is customizable and gives you lots of options. Since I just measure the temperature and humidity, mine looks like this:

![](https://i.imgur.com/uvoZ91V.png)

You could also click the title of each block, for example "Temperature C", to see a more detailed view of all the collected data:

![](https://i.imgur.com/erDjmXv.png)

As you can see in the picture above, the data is being sent every minute automatically. 

From my understanding, at least with the free version of AdaFruit, you can't set a trigger for certain values passed from the the sensor. You could however recieve a notification per E-mail if the AdaFruit doesnt receive new data within your prefered time (1 hour, 2 days etc).

As I didnt want to build my own local database to store the data, I choose Adafruit to act as a database for the transmitted data.

## Finalized design

Sadly enough my end result for this course is no more than this:
![](https://i.imgur.com/CcqHFaD.jpg)

![](https://i.imgur.com/vd6YQOZ.jpg)



Unfortunately I didnt recive my kit until until the last week of the course, so I didnt have much time to tinker and experiment with this project as much as I'd hoped to. 

However, fortunately for both me and my grandpa, I cant really give this present to him until late august, so it gives me some time to finalize the project for real. 

My plan for the future is to:
* Solder the pins to the ESP32 board (you can as of now remove the pins) to increase stability
* Fix a battery (solar?) driven power source
* Fix a nice discreate casing for the device which is attachable to glass
* Add other sensors, perhaps something for soil-samples
