Created
March 2, 2025 09:41
-
-
Save rgee0/b71070d6b4b675f342a5ce7842552de4 to your computer and use it in GitHub Desktop.
pico pi code for a DHT22 sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from machine import Pin | |
from dht import DHT22 | |
import urequests as requests | |
import network | |
import ujson | |
import utime | |
import gc | |
SSID='' | |
WIFI_PASS='' | |
HEADERS = { | |
"Content-type": "application/json", | |
"User-agent": "pico-dht22/conservatory" | |
} | |
POST_URL='https://putsreq.com/whatever' | |
INTERVAL=10 | |
DATA_PIN=22 | |
def connect_wifi(): | |
led = Pin("LED", Pin.OUT) | |
wifi = network.WLAN(network.STA_IF) | |
if not wifi.active(): | |
wifi.active(True) | |
if not wifi.isconnected(): | |
led.off() | |
wifi.connect(SSID, WIFI_PASS) | |
while not wifi.isconnected(): | |
pass | |
led.on() | |
def measure_send(): | |
sensor.measure() | |
data = { | |
"time": utime.time(), | |
"celsius": sensor.temperature(), | |
"humidity": sensor.humidity() | |
} | |
requests.post(POST_URL, data=ujson.dumps(data), headers=HEADERS).close() | |
led = Pin("LED", Pin.OUT) | |
sensor = DHT22(Pin(DATA_PIN)) | |
while True: | |
gc.collect() | |
connect_wifi() | |
try: | |
measure_send() | |
except: | |
pass | |
utime.sleep(INTERVAL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment