Skip to content

Commit 68f169d

Browse files
authored
Merge pull request #2070 from kattni/more-template-code
LC709203 simple data and Adafruit IO send/receive examples
2 parents 6420d67 + d6e8a18 commit 68f169d

File tree

2 files changed

+106
-0
lines changed
  • CircuitPython_Templates

2 files changed

+106
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
CircuitPython Simple Example for LC709203 Sensor
5+
"""
6+
import time
7+
import board
8+
from adafruit_lc709203f import LC709203F, PackSize
9+
10+
# Create sensor object, using the board's default I2C bus.
11+
battery_monitor = LC709203F(board.I2C())
12+
13+
# Update to match the mAh of your battery for more accurate readings.
14+
# Can be MAH100, MAH200, MAH400, MAH500, MAH1000, MAH2000, MAH3000.
15+
# Choose the closest match. Include "PackSize." before it, as shown.
16+
battery_monitor.pack_size = PackSize.MAH400
17+
18+
while True:
19+
print("Battery Percent: {:.2f} %".format(battery_monitor.cell_percent))
20+
print("Battery Voltage: {:.2f} V".format(battery_monitor.cell_voltage))
21+
time.sleep(2)

0 commit comments

Comments
 (0)