---
# System prepended metadata

title: Temp/humidity IoT device

---

# Temp/humidity IoT device
My name is Johnny Norrman, jn224cp, and I've made a small DHT11 temperature/humidity device which is connected to the Helium network through a LoRaWAN. 
The project took about a day to complete, going from nothing to a complete device. 

## Objective
The objective behind this project is to make a device which can monitor temperature and humidity then send the data to the internet for monitoring.

Another idea is to be able to make it portable, by adding a battery and 3d print a case to it (that is a future me problem however).

This project will be my first introduction into IoT devices using a LoRaWAN, and it should give me some insight in how a decentralized network operates.

## Material
Below you can see what items I used to make this project happen.

| Item | Link | Price | Picture|
|:-------- |----------|----------|-------:|
|Raspberry Pi Pico WH|[Electrokit](https://www.electrokit.com/produkt/raspberry-pi-pico-wh/)| 109 kr     | ![](https://hackmd.io/_uploads/BkUl3yod2.png)     |
| DHT11 |   [Electrokit](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/)   | 49 kr     | ![](https://hackmd.io/_uploads/H1hWn1sO3.png)     |
| Kopplingsdäck 840 anslutningar|   [Electrokit](https://www.electrokit.com/produkt/kopplingsdack-840-anslutningar/)   | 69 kr     | ![](https://hackmd.io/_uploads/Sklmnks_3.png)     |
| M5Stack LoRa module 868MHz      |   [Electrokit](https://www.electrokit.com/produkt/addon-kit-applied-iot-at-linnaeus-university-2023/)   | 349 kr     |  ![](https://hackmd.io/_uploads/B14dh1ou3.png)   |
| Labbsladd Grove – 0.64mm hylsor 4-pol 200mm     |   [Electrokit](https://www.electrokit.com/produkt/labbsladd-grove-0-64mm-hylsor-4-pol-200mm/)   | 13 kr (provided with the LoRa)     | ![](https://hackmd.io/_uploads/SkK5nyju3.png)     |
| USB-kabel A-hane – micro B 5p hane 1.8m     |   [Electrokit](https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1-8m/)   | 39 kr     | ![](https://hackmd.io/_uploads/ry8Rhyjuh.png)     |
| Labbsladd 40-pin 30cm hane/hane     |   [Electrokit](https://www.electrokit.com/produkt/labbsladd-40-pin-30cm-hane-hane/)   | 49 kr     | ![](https://hackmd.io/_uploads/SJqnoJo_n.png)|



## Computer setup

I'm using openSUSE Tumbleweed as my os.

### IDE
VScode is usually my preferred IDE, however for the sake of this project I went with Thonny.
It's easy to work with and also easy to install.
```
sudo zypper dup
sudo zypper in thonny
```


### Firmware
To get the Raspberry Pi Pico W to understand MicroPython a firmware update is required. 
The steps are best explained on raspberrypi.org, [here](https://projects.raspberrypi.org/en/projects/get-started-pico-w/1), which also explains how to get Thonny setup for the Pico.

#### Some "extra" steps will be needed if you are using Linux.
To give Thonny permission to use the Pico the following command will have to be used:
```
sudo chown <username> /dev/ttyACM0
```
Also you will have to set your user to be part of the dialout group, there are two ways on Tumbleweed which can be used.
1. Open up YaST. Navigate to "User and Group Management" under Security and Users. Select your user and hit Edit, now navigate to the Details tab and under Additional Groups check the dialout group. Done.
2. Open up your terminal and enter ```sudo usermod -aG dialout $USER```. Logout/reboot your system.

## Putting everything together
I created a design in Fritzing (minus the LoRaWAN) with the Pico, DHT11 and a breadboard, then hacked the LoRaWAN with [GIMP](https://www.gimp.org/) for demonstration purposes (because I can't find any Fritzing part for LoRaWAN).

The DHT sensor is connected to the Raspberry with a 3.3V (red wire) on pin 36, ground (black wire) on pin 38 and data (blue wire) on pin 32.

The LoRaWAN connects with TX on pin 2 (yellow), RX on pin 1 (white), 3.3V (red) on pin 36 and ground (black) on pin 38.

![](https://hackmd.io/_uploads/rkYfE6ft3.png)




## Platform
I chose Datacake, which is cloud-based, because of their integrations with LoRaWAN networks and it's also free up to 5 devices. It offers data visualization, SMS and email notifications, and more.

It's really easy to get started with 

If I wanted to scale beyond 5 devices then I would probably go with a self-hosted route, maybe TIG-stack.

## The code
I used two libraries for my project, the [DHT](https://github.com/iot-lnu/applied-iot/blob/master/1DV027/DHT11-pico-w/dht.py) and [LoRaWAN](https://github.com/iot-lnu/applied-iot/blob/master/Raspberry%20Pi%20Pico%20(W)%20Micropython/network-examples/N4_LoRaWAN_Connection/LoRaWAN.py), both from applied-iot's GitHub.

The main.py looks like this:

```python=
import struct
import binascii
import time
from LoRaWAN import lora
from machine import Pin
import utime as time
from dht import DHT11


lora = lora()
#insert your keys
DEV_EUI = "0000000000000000"
APP_EUI = "0000000000000000"
APP_KEY = "00000000000000000000000000000000"

lora.configure(DEV_EUI, APP_EUI, APP_KEY)

led = machine.Pin("LED", machine.Pin.OUT)

#blinking led when connecting
lora.startJoin()
print("Start Join.....")
while not lora.checkJoinStatus():
  print("Joining....")
  led.toggle()
  time.sleep(1)
print("Join success!")
led.on()

#pin and sensor for dht
pin = Pin(27, Pin.OUT, Pin.PULL_DOWN)
sensor = DHT11(pin)

#send payload and also print on vREPL
while True:
  try:
    sensor.measure()

    # Convert the float values to integers by multiplying them by a factor (example: 10)
    temp_int = int(sensor.temperature * 10)
    humidity_int = int(sensor.humidity * 10)

    payload = struct.pack(">hH", temp_int, humidity_int)
    payload = binascii.hexlify(payload).decode("utf-8")
    
    lora.sendMsg(payload)
    print("Sent message:", payload)
    print("Temperature: {} Humidity: {}".format(temp_int, humidity_int))

    response = lora.receiveMsg()

    if (response != ""):
      print("Received: ", end=": ")
      print(response) 

  
    time.sleep(150)
  except:
    print("An exception occurred")
    continue
    
    
```
So basically the code does the following: import needed libraries, connect to the Helium network and flash the onboard LED, if connected then LED stays on. Prepare payload and then ultimately send it and print the temperature and humidity on console.

## Transmitting the data / connectivity
The data is transmitted every 150th second using the LoRa protocol to the Helium network then onwards to Datacake where it is decoded and displayed. 

The payload decoder on Datacake decodes the payload by splitting it in two, temperature and humidity and then divide it by 10 to get the float numbers from integers.
```javascript=
function Decoder(payload, port) {
    return [{
            field: "TEMPERATURE",
            // To view negative values, we shift bits.
            value: (payload[0] << 24 >> 16 | payload[1]) / 10
        },
        {
            field: "HUMIDITY",
            value: ((payload[2] << 8) | payload[3]) / 10
        }
    ];
}
```

## Presenting the data
The data is prestented with Datacakes widget functions.
The data is stored for 7 days or 500datapoints/day (if you use the free plan).

![](https://hackmd.io/_uploads/H1ZP4U2u3.png)
![](https://hackmd.io/_uploads/HyteCLhO3.png)


## Finalizing the design

![](https://hackmd.io/_uploads/SyX1iEiO3.png)

I think this was a simple yet fun project, great way to get into IoT. A few things that I could've done different is to add another DHT11 sensor and take the average value of the two sensors as the output for a more reliable reading as the DHT11 has an error of ±2 degrees.

I wish I had a 3d-printer so I could finalize it by printing a case for everything to tuck into.