Skip to content

Commit 1414624

Browse files
authored
Merge pull request #35 from brentru/add-cellular-example
Add Example for Cellular Interfaces
2 parents 966b7b3 + 6e361dc commit 1414624

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-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: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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
29+
uart = busio.UART(board.TX, board.RX)
30+
rst = digitalio.DigitalInOut(board.D4)
31+
32+
# Initialize FONA module (this may take a few seconds)
33+
fona = FONA(uart, rst)
34+
35+
# initialize gsm
36+
gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
37+
38+
while not gsm.is_attached:
39+
print("Attaching to network...")
40+
time.sleep(0.5)
41+
42+
while not gsm.is_connected:
43+
print("Connecting to network...")
44+
gsm.connect()
45+
time.sleep(5)
46+
47+
# Define callback functions which will be called when certain events happen.
48+
# pylint: disable=unused-argument
49+
def connected(client):
50+
# Connected function will be called when the client is connected to Adafruit IO.
51+
# This is a good place to subscribe to feed changes. The client parameter
52+
# passed to this function is the Adafruit IO MQTT client so you can make
53+
# calls against it easily.
54+
print("Connected to Adafruit IO! Listening for DemoFeed changes...")
55+
# Subscribe to changes on a feed named DemoFeed.
56+
client.subscribe("DemoFeed")
57+
58+
59+
def subscribe(client, userdata, topic, granted_qos):
60+
# This method is called when the client subscribes to a new feed.
61+
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
62+
63+
64+
def unsubscribe(client, userdata, topic, pid):
65+
# This method is called when the client unsubscribes from a feed.
66+
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
67+
68+
69+
# pylint: disable=unused-argument
70+
def disconnected(client):
71+
# Disconnected function will be called when the client disconnects.
72+
print("Disconnected from Adafruit IO!")
73+
74+
75+
# pylint: disable=unused-argument
76+
def message(client, feed_id, payload):
77+
# Message function will be called when a subscribed feed has a new value.
78+
# The feed_id parameter identifies the feed, and the payload parameter has
79+
# the new value.
80+
print("Feed {0} received new value: {1}".format(feed_id, payload))
81+
82+
83+
# Initialize MQTT interface with the ethernet interface
84+
MQTT.set_socket(cellular_socket, fona)
85+
86+
# Initialize a new MQTT Client object
87+
mqtt_client = MQTT.MQTT(
88+
broker="http://io.adafruit.com",
89+
username=secrets["aio_user"],
90+
password=secrets["aio_key"],
91+
)
92+
93+
# Initialize an Adafruit IO MQTT Client
94+
io = IO_MQTT(mqtt_client)
95+
96+
# Connect the callback methods defined above to Adafruit IO
97+
io.on_connect = connected
98+
io.on_disconnect = disconnected
99+
io.on_subscribe = subscribe
100+
io.on_unsubscribe = unsubscribe
101+
io.on_message = message
102+
103+
# Connect to Adafruit IO
104+
print("Connecting to Adafruit IO...")
105+
io.connect()
106+
107+
# Below is an example of manually publishing a new value to Adafruit IO.
108+
last = 0
109+
print("Publishing a new message every 10 seconds...")
110+
while True:
111+
# Explicitly pump the message loop.
112+
io.loop()
113+
# Send a new message every 10 seconds.
114+
if (time.monotonic() - last) >= 5:
115+
value = randint(0, 100)
116+
print("Publishing {0} to DemoFeed.".format(value))
117+
io.publish("DemoFeed", value)
118+
last = time.monotonic()

0 commit comments

Comments
 (0)