-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempAndHumidityRead.py
67 lines (53 loc) · 1.68 KB
/
tempAndHumidityRead.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import time
import board
import adafruit_dht
from datetime import datetime, timedelta
import requests
#Connect power to pin 2, ground to pin 6, and data to pin 7 /GPIO4
#Initial the dht device, with data pin connected to:
dhtDevice = adafruit_dht.DHT11(board.D4)
time.sleep(2)
#Server info, change IP address of serverURL to the IP where the server is running
#serverURL = "http://localhost:8081/postTemp"
serverURL = "http://192.168.1.115:8081/postTemp"
dataJSON = "";
while True:
try :
errorStatus =0
#Collect data from connected DHT11 sensor
temperature_c = dhtDevice.temperature
temperature_f = temperature_c * (9 / 5) + 32
humidity = dhtDevice.humidity
currDateTime = datetime.now()
dataDateTime = currDateTime.strftime("%Y-%m-%d %H:%M:%S") #dateTime format
print("Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format(temperature_f, temperature_c, humidity))
dataJSON = {
"DateTime": dataDateTime,
"Temp_C": temperature_c,
"Temp_F": temperature_f,
"Humidity": humidity
}
print(dataJSON)
except RuntimeError as error:
errorStatus =1
print(error.args[0])
time.sleep(2)
# Time intervals between samples, uncomment to switch
#waitTime =3600 # 1 hour
#waitTime =1800 # 30 minutes
#waitTime =900 # 15 minutes
waitTime = 300 # 5 minutes
if errorStatus ==0:
try:
response = requests.post(serverURL, json=dataJSON) #POST request to server
if response.status_code // 100 == 2:
print("POST successful")
else:
print("POST failed")
waitTime = 5; #retry
except requests.RequestException as e:
print(f"ERROR making the request: {e}")
waitTime = 5; #retry
time.sleep(waitTime)
else:
time.sleep(5)