From e6e9e63f7e3db85dd0dc4c11a11b792b58c14dec Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 May 2020 11:51:18 -0400 Subject: [PATCH 1/2] add aio cellular test --- examples/minimqtt_adafruitio_cellular.py | 101 +++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100755 examples/minimqtt_adafruitio_cellular.py diff --git a/examples/minimqtt_adafruitio_cellular.py b/examples/minimqtt_adafruitio_cellular.py new file mode 100755 index 0000000..4e68805 --- /dev/null +++ b/examples/minimqtt_adafruitio_cellular.py @@ -0,0 +1,101 @@ +import time +import board +import busio +import digitalio +from adafruit_fona.adafruit_fona import FONA +import adafruit_fona.adafruit_fona_socket as socket + +import adafruit_minimqtt as MQTT + +# Get Adafruit IO details and more from a secrets.py file +try: + from secrets import secrets +except ImportError: + print("GPRS secrets are kept in secrets.py, please add them there!") + raise + +### Cellular ### + +# 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) + +### Feeds ### + +# Setup a feed named 'photocell' for publishing to a feed +photocell_feed = secrets["aio_username"] + "/feeds/photocell" + +# Setup a feed named 'onoff' for subscribing to changes +onoff_feed = secrets["aio_username"] + "/feeds/onoff" + +### Code ### + +# Define callback methods which are called when events occur +# pylint: disable=unused-argument, redefined-outer-name +def connected(client, userdata, flags, rc): + # This function will be called when the client is connected + # successfully to the broker. + print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed) + # Subscribe to all changes on the onoff_feed. + client.subscribe(onoff_feed) + + +def disconnected(client, userdata, rc): + # This method is called when the client is disconnected + print("Disconnected from Adafruit IO!") + + +def message(client, topic, message): + # This method is called when a topic the client is subscribed to + # has a new message. + print("New message on topic {0}: {1}".format(topic, message)) + + +# Connect to Cellular Network +print("Initializing FONA (this may take a few seconds)") +fona = FONA(uart, rst, debug=True) + +# Enable GPS +fona.gps = True + +# Bring up cellular connection +fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"])) + +# Bring up GPRS +fona.gprs = True +print("FONA initialized") + +# Initialize MQTT interface with the cellular interface +MQTT.set_socket(socket, fona) + +# Set up a MiniMQTT Client +# NOTE: We'll need to connect insecurely for ethernet configurations. +mqtt_client = MQTT.MQTT( + broker="http://io.adafruit.com", + username=secrets["aio_username"], + password=secrets["aio_key"], +) + +# Setup the callback methods above +mqtt_client.on_connect = connected +mqtt_client.on_disconnect = disconnected +mqtt_client.on_message = message + + +# Connect the client to the MQTT broker. +print("Connecting to Adafruit IO...") +mqtt_client.connect() + +photocell_val = 0 +while True: + # Poll the message queue + mqtt_client.loop() + + # Send a new message + print("Sending photocell value: %d..." % photocell_val) + mqtt_client.publish(photocell_feed, photocell_val) + print("Sent!") + photocell_val += 1 + time.sleep(5) From 418f6de7fcb17513858642bc88121c0bc77e3544 Mon Sep 17 00:00:00 2001 From: brentru Date: Tue, 5 May 2020 15:15:40 -0400 Subject: [PATCH 2/2] add updated examples --- examples/minimqtt_adafruitio_cellular.py | 25 ++--- examples/minimqtt_simpletest_cellular.py | 112 +++++++++++++++++++++++ 2 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 examples/minimqtt_simpletest_cellular.py diff --git a/examples/minimqtt_adafruitio_cellular.py b/examples/minimqtt_adafruitio_cellular.py index 4e68805..8cae590 100755 --- a/examples/minimqtt_adafruitio_cellular.py +++ b/examples/minimqtt_adafruitio_cellular.py @@ -3,6 +3,7 @@ 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 socket import adafruit_minimqtt as MQTT @@ -21,6 +22,8 @@ # 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 +fona = FONA(uart, rst) ### Feeds ### @@ -53,19 +56,19 @@ def message(client, topic, message): print("New message on topic {0}: {1}".format(topic, message)) -# Connect to Cellular Network -print("Initializing FONA (this may take a few seconds)") -fona = FONA(uart, rst, debug=True) +# Initialize GSM modem +gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) -# Enable GPS -fona.gps = True +while not gsm.is_attached: + print("Attaching to network...") + time.sleep(0.5) +print("Attached to network!") -# Bring up cellular connection -fona.configure_gprs((secrets["apn"], secrets["apn_username"], secrets["apn_password"])) - -# Bring up GPRS -fona.gprs = True -print("FONA initialized") +while not gsm.is_connected: + print("Connecting to network...") + gsm.connect() + time.sleep(5) +print("Connected to network!") # Initialize MQTT interface with the cellular interface MQTT.set_socket(socket, fona) diff --git a/examples/minimqtt_simpletest_cellular.py b/examples/minimqtt_simpletest_cellular.py new file mode 100644 index 0000000..875caf1 --- /dev/null +++ b/examples/minimqtt_simpletest_cellular.py @@ -0,0 +1,112 @@ +import time +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 socket + +import adafruit_minimqtt as MQTT + +### Cellular ### + +# Get cellular details and more from a secrets.py file +try: + from secrets import secrets +except ImportError: + print("Cellular 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 +fona = FONA(uart, rst) + +### Topic Setup ### + +# MQTT Topic +# Use this topic if you'd like to connect to a standard MQTT broker +mqtt_topic = "test/topic" + +# Adafruit IO-style Topic +# Use this topic if you'd like to connect to io.adafruit.com +# mqtt_topic = 'aio_user/feeds/temperature' + +### Code ### + +# Define callback methods which are called when events occur +# pylint: disable=unused-argument, redefined-outer-name +def connect(client, userdata, flags, rc): + # This function will be called when the client is connected + # successfully to the broker. + print("Connected to MQTT Broker!") + print("Flags: {0}\n RC: {1}".format(flags, rc)) + + +def disconnect(client, userdata, rc): + # This method is called when the client disconnects + # from the broker. + print("Disconnected from MQTT Broker!") + + +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)) + + +def publish(client, userdata, topic, pid): + # This method is called when the client publishes data to a feed. + print("Published to {0} with PID {1}".format(topic, pid)) + + +# Initialize GSM modem +gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"])) + +while not gsm.is_attached: + print("Attaching to network...") + time.sleep(0.5) +print("Attached to network!") + +while not gsm.is_connected: + print("Connecting to network...") + gsm.connect() + time.sleep(5) +print("Connected to network!") + +# Initialize MQTT interface with the cellular interface +MQTT.set_socket(socket, fona) + +# Set up a MiniMQTT Client +client = MQTT.MQTT( + broker=secrets["broker"], username=secrets["user"], password=secrets["pass"] +) + +# Connect callback handlers to client +client.on_connect = connect +client.on_disconnect = disconnect +client.on_subscribe = subscribe +client.on_unsubscribe = unsubscribe +client.on_publish = publish + +print("Attempting to connect to %s" % client.broker) +client.connect() + +print("Subscribing to %s" % mqtt_topic) +client.subscribe(mqtt_topic) + +print("Publishing to %s" % mqtt_topic) +client.publish(mqtt_topic, "Hello Broker!") + +print("Unsubscribing from %s" % mqtt_topic) +client.unsubscribe(mqtt_topic) + +print("Disconnecting from %s" % client.broker) +client.disconnect()