-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdht_sensor.py
More file actions
40 lines (30 loc) · 945 Bytes
/
Copy pathdht_sensor.py
File metadata and controls
40 lines (30 loc) · 945 Bytes
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
import requests
import adafruit_dht
import board
import json
import time
BLYNK_TOKEN = "W_oWJ1QxAj7f_u-BDfvAynUFtdlkiK5z"
dht = adafruit_dht.DHT11(board.D5)
def send(pin, value):
url = f"https://blynk.cloud/external/api/update?token={BLYNK_TOKEN}&{pin}={value}"
try:
r = requests.get(url, timeout=5)
print("Sent:", pin, value, "| Response:", r.status_code)
except Exception as e:
print("Error:", e)
while True:
try:
temperature = dht.temperature
humidity = dht.humidity
if temperature is not None and humidity is not None:
data = {
"temperature": temperature,
"humidity": humidity,
"timestamp": time.time()
}
print(json.dumps(data))
send("v0", temperature)
send("v1", humidity)
except RuntimeError:
print("Read failed, retrying...")
time.sleep(2)