-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLab Extra OLED Display SSD1306 DHT11 Favoriot MQTTS Publish.py
More file actions
118 lines (90 loc) · 3.28 KB
/
Lab Extra OLED Display SSD1306 DHT11 Favoriot MQTTS Publish.py
File metadata and controls
118 lines (90 loc) · 3.28 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import ssl
import time
import wifi
import json
import mqtt
import board
import busio
import displayio
import socketpool
import terminalio
import adafruit_display_text.label as label
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import adafruit_displayio_ssd1306
import adafruit_dht
import favoriot_ca
displayio.release_displays()
i2c_oled = busio.I2C(scl=board.GP5, sda=board.GP4)
oled_bus = displayio.I2CDisplay(i2c_oled, device_address=0x3C)
oled = adafruit_displayio_ssd1306.SSD1306(oled_bus, width=128, height=64)
dht11 = adafruit_dht.DHT11(board.GP15)
now = time.monotonic()
ssid = os.getenv("WIFI_SSID")
password = os.getenv("WIFI_PASSWORD")
print('''
______ _ __
/ ____/___ __ ______ _____(_)___ / /_
/ /_ / __ `/ | / / __ \/ ___/ / __ \/ __/
/ __/ / /_/ /| |/ / /_/ / / / / /_/ / /_
/_/ \__,_/ |___/\____/_/ /_/\____/\__/ v3.0.0''' + ' (Microcontroller: ' + os.uname()[0] + ')\n')
print("Connecting to Wi-Fi '" + ssid + "' ... ", end="")
wifi.radio.connect(ssid, password)
print('connected.')
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
context = ssl.create_default_context()
context.load_verify_locations(cadata=favoriot_ca.cert)
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=os.getenv("FAVORIOT_MQTT_BROKER_HOST"),
port=os.getenv("FAVORIOT_MQTT_BROKER_PORT"),
username=os.getenv("FAVORIOT_DEVICE_ACCESS_TOKEN"),
password=os.getenv("FAVORIOT_DEVICE_ACCESS_TOKEN"),
socket_pool=pool,
ssl_context=context,
)
# Connect callback handlers to mqtt_client
mqtt_client.on_connect = mqtt.connect
mqtt_client.on_disconnect = mqtt.disconnect
mqtt_client.on_publish = mqtt.publish
print("Connecting to MQTT broker '%s' with secure port ... " % mqtt_client.broker, end="")
mqtt_client.connect()
while True:
try:
temperature = dht11.temperature
humidity = dht11.humidity
print("Temperature: {} °C, Humidity: {} %RH ".format(temperature, humidity))
screen = displayio.Group()
text = "DHT11 SENSOR"
text_font = terminalio.FONT
text_color = 0xFFFFFF
text_area = label.Label(text_font, text=text, color=text_color, x=0, y=10)
screen.append(text_area)
text = "Temperature: {} *C".format(temperature)
text_area = label.Label(text_font, text=text, color=text_color, x=0, y=25)
screen.append(text_area)
text = "Humidity : {} %RH".format(humidity)
text_area = label.Label(text_font, text=text, color=text_color, x=0, y=37)
screen.append(text_area)
oled.show(screen)
except RuntimeError as error:
print(error.args[0])
time.sleep(2.0)
continue
except Exception as error:
dht11.exit()
raise error
time.sleep(2.0)
if (now + 15) < time.monotonic():
now = time.monotonic()
data = {
'device_developer_id': os.getenv('FAVORIOT_DEVICE_DEVELOPER_ID'),
'data': {
'temperature': temperature,
'humidity': humidity
}
}
mqtt_topic = '{}/v2/streams'.format(os.getenv('FAVORIOT_DEVICE_ACCESS_TOKEN'))
mqtt_client.publish(mqtt_topic, json.dumps(data))
print()