-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpico-dht.py
45 lines (35 loc) · 1005 Bytes
/
pico-dht.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import machine
import urequests
import network, time
import dht
HTTP_HEADERS = {'Content-Type': 'application/json'}
ssid = 'ssid'
password = 'password'
# connect to wifi
sta_if=network.WLAN(network.STA_IF)
sta_if.active(True)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
sensor_temp = machine.ADC(4)
sensor = dht.DHT22(machine.Pin(0))
# change on each sensor
sensorid = "room name"
while True:
time.sleep(5)
try:
sensor.measure()
temp = sensor.temperature()
hum = sensor.humidity()
except:
print("error: sensor not found")
continue
dht_readings = {'room': sensorid, 'temperature':temp, 'humidity': hum}
try:
request = urequests.post( '/upload.php', json = dht_readings, headers = HTTP_HEADERS )
request.close()
except:
print ("error: upload failed - offline ?")
time.sleep(300)