Skip to content

Commit 418f6de

Browse files
author
brentru
committed
add updated examples
1 parent e6e9e63 commit 418f6de

File tree

2 files changed

+126
-11
lines changed

2 files changed

+126
-11
lines changed

examples/minimqtt_adafruitio_cellular.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import busio
44
import digitalio
55
from adafruit_fona.adafruit_fona import FONA
6+
from adafruit_fona.adafruit_fona_gsm import GSM
67
import adafruit_fona.adafruit_fona_socket as socket
78

89
import adafruit_minimqtt as MQTT
@@ -21,6 +22,8 @@
2122
# For other boards set RX = GPS module TX, and TX = GPS module RX pins.
2223
uart = busio.UART(board.TX, board.RX, baudrate=4800)
2324
rst = digitalio.DigitalInOut(board.D4)
25+
# Initialize FONA
26+
fona = FONA(uart, rst)
2427

2528
### Feeds ###
2629

@@ -53,19 +56,19 @@ def message(client, topic, message):
5356
print("New message on topic {0}: {1}".format(topic, message))
5457

5558

56-
# Connect to Cellular Network
57-
print("Initializing FONA (this may take a few seconds)")
58-
fona = FONA(uart, rst, debug=True)
59+
# Initialize GSM modem
60+
gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
5961

60-
# Enable GPS
61-
fona.gps = True
62+
while not gsm.is_attached:
63+
print("Attaching to network...")
64+
time.sleep(0.5)
65+
print("Attached to network!")
6266

63-
# Bring up cellular connection
64-
fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
65-
66-
# Bring up GPRS
67-
fona.gprs = True
68-
print("FONA initialized")
67+
while not gsm.is_connected:
68+
print("Connecting to network...")
69+
gsm.connect()
70+
time.sleep(5)
71+
print("Connected to network!")
6972

7073
# Initialize MQTT interface with the cellular interface
7174
MQTT.set_socket(socket, fona)
+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)