---
# System prepended metadata

title: 'Tutorial - Ughhhh, is it hot in here?'

---

# Tutorial - Ughhhh, is it hot in here?
**Name:** Miranda Alvenkrona
**LIN-ID:** ma225rx

This project is about using a DHT11 sensor to measure the temperature and humidity inside a room and send the data to the platform Pybytes. Depending on how much experience one have the time is going to difference a lot, but for a beginner the estimated time should be around 30-40 hours. In these hours I do count in the time learning about IoT, Pybytes, Pycom etc.


## Obective
I did choose this project because I did not want to set too high ambitions as I have no previous experience of IoT and computer science. It was important for me to keep the project on an achievable level too keep it fun. The purpose of the project is not about that I need a thermometer, it was more about journey of learning and understanding. If one is new to IoT there might be insights, but for one with more experience it would probably not be any as the project is quite simple.

## Material
All the material is bought from Electrokit (https://www.electrokit.com) or Pycom (https://pycom.io). The chart below includes all material that was used in the project and information about their cost and from which website they come from.

| Device | price | Place |
| -------- | -------- | -------- |
| LoPy4                     | 34.95 EUR     | Pycom          |
| Antenna                   | 9.00 EUR     | Pycom          |
| Expansion Board 3.0       | 16.00 EUR     | Pycom          |
| Breadboard                | 59 SEK        | Electrokit     |
| Sensor kit (25 sensors)   | 299 SEK       | Electrokit     |
| Wires                     | 29 SEK        | Electrokit     |

**LoPy4**
In this project did I choose to work with the Pycom LoPy4 device, see Figure 1. LoPy4 is a development board that is programmed by MiroPython and its works with different brands of connectivity as LoRa, Sigfox, WiFi and Bluetooth. 
![](https://i.imgur.com/7yjGyNr.png) Figure 1

**Antenna**
The LoPy4 can be hook together with an external antenna. The antenna is neede if the Wifi is blocked and one want to connect to LoRa or Sigfox.
![](https://i.imgur.com/Y5AynGL.jpg) Figure 2

**Expansion Board 3.0**
The Expansion Board 3.0 is an expansion bord that is compatible with the Lopy4, see Figure 3. The Expansion Board have a lot of a features, a few of them is the JST style battery connector, button to enter into “safe mode” easily, power LED and charge status LED, etc.
![](https://i.imgur.com/f5fpPYs.png) Figure 3

**Breadboard**
The breadboard is optional, but it can be used to make the construction easier when connecting sensors to your device, see Figure 4.
![](https://i.imgur.com/ozePd1B.png) Figure 4

**Wires**
If not want to use a breadboard use wires with male to female, see Figure 5. If using a breadboard wires male to male is needed, see Figure 6.
![](https://i.imgur.com/TzxU5wp.jpg) Figure 5
![](https://i.imgur.com/lI3K1V7.jpg) Figure 6

**Sensor**
The sensor DHT11 is a sensor that measures the temperature and humidity, see Figure 7. It has three pins data, power, GND. In Figure 8 are the specifications for the sensor.
![](https://i.imgur.com/viRxnZ3.png) Figure 7
![](https://i.imgur.com/99azavZ.png) Figure 8

## Computer setup
I have Windows 10 as operative system, and I did choose to Atom.io as my IDE (Integrated Development Environment).

**Step 1.** Download Atom.io from the website.

**Step 2.** Install the package **pymakr** in Atom. Start Atom, in the Welcome Guide press on “Install a Package” and then “Open Installer”. Search for “pymakr” and press “Install”. 

**Step 3.** Connect the Expansion board with the LoPy4 and then connect it to the computer with a Micro USB cable to USB cable.

**Step 4.** Make an account on Pybytes (https://pybytes.pycom.io). 

**Step 5.** Log in on Pybytes and then press the “CONFIGURE NETWORKS” in the “Getting Started” panel to add your WiFi. After adding your WiFi press the “Add device” in the “Getting Started” panel, thereafter “ADD DEVICE via USB”. Choose your devise (LoPy) and then choose WiFi and press “NEXT”. Change the name if you want and choose your WiFi and press “SAVE”. 
Copy the number code (marked yellow) as in Figure 9.

![](https://i.imgur.com/5gOh8c6.png) Figure 9

**Step 6.** Download the firmware setup form the website (https://docs.pycom.io/gettingstarted/installation/firmwaretool/). 

**Step 7.** Start the firmware setup program and follow the instructions. On “**Communication**” see Figure 10. On the next slide “**Pybytes registration**”, see Figure 11, enter your code from **step 5**. 

![](https://i.imgur.com/JXmn7QP.png) Figure 10

![](https://i.imgur.com/KQyLzKL.png) Figure 11

On next step “**Advanced Settings**” see Figure 12 and press continue.

![](https://i.imgur.com/qu203PB.png) Figure 12


**Step 8.** Now you can see on Pybytes website that your device has been connected, see Figure 13. 

![](https://i.imgur.com/2uX79yi.png) Figure 13


## Putting everything together

The green wirer is GND, the red wirer is power, and orange is data. Figure 14 showes how the sensor is connected with thepycom device.

![](https://i.imgur.com/VngRims.png) Figure 14

## Platform
In this project have Pycoms own platform Pybytes been used for collecting data and display it. Pybytes is a free cloud-based device management platform available for all Pycom development boards and modules. Within seconds can one get data from your device to your computer or to your smartphone.

## The code
The code below is the main code (main.py).

``` python=
import pycom
import time
from machine import Pin
from machine import Timer
from dth import DHT #DHT from dth library

#Funktion testing if it is too cold, returning True if it is too cold and False if it is NOT too cold
def IsItTooCold(arg):

    if result.temperature < 0: #Check if the temperature is below freezing
        print("Brr. It's freezing outside") #Output message to console
        return True

    return False

pycom.heartbeat(False) #Disable pycom rgbled heartbeat function
th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN),0) #Create an object called th which is an instance of DTH using P23
time.sleep(1)

#Read and send data to pybytes one time before the loop + print to the console
result = th.read()
print("DHT11 - Temperature at boot-up: %d C" % result.temperature)
print("DHT11 - Humidity at boot-up: %d %%" % result.humidity)
pybytes.send_signal(1,result.temperature)
pybytes.send_signal(2,result.humidity)


chrono = Timer.Chrono() #Create a Chrono object from Timer to measure time
chrono.start() #Start measuring time

#Main loop for sending measurements, time based or manually triggered with push button
while True:
    elapsed_seconds = chrono.read() #Get the elapsed time since start of chrono (or reset)

    if elapsed_seconds < 3600: #Check that the elapsed time is below threshold
        time.sleep(1) #Sleep before next loop

    elif elapsed_seconds > 3600: #Timer threshold has been reached, send measurements to PyBytes and output to terminal

        result = th.read()  #Read temperature and humidity from DHT11 sensor

        if IsItTooCold(result) == True:
            print("It's too cold to do anything!!")

        elif IsItTooCold(result) == False:
            print("DHT11 - Temperature at boot-up: %d C" % result.temperature) #Output the DHT11 temperature at boot-up
            print("DHT11 - Humidity at boot-up: %d %%" % result.humidity) #Output the DHT11 humidity at boot-u
            #Sending the temperature and humidity to pybytes
            pybytes.send_signal(1,result.temperature)
            pybytes.send_signal(2,result.humidity)

            time.sleep(1)
            chrono.reset() #Reseting the time to 0

```

## Transmitting the data / connectivity
Every hour is data sent to Pybytes and the wireless protocol that is used is WiFi. Pybytes is using an open MQTT Broker that has connections for internet connection.

## Presenting the data
The diagrams below are receiving data every hour from the Pycom device and saving it. Figure 15 shows the temperature and Figure 16 is showing the humidity
![](https://i.imgur.com/ioZVx1p.png) Figure 15

![](https://i.imgur.com/wGrVvLb.png) Figure 16


## Finalizing the design

This project was both interesting and exhausting. There were a lot of information to take in and to understand which made the two first weeks difficult and confusing. If I had more time or had progressed faster in the two first weeks, I would have liked to implement a sleep function for conserving battery between measurements and to use a better encoding to reduce size of payload. My final thoughts on the project is that it was a good experience and I want in the future to keep experimenting and learn more about IoT.
![](https://i.imgur.com/EJdRE4C.jpg)

