---
# System prepended metadata

title: Untitled

---


**Temperature & Humidity sensor**
Student: David Larsson
Student ID: dl222ip

**Short project overview**
Time: approx 60min

The project covers the building and coding process to create a temperature & humidity sensor for display online through the PyBytes platform via WiFi. 

**Why I chose this project?**
I chose to build this project because I't seemed quite easy to start with, and the usage of the device is very useful when growing plants to keep track of the environment in the greenhouse. 


**Materials** 
You will need a LoPy4 device or anything close to it, accompanied by an expansion board. 
You will also need a temperature/humidity-sensor with jumper wires. 

**List of material**
LoPy4 Development Board - 34€ on Pycom.io
PyMakr Expansion Board - 16€ on Pycom.io
DHT11 Humidity Sensor - 10€ on Kjell.com.
Jumper wires - usually comes with other stuff you buy.


The LoPy4 is a nice board with alot of potential when working on IoT. Wifi and antenna connectivity is built in which makes it very suitable for this project. 

The expansion board is used to make it easier to connect things to the LoPy4 in the development stage and to be able to connect a USB-cable to the LoPy4 when coding. 

The Humidity and temperature sensor is a real classic DHT11. It doesn't get any more classic in this kind of cases. With three wires, we connect it and it leaves us with two values for humidity and temeperature. NICE. 

**Computer setup**
First off we need to create an account at https://pybytes.pycom.io/.
When this is done we need to add a device. Click on the "Add Device" button which will take you through a guide to set this up. 
When you're done you will be asked to connect hardware.
The easiest way to do this is to download the firmware updater which can be seen on the top of the page.
This update tool, will help you with update of the firmware and connection to the PyBytes as well. 
When this is done, hopefully the device will be shown as connected on the PyBytes platform.
Now we can start code!

**Chosen IDE**
To get started with the coding we need to download a suitable IDE (Integrated Development Environment). Which in this case means, text editor. I've chosen to go with the Atom.io because it has a well developed interface for the Pymakr-plugin that we will install next. 
When opening Atom you will be greeted by a welcome-window that has some options for you. Start by clicking "Install a package". Search for "PyMakr" and install. This will install a REPL interface with a nice interface for you to interact with your LoPy4 later.

Now when you connect your device it should connect to the REPL-interface automaticly. When this is done you should be able to click the "download" button, which will download the source-files from the LoPy4 and make them visible for you to edit. There should be three files shown, one called boot.py which runs code only at bootup and one called main.py which runs the main code, then we should get a file called pybytes_config.json as well. That file stores all the settings for the current device/project. This has been setup by the firmware updater that we downloaded from PyBytes and should contain info about wifi connection etc. that you've entered in the "Add device"-part on the PyBytes platform. Now all we need to do is put some code into the files and press "Run" which will run the code directly or "Upload" which will upload your code and reboot the device.  

To make it easier while working with sensors, there are some smart people out there who likes to make things easy for people by creating "libraries" for us. In this case, there is a library for using the sensor (DHT11) that we will be using. This can be found here: https://github.com/iot-lnu/applied-iot-20/tree/master/sensor-examples/DHT11-22/lib

By copying the information in the document and pasting it to a new file named dht.py in your project folder. It can be used in our project. Don't forget to upload the file for it to start working. 

**Electronics**
The electronic that needs to be connected here is the DHT11-sensor. And for that to be possible we need three jumper-wires. 

1. Connect a wire from the GND pin on the LoPy to the (-)/GND pin on the sensor.
2. Then connect the 3v3 pin on the LoPy to the middle pin (+)VCC on the sensor.
3. Then at last connect the P23 pin on the LoPy to the S-pin on the sensor which will be our serial-bus. 


Now everything is set up. 
Let's get some code going. 


**Circuit**
Here's an image of the circuit as I made it. If you did the same, you're on the right track. 
![](https://i.imgur.com/Cgr77jm.jpg)

Let's insert code! 

main.py
```
#import modules#
import time
import pycom
from machine import Pin


#Import the library we borrowed online#
from dht import DHT 
#LINK to library online: https://github.com/JurassicPork/DHT_PyCom

#Set the pins
th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0)
time.sleep(2)

#Loop-function
while True:
    result = th.read()
    while not result.is_valid():
        time.sleep(.5)
        result = th.read()
    print('Temperatur:', result.temperature)
    print('Luftfuktighet:', result.humidity)
    
    #Send signal to PyBytes-online
    pybytes.send_signal(1,result.temperature)
    pybytes.send_signal(2,result.humidity)
    
    #Sleep for 5 seconds
    time.sleep(5)

```

**Transmitting the data / connectivity**
As mentioned before we're using the PyBytes platform for this project, and that is because it is very easy to get started with when developing. The project will be based on WiFi-protocol because it is destined to be located at home at all times. This can of course be upgraded in a later stage if you feel so. 

In this version we will be sending data every 5 seconds. May seem a little frequent right now, but it can be adjusted later. 

**PyBytes Visuals**
Temperature
![](https://i.imgur.com/gZJQ5cg.png)
Humidity
![](https://i.imgur.com/2TpNgkN.png)
The diagrams get an update every 5 seconds when it receives new data. NEAT!

**Final piece**
Here's an image of the device in the greenhouse where it is supposed to be. 
![](https://i.imgur.com/3bdmbP4.jpg)

Reflections: 

I think it was really funny doing this kind of project, would love to add a soil-moisture-sensor in a later stage, and of course create a casing for the devise as well. 
Right now I still need to run this through the isb cable. But I will order a battery connector ASAP to get it to be totally wireless. 