`Author: Gin Lindelöw [ gl222pg ]`
In this tutorial we will build a simple temperature and humidity monitor running on a Raspberry Pi Pico WH.
We will be using [MicroPython](https://micropython.org/) for the code and [AdaFruit IO](https://io.adafruit.com/) for displaying the collected data.
Time to complete: ~2 hours. (following the tutorial, given that you don't run into any issues)
## Objective
I chose this project to be able to monitor my new apartment since it's awfully warm a large part of the day. I was interested to see just how the temperature and humidity changes over time as well as having a quick way to check the current values of these even on the go.
With this info I could see how effective different techniques to cool down the appartment as well.
## Material
### What you will need:
|__Raspberry Pi Pico WH (with headers)__|
| -------- |
This is the main device that runs the code as well as sends and receives the data.
 Fig 1. Raspberry Pi Pico WH with headers.
|__USB-Cable (A-Male to B-Male)__|
| -------- |
For powering the Pico WH. `Keep in mind it's important to have a cable with data transfer capabilities.`

Fig 2. A-Male to B-Male USB cable.
|__Breadboard__|
| -------- |
Used to connect the components together.

Fig 3. Breadboard
|__DHT11__|
| -------- |
Temperature and humidity sensor.

Fig 4. DHT11 Temerature and humidity sensor.
|__Jumper Wires__|
| -------- |
At least 3 Male-to-Male wires needed to connect the components on the breadboard.

Fig 5. Male-to-Male Jumper Wires.
These parts were bought as a part of a [larger kit](https://www.electrokit.com/produkt/start-kit-applied-iot-at-linnaeus-university-2023/) from [electrokit.com](https://www.electrokit.com) for 399 sek. You could also buy these individually from there.
## Computer setup
In this tutorial we will use the text editor VSCode and the extension PyMaker to streamline getting our code onto the Pico WH.These tools are available on Windows, Linux and MacOS.
Fist of all we will need to install Python, available here: https://www.python.org/downloads/
VSCode is available here: https://code.visualstudio.com/download
To use PyMaker we first need to install Node.JS onto our computer. You can find a download link here: https://nodejs.org/en/download
After that is installed you can go to the extensions tab in VSCode and search for PyMaker, select the entry with the same name and press install.

Fig 6. The PyMakr extension.
Once installed we are almost ready to start coding.
But before we can run any of our own MicroPython code we need to flash the board with UF2 file to get it ready.
For the Pico WH we need the latest release from here: https://micropython.org/download/rp2-pico-w/
To flash the board, hold down the white button on the Pico WH and plug in the usb cord in the computer.
When doing this the Pico will connect in storage mode and we can place the UF2 file we downloaded onto the device. It will then automatically install the file and restart, ready to run any MicroPython code we write.
The device should be automatically detected by PyMakr in VSCode and selectable when we now create a new PyMakr project from the PyMakr tab on the side of VSCode.
## Putting everything together
With the use of a breadboard assembly will be a lot easier than otherwise. Place your Pico WH on the top of the board as in the picture bellow, making sure that all pins are in their respective holes as to not accidentally bend them, then give it a firm push and it will slot into the board.
After this, choose a set of pins for the DHT11 sensor. After inserting the sensor, take Your jumper cables and connect to pin 1, 2 and 4 as these are the ones we need. As is shown in the picture bellow, Pin 1 should connect to power, pin 4 to ground and lastly pin 2 to a GPIO pin on the Pico WH, I have chosen GPIO 15.

Fig 7. Circuit diagram.
## Platform
As a hosting platform I have chosen AdaFruit IO. This is a cloud based platform that is quite popular and good for smaller projects as you can get up to two devices, 10 data feeds and can recieve data 30 times per minute (or once per second) for free, which is what I prioratised for the project.
They also supply their own MQTT Broker so you don't have to host one yourself and on top of this a customizable dashboard to display your data.
If you in the future wanted to scale you also do have the options of their quite cheap paid tier.
## The code
The overall code is quite simple and is all available on this GitHub repository: https://github.com/lindelovv/IoT-TempHumidMessure.
We first connect the Pico WH to the wifi network. I have here put my wifi credentials in a seperate file not shared publicly.
```python=1
def connect_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(private.NEWWORK_SSID, private.NETWORK_PASSWORD)
while not wifi.isconnected():
print('Waiting for connection...')
time.sleep(1)
print("Connected to WIFI: " + private.NEWWORK_SSID + " (" + str(wifi.ifconfig()[0]) + ")")
return wifi.ifconfig()[0]
```
We then construct our MQTTClient. To use the umqtt MicroPython library, download umqtt.simple from [here](https://pypi.org/project/micropython-umqtt.simple/#files) and put the simple.py (which I renamed to umqtt.py) it in a folder called 'lib' in the project directory.
```python=1
from lib.umqtt import MQTTClient
#...
def connect_mqtt():
print("Attempting to connect to MQTT Broker")
client = MQTTClient(
client_id = binascii.hexlify(unique_id()),
server = "io.adafruit.com",
port = 1883,
user = private.ADAFRUIT_USERNAME,
password = private.ADAFRUIT_PASSWORD,
keepalive = 60
)
try:
client.connect()
print("MQTT connected successfully.")
return client
except Exception as e:
print("Error: Could not connect to MQTT Broker; {}{}".format(type(e).__name__, e))
reset()
```
After we connect to the AdaFruit IO MQTT Broker weare ready to publish our sensor readings to their respective feeds. We then do this every 60 seconds.
```python=1
dht11 = dht.DHT11(Pin(15, Pin.IN, Pin.PULL_UP))
def get_readings():
dht11.measure()
return (dht11.temperature(), dht11.humidity())
#...
while True:
temperature, humidity = get_readings()
if temperature is None:
print("Error: No temperature output to measure.")
else:
print("\tTemperature:\t" + str(temperature) + " C\n\tHumidity:\t" + str(humidity) + " %")
mqtt_client.publish( private.TEMPERATURE_FEED, str(temperature).encode() )
mqtt_client.publish( private.HUMIDITY_FEED, str(humidity).encode() )
time.sleep(60)
```
## Transmitting the data / connectivity
The data is sent once every minute directly through the AdaFruit IO MQTT Broker to both the created 'temperature' and 'humidity' feeds.
The device is set up through WiFi ad it is included functionality in the Pico WH.
## Presenting the data
The dashboard is built with AdaFruit IO's gui builder. We can there choose between a few preset ways to display the data from the feeds we have set up.

Fig 8. AdaFruit IO Dashboard
Data is saved every minute when we publish the data from the Pico WH through the MQTT Broker.
## Finalizing the design
This is the final results of the project! I think it went quite well but with some minor issues along the way. I really learned a lot as every part of the course was completly new to me.

Fig 9. The project assembeled.
If I were to redo the project I would first of all look through more alternatives for a dashboard service.
I was also tempted to try to write the code in C and compile that directly into UF2 as that would be even more fun, but as it's the first time interfacing with microcontrollers (and hardware overall) I didn't want to accidentally break something first try.