-
Notifications
You must be signed in to change notification settings - Fork 31
Add Example for Cellular Interfaces #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
examples/adafruit_io_mqtt/adafruit_io_simpletest_cellular.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Example of using the Adafruit IO CircuitPython MQTT client | ||
# to subscribe to an Adafruit IO feed and publish random data | ||
# to be received by the feed. | ||
# | ||
# Example by Tony DiCola for Adafruit Industries | ||
# Modified by Brent Rubell for Adafruit Industries, 2019 | ||
import time | ||
from random import randint | ||
|
||
import board | ||
import busio | ||
import digitalio | ||
|
||
from adafruit_fona.adafruit_fona import FONA | ||
from adafruit_fona.adafruit_fona_gsm import GSM | ||
import adafruit_fona.adafruit_fona_socket as cellular_socket | ||
|
||
from adafruit_io.adafruit_io import IO_MQTT | ||
import adafruit_minimqtt as MQTT | ||
|
||
# Get MQTT details and more from a secrets.py file | ||
try: | ||
from secrets import secrets | ||
except ImportError: | ||
print("MQTT secrets are kept in secrets.py, please add them there!") | ||
raise | ||
|
||
# Create a serial connection for the FONA connection using 4800 baud. | ||
# These are the defaults you should use for the FONA Shield. | ||
# For other boards set RX = GPS module TX, and TX = GPS module RX pins. | ||
uart = busio.UART(board.TX, board.RX, baudrate=4800) | ||
rst = digitalio.DigitalInOut(board.D4) | ||
|
||
# Initialize FONA module (this may take a few seconds) | ||
fona = FONA(uart, rst) | ||
|
||
# initialize gsm | ||
gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) | ||
|
||
while not gsm.is_attached: | ||
print("Attaching to network...") | ||
time.sleep(0.5) | ||
|
||
while not gsm.is_connected: | ||
print("Connecting to network...") | ||
gsm.connect() | ||
time.sleep(5) | ||
|
||
# Define callback functions which will be called when certain events happen. | ||
# pylint: disable=unused-argument | ||
def connected(client): | ||
# Connected function will be called when the client is connected to Adafruit IO. | ||
# This is a good place to subscribe to feed changes. The client parameter | ||
# passed to this function is the Adafruit IO MQTT client so you can make | ||
# calls against it easily. | ||
print("Connected to Adafruit IO! Listening for DemoFeed changes...") | ||
# Subscribe to changes on a feed named DemoFeed. | ||
client.subscribe("DemoFeed") | ||
|
||
|
||
def subscribe(client, userdata, topic, granted_qos): | ||
# This method is called when the client subscribes to a new feed. | ||
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) | ||
|
||
|
||
def unsubscribe(client, userdata, topic, pid): | ||
# This method is called when the client unsubscribes from a feed. | ||
print("Unsubscribed from {0} with PID {1}".format(topic, pid)) | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def disconnected(client): | ||
# Disconnected function will be called when the client disconnects. | ||
print("Disconnected from Adafruit IO!") | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def message(client, feed_id, payload): | ||
# Message function will be called when a subscribed feed has a new value. | ||
# The feed_id parameter identifies the feed, and the payload parameter has | ||
# the new value. | ||
print("Feed {0} received new value: {1}".format(feed_id, payload)) | ||
|
||
|
||
# Initialize MQTT interface with the ethernet interface | ||
MQTT.set_socket(cellular_socket, fona) | ||
|
||
# Initialize a new MQTT Client object | ||
mqtt_client = MQTT.MQTT( | ||
broker="http://io.adafruit.com", | ||
username=secrets["aio_user"], | ||
password=secrets["aio_key"], | ||
) | ||
|
||
# Initialize an Adafruit IO MQTT Client | ||
io = IO_MQTT(mqtt_client) | ||
|
||
# Connect the callback methods defined above to Adafruit IO | ||
io.on_connect = connected | ||
io.on_disconnect = disconnected | ||
io.on_subscribe = subscribe | ||
io.on_unsubscribe = unsubscribe | ||
io.on_message = message | ||
|
||
# Connect to Adafruit IO | ||
print("Connecting to Adafruit IO...") | ||
io.connect() | ||
|
||
# Below is an example of manually publishing a new value to Adafruit IO. | ||
last = 0 | ||
print("Publishing a new message every 10 seconds...") | ||
while True: | ||
# Explicitly pump the message loop. | ||
io.loop() | ||
# Send a new message every 10 seconds. | ||
if (time.monotonic() - last) >= 5: | ||
value = randint(0, 100) | ||
print("Publishing {0} to DemoFeed.".format(value)) | ||
io.publish("DemoFeed", value) | ||
last = time.monotonic() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.