/ examples / adafruit_io_mqtt / adafruit_io_simpletest_cellular.py
adafruit_io_simpletest_cellular.py
  1  # Example of using the Adafruit IO CircuitPython MQTT client
  2  # to subscribe to an Adafruit IO feed and publish random data
  3  # to be received by the feed.
  4  #
  5  # Example by Tony DiCola for Adafruit Industries
  6  # Modified by Brent Rubell for Adafruit Industries, 2019
  7  import time
  8  from random import randint
  9  
 10  import board
 11  import busio
 12  import digitalio
 13  
 14  from adafruit_fona.adafruit_fona import FONA
 15  from adafruit_fona.adafruit_fona_gsm import GSM
 16  import adafruit_fona.adafruit_fona_socket as cellular_socket
 17  
 18  from adafruit_io.adafruit_io import IO_MQTT
 19  import adafruit_minimqtt.adafruit_minimqtt as MQTT
 20  
 21  # Get MQTT details and more from a secrets.py file
 22  try:
 23      from secrets import secrets
 24  except ImportError:
 25      print("MQTT secrets are kept in secrets.py, please add them there!")
 26      raise
 27  
 28  # Create a serial connection for the FONA connection
 29  uart = busio.UART(board.TX, board.RX)
 30  rst = digitalio.DigitalInOut(board.D4)
 31  
 32  # Initialize FONA module (this may take a few seconds)
 33  fona = FONA(uart, rst)
 34  
 35  # initialize gsm
 36  gsm = GSM(fona, (secrets["apn"], secrets["apn_username"], secrets["apn_password"]))
 37  
 38  while not gsm.is_attached:
 39      print("Attaching to network...")
 40      time.sleep(0.5)
 41  
 42  while not gsm.is_connected:
 43      print("Connecting to network...")
 44      gsm.connect()
 45      time.sleep(5)
 46  
 47  # Define callback functions which will be called when certain events happen.
 48  # pylint: disable=unused-argument
 49  def connected(client):
 50      # Connected function will be called when the client is connected to Adafruit IO.
 51      # This is a good place to subscribe to feed changes.  The client parameter
 52      # passed to this function is the Adafruit IO MQTT client so you can make
 53      # calls against it easily.
 54      print("Connected to Adafruit IO!  Listening for DemoFeed changes...")
 55      # Subscribe to changes on a feed named DemoFeed.
 56      client.subscribe("DemoFeed")
 57  
 58  
 59  def subscribe(client, userdata, topic, granted_qos):
 60      # This method is called when the client subscribes to a new feed.
 61      print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
 62  
 63  
 64  def unsubscribe(client, userdata, topic, pid):
 65      # This method is called when the client unsubscribes from a feed.
 66      print("Unsubscribed from {0} with PID {1}".format(topic, pid))
 67  
 68  
 69  # pylint: disable=unused-argument
 70  def disconnected(client):
 71      # Disconnected function will be called when the client disconnects.
 72      print("Disconnected from Adafruit IO!")
 73  
 74  
 75  # pylint: disable=unused-argument
 76  def message(client, feed_id, payload):
 77      # Message function will be called when a subscribed feed has a new value.
 78      # The feed_id parameter identifies the feed, and the payload parameter has
 79      # the new value.
 80      print("Feed {0} received new value: {1}".format(feed_id, payload))
 81  
 82  
 83  # Initialize MQTT interface with the ethernet interface
 84  MQTT.set_socket(cellular_socket, fona)
 85  
 86  # Initialize a new MQTT Client object
 87  mqtt_client = MQTT.MQTT(
 88      broker="io.adafruit.com", username=secrets["aio_user"], password=secrets["aio_key"],
 89  )
 90  
 91  # Initialize an Adafruit IO MQTT Client
 92  io = IO_MQTT(mqtt_client)
 93  
 94  # Connect the callback methods defined above to Adafruit IO
 95  io.on_connect = connected
 96  io.on_disconnect = disconnected
 97  io.on_subscribe = subscribe
 98  io.on_unsubscribe = unsubscribe
 99  io.on_message = message
100  
101  # Connect to Adafruit IO
102  print("Connecting to Adafruit IO...")
103  io.connect()
104  
105  # Below is an example of manually publishing a new  value to Adafruit IO.
106  last = 0
107  print("Publishing a new message every 10 seconds...")
108  while True:
109      # Explicitly pump the message loop.
110      io.loop()
111      # Send a new message every 10 seconds.
112      if (time.monotonic() - last) >= 5:
113          value = randint(0, 100)
114          print("Publishing {0} to DemoFeed.".format(value))
115          io.publish("DemoFeed", value)
116          last = time.monotonic()