Skip to content

Commit 5542514

Browse files
author
brentru
committed
add cell example, black and pylint
1 parent 966b7b3 commit 5542514

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
"AdafruitAdafruit_IO Library Documentation",
137137
author,
138138
"manual",
139-
),
139+
)
140140
]
141141

142142
# -- Options for manual page output ---------------------------------------
@@ -167,5 +167,5 @@
167167
"AdafruitAdafruit_IOLibrary",
168168
"One line description of project.",
169169
"Miscellaneous",
170-
),
170+
)
171171
]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Example of using the Adafruit IO CircuitPython MQTT client
2+
# to subscribe to an Adafruit IO feed and publish random data
3+
# to be received by the feed.
4+
#
5+
# Example by Tony DiCola for Adafruit Industries
6+
# Modified by Brent Rubell for Adafruit Industries, 2019
7+
import time
8+
from random import randint
9+
10+
import board
11+
import busio
12+
import digitalio
13+
14+
from adafruit_fona.adafruit_fona import FONA
15+
from adafruit_fona.adafruit_fona_gsm import GSM
16+
import adafruit_fona.adafruit_fona_socket as cellular_socket
17+
18+
from adafruit_io.adafruit_io import IO_MQTT
19+
import adafruit_minimqtt as MQTT
20+
21+
# Get MQTT details and more from a secrets.py file
22+
try:
23+
from secrets import secrets
24+
except ImportError:
25+
print("MQTT secrets are kept in secrets.py, please add them there!")
26+
raise
27+
28+
# Create a serial connection for the FONA connection using 4800 baud.
29+
# These are the defaults you should use for the FONA Shield.
30+
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
31+
uart = busio.UART(board.TX, board.RX, baudrate=4800)
32+
rst = digitalio.DigitalInOut(board.D4)
33+
34+
# Initialize FONA module (this may take a few seconds)
35+
fona = FONA(uart, rst)
36+
37+
# initialize gsm
38+
gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
39+
40+
while not gsm.is_attached:
41+
print("Attaching to network...")
42+
time.sleep(0.5)
43+
44+
while not gsm.is_connected:
45+
print("Connecting to network...")
46+
gsm.connect()
47+
time.sleep(5)
48+
49+
# Define callback functions which will be called when certain events happen.
50+
# pylint: disable=unused-argument
51+
def connected(client):
52+
# Connected function will be called when the client is connected to Adafruit IO.
53+
# This is a good place to subscribe to feed changes. The client parameter
54+
# passed to this function is the Adafruit IO MQTT client so you can make
55+
# calls against it easily.
56+
print("Connected to Adafruit IO! Listening for DemoFeed changes...")
57+
# Subscribe to changes on a feed named DemoFeed.
58+
client.subscribe("DemoFeed")
59+
60+
61+
def subscribe(client, userdata, topic, granted_qos):
62+
# This method is called when the client subscribes to a new feed.
63+
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
64+
65+
66+
def unsubscribe(client, userdata, topic, pid):
67+
# This method is called when the client unsubscribes from a feed.
68+
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
69+
70+
71+
# pylint: disable=unused-argument
72+
def disconnected(client):
73+
# Disconnected function will be called when the client disconnects.
74+
print("Disconnected from Adafruit IO!")
75+
76+
77+
# pylint: disable=unused-argument
78+
def message(client, feed_id, payload):
79+
# Message function will be called when a subscribed feed has a new value.
80+
# The feed_id parameter identifies the feed, and the payload parameter has
81+
# the new value.
82+
print("Feed {0} received new value: {1}".format(feed_id, payload))
83+
84+
85+
# Initialize MQTT interface with the ethernet interface
86+
MQTT.set_socket(cellular_socket, fona)
87+
88+
# Initialize a new MQTT Client object
89+
mqtt_client = MQTT.MQTT(
90+
broker="http://io.adafruit.com",
91+
username=secrets["aio_user"],
92+
password=secrets["aio_key"],
93+
)
94+
95+
# Initialize an Adafruit IO MQTT Client
96+
io = IO_MQTT(mqtt_client)
97+
98+
# Connect the callback methods defined above to Adafruit IO
99+
io.on_connect = connected
100+
io.on_disconnect = disconnected
101+
io.on_subscribe = subscribe
102+
io.on_unsubscribe = unsubscribe
103+
io.on_message = message
104+
105+
# Connect to Adafruit IO
106+
print("Connecting to Adafruit IO...")
107+
io.connect()
108+
109+
# Below is an example of manually publishing a new value to Adafruit IO.
110+
last = 0
111+
print("Publishing a new message every 10 seconds...")
112+
while True:
113+
# Explicitly pump the message loop.
114+
io.loop()
115+
# Send a new message every 10 seconds.
116+
if (time.monotonic() - last) >= 5:
117+
value = randint(0, 100)
118+
print("Publishing {0} to DemoFeed.".format(value))
119+
io.publish("DemoFeed", value)
120+
last = time.monotonic()

0 commit comments

Comments
 (0)