minimqtt_simpletest.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 # Connect to WiFi 90 print("Connecting to WiFi...") 91 wifi.connect() 92 print("Connected!") 93 94 # Initialize MQTT interface with the esp interface 95 MQTT.set_socket(socket, esp) 96 97 # Set up a MiniMQTT Client 98 client = MQTT.MQTT( 99 broker=secrets["broker"], username=secrets["user"], password=secrets["pass"] 100 ) 101 102 # Connect callback handlers to client 103 client.on_connect = connect 104 client.on_disconnect = disconnect 105 client.on_subscribe = subscribe 106 client.on_unsubscribe = unsubscribe 107 client.on_publish = publish 108 109 print("Attempting to connect to %s" % client.broker) 110 client.connect() 111 112 print("Subscribing to %s" % mqtt_topic) 113 client.subscribe(mqtt_topic) 114 115 print("Publishing to %s" % mqtt_topic) 116 client.publish(mqtt_topic, "Hello Broker!") 117 118 print("Unsubscribing from %s" % mqtt_topic) 119 client.unsubscribe(mqtt_topic) 120 121 print("Disconnecting from %s" % client.broker) 122 client.disconnect()