-
Notifications
You must be signed in to change notification settings - Fork 2
/
code.py
85 lines (73 loc) · 2.59 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
# https://learn.adafruit.com/mqtt-in-circuitpython/circuitpython-wifi-usage
# https://learn.adafruit.com/mqtt-in-circuitpython/connecting-to-a-mqtt-broker
# required from adafruit_bundle:
# - adafruit_requests
# - adafruit_minimqtt
# - adafruit_bus_device
# - adafruit_register
# - adafruit_si7021
import time
import ssl
import socketpool
import wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
import board
from digitalio import DigitalInOut, Direction, Pull
from analogio import AnalogIn
import adafruit_si7021
# Add a secrets.py to your filesystem that has a dictionary like
# that in secrets.py.example
# pylint: disable=no-name-in-module,wrong-import-order
try:
from secrets import secrets
except ImportError:
print("WiFi secrets are kept in secrets.py, please add them there!")
raise
print("Connecting to %s" % secrets["ssid"])
wifi.radio.connect(secrets["ssid"], secrets["password"])
print("Connected to %s!" % secrets["ssid"])
### Feeds ###
light_feed = "greenhouse/light"
temp_feed = "greenhouse/temperature"
humidity_feed = "greenhouse/humidity"
# Define callback methods which are called when events occur
# pylint: disable=unused-argument, redefined-outer-name
def connected(client, userdata, flags, rc):
# This function will be called when the client is connected
# successfully to the broker.
print("Connected to MQTT!")
def disconnected(client, userdata, rc):
# This method is called when the client is disconnected
print("Disconnected from MQTT!")
# Create a socket pool
pool = socketpool.SocketPool(wifi.radio)
# Set up a MiniMQTT Client
mqtt_client = MQTT.MQTT(
broker=secrets["broker"],
port=secrets["port"],
username=secrets["aio_username"],
password=secrets["aio_key"],
socket_pool=pool,
ssl_context=ssl.create_default_context(),
)
# Setup the callback methods above
mqtt_client.on_connect = connected
mqtt_client.on_disconnect = disconnected
# Connect the client to the MQTT broker.
print("Connecting to MQTT...")
mqtt_client.connect()
# Create library object using our Bus I2C port
sensor = adafruit_si7021.SI7021(board.I2C())
light_pin = AnalogIn(board.IO4)
while True:
# Poll the message queue
mqtt_client.loop()
# get the current temperature
light_val = light_pin.value
temp_val = ((sensor.temperature * 9)/5) + 32
humidity_val = sensor.relative_humidity
# Send a new messages
mqtt_client.publish(light_feed, light_val)
mqtt_client.publish(temp_feed, temp_val)
mqtt_client.publish(humidity_feed, humidity_val)
time.sleep(0.5)