Skip to content

Commit 7542a81

Browse files
committed
Adafruit IO and LC709203 code.
1 parent 3899ad7 commit 7542a81

File tree

2 files changed

+84
-0
lines changed
  • CircuitPython_Templates

2 files changed

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

CircuitPython_Templates/lc709203_simple_data/code.py

Whitespace-only changes.

0 commit comments

Comments
 (0)