/ examples / minimqtt_certificate.py
minimqtt_certificate.py
  1  import board
  2  import busio
  3  from digitalio import DigitalInOut
  4  import neopixel
  5  from adafruit_esp32spi import adafruit_esp32spi
  6  from adafruit_esp32spi import adafruit_esp32spi_wifimanager
  7  import adafruit_esp32spi.adafruit_esp32spi_socket as socket
  8  
  9  import adafruit_minimqtt.adafruit_minimqtt as MQTT
 10  
 11  ### WiFi ###
 12  
 13  # Get wifi details and more from a secrets.py file
 14  try:
 15      from secrets import secrets
 16  except ImportError:
 17      print("WiFi secrets are kept in secrets.py, please add them there!")
 18      raise
 19  
 20  # If you are using a board with pre-defined ESP32 Pins:
 21  esp32_cs = DigitalInOut(board.ESP_CS)
 22  esp32_ready = DigitalInOut(board.ESP_BUSY)
 23  esp32_reset = DigitalInOut(board.ESP_RESET)
 24  
 25  # If you have an externally connected ESP32:
 26  # esp32_cs = DigitalInOut(board.D9)
 27  # esp32_ready = DigitalInOut(board.D10)
 28  # esp32_reset = DigitalInOut(board.D5)
 29  
 30  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 31  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 32  """Use below for Most Boards"""
 33  status_light = neopixel.NeoPixel(
 34      board.NEOPIXEL, 1, brightness=0.2
 35  )  # Uncomment for Most Boards
 36  """Uncomment below for ItsyBitsy M4"""
 37  # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
 38  # Uncomment below for an externally defined RGB LED
 39  # import adafruit_rgbled
 40  # from adafruit_esp32spi import PWMOut
 41  # RED_LED = PWMOut.PWMOut(esp, 26)
 42  # GREEN_LED = PWMOut.PWMOut(esp, 27)
 43  # BLUE_LED = PWMOut.PWMOut(esp, 25)
 44  # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
 45  wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 46  
 47  ### Topic Setup ###
 48  
 49  # MQTT Topic
 50  # Use this topic if you'd like to connect to a standard MQTT broker
 51  mqtt_topic = "test/topic"
 52  
 53  # Adafruit IO-style Topic
 54  # Use this topic if you'd like to connect to io.adafruit.com
 55  # mqtt_topic = 'aio_user/feeds/temperature'
 56  
 57  ### Code ###
 58  
 59  # Define callback methods which are called when events occur
 60  # pylint: disable=unused-argument, redefined-outer-name
 61  def connect(client, userdata, flags, rc):
 62      # This function will be called when the client is connected
 63      # successfully to the broker.
 64      print("Connected to MQTT Broker!")
 65      print("Flags: {0}\n RC: {1}".format(flags, rc))
 66  
 67  
 68  def disconnect(client, userdata, rc):
 69      # This method is called when the client disconnects
 70      # from the broker.
 71      print("Disconnected from MQTT Broker!")
 72  
 73  
 74  def subscribe(client, userdata, topic, granted_qos):
 75      # This method is called when the client subscribes to a new feed.
 76      print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
 77  
 78  
 79  def unsubscribe(client, userdata, topic, pid):
 80      # This method is called when the client unsubscribes from a feed.
 81      print("Unsubscribed from {0} with PID {1}".format(topic, pid))
 82  
 83  
 84  def publish(client, userdata, topic, pid):
 85      # This method is called when the client publishes data to a feed.
 86      print("Published to {0} with PID {1}".format(topic, pid))
 87  
 88  
 89  # Get certificate and private key from a certificates.py file
 90  try:
 91      from certificates import DEVICE_CERT, DEVICE_KEY
 92  except ImportError:
 93      print(
 94          "Certificate and private key data is kept in certificates.py, please add them there!"
 95      )
 96      raise
 97  
 98  # Set Device Certificate
 99  esp.set_certificate(DEVICE_CERT)
100  
101  # Set Private Key
102  esp.set_private_key(DEVICE_KEY)
103  
104  # Connect to WiFi
105  print("Connecting to WiFi...")
106  wifi.connect()
107  print("Connected!")
108  
109  # Initialize MQTT interface with the esp interface
110  MQTT.set_socket(socket, esp)
111  
112  # Set up a MiniMQTT Client
113  client = MQTT.MQTT(
114      broker=secrets["broker"], username=secrets["user"], password=secrets["pass"]
115  )
116  
117  # Connect callback handlers to client
118  client.on_connect = connect
119  client.on_disconnect = disconnect
120  client.on_subscribe = subscribe
121  client.on_unsubscribe = unsubscribe
122  client.on_publish = publish
123  
124  print("Attempting to connect to %s" % client.broker)
125  client.connect()
126  
127  print("Subscribing to %s" % mqtt_topic)
128  client.subscribe(mqtt_topic)
129  
130  print("Publishing to %s" % mqtt_topic)
131  client.publish(mqtt_topic, "Hello Broker!")
132  
133  print("Unsubscribing from %s" % mqtt_topic)
134  client.unsubscribe(mqtt_topic)
135  
136  print("Disconnecting from %s" % client.broker)
137  client.disconnect()