---
# System prepended metadata

title: Tutorial on how build an IoT device to gain insight

---

# Tutorial on how to build an IoT device to gain insight

### Who am I
My name is **Johnny Hansson** and i have the student credentials **jh225ie**. 
To understand my choices: I'm a farmer, a grown up boy at 70+. You know, farmers never die, they just go to another field!

### Project Overview
To sumerize the project:
Build small blocks of assemblies, code and connections. 
Put all together at the end to get a full working IoT device.

The goal is to have a base of how to implement a farming management system, this project solving the sensor part, communication and display of the data collected.

### Time Needed
Time needed to full fill this part would be 2-4 hours and you will be up and running a test **if** all is working which seldom happens!

### Why this Project
I’m a farmer with an interest to monitor and manage growing plants. With introduction to precision farming you need to collect data and this is the beginning.
### What Purpose
The IoT device shall be the base of a whole farm system, monitoring both plants and livestock. 
### Insights
The reality came smashing this grand idea mainly because lack of time and therefor I have chosen the following path:
* Build the complete structure (needed to pass)
* Make relevant choices (reflect and explain)
* Learn and get insights
 
I will then gain experience of how to implement my goal. It will also give insights of modern technology in the field of IoT and how to collect data, opportunities and limitations. Last but not least, it will give an more accurate time consumption which can (and will) be used to evaluate commercial solutions.

## List of Material
### Microcontroller
In this project I have chosen to use the **Raspberry PicoW**. 
(It has a little brother without built-in connectivity so make sure you get the correct version. Pico W**H** has soldered legs as well if needed.)
![](https://hackmd.io/_uploads/BkyexQQuh.png)
Data 
* RP2040 CPU
* ARM Cortex-M0+ 133MHz
* 256kB RAM
* 30 GPIO pins
* 2MB on-board QSPI Flash
* CYW43439 wireless chip
* IEEE 802.11 b/g/n wireless LAN

Having a price about SEK 110:-
I obtained mine from electrokit.com as part of the kit needed to the course. 
Link to PicoWH https://www.electrokit.com/produkt/raspberry-pi-pico-wh/
In the rest of this report I will call the used PicoWH for just Pico

### Sensor
In this project I have chosen to use the **DHT11**.
(It has a big brother **DHT22** which has better accuracy and a bigger measure span. You differ them apart that DHT11 is blue and the DHT22 is white.)
![](https://hackmd.io/_uploads/ryg3GX7_n.png)
Data:
* Arbetsspänning: 3 – 5VDC
* Strömförbrukning: 0.5 – 2.5mA
* Viloström: 100 – 150uA
* Mätbart område: 20 – 80% RH
* Arbetstemperatur: 0 till +50°C
* Noggrannhet (RH): ±5%
* Noggrannhet (temp): ±2°C
* 
DHT11 having a price about SEK50:- and the DHT22 about SEK120:-
I obtained mine from electrokit.com as part of the kit needed to the course.
Link to DHT11[ ]https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/

### Breadboard
In this project I have chosen to use the **breadboard** delivered with the kit. A similar board can be found on the net, here I show the 840 holes version from electrokit.com (which by the way look very much like the one I used).
![](https://hackmd.io/_uploads/HJUQNmX_3.png)
It has a price of SEK70:- from electrokit.com
https://www.electrokit.com/produkt/kopplingsdack-840-anslutningar/

### Cables
You will need 2 types of cables:
First an ordinary **USB-kabel A-hane – micro B 5p hane** to connect the Pico to your computer (needed for programming and powering the Pico). I have used the one delivered with the kit but no need to buy a new one for this project if you already got a cable. What you have to confirm is that it is a cable design for data transmission as some cable intended for loading battery may only have two leads and therefor not suitable for this project. It need to have all 4 leads.

Such a cable is obtainable from the net, stores like Kjell&Co and are priced from SEK75:- and up depending on length.

Secondly you need **connecting cables** to connect the sensor with the Pico. Again, I used the cables supplied by the kit, but a set from electrokit.com is priced at SEK50:-
![](https://hackmd.io/_uploads/SyVMFX7un.png)
Link to electrokit https://www.electrokit.com/produkt/labbsladd-40-pin-30cm-hane-hane/
These are only male-male cables, if just connecting sensors which are connected to the breadbord you do not need male-female.

With these part you are ready to go, but you need a computer as well...

## COMPUTER SETUP
I have chosen a Linux computer running Ubuntu. No need to choose Linux as Thonny, the program used to programming the Pico is available for MacOS, Windows as well on the Raspberry Pi.
Why choose Thonny? 
I like the sight of it (!) and also it's the IDE used in the raspberry community and is standard on Raspberry Pi - the big big brother of the Pico.
No, you will not get a set of "bell and whistles", you have to keep track of things, but this will prevent you from getting over complicated!

### Installing Thonny
If you are using a Raspberry Pi it is already installed from the start.

For all other OS and to get the latest head over to www.thonny.org.
![](https://hackmd.io/_uploads/SycgaXQu2.png)
Choose your operating system and download, then follow instruction and you are set to "rock and roll"!

As stated above, Thonny is "complete" as downloaded to what we are to use it for. We will need to download the**.UF2** file for updating off the Pico and a library - but more on that later.

### Upload the Python Firmware (python interpretator) to the Pico
We need to update the firmware on the Pico as we are to use the micropython to program it. The micropython interpretator used on the Pico can be found at https://rpf.io/pico-w-firmware . Just click that link and the download starts automaticly. You will then have a file called **rp2-pico-w-20230426-v1.20.0.uf2** in your file manager (or alike with another date)

Please note that there are different versions available, and try to choose a stable version. At the writing of this report there are two stable “floatng” around, version 1.19… and version 1.20….

I have been testing both and selected version 1.20..., even though the simple test of the onboard LED did not work. More on that later.

### To Update
To update the Pico just press and hold the button marked “BOTSEL” on the Pico while you connect the USB cable. Then count to 3, release the button and you will find a new "storageplace" called RPI-RP2, an extra drive in the file manager of your computer.
![](https://hackmd.io/_uploads/S1lILVNO2.png)

Just copy the previously downloaded .UF2 file to that storage and after some seconds it will disappear and the Pico is restarting, now as a microcontroller and you are ready to start programming.
![](https://hackmd.io/_uploads/ByK9K4Nu2.png)

## Start the Project
We are now set to start the programming of our project and I propose the following way:
1. Connect all parts on the breadboard
1. Start Thonny and configure (set it up)
1. Start coding
    1. Connect and start reading data from the DHT11
    1.  Connect to WiFi
    1. Open an account on Adafruit IO
    1. Connect to Adafruit Io
    1. Set up a Dashboard in Adafruit IO
    1. Connect our feed to the dashboard and send data
    1. Look at the output and tap our shoulder


### 1.Connecting all Parts on the Breadboard

Insert your Pico to the breadboard. If you align it with the edge you can use the numbering of the breadboard, on the other hand you USB connector will not be protected from bending

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


| Pico     | DHT11    | What         |
| -------- | -------- | --------     |
|Pin 38    | GND      |Ground        | 
|Pin 36    |3,3 V out |Power to DHT11|
|Pin 34    |S on DHT11|Data signal   |

When connecting the DHT11 be sure to check configuration of connectors.

Of course you need to connect the USB cable between the Pico and your computer.
That concludes the connecting.

### (Electrical Calculations
This covered at the end under the heading "Some Electrical Calculations")

### 2.Start Thonny and Configure
Start Thonny and look in the lower right corner. It should read **MicroPython (Raspberry Pi Pico)**. If not click on the text and select it on the drop down list. (See below for picture)
### 3 Start coding
Ok, now enter the code.
#### Connect and Start Reading Data from the DHT11

```python=
from machine import Pin
import time
import dht

sensor = dht.DHT11(Pin(28))
r=1


while True:
    sensor.measure()
    print("Temperature: {} C Humidity: {} % Reading: {}".format(sensor.temperature(), sensor.humidity(),r))
    r = 1+r
    time.sleep(2)
```

Let us have quick glance of this master piece of code !

We need to import some libraries to be able to run
| Library  | To be able to                      | 
| -------- | --------                           |
| machine  |access the Pins of the Pico         |
|time      |use time function, in this case to have the program to wait 2 seconds making the DHT11 ready for the next read|
|dht       |access the DHT11's functions (temperature and humidity|


We tell the program on 
line 5 which Pin to look at for data
line 6 we set a "counter" called "r" to the value of "1"
line 9 we start a forever going loop
line 10 we read the values
line 11 we print the values. Here I have formatted the output
line 12 we increment the value of "r" one (add 1 to the value of r)
line 13 we wait for 2 seconds

After entering the code, click the green button in Thonny upper left corner to run the program. You will be asked where to save the program first. Chose "This computer" - you can save it to Pico later under a special name making it start automatic if you wish. (More on that later)

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

Let us look at the screen from Thonny. This is Thonny 3.3.14 running under Ubuntu 22.04.2 LTS, if you are running under a different OS it may look different.
#### 1
Green button starts the program entered in the area of "2"
Red button stops and a second click will restart the Pico (partly) or reconnect if you have disconnect the Pico
#### 2
Here you enter your code and this is the code which are running when pressing the green button 
#### 3
This is the output from the program
#### 4
This is a feature in Thonny. A graph of the output
#### 5 
By sliding the boarder to the left you open this graphing window
#### 6
This is the bottom left corner where you set the interpretator you will Thonny to use (see above)


We are reading the DHT11
#### Connect to the WiFi
Why WiFi?
Well, living in the almost middle of nowhere there are not any connection to the LoRaWAN. And also, electrokit do not have any more extra kits making me only having one LoRa transmitter and no receiver. In a later stage I will purchase LoRa WAN equipment making it possible to transmit and receive. I will comment on this later. So I have chosen to go via my WiFi and then connect to the internet etc.

I have searched for how to connect and ended up with this code. You may read about this under "Personal comment" at the end of this report.

In many descriptions you are supposed to have a separate file with you credentials. I have chosen not to because one more thing that can go wrong and why introduce that possibility? In the code I have blanked my credentials.

Here is the code used.
```python=
import network, utime
import socket

# Replace the following with your WIFI Credentials
SSID = "xx"
SSID_PASSWORD = "yy"


def do_connect():
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect(SSID, SSID_PASSWORD)
        while not sta_if.isconnected():
            print("Attempting to connect....")
            utime.sleep(1)
    print('Connected! Network config:', sta_if.ifconfig())
    
print("Connecting to your wifi...")
do_connect()


# This part shows how to use socket 

addr = socket.getaddrinfo('micropython.org', 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.1\r\nHost: micropython.org\r\n\r\n')
data = s.recv(1000)
print(data)
s.close()
```



Included are also how to connect a socket.

Note that the WiFi connection will "stay up" until you reboot the Pico. This makes it possible to add new parts of code and test possibilities and thereby know where the code "failed"

From Thonny we get the following:
![](https://hackmd.io/_uploads/Sk9j1tEu2.png)

##### 1
Here is the part where you fill your own credentials for the WiFi network
##### 2
Here we see that it has connected to the WiFi network and was given an IP address of 192.168.8.161. The rest of the text is information we got after we set up a socket and retrieved data from micropython.org .
##### 3
"Old data" will stay and will be "moved" left. New data enters from right but this program dopes not produce any outpu that Thonny will be able to make a graph on.

#### Open an Account on Adafruit IO
This is done by open a window in a browser pointing to www.adafruit.io

In the upper right corner you click on **Get started for free** and on the next page you fill in the requested data.

The main thing that you should make a note on is the following:
On the next page you will find **Active key**.  Save it for later, we need it in the code.

Now we need to create a "Feed"
![](https://hackmd.io/_uploads/SkyLdKE_n.png)

Click on **Feeds**
![](https://hackmd.io/_uploads/HynGYFVd3.png)


 1 Here your name will appear
 
 Click on **New Feed**
 
![](https://hackmd.io/_uploads/By9BYt4dn.png)
Here you create a new feed which is the name of your "datastream". 

Click **Create** after you have given it a name (and Description)

Then click on **Open the Feed**

On the next page you will find **Feed Info**. Click on that and on the next page you will find the **MQTT by Key**. Make a note on that value as well as we need it in the code.

If you copied the first value (Active Key) to the clipboard, copying this as well, will overwrite the first one so save both to a document!

In the same way as we created the feed create a Dashboard and open it.

There you click on **Create a new block** For this report we select **Line chart** which is outmost right on the second row.

Done that you need to connect your feed 
![](https://hackmd.io/_uploads/H1l2aFVuh.png)
You just tick on your feed
But to get a god dashboard you need to change the value of **Show History** to **Live(no history)**
![](https://hackmd.io/_uploads/r1mUJcVdn.png)

Now we have set up a dashboard and we leave it there for the next step.

### Connect our Feed to the Dashboard and send Data

It is here we need that library I mention before.

We are going to use a library called **umqtt.simple**.

In Thonny click on Tools and then Manage packages and search for **umqtt. simple**
![](https://hackmd.io/_uploads/rJ9bXANu2.png)

Chose **pycopy-umqtt.simple** and click install

To check that you have downloaded, check the files in Thonny. If you don't see any files in Thonny click View and select **Files**
![](https://hackmd.io/_uploads/HyzimRNu3.png)

Last thing to do:
Have some data and send it to the dashboard.
Looking at a straight line could be OK but I needed some data that changed without having to open the window (remember, we are monitoring the temperature of my room and that does not change drastic!)so I randomized a sinusvalue. (No worries, at the end I will collect all "corrected" code and we will look at a straight line!)

But first some code for publishing values

```python=
import network
import time
from math import sin
from umqtt.simple import MQTTClient


# Fill in your Adafruit IO Authentication and Feed MQTT Topic details
mqtt_host = "io.adafruit.com"
mqtt_username = "xx"  # Your Adafruit IO username
mqtt_password = "xx"  # Adafruit IO Key
mqtt_publish_topic = "ynnhoj/feeds/mydatafeed"  # The MQTT topic for your Adafruit IO Feed

# Enter a random ID for this MQTT Client
# It needs to be globally unique across all of Adafruit IO.
mqtt_client_id = "ynnhojdatafeed123"

# Initialize our MQTTClient and connect to the MQTT server
mqtt_client = MQTTClient(
        client_id=mqtt_client_id,
        server=mqtt_host,
        user=mqtt_username,
        password=mqtt_password)

mqtt_client.connect()

# Publish a data point to the Adafruit IO MQTT server every 3 seconds
# Note: Adafruit IO has rate limits in place, every 3 seconds is frequent
#  enough to see data in realtime without exceeding the rate limit.
counter = 0
try:
    while True:
        # Generate some dummy data that changes every loop
        sine = sin(counter)
        counter += .8
        
        # Publish the data to the topic!
        print(f'Publish {sine:.2f}')
        mqtt_client.publish(mqtt_publish_topic, str(sine))
        
        # Delay a bit to avoid hitting the rate limit
        time.sleep(3)
except Exception as e:
    print(f'Failed to publish message: {e}')
finally:
    mqtt_client.disconnect()
```

Some "highlights" in the code:
Line 3: Just to be able to get a sinus data to have something to show. Used in lines 33 and 34
Line 8-11 are the credentials you need to connect to Adafruit. We got them when we set up the account, feed and dashboard
Line 38: This is the line doing the work!
Line 41 Adafruit has a time limit but every 3 second is OK.

### Look at the Output and tap our Shoulder
And this is the Thonny:
![](https://hackmd.io/_uploads/ByaJiCVOn.png)

You see the graph at the bottom. This graph you will see in the Adafruit as well.

![](https://hackmd.io/_uploads/rylEj0Vu2.png)
But of course, I have added 2 more "displays"
* a gauge
* a list of the values


For those with eyesight will notice the value **0.91** in Thonny REPL as well as graph and in the graph, gauge and listing on Adafruit.

## Final Adjustment
I will remove the random generator and insert the DHT11 code to obtain temperature readings which then are published to Adafruit every 15 min as there is no need to know temperature changes on a shorter inter-wall.

If needed for "autostart", meaning you power the Pico with a power supply instead of feeding it from your computer you can download the part that connects to the WiFi as **boot.py** and the rest as **main.py**.

The Pico will then connect at bootup to the WiFi and then start reading the sensor and publish the readings.


# Platform, General Comments etc
With the risk of repeating me, I here summarize the project:
I read a temperature value every 15 min and that is sent to a Adafruit dashboard. (using socket and MQTT)
It was planed to be collected and made some calculation on the data, but as the connection (LoRa) not was at hand, I have made it stored on the Pico as a .csv file which can be downloaded and imported to a XL sheet, an Access database or what ever suitable. The calculation part is not finalized even thou I know the arithmetic.

Due to limitations in HackMD the picture of the complete set up is not what I wanted, but here is the breadboard with the Pico and DHT11
![](https://hackmd.io/_uploads/HJfyppHu2.png)

## Private Notes/Comments

### My Intentions - Goals

My intention when learned about this course was high, and I was aiming at a full blown farming management system, calculating when to harvest the hay is due, amount of rain, wind, sunshine, earth moisture and temperature and more. It should also be an off the grid solution, not using the internet and the electrical mains as I want to be in full control.

I was a bit disappointed when only one LoRa transmitter/receiver was in the kit and set back when I noticed that electrokit did not have any more. But, that which not kill you, will just make you stronger, but not here in this time frame. I do have the farm as well to attend to.

But, I have learned that no such thing as impossible exists and I will continue with expanding it to a (almost) full blown farming management system.


## Personal Comment when steam is coming out!
In this section I have put some notes about "experiences" while doing my project and report writing. It is my **personal opinion**, written down when having steam coming out of my ears!

#### LED or Pin 25?
If you are reading about how to flush/download the Pico you will find a note of how to test your installation using the onboard LED

In most instruction you are now instructed to enter some code and upload that to the Pico and run it and the onboard LED should blink! It will probably not blink! Don’t worry, it is just that this LED was connected to a GPIO pin on the first version of the Pico (that without the “W”) and at those we call it Pin25. In the new version of Pico W that was not any longer Pin25 because that was used for something else and now you address the onboard LED it with the word “LED”. Unfortunate, everyone who is programming the interpretators do not know this! So in some circumstances it works, in some don’t. If you have a Pico (without W) Pin25 works ! The only drawback when using a Pico**W** is that the onboard LED do not blink when instructed to in some example – if you need a blinking LED set one outside and connect it to another Pin and change the code accordingly!

#### Connecting to WiFi
There are many ways to connect code wise. I have tried some and what will happen is a message telling me it is trying. That will make no one happy. I do not know why the code works when doing a definition and then run that. When testing the code without "definition" it does not. That is why i have coded as shown above.

# "Sidesteps"
As you have noticed, I have build my projects in "steps" making it possible to stay on top of what is happening. Then I have made some sidesteps which I will share here. (Don't expect earth braking news but maybe you can get some ideas).

## Save to File
Having a lot of readings popping by in a window (the REPL) is fun but even better if they are stored. Imaging this: You have a IoT device sending you data but suddenly the connection is lost. Saving the data to the Pico will give you a way to retrieve them later. 
And also, as long as you are connected to the WiFi you got the time from the net and the RTC (Real Time Clock) onboard the Pico will hold it. Then if you time stamp as well and you are "home"
This code will do just that.

```python=
from machine import Pin
import machine
import utime
import dht

sensor = dht.DHT11(Pin(28))
rtc=machine.RTC()
timestamp=rtc.datetime()
timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] + timestamp[4:7])

r=1

file = open("add_2", "a")
file.write(" Date,                Temp, Hum, Run" +"\n")
while True:
    sensor.measure()
    timestamp=rtc.datetime()
    timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] + timestamp[4:7])
    print(" {} ,{},{},{}".format(timestring, sensor.temperature(), sensor.humidity(),r,))
    file.write(" {} ,{},{},{}".format(timestring, sensor.temperature(), sensor.humidity(),r,) + "\n")
    file.flush()
    r = 1+r
    utime.sleep(2)
    
```
And the file output looks like this.
 Date,                Temp, Hum, Run
 2023-06-25 12:26:14 ,26,34,1
 2023-06-25 12:26:17 ,26,35,2
 2023-06-25 12:26:19 ,26,34,3
 2023-06-25 12:26:21 ,26,34,4
 2023-06-25 12:26:24 ,26,35,5
   (The headlines are spaced but not in HackMD!)

You can download the file from Thonny by right click on it in the file Window.ansökan
If you will be really sure about the timestamps there are Real Time Clocks you can connect.

## Broker
To full fill my grand plan I have now found out that I need a Broker. So just to try it out I installed the Mosquitto broker on a Raspberry Pi 4.

I followed the RANDOM NERD TUTORIAL: "Install Mosquitto MQTT Broker on Raspberry Pi". I opted for this instruction as the course instruction was describing installation under Centos and thereby running on a "big power hungry" computer( Remember I want it to be of the grid). You will find that tutorial here:
https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/#mosquitto-no-authentication.
(Rui, the creator follow up with how to install Nod-Red and how to publish data on dashboard)
Here are some pictures. (Sorry about the quality, but they are taken with my phone, sent to my email, displayed and the taken a screendump. Then we are under the 1MB limit of HackMD!)
![](https://hackmd.io/_uploads/rJGY1Ardh.png)
Close up to se message sent:
![](https://hackmd.io/_uploads/HJANgRru3.png)
and received
![](https://hackmd.io/_uploads/SJ28gRSOh.png)



## Some Electrical Calculations
Having the Pico running consumes according to www.peppe80.com/raspberry-pi-pico-power-consumption 60mA with WiFi connected and answering remote ping requests.
The DHT11 uses 2,5 mA according to electrokit.com
This gives us 60+2,5=62,5 mA.

My plan was to use a solar cell lighting led lamp from Jula. Ripping out the LEDs and using the electronics and the solar cell to charge the battery. According to the box it is capable of 2400mAh at 3.7V.
You can feed the Pico with 1.8-5.5ansökanV so 3.7 is within the range.
2400 / 62,5 will give us 38 hours meaning it will last over night when the sun is not shining.
![](https://hackmd.io/_uploads/SyuOw2Bu2.png)

Besides that, you can put the Pico in a sleepmode and then the battery will last longer.

## Useful Links
Beside all the documentation on the course pages I have used:

For the Adafruit connection:
Core electronics
https://www.youtube.com/watch?v=ybCMXqsQyDw&t=97s and in witten format: https://core-electronics.com.au/guides/getting-started-with-mqtt-on-raspberry-pi-pico-w-connect-to-the-internet-of-things/

For the Mosquitto set up: 
RANDOM NERD TUTORIALSansökan
https://randomnerdtutorials.com/how-to-install-mosquitto-broker-on-raspberry-pi/#mosquitto-no-authentication

For electric calculations:
peppe80
www.peppe80.com/raspberry-pi-pico-power-consumption
Raspberry documention
raspberrypi.com
Lamp specification
https://www.jula.se/catalog/el-och-belysning/armaturer/utomhusbelysning/solcellslampor/solcellsbelysning-led-018354/


# Final Word
I have now used about 24000 characters of the 25000 allowed.
So it's time to stop and **thanks for reading**.

See you on another field!















