|
| 1 | +# SPDX-FileCopyrightText: 2021 Dylan Herrada for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import ssl |
| 5 | +import time |
| 6 | +import board |
| 7 | +import digitalio |
| 8 | +import socketpool |
| 9 | +import wifi |
| 10 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 11 | +from adafruit_io.adafruit_io import IO_MQTT |
| 12 | + |
| 13 | +led = digitalio.DigitalInOut(board.IO8) |
| 14 | +led.direction = digitalio.Direction.OUTPUT |
| 15 | + |
| 16 | +btn1 = digitalio.DigitalInOut(board.IO9) |
| 17 | +btn1.direction = digitalio.Direction.INPUT |
| 18 | +btn1.pull = digitalio.Pull.DOWN |
| 19 | + |
| 20 | +ALARM = None |
| 21 | +### WiFi ### |
| 22 | + |
| 23 | +# Get wifi details and more from a secrets.py file |
| 24 | +try: |
| 25 | + from secrets import secrets |
| 26 | +except ImportError: |
| 27 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 28 | + raise |
| 29 | + |
| 30 | +print("Connecting to %s" % secrets["ssid"]) |
| 31 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 32 | +print("Connected to %s!" % secrets["ssid"]) |
| 33 | + |
| 34 | +# Define callback functions which will be called when certain events happen. |
| 35 | +# pylint: disable=unused-argument |
| 36 | +def connected(client): |
| 37 | + # Connected function will be called when the client is connected to Adafruit IO. |
| 38 | + # This is a good place to subscribe to feed changes. The client parameter |
| 39 | + # passed to this function is the Adafruit IO MQTT client so you can make |
| 40 | + # calls against it easily. |
| 41 | + print("Connected to Adafruit IO!") |
| 42 | + client.subscribe("alarm-clock.alarm") |
| 43 | + |
| 44 | + |
| 45 | +def subscribe(client, userdata, topic, granted_qos): |
| 46 | + # This method is called when the client subscribes to a new feed. |
| 47 | + print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) |
| 48 | + |
| 49 | + |
| 50 | +def unsubscribe(client, userdata, topic, pid): |
| 51 | + # This method is called when the client unsubscribes from a feed. |
| 52 | + print("Unsubscribed from {0} with PID {1}".format(topic, pid)) |
| 53 | + |
| 54 | + |
| 55 | +# pylint: disable=unused-argument |
| 56 | +def disconnected(client): |
| 57 | + # Disconnected function will be called when the client disconnects. |
| 58 | + print("Disconnected from Adafruit IO!") |
| 59 | + |
| 60 | + |
| 61 | +# pylint: disable=unused-argument |
| 62 | +def message(client, feed_id, payload): |
| 63 | + # Message function will be called when a subscribed feed has a new value. |
| 64 | + # The feed_id parameter identifies the feed, and the payload parameter has |
| 65 | + # the new value. |
| 66 | + print("Feed {0} received new value: {1}".format(feed_id, payload)) |
| 67 | + |
| 68 | + |
| 69 | +def on_alarm(client, feed_id, payload): |
| 70 | + global ALARM |
| 71 | + print(payload) |
| 72 | + ALARM = eval(payload) |
| 73 | + |
| 74 | + |
| 75 | +# Create a socket pool |
| 76 | +pool = socketpool.SocketPool(wifi.radio) |
| 77 | + |
| 78 | +# Initialize a new MQTT Client object |
| 79 | +mqtt_client = MQTT.MQTT( |
| 80 | + broker="io.adafruit.com", |
| 81 | + username=secrets["aio_username"], |
| 82 | + password=secrets["aio_key"], |
| 83 | + socket_pool=pool, |
| 84 | + ssl_context=ssl.create_default_context(), |
| 85 | +) |
| 86 | + |
| 87 | +# Initialize an Adafruit IO MQTT Client |
| 88 | +io = IO_MQTT(mqtt_client) |
| 89 | + |
| 90 | +# Connect the callback methods defined above to Adafruit IO |
| 91 | +io.on_connect = connected |
| 92 | +io.on_disconnect = disconnected |
| 93 | +io.on_subscribe = subscribe |
| 94 | +io.on_unsubscribe = unsubscribe |
| 95 | +io.on_message = message |
| 96 | + |
| 97 | +io.add_feed_callback("alarm-clock.alarm", on_alarm) |
| 98 | + |
| 99 | +# Connect to Adafruit IO |
| 100 | +print("Connecting to Adafruit IO...") |
| 101 | +io.connect() |
| 102 | + |
| 103 | +io.get("alarm-clock.alarm") |
| 104 | + |
| 105 | +LAST = 0 |
| 106 | +while True: |
| 107 | + io.loop() |
| 108 | + if ALARM and time.monotonic() - LAST >= 0.2: |
| 109 | + led.value = not led.value |
| 110 | + LAST = time.monotonic() |
| 111 | + if btn1.value: |
| 112 | + io.publish("alarm-clock.alarm", "False") |
| 113 | + led.value = False |
| 114 | + led.value = True |
| 115 | + time.sleep(1) |
| 116 | + led.value = False |
0 commit comments