|
| 1 | +# SPDX-FileCopyrightText: 2021 Ladyada for Adafruit Industries |
| 2 | +# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import time |
| 5 | +import ssl |
| 6 | +import socketpool |
| 7 | +import wifi |
| 8 | +import board |
| 9 | +import neopixel |
| 10 | +import microcontroller |
| 11 | +import adafruit_minimqtt.adafruit_minimqtt as MQTT |
| 12 | +from adafruit_io.adafruit_io import IO_MQTT |
| 13 | + |
| 14 | +try: |
| 15 | + from secrets import secrets |
| 16 | +except ImportError: |
| 17 | + print("WiFi and Adafruit IO credentials are kept in secrets.py - please add them there!") |
| 18 | + raise |
| 19 | + |
| 20 | +# Set your Adafruit IO Username and Key in secrets.py |
| 21 | +# (visit io.adafruit.com if you need to create an account, |
| 22 | +# or if you need your Adafruit IO key.) |
| 23 | +aio_username = secrets["aio_username"] |
| 24 | +aio_key = secrets["aio_key"] |
| 25 | + |
| 26 | +# WiFi |
| 27 | +print("Connecting to %s" % secrets["ssid"]) |
| 28 | +wifi.radio.connect(secrets["ssid"], secrets["password"]) |
| 29 | +print("Connected to %s!" % secrets["ssid"]) |
| 30 | + |
| 31 | +# Initialize NeoPixel |
| 32 | +pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.3) |
| 33 | + |
| 34 | + |
| 35 | +# Define callback functions which will be called when certain events happen. |
| 36 | +# pylint: disable=unused-argument |
| 37 | +def connected(client): |
| 38 | + print("Connected to Adafruit IO! Listening for NeoPixel changes...") |
| 39 | + # Subscribe to Adafruit IO feed called "neopixel" |
| 40 | + client.subscribe("neopixel") |
| 41 | + |
| 42 | + |
| 43 | +# pylint: disable=unused-argument |
| 44 | +def message(client, feed_id, payload): |
| 45 | + print("Feed {0} received new value: {1}".format(feed_id, payload)) |
| 46 | + if feed_id == "neopixel": |
| 47 | + pixel.fill(int(payload[1:], 16)) |
| 48 | + |
| 49 | + |
| 50 | +# Create a socket pool |
| 51 | +pool = socketpool.SocketPool(wifi.radio) |
| 52 | + |
| 53 | +# Initialize a new MQTT Client object |
| 54 | +mqtt_client = MQTT.MQTT( |
| 55 | + broker="io.adafruit.com", |
| 56 | + username=secrets["aio_username"], |
| 57 | + password=secrets["aio_key"], |
| 58 | + socket_pool=pool, |
| 59 | + ssl_context=ssl.create_default_context(), |
| 60 | +) |
| 61 | + |
| 62 | +# Initialize Adafruit IO MQTT "helper" |
| 63 | +io = IO_MQTT(mqtt_client) |
| 64 | + |
| 65 | +# Set up the callback methods above |
| 66 | +io.on_connect = connected |
| 67 | +io.on_message = message |
| 68 | + |
| 69 | +# Connect the client to the MQTT broker. |
| 70 | +print("Connecting to Adafruit IO...") |
| 71 | +io.connect() |
| 72 | + |
| 73 | +last = 0 |
| 74 | +while True: |
| 75 | + # Explicitly pump the message loop. |
| 76 | + io.loop() |
| 77 | + |
| 78 | + # Obtain the CPU temperature. |
| 79 | + temperature = "{:.2f}".format(microcontroller.cpu.temperature) |
| 80 | + |
| 81 | + # Print and publish the CPU temperature every 10 seconds. |
| 82 | + if (time.monotonic() - last) >= 10: |
| 83 | + print("Current CPU temperature: {0} C".format(temperature)) |
| 84 | + io.publish("cpu-temperature", temperature) |
| 85 | + last = time.monotonic() |
0 commit comments