Skip to content

Commit fe70374

Browse files
authored
Merge pull request adafruit#32 from brentru/add-cellular
Add cellular usage examples
2 parents 195bfc0 + 042f5d5 commit fe70374

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import time
2+
import board
3+
import busio
4+
import digitalio
5+
from adafruit_fona.adafruit_fona import FONA
6+
from adafruit_fona.adafruit_fona_gsm import GSM
7+
import adafruit_fona.adafruit_fona_socket as socket
8+
9+
import adafruit_minimqtt as MQTT
10+
11+
# Get Adafruit IO details and more from a secrets.py file
12+
try:
13+
from secrets import secrets
14+
except ImportError:
15+
print("GPRS secrets are kept in secrets.py, please add them there!")
16+
raise
17+
18+
### Cellular ###
19+
20+
# Create a serial connection for the FONA connection using 4800 baud.
21+
# These are the defaults you should use for the FONA Shield.
22+
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
23+
uart = busio.UART(board.TX, board.RX, baudrate=4800)
24+
rst = digitalio.DigitalInOut(board.D4)
25+
# Initialize FONA
26+
fona = FONA(uart, rst)
27+
28+
### Feeds ###
29+
30+
# Setup a feed named 'photocell' for publishing to a feed
31+
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
32+
33+
# Setup a feed named 'onoff' for subscribing to changes
34+
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
35+
36+
### Code ###
37+
38+
# Define callback methods which are called when events occur
39+
# pylint: disable=unused-argument, redefined-outer-name
40+
def connected(client, userdata, flags, rc):
41+
# This function will be called when the client is connected
42+
# successfully to the broker.
43+
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
44+
# Subscribe to all changes on the onoff_feed.
45+
client.subscribe(onoff_feed)
46+
47+
48+
def disconnected(client, userdata, rc):
49+
# This method is called when the client is disconnected
50+
print("Disconnected from Adafruit IO!")
51+
52+
53+
def message(client, topic, message):
54+
# This method is called when a topic the client is subscribed to
55+
# has a new message.
56+
print("New message on topic {0}: {1}".format(topic, message))
57+
58+
59+
# Initialize GSM modem
60+
gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
61+
62+
while not gsm.is_attached:
63+
print("Attaching to network...")
64+
time.sleep(0.5)
65+
print("Attached to network!")
66+
67+
while not gsm.is_connected:
68+
print("Connecting to network...")
69+
gsm.connect()
70+
time.sleep(5)
71+
print("Connected to network!")
72+
73+
# Initialize MQTT interface with the cellular interface
74+
MQTT.set_socket(socket, fona)
75+
76+
# Set up a MiniMQTT Client
77+
# NOTE: We'll need to connect insecurely for ethernet configurations.
78+
mqtt_client = MQTT.MQTT(
79+
broker="http://io.adafruit.com",
80+
username=secrets["aio_username"],
81+
password=secrets["aio_key"],
82+
)
83+
84+
# Setup the callback methods above
85+
mqtt_client.on_connect = connected
86+
mqtt_client.on_disconnect = disconnected
87+
mqtt_client.on_message = message
88+
89+
90+
# Connect the client to the MQTT broker.
91+
print("Connecting to Adafruit IO...")
92+
mqtt_client.connect()
93+
94+
photocell_val = 0
95+
while True:
96+
# Poll the message queue
97+
mqtt_client.loop()
98+
99+
# Send a new message
100+
print("Sending photocell value: %d..." % photocell_val)
101+
mqtt_client.publish(photocell_feed, photocell_val)
102+
print("Sent!")
103+
photocell_val += 1
104+
time.sleep(5)
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import time
2+
import board
3+
import busio
4+
import digitalio
5+
from adafruit_fona.adafruit_fona import FONA
6+
from adafruit_fona.adafruit_fona_gsm import GSM
7+
import adafruit_fona.adafruit_fona_socket as socket
8+
9+
import adafruit_minimqtt as MQTT
10+
11+
### Cellular ###
12+
13+
# Get cellular details and more from a secrets.py file
14+
try:
15+
from secrets import secrets
16+
except ImportError:
17+
print("Cellular secrets are kept in secrets.py, please add them there!")
18+
raise
19+
20+
# Create a serial connection for the FONA connection using 4800 baud.
21+
# These are the defaults you should use for the FONA Shield.
22+
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
23+
uart = busio.UART(board.TX, board.RX, baudrate=4800)
24+
rst = digitalio.DigitalInOut(board.D4)
25+
# Initialize FONA
26+
fona = FONA(uart, rst)
27+
28+
### Topic Setup ###
29+
30+
# MQTT Topic
31+
# Use this topic if you'd like to connect to a standard MQTT broker
32+
mqtt_topic = "test/topic"
33+
34+
# Adafruit IO-style Topic
35+
# Use this topic if you'd like to connect to io.adafruit.com
36+
# mqtt_topic = 'aio_user/feeds/temperature'
37+
38+
### Code ###
39+
40+
# Define callback methods which are called when events occur
41+
# pylint: disable=unused-argument, redefined-outer-name
42+
def connect(client, userdata, flags, rc):
43+
# This function will be called when the client is connected
44+
# successfully to the broker.
45+
print("Connected to MQTT Broker!")
46+
print("Flags: {0}\n RC: {1}".format(flags, rc))
47+
48+
49+
def disconnect(client, userdata, rc):
50+
# This method is called when the client disconnects
51+
# from the broker.
52+
print("Disconnected from MQTT Broker!")
53+
54+
55+
def subscribe(client, userdata, topic, granted_qos):
56+
# This method is called when the client subscribes to a new feed.
57+
print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
58+
59+
60+
def unsubscribe(client, userdata, topic, pid):
61+
# This method is called when the client unsubscribes from a feed.
62+
print("Unsubscribed from {0} with PID {1}".format(topic, pid))
63+
64+
65+
def publish(client, userdata, topic, pid):
66+
# This method is called when the client publishes data to a feed.
67+
print("Published to {0} with PID {1}".format(topic, pid))
68+
69+
70+
# Initialize GSM modem
71+
gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
72+
73+
while not gsm.is_attached:
74+
print("Attaching to network...")
75+
time.sleep(0.5)
76+
print("Attached to network!")
77+
78+
while not gsm.is_connected:
79+
print("Connecting to network...")
80+
gsm.connect()
81+
time.sleep(5)
82+
print("Connected to network!")
83+
84+
# Initialize MQTT interface with the cellular interface
85+
MQTT.set_socket(socket, fona)
86+
87+
# Set up a MiniMQTT Client
88+
client = MQTT.MQTT(
89+
broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
90+
)
91+
92+
# Connect callback handlers to client
93+
client.on_connect = connect
94+
client.on_disconnect = disconnect
95+
client.on_subscribe = subscribe
96+
client.on_unsubscribe = unsubscribe
97+
client.on_publish = publish
98+
99+
print("Attempting to connect to %s" % client.broker)
100+
client.connect()
101+
102+
print("Subscribing to %s" % mqtt_topic)
103+
client.subscribe(mqtt_topic)
104+
105+
print("Publishing to %s" % mqtt_topic)
106+
client.publish(mqtt_topic, "Hello Broker!")
107+
108+
print("Unsubscribing from %s" % mqtt_topic)
109+
client.unsubscribe(mqtt_topic)
110+
111+
print("Disconnecting from %s" % client.broker)
112+
client.disconnect()

0 commit comments

Comments
 (0)