/ examples / aws_iot_simpletest.py
aws_iot_simpletest.py
  1  import time
  2  import json
  3  import board
  4  import busio
  5  from digitalio import DigitalInOut
  6  import neopixel
  7  from adafruit_esp32spi import adafruit_esp32spi
  8  from adafruit_esp32spi import adafruit_esp32spi_wifimanager
  9  import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 10  import adafruit_minimqtt as MQTT
 11  from adafruit_aws_iot import MQTT_CLIENT
 12  
 13  ### WiFi ###
 14  
 15  # Get wifi details and more from a secrets.py file
 16  try:
 17      from secrets import secrets
 18  except ImportError:
 19      print("WiFi secrets are kept in secrets.py, please add them there!")
 20      raise
 21  
 22  # Get device certificate
 23  try:
 24      with open("aws_cert.pem.crt", "rb") as f:
 25          DEVICE_CERT = f.read()
 26  except ImportError:
 27      print("Certificate (aws_cert.pem.crt) not found on CIRCUITPY filesystem.")
 28      raise
 29  
 30  # Get device private key
 31  try:
 32      with open("private.pem.key", "rb") as f:
 33          DEVICE_KEY = f.read()
 34  except ImportError:
 35      print("Certificate (private.pem.key) not found on CIRCUITPY filesystem.")
 36      raise
 37  
 38  # If you are using a board with pre-defined ESP32 Pins:
 39  esp32_cs = DigitalInOut(board.ESP_CS)
 40  esp32_ready = DigitalInOut(board.ESP_BUSY)
 41  esp32_reset = DigitalInOut(board.ESP_RESET)
 42  
 43  # If you have an externally connected ESP32:
 44  # esp32_cs = DigitalInOut(board.D9)
 45  # esp32_ready = DigitalInOut(board.D10)
 46  # esp32_reset = DigitalInOut(board.D5)
 47  
 48  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 49  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 50  
 51  # Verify nina-fw version >= 1.4.0
 52  assert (
 53      int(bytes(esp.firmware_version).decode("utf-8")[2]) >= 4
 54  ), "Please update nina-fw to >=1.4.0."
 55  
 56  # Use below for Most Boards
 57  status_light = neopixel.NeoPixel(
 58      board.NEOPIXEL, 1, brightness=0.2
 59  )  # Uncomment for Most Boards
 60  # Uncomment below for ItsyBitsy M4
 61  # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
 62  # Uncomment below for an externally defined RGB LED
 63  # import adafruit_rgbled
 64  # from adafruit_esp32spi import PWMOut
 65  # RED_LED = PWMOut.PWMOut(esp, 26)
 66  # GREEN_LED = PWMOut.PWMOut(esp, 27)
 67  # BLUE_LED = PWMOut.PWMOut(esp, 25)
 68  # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
 69  wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 70  
 71  ### Code ###
 72  
 73  topic = "circuitpython/aws"
 74  
 75  # Define callback methods which are called when events occur
 76  # pylint: disable=unused-argument, redefined-outer-name
 77  def connect(client, userdata, flags, rc):
 78      # This function will be called when the client is connected
 79      # successfully to the broker.
 80      print("Connected to MQTT Broker!")
 81      print("Flags: {0}\n RC: {1}".format(flags, rc))
 82  
 83      # Subscribe to topic circuitpython/aws
 84      print("Subscribing to topic {}".format(topic))
 85      aws_iot.subscribe(topic)
 86  
 87  
 88  def disconnect(client, userdata, rc):
 89      # This method is called when the client disconnects
 90      # from the broker.
 91      print("Disconnected from MQTT Broker!")
 92  
 93  
 94  def subscribe(client, userdata, topic, granted_qos):
 95      # This method is called when the client subscribes to a new topic.
 96      print("Subscribed to {0} with QOS level {1}".format(topic, granted_qos))
 97  
 98      # Create a json-formatted message
 99      message = {"message": "Hello from AWS IoT CircuitPython"}
100      # Publish message to topic
101      aws_iot.publish(topic, json.dumps(message))
102  
103  
104  def unsubscribe(client, userdata, topic, pid):
105      # This method is called when the client unsubscribes from a topic.
106      print("Unsubscribed from {0} with PID {1}".format(topic, pid))
107  
108  
109  def publish(client, userdata, topic, pid):
110      # This method is called when the client publishes data to a topic.
111      print("Published to {0} with PID {1}".format(topic, pid))
112  
113  
114  def message(client, topic, msg):
115      # This method is called when the client receives data from a topic.
116      print("Message from {}: {}".format(topic, msg))
117  
118  
119  # Set AWS Device Certificate
120  esp.set_certificate(DEVICE_CERT)
121  
122  # Set AWS RSA Private Key
123  esp.set_private_key(DEVICE_KEY)
124  
125  # Connect to WiFi
126  print("Connecting to WiFi...")
127  wifi.connect()
128  print("Connected!")
129  
130  # Initialize MQTT interface with the esp interface
131  MQTT.set_socket(socket, esp)
132  
133  # Set up a new MiniMQTT Client
134  client = MQTT.MQTT(broker=secrets["broker"], client_id=secrets["client_id"])
135  
136  # Initialize AWS IoT MQTT API Client
137  aws_iot = MQTT_CLIENT(client)
138  
139  # Connect callback handlers to AWS IoT MQTT Client
140  aws_iot.on_connect = connect
141  aws_iot.on_disconnect = disconnect
142  aws_iot.on_subscribe = subscribe
143  aws_iot.on_unsubscribe = unsubscribe
144  aws_iot.on_publish = publish
145  aws_iot.on_message = message
146  
147  print("Attempting to connect to %s" % client.broker)
148  aws_iot.connect()
149  
150  # Pump the message loop forever, all events
151  # are handled in their callback handlers
152  # while True:
153  #   aws_iot.loop()
154  
155  # Start a blocking message loop...
156  # NOTE: NO code below this loop will execute
157  # NOTE: Network reconnection is handled within this loop
158  while True:
159      try:
160          aws_iot.loop()
161      except (ValueError, RuntimeError) as e:
162          print("Failed to get data, retrying\n", e)
163          wifi.reset()
164          aws_iot.reconnect()
165          continue
166      time.sleep(1)