minimqtt_simpletest_cellular.py
1 import time 2 import board 3 import busio 4 import digitalio 5 from adafruit_fona.adafruit_fona import FONA 6 import adafruit_fona.adafruit_fona_network as network 7 import adafruit_fona.adafruit_fona_socket as socket 8 9 import adafruit_minimqtt.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 21 uart = busio.UART(board.TX, board.RX) 22 rst = digitalio.DigitalInOut(board.D4) 23 # Initialize FONA 24 fona = FONA(uart, rst) 25 26 ### Topic Setup ### 27 28 # MQTT Topic 29 # Use this topic if you'd like to connect to a standard MQTT broker 30 mqtt_topic = "test/topic" 31 32 # Adafruit IO-style Topic 33 # Use this topic if you'd like to connect to io.adafruit.com 34 # mqtt_topic = 'aio_user/feeds/temperature' 35 36 ### Code ### 37 38 # Define callback methods which are called when events occur 39 # pylint: disable=unused-argument, redefined-outer-name 40 def connect(client, userdata, flags, rc): 41 # This function will be called when the client is connected 42 # successfully to the broker. 43 print("Connected to MQTT Broker!") 44 print("Flags: {0}\n RC: {1}".format(flags, rc)) 45 46 47 def disconnect(client, userdata, rc): 48 # This method is called when the client disconnects 49 # from the broker. 50 print("Disconnected from MQTT Broker!") 51 52 53 def subscribe(client, userdata, topic, granted_qos): 54 # This method is called when the client subscribes to a new feed. 55 print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos)) 56 57 58 def unsubscribe(client, userdata, topic, pid): 59 # This method is called when the client unsubscribes from a feed. 60 print("Unsubscribed from {0} with PID {1}".format(topic, pid)) 61 62 63 def publish(client, userdata, topic, pid): 64 # This method is called when the client publishes data to a feed. 65 print("Published to {0} with PID {1}".format(topic, pid)) 66 67 68 # Initialize cellular data network 69 network = network.CELLULAR( 70 fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]) 71 ) 72 73 while not network.is_attached: 74 print("Attaching to network...") 75 time.sleep(0.5) 76 print("Attached!") 77 78 while not network.is_connected: 79 print("Connecting to network...") 80 network.connect() 81 time.sleep(0.5) 82 print("Network Connected!") 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"], 90 username=secrets["user"], 91 password=secrets["pass"], 92 is_ssl=False, 93 ) 94 95 # Connect callback handlers to client 96 client.on_connect = connect 97 client.on_disconnect = disconnect 98 client.on_subscribe = subscribe 99 client.on_unsubscribe = unsubscribe 100 client.on_publish = publish 101 102 print("Attempting to connect to %s" % client.broker) 103 client.connect() 104 105 print("Subscribing to %s" % mqtt_topic) 106 client.subscribe(mqtt_topic) 107 108 print("Publishing to %s" % mqtt_topic) 109 client.publish(mqtt_topic, "Hello Broker!") 110 111 print("Unsubscribing from %s" % mqtt_topic) 112 client.unsubscribe(mqtt_topic) 113 114 print("Disconnecting from %s" % client.broker) 115 client.disconnect()