|
| 1 | +# CircuitPython MiniMQTT Library |
| 2 | +# Adafruit IO SSL/TLS Example for WiFi (ESP32SPI) |
| 3 | +import time |
| 4 | +import board |
| 5 | +import busio |
| 6 | +from digitalio import DigitalInOut |
| 7 | +from adafruit_esp32spi import adafruit_esp32spi |
| 8 | +import adafruit_esp32spi.adafruit_esp32spi_socket as socket |
| 9 | +from adafruit_minimqtt import 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 | + |
| 33 | +### Adafruit IO Setup ### |
| 34 | + |
| 35 | +# Setup a feed named `testfeed` for publishing. |
| 36 | +default_topic = secrets['user']+'/feeds/testfeed' |
| 37 | + |
| 38 | +### Code ### |
| 39 | + |
| 40 | +def connect_wifi(): |
| 41 | + print("Connecting to %s..."%secrets['ssid']) |
| 42 | + while not esp.is_connected: |
| 43 | + try: |
| 44 | + esp.connect_AP(secrets['ssid'], secrets['password']) |
| 45 | + except RuntimeError as e: |
| 46 | + print("could not connect to AP, retrying: ",e) |
| 47 | + continue |
| 48 | + print("Connected to", str(esp.ssid, 'utf-8'), "\tRSSI:", esp.rssi) |
| 49 | + print("IP: ", esp.pretty_ip(esp.ip_address)) |
| 50 | + |
| 51 | +# Define callback methods which are called when events occur |
| 52 | +# pylint: disable=unused-argument |
| 53 | +def connected(client, userdata, flags, rc): |
| 54 | + # This function will be called when the client is connected |
| 55 | + # successfully to the broker. |
| 56 | + print('Connected to MQTT broker! Listening for topic changes on %s'%default_topic) |
| 57 | + # Subscribe to all changes on the default_topic feed. |
| 58 | + client.subscribe(default_topic) |
| 59 | + |
| 60 | +def disconnected(client, userdata, rc): |
| 61 | + # This method is called when the client is disconnected |
| 62 | + print('Disconnected from MQTT Broker!') |
| 63 | + |
| 64 | +def message(client, topic, message): |
| 65 | + """Method callled when a client's subscribed feed has a new |
| 66 | + value. |
| 67 | + :param str topic: The topic of the feed with a new value. |
| 68 | + :param str message: The new value |
| 69 | + """ |
| 70 | + print('New message on topic {0}: {1}'.format(topic, message)) |
| 71 | + |
| 72 | +# Connect to WiFi |
| 73 | +connect_wifi() |
| 74 | + |
| 75 | +# Initialize a MiniMQTT Client |
| 76 | +mqtt_client = MQTT(socket, |
| 77 | + broker = secrets['broker'], |
| 78 | + username = secrets['user'], |
| 79 | + password = secrets['pass'], |
| 80 | + esp = esp) |
| 81 | + |
| 82 | +# Setup the callback methods above |
| 83 | +mqtt_client.on_connect = connected |
| 84 | +mqtt_client.on_disconnect = disconnected |
| 85 | +mqtt_client.on_message = message |
| 86 | + |
| 87 | +# Connect the client to the MQTT broker. |
| 88 | +mqtt_client.connect() |
| 89 | + |
| 90 | +# Start a blocking message loop |
| 91 | +# If you only want to listen to incoming messages, |
| 92 | +# you'll want to loop_forever as it handles network reconnections |
| 93 | +# No code below this line will execute. |
| 94 | +mqtt_client.loop_forever() |
0 commit comments