-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
142 lines (115 loc) · 4.06 KB
/
code.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import time
import math
import board
import digitalio
import microcontroller
# import adafruit_shtc3
from adafruit_bme280 import basic as adafruit_bme280
# import adafruit_ltr390
import wifi
import socketpool
import ssl
import adafruit_minimqtt.adafruit_minimqtt as MQTT
# Set-up pin to LOW, ready to send TPL5110 a signal.
pin = digitalio.DigitalInOut(board.D20)
pin.direction = digitalio.Direction.OUTPUT
pin.value = False
# Get wifi details from the secrets.py file
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
# WiFi
try:
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
# Wi-Fi connectivity fails with error messages,
# not specific errors, so this except is broad.
except Exception as e:
print(
"Failed to connect to WiFi. Error:", e, "\nBoard will hard reset in 30 seconds."
)
time.sleep(30)
microcontroller.reset()
def connect(mqtt_client, userdata, flags, rc):
# This function will be called when the mqtt_client is connected
# successfully to the broker.
print("Connected to MQTT Broker!")
print("Flags: {0}\n RC: {1}".format(flags, rc))
def disconnect(mqtt_client, userdata, rc):
# This method is called when the mqtt_client disconnects
# from the broker.
print("Disconnected from MQTT Broker!")
def subscribe(mqtt_client, userdata, topic, granted_qos):
# This method is called when the mqtt_client subscribes to a new feed.
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
def unsubscribe(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client unsubscribes from a feed.
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
def publish(mqtt_client, userdata, topic, pid):
# This method is called when the mqtt_client publishes data to a feed.
print("Published to {0} with PID {1}".format(topic, pid))
def message(client, topic, message):
# Method called when a client's subscribed feed has a new value.
print("New message on topic {0}: {1}".format(topic, message))
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
mqtt_client = MQTT.MQTT(
broker=secrets["mqtt-broker"],
port=1883,
username=secrets["user"],
password=secrets["pass"],
socket_pool=pool,
ssl_context=ssl.create_default_context(),
)
# Connect callback handlers to mqtt_client
mqtt_client.on_connect = connect
mqtt_client.on_disconnect = disconnect
mqtt_client.on_subscribe = subscribe
mqtt_client.on_unsubscribe = unsubscribe
mqtt_client.on_publish = publish
mqtt_client.on_message = message
# Connect the client to the MQTT broker.
try:
mqtt_client.connect()
except Exception as e:
print(
"Failed to connect to Broker. Error:",
e,
"\nBoard will hard reset in 30 seconds.",
)
time.sleep(30)
microcontroller.reset()
i2c = board.I2C() # uses board.SCL and board.SDA
# sht = adafruit_shtc3.SHTC3(i2c)
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
# ltr = adafruit_ltr390.LTR390(i2c)
#while True:
#pin.value = False
#temperature, relative_humidity = sht.measurements
#print("Temperature: %0.1f C" % temperature)
#print("Humidity: %0.1f %%" % relative_humidity)
#print("\nTemperature: %0.1f C" % bme280.temperature)
#print("Humidity: %0.1f %%" % bme280.humidity)
#print("Pressure: %0.1f hPa" % bme280.pressure)
#print("")
#Dew point
#b = 17.62
#c = 243.12
#gamma = (b * bme280.temperature /(c + bme280.temperature)) + math.log(bme280.humidity / 100.0)
#dewpoint = (c * gamma) / (b - gamma)
#print("Dew point: %0.1f" % dewpoint)
#print("UV:", ltr.uvs, "\t\tAmbient Light:", ltr.light)
#print("UVI:", ltr.uvi, "\t\tLux:", ltr.lux)
try:
mqtt_client.loop()
mqtt_client.publish("homeassistant/sensor/shtc3_temp", bme280.temperature)
# Send TPL5110 a signal:
pin.value = True
time.sleep(5)
except Exception as err:
print("An error occured: {}".format(err))
time.sleep(30)
microcontroller.reset()