Projects / Pico WH comfort page

Pico WH comfort page

Beginner2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. PICO

    Wire the sensor

    Pin 1 to 3V3(OUT), pin 2 to GP15, pin 4 to GND.

  2. PICO

    Add the pull-up

    10 kilo-ohms between pin 2 and 3V3(OUT).

  3. PICO

    Edit the program

    Put your Wi-Fi name and password in the placeholders.

  4. PICO

    Run it

    The shell prints an address like http://192.168.1.50.

  5. PICO

    Open the address

    On a phone or computer on the same network.

  6. PICO

    Breathe on the sensor

    After the next refresh the humidity is higher.

  7. PICO

    Save as main.py

    So it starts on power-up.

In short

How do you build a pico WH comfort page?

A Pico WH reads a DHT22 and serves temperature, humidity and a comfort word on a local web page. You need Raspberry Pi Pico WH, DHT22 Sensor, Resistor 10 kΩ and Breadboard 830 Tie Points. It takes 7 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
DHT22 pin 1 (VCC)Pico WH 3V3(OUT) pin 36Power at 3.3 V
DHT22 pin 2 (data)Pico WH GP15 pin 20One-wire style data
DHT22 pin 4 (GND)Pico WH GND pin 38Common ground
10 kilo-ohm resistorBetween DHT22 data and 3V3(OUT)Pull-up for the data line

Code

The DHT22 is a low-cost temperature and humidity sensor that sends its reading as a train of timed pulses on one wire. MicroPython has a dht module that decodes them, so the program only has to call measure and then read the temperature and humidity. The pull-up resistor holds the data line high between readings. The sensor is slow and must be left for two seconds between readings, so the program reads it no more than once every three seconds, whenever the page is opened. A single number for humidity does not say how a room feels, so the program works out the dew point, which is the temperature at which the water in the air would start to condense, and turns it into a word: dry, comfortable, humid, muggy or oppressive. The Pico WH has the header pins soldered on and is otherwise the same as a Pico W. The page is for your home network only, so do not forward a router port to it. The Wi-Fi name and password are placeholders; use your own and keep them out of anything you share.

import time
import math
import network
import socket
import dht
from machine import Pin

SSID = "YOUR_WIFI_NAME"                             # placeholders: use your own network
PASSWORD = "YOUR_WIFI_PASSWORD"

sensor = dht.DHT22(Pin(15))

def dew_point(t, rh):
    gamma = math.log(rh / 100.0) + (17.62 * t) / (243.12 + t)
    return 243.12 * gamma / (17.62 - gamma)

def comfort(dew):
    if dew < 10:
        return "Dry"
    if dew < 16:
        return "Comfortable"
    if dew < 18:
        return "Humid"
    if dew < 21:
        return "Muggy"
    return "Oppressive"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(30):
    if wlan.isconnected():
        break
    time.sleep(0.5)
if not wlan.isconnected():
    raise RuntimeError("Wi-Fi did not connect, check the name and password")
print("Open http://" + wlan.ifconfig()[0])

server = socket.socket()
server.bind(("0.0.0.0", 80))
server.listen(1)

temp = 0.0
hum = 0.0
last_read = time.ticks_add(time.ticks_ms(), -5000)

while True:
    client, addr = server.accept()
    client.recv(1024)
    if time.ticks_diff(time.ticks_ms(), last_read) >= 3000:   # the DHT22 needs 2 seconds between reads
        try:
            sensor.measure()
            temp = sensor.temperature()
            hum = sensor.humidity()
            last_read = time.ticks_ms()
        except OSError:
            pass
    word = comfort(dew_point(temp, hum)) if hum > 0 else "Reading..."
    page = ("<!DOCTYPE html><html><head><meta charset='utf-8'>"
            "<meta name='viewport' content='width=device-width, initial-scale=1'>"
            "<meta http-equiv='refresh' content='30'><title>Room comfort</title></head>"
            "<body style='font-family:sans-serif;text-align:center;margin-top:3em'>"
            "<h1>" + word + "</h1><p style='font-size:3em'>%.1f &deg;C</p>"
            "<p style='font-size:3em'>%.0f %% humidity</p></body></html>" % (temp, hum))
    client.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
    client.send(page)
    client.close()

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Open the pageTemperature, humidity and a word
Wait thirty secondsThe page reloads by itself
Breathe on the sensorHumidity rises
Warm room in the eveningComfortable or Humid
Unplug and plug inThe page works again at the same address

If it does not work

Page shows zero

Check the data wire on GP15 and the pull-up resistor.

OSError

Reading too often, or a loose wire, wait and try again.

Wi-Fi does not connect

Check the name and password, and that the network is 2.4 GHz.

Page does not open

Use the exact address from the shell and the same Wi-Fi.

Temperature looks high

Keep the sensor away from the Pico and from heat sources.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the pico WH comfort page?

Raspberry Pi Pico WH, DHT22 Sensor, Resistor 10 kΩ and Breadboard 830 Tie Points.

How long does the pico WH comfort page take to build?

About 120 minutes for a beginner maker, in 7 steps.

How do I know the pico WH comfort page works?

Open the page: Temperature, humidity and a word. Wait thirty seconds: The page reloads by itself. Breathe on the sensor: Humidity rises.

Page shows zero: what should I check?

Check the data wire on GP15 and the pull-up resistor.

OSError: what should I check?

Reading too often, or a loose wire, wait and try again.

Wi-Fi does not connect: what should I check?

Check the name and password, and that the network is 2.4 GHz.

Page does not open: what should I check?

Use the exact address from the shell and the same Wi-Fi.