|
| 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