---
# System prepended metadata

title: 'Tutorial for measuring temperature, moisture and humidity with LoPy'

---

# Tutorial for measuring temperature, moisture and humidity with LoPy  


###### Albin Sartoft/as226yi
## Objective
I created this as a prototype for a greenhouse,right now it can be used as indoor use but the concept is the same. A simple and easy way to measure the temperature, humidity and the soil with the data being sent via wifi which makes it easy to access wherever you are. This is a good and quick way to get introduced to IoT. 

I've made this tutorial as compact as i could, there is already great guides on how to get rolling with the Pycom devices and how to set them up so i will point in their direction with links and tips.

Everything is fairly easy and quick to setup but these things often come with a bit of tinkering. Estimated time to get up and running is about 3-4 hours. A bit more if you're going to solder your components.


## Material
| Hardware         | Pricing              |
| ----------------- |:----------------------- |
| [LoPy4 Multipack](https://pycom.io/product/lopy4-multipack/)         | €62.04   |
|   [DTH11](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/) |  49kr    |
| [Soil Moisture Sensor](https://www.az-delivery.de/en/products/feuchtigkeitssensor-modul)         | €4.79     |
| [A weatherproof enclosure (optional)](https://www.amazon.se/kopplingsdosa-vattent%C3%A4t-anpassningsbar-utomhusbelysningskabel-anslutning/dp/B07NVRDG1V/ref=sr_1_6?dchild=1&keywords=kopplingsdosa&qid=1627816642&sr=8-6)      | ~100kr    | 
| [Lipo battery (optional)](https://www.electrokit.com/produkt/batteri-lipo-3-7v-4400mah/) | 249kr | 

I 3d-printed [this case](https://www.myminifactory.com/object/3d-print-flange-mount-enclosure-for-pycom-boards-88497) for mine. This has the measurements for the Pytrack & Pysense so you will need to make a few adjustments for the expansion board and battery

You also need a basic breadboard, jumper wires and a Micro USB cable for the LoPy4

If you choose to use a breadboard instead of soldering, make sure that it fits inside the enclosure

## Setup
#### Pybytes & LoPy4
Firstly set up your LoPy 4 and update its firmware before anything else. I would recommend creating a Pybytes account for easy access to your device, you can connect your device through the firmware updater.

First follow the instructions on the "get started"-page on pycoms website: https://docs.pycom.io/gettingstarted/

Follow pycoms guide to update your firmware & to connect to Pybytes: https://docs.pycom.io/pybytes/gettingstarted/


#### Atom & Pymakr
To program your device you need an IDE. Download Atom from: https://atom.io/

Install the Pymakr package in Atom, click "install package" in the welcome guide that pops up, search for "Pymakr" and click install.

#### Connecting to wifi
Pybytes makes it easy to connect to a network, just keep your device online and follow these steps:

Configure your network on Pybytes on the "Networks" page. Go to your device, click configuration to the far right and add your network, click save and then click deploy configuration.

#### Uploading the code
1. Create a new project in Atom
2. Put the main.py file in it
3. Click "Upload" in Pymakr


## Putting everything together
Connect everything together following this chart:
(Use pin 23 for the DHT11 and pin 15 for the moisture sensor)
![](https://i.imgur.com/kqwEZ9C.jpg)

You wont need any resistors since the sensors already have them built in.

## Platform
You need to register the signals coming from the device being sent to Pybytes. Go to your device, click "Signals" and "Define new signal".

Signal 1. Temperature, unit "C"
Signal 2. Humidity, unit "%"
Signal 3. Moisture, unit " "

#### Why Pybytes
Pybytes is a great service and it's easy to use after you've gotten your setup right. I chose it because it comes with the hardware and is built by Pycom. It's also quite fast to get up and running while updating firmware which means less steps.

## The code
- Main：
```javascript=0
import machine
import pycom
import time
import math

from machine import Pin
from machine import ADC
from dht import DHT # https://github.com/JurassicPork/DHT_PyCom

rtc=machine.RTC()
rtc.ntp_sync("pool.ntp.org")


time.sleep(2) #sleep 2s before reading values

#DHT
DHT11_read = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0) # Type 0 = dht11

def dht_readvalue():
    P23_result = DHT11_read.read()
    if P23_result.is_valid():
        pybytes.send_signal(1, P23_result.temperature)
        pybytes.send_signal(2, P23_result.humidity)
        temperature = P23_result.temperature
        humidity = P23_result.humidity
        return temperature, humidity

#MOISTURE
adc = machine.ADC()
P15 = Pin('P15', mode = Pin.IN)
apin = adc.channel(pin=P15, attn=ADC.ATTN_11DB)

def read_moisture():
    value = apin()
    if value == 0:
         time.sleep(2)
         pybytes.send_signal(3, 'HIGH')
         return 'HIGH'

    else:
         time.sleep(2)
         pybytes.send_signal(3, 'LOW')
         return 'LOW'

#CALL FUNCTIONS
while True:
    time.sleep(2) #sleep 2s before reading values
    dht_readvalue()
    read_moisture()
    time.sleep(30*60) #sleep 30min
```
#### DHT11 Library
You will need to download the library by JurassicPork at: https://github.com/JurassicPork/DHT_PyCom

Put the "dth.py" file in your main folder and rename it to "dht.py"

## Transmitting the data / connectivity
Pybytes is used to transmit the data, Pybtes uses the MQTT protocol. 

The data will be measured and sent every 30 minutes.

## Presenting the data
The data will be presented on the "SIGNALS" page on your device at Pybytes website and will look like this:

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

You can also create charts of different types displaying your data, more info on [Pybytes website](https://docs.pycom.io/pybytes/dashboard/)


## Finalizing the design & Conclusion
### How it looks while setting up with the breadboard:
![](https://i.imgur.com/TkoJMjQ.jpg)

### In 3d-printed case: ![](https://i.imgur.com/7mSBmr9.jpg)

The plan for the future is to solder the components to a board for permanent use, design a better case and add some more functionality. I will also be saving the values with dates.