Building a Smart Home Temperature and Humidity Monitor (with online dashboard) | Home Assistant | PiShop | Smart Furniture
One of the standout projects we showcased at Hobby-X this year was a DIY smart thermostat powered by Home Assistant, a Raspberry Pi Pico WH, and a DHT22 temperature and humidity sensor. As promised in our Hobby-X Projects article, we’re now sharing the full setup guide—so you can build your own smart-home climate monitor just in time for the winter chill!
This project uses a Raspberry Pi Pico WH connected to a DHT22 sensor to monitor temperature and humidity, then wirelessly uploads that data to Home Assistant, a popular open-source smart home platform. The Pico WH’s onboard Wi-Fi makes it a low-cost, compact, and efficient solution for real-time environmental monitoring.
By monitoring your indoor climate, you can make smarter decisions about when to adjust the thermostat, open a window, or run a humidifier. Unlike off-the-shelf solutions, this DIY setup gives you full control, local data ownership, and the satisfaction of building it yourself, at a fraction of the cost of commercial sensors.
General Use cases:
- Smart Thermostat Integration: Automatically adjust heating or cooling based on room conditions.
- Greenhouse Monitoring: Keep track of temperature and humidity levels to maintain optimal growing conditions.
- Server Room Alerts: Set up Home Assistant automations to alert you if your hardware environment gets too hot or humid.
- Basement or Attic Tracking: Monitor less-visible areas of your home for moisture or extreme temperature fluctuations.
The base components of this project are the following:
- DHT22 Temperature And Humidity Sensor Module – This digital temperature and humidity module is a digital output signal containing a calibrated temperature and humidity combined sensor.
- Raspberry Pi Pico GPIO Expansion Board female headers and screw terminals
- Raspberry Pi Pico WH with pre-soldered headers
Make sure to read the comments in the script below to get a good understanding of how we handled this project!
main.py
import network
import secrets
import socket
from time import sleep
import dht
from machine import Pin
import json
intled = machine.Pin("LED", machine.Pin.OUT)
sensor = dht.DHT22(Pin(2))
bat1 = machine.ADC(26)
#Indicate that the app started
intled.value(1)
sleep(3)
intled.value(0)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.config(pm = 0xa11140) # Prevent the wireless chip from activating power-saving mode when it is idle
wlan.ifconfig(('10.0.0.93', '255.255.255.0', '10.0.0.2', '8.8.8.8')) # Sets a static ip for this Pico, change this to your network settings!
wlan.connect(secrets.SSID, secrets.PASSWORD) # Connect to your AP using the login details received from secrets.py
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
intled.value(1)
break
max_wait -= 1
intled.value(1)
print('waiting for connection...')
sleep(1)
intled.value(0)
sleep(1)
if wlan.status() != 3:
y = 0
for y in range(0, 4):
intled.value(1)
sleep(0.25)
intled.value(0)
sleep(0.25)
y += 1
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(1)
print('listening on', addr)
# Listen for connections
while True:
if wlan.status() == 3: # If pico is connected to wifi put the onboard LED on else off
intled.value(1)
else:
break
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(1024)
request = str(request)
print(request)
# Get the data from the DHT11 sensor
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
print("Temperature: {}°C Humidity: {:.0f}% ".format(temperature, humidity))
# Get the data from voltage sensor
batvolt = (bat1.read_u16()/4000)
print("Battery : " + str(batvolt))
# Prepare the data to send to Home Assistant as type Json
data = { "hum": humidity, "temp": temperature, "volts": batvolt }
JsonData = json.dumps(data)
# Send headers notifying the receiver that the data is of type Json for application consumption
cl.send('HTTP/1.0 200 OK\r\nContent-type: application/json\r\n\r\n')
# Send the Json data
cl.send(JsonData)
# Close the connection
cl.close()
except OSError as e:
cl.close()
print('connection closed')
secret.py
SSID = "****(Wifi name)"
PASSWORD = "****(Wifi password)"
Tips & Tricks
1. Use Deep Sleep to Save Power
If your sensor doesn’t need to be “always on,” you can dramatically extend battery life or power usage by using deep sleep cycles. Wake up every 5–10 minutes, take a reading, transmit data, then go back to sleep. This is perfect for remote or battery-powered use.
2. Add a Simple Web Interface
Include a basic HTML page in your Pico server that displays the readings for quick local viewing on a browser without needing Home Assistant. Great for debugging or sharing access with others.
3. Store Historical Data in Home Assistant
Enable long-term statistics in Home Assistant so you can visualize trends over time (like how your heating system performs overnight or if a window is affecting humidity).
4. Use Home Assistant Automations
Once the data is live in Home Assistant, unlock powerful automations:
- Turn on a dehumidifier if humidity > 70%
- Flash a smart light red if temperature drops below 15°C
- Send an alert if the battery voltage drops below a safe threshold
5. I2C OLED Display (e.g., SSD1306)
Display temperature, humidity, and battery voltage locally on a tiny OLED screen. It’s a great touch for workshops or plant monitoring. Or use a BME280 to get an added air quality data set.
With just a few components and a bit of code, you’ve now got a powerful, customizable smart climate sensor that integrates seamlessly with Home Assistant. Whether you’re tracking conditions in your living room, greenhouse, or server cabinet, this project is a fantastic stepping stone into the world of DIY home automation.
If you enjoyed this build, be sure to check out the rest of our blog for more hands-on projects, smart home tips, and maker tutorials. Don’t forget to follow us on social media for the latest updates, behind-the-scenes content from events like Hobby-X, and sneak peeks at what we’re building next!
