Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThis script reads a DHT22 once a minute and sends a Telegram message through the bot API when the temperature goes above 35 degrees, then again when it falls back to normal. It uses the requests library and a bot you create with BotFather. The token and chat ID are kept in a private file outside the script. Useful for a server cupboard, a workshop or a plant room.
Tick anything you already have and the total updates.
microSD card for the operating system
Follow the colours: red is power, dark grey is ground, blue is a signal.
Official Pi 4 power supply.
heat_alert.pyimport os, time
import requests, adafruit_dht, board
with open(os.path.expanduser("~/.tgcfg")) as f:
TOKEN, CHAT_ID = [l.strip() for l in f.readlines()[:2]]
LIMIT_C = 35.0
dht = adafruit_dht.DHT22(board.D4)
alerting = False
def send(text):
try:
requests.post("https://api.telegram.org/bot%s/sendMessage" % TOKEN,
data={"chat_id": CHAT_ID, "text": text}, timeout=10)
except requests.RequestException as e:
print("Telegram failed:", e)
while True:
try:
t, h = dht.temperature, dht.humidity
if t is not None:
if t > LIMIT_C and not alerting:
alerting = True
send("Too hot: %.1f C (%.0f%% humidity)" % (t, h))
elif t < LIMIT_C - 2 and alerting:
alerting = False
send("Back to normal: %.1f C" % t)
except RuntimeError:
pass # DHT22 misses a read sometimes; try again next minute
time.sleep(60)
Press Start in your bot's chat, and check the token and chat ID in ~/.tgcfg.
The DHT22 sometimes misses a read; the script retries each minute.
The alerting flag sends only one alert per hot period.
Better than in code. Keep it out of shared folders.
Yes, with a getUpdates loop that answers a /temp command.
Yes, with the same wiring and updated libraries.
6 parts: Raspberry Pi 4 Model B 2GB RAM Single Board Computer, Official Raspberry Pi 4 USB-C Power Supply – 15.3W, Official Raspberry Pi 32GB A2 Class Micro SD Card, DHT22 AM2302 Digital Temperature & Humidity Sensor, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (heat_alert.py).
About ₹8,593 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Calling the Telegram bot API with requests; Alerting on a threshold and on recovery; Keeping tokens out of code.
Ideas that reuse this build: Raspberry Pi DHT22 Flask dashboard. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.