-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLab 12 Favoriot HTTPS.py
90 lines (68 loc) · 2.21 KB
/
Lab 12 Favoriot HTTPS.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import os
import time
import board
import ssl
import wifi
import socketpool
import adafruit_requests as requests
import adafruit_dht
import favoriot_ca
dht11 = adafruit_dht.DHT11(board.GP15)
ssid = os.getenv("WIFI_SSID")
password = os.getenv("WIFI_PASSWORD")
print("""
______ _ __
/ ____/___ __ ______ _____(_)___ / /_
/ /_ / __ `/ | / / __ \/ ___/ / __ \/ __/
/ __/ / /_/ /| |/ / /_/ / / / / /_/ / /_
/_/ \__,_/ |___/\____/_/ /_/\____/\__/ v3.2.0""" + " (Microcontroller: " + os.uname()[0] + ")\n")
print("Connecting to '" + ssid + "' ... ", end="")
wifi.radio.connect(ssid, password)
print("connected.")
print()
pool = socketpool.SocketPool(wifi.radio)
context = ssl.create_default_context()
context.load_verify_locations(cadata=favoriot_ca.cert)
https = requests.Session(pool, context)
now = time.monotonic()
while True:
try:
temperature = dht11.temperature
humidity = dht11.humidity
print("Temperature: {} °C, Humidity: {} %RH ".format(temperature, humidity))
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dht11.exit()
raise error
time.sleep(2)
if (now + 15) < time.monotonic():
now = time.monotonic()
headers = {
"content-type": "application/json",
"apikey": os.getenv('FAVORIOT_DEVICE_ACCESS_TOKEN')
}
json = {
"device_developer_id": os.getenv('FAVORIOT_DEVICE_DEVELOPER_ID'),
"data": {
"temperature": temperature,
"humidity": humidity
}
}
request = https.request(
os.getenv("FAVORIOT_HTTPS_METHOD"),
os.getenv("FAVORIOT_HTTPS_API"),
timeout = 5,
headers = headers,
json = json
)
response = request.json()
print("Favoriot HTTPS Request: ", end="")
if response["statusCode"] == 201:
print("Success: " + response["message"])
else:
print("Error: " + response["message"])
request.close()
print()