code.py
  1  # SPDX-FileCopyrightText: 2020 Brent Rubell for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  import board
  7  import busio
  8  from digitalio import DigitalInOut
  9  import neopixel
 10  from adafruit_esp32spi import adafruit_esp32spi
 11  from adafruit_esp32spi import adafruit_esp32spi_wifimanager
 12  import adafruit_esp32spi.adafruit_esp32spi_socket as socket
 13  
 14  import adafruit_minimqtt.adafruit_minimqtt as MQTT
 15  
 16  ### WiFi ###
 17  
 18  # Get wifi details and more from a secrets.py file
 19  try:
 20      from secrets import secrets
 21  except ImportError:
 22      print("WiFi secrets are kept in secrets.py, please add them there!")
 23      raise
 24  
 25  # If you are using a board with pre-defined ESP32 Pins:
 26  esp32_cs = DigitalInOut(board.ESP_CS)
 27  esp32_ready = DigitalInOut(board.ESP_BUSY)
 28  esp32_reset = DigitalInOut(board.ESP_RESET)
 29  
 30  # If you have an externally connected ESP32:
 31  # esp32_cs = DigitalInOut(board.D9)
 32  # esp32_ready = DigitalInOut(board.D10)
 33  # esp32_reset = DigitalInOut(board.D5)
 34  
 35  spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
 36  esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
 37  """Use below for Most Boards"""
 38  status_light = neopixel.NeoPixel(
 39      board.NEOPIXEL, 1, brightness=0.2
 40  )  # Uncomment for Most Boards
 41  """Uncomment below for ItsyBitsy M4"""
 42  # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
 43  # Uncomment below for an externally defined RGB LED
 44  # import adafruit_rgbled
 45  # from adafruit_esp32spi import PWMOut
 46  # RED_LED = PWMOut.PWMOut(esp, 26)
 47  # GREEN_LED = PWMOut.PWMOut(esp, 27)
 48  # BLUE_LED = PWMOut.PWMOut(esp, 25)
 49  # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
 50  wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
 51  
 52  # Set up a pin for controlling the relay
 53  power_pin = DigitalInOut(board.D3)
 54  power_pin.switch_to_output()
 55  
 56  ### Feeds ###
 57  # Set up a feed named Relay for subscribing to the relay feed on Adafruit IO
 58  feed_relay = secrets["aio_username"] + "/feeds/relay"
 59  
 60  ### Code ###
 61  
 62  # Define callback methods which are called when events occur
 63  # pylint: disable=unused-argument, redefined-outer-name
 64  def connected(client, userdata, flags, rc):
 65      # This function will be called when the client is connected
 66      # successfully to the broker.
 67      print("Connected to Adafruit IO!")
 68  
 69  
 70  def disconnected(client, userdata, rc):
 71      # This method is called when the client is disconnected
 72      print("Disconnected from Adafruit IO!")
 73  
 74  
 75  def subscribe(client, userdata, topic, granted_qos):
 76      # This method is called when the client subscribes to a new feed.
 77      print("Subscribed to {0}".format(topic))
 78  
 79  
 80  def unsubscribe(client, userdata, topic, pid):
 81      # This method is called when the client unsubscribes from a feed.
 82      print("Unsubscribed from {0} with PID {1}".format(topic, pid))
 83  
 84  def on_message(client, topic, message):
 85      # Method callled when a client's subscribed feed has a new value.
 86      print("New message on topic {0}: {1}".format(topic, message))
 87  
 88  
 89  def on_relay_msg(client, topic, value):
 90      # Called when relay feed obtains a new value
 91      print("Turning Relay %s"%value)
 92      if value == "ON":
 93          power_pin.value = True
 94      elif value == "OFF":
 95          power_pin.value = False
 96      else:
 97          print("Unexpected value received on relay feed.")
 98  
 99  # Connect to WiFi
100  print("Connecting to WiFi...")
101  wifi.connect()
102  print("Connected!")
103  
104  MQTT.set_socket(socket, esp)
105  
106  # Set up a MiniMQTT Client
107  client = MQTT.MQTT(
108      broker="io.adafruit.com",
109      username=secrets["aio_username"],
110      password=secrets["aio_key"],
111  )
112  
113  # Setup the callback methods above
114  client.on_connect = connected
115  client.on_disconnect = disconnected
116  client.on_subscribe = subscribe
117  client.on_unsubscribe = unsubscribe
118  client.on_message = on_message
119  # Add a callback to the relay feed
120  client.add_topic_callback(feed_relay, on_relay_msg)
121  
122  # Connect the client to Adafruit IO
123  print("Connecting to Adafruit IO...")
124  client.connect()
125  
126  # Subscribe to all updates on relay feed
127  client.subscribe(feed_relay)
128  
129  while True:
130      try: # Poll for new messages on feed_relay
131          client.loop()
132      except (ValueError, RuntimeError, ConnectionError, OSError) as e:
133          print("Failed to get data, retrying\n", e)
134          wifi.reset()
135          client.reconnect()
136          continue
137      time.sleep(0.05)