|
| 1 | +# Adafruit MiniMQTT Pub/Sub Example |
| 2 | +# Written by Tony DiCola for Adafruit Industries |
| 3 | +# Modified by Brent Rubell for Adafruit Industries |
1 | 4 | import time |
2 | 5 | import board |
3 | 6 | import busio |
|
30 | 33 | spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
31 | 34 | esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
32 | 35 | """Use below for Most Boards""" |
33 | | -status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards |
| 36 | +status_light = neopixel.NeoPixel( |
| 37 | + board.NEOPIXEL, 1, brightness=0.2) # Uncomment for Most Boards |
34 | 38 | """Uncomment below for ItsyBitsy M4""" |
35 | 39 | # status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2) |
36 | 40 | # Uncomment below for an externally defined RGB LED |
|
40 | 44 | # GREEN_LED = PWMOut.PWMOut(esp, 27) |
41 | 45 | # BLUE_LED = PWMOut.PWMOut(esp, 25) |
42 | 46 | # status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED) |
43 | | -wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 47 | +wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager( |
| 48 | + esp, secrets, status_light) |
44 | 49 |
|
45 | | -### Adafruit IO Setup ### |
| 50 | +### Feeds ### |
46 | 51 |
|
47 | | -# Setup a feed named `photocell` for publishing. |
48 | | -aio_publish_feed = secrets['user']+'/feeds/photocell' |
| 52 | +# Setup a feed named 'photocell' for publishing to a feed |
| 53 | +photocell_feed = secrets['aio_username'] + '/feeds/photocell' |
49 | 54 |
|
50 | | -# Setup a feed named `onoffbutton` for subscribing to changes. |
51 | | -aio_subscribe_feed = secrets['user']+'/feeds/onoffbutton' |
| 55 | +# Setup a feed named 'onoff' for subscribing to changes |
| 56 | +onoff_feed = secrets['aio_username'] + '/feeds/onoff' |
52 | 57 |
|
53 | 58 | ### Code ### |
54 | 59 |
|
55 | | -# pylint: disable=unused-argument |
56 | | -def on_message(client, topic, message): |
57 | | - # This method is called whenever a new message is received |
58 | | - # from the server. |
| 60 | +# Define callback methods which are called when events occur |
| 61 | +# pylint: disable=unused-argument, redefined-outer-name |
| 62 | +def connected(client, userdata, flags, rc): |
| 63 | + # This function will be called when the client is connected |
| 64 | + # successfully to the broker. |
| 65 | + print('Connected to Adafruit IO! Listening for topic changes on %s' % onoff_feed) |
| 66 | + # Subscribe to all changes on the onoff_feed. |
| 67 | + client.subscribe(onoff_feed) |
| 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 message(client, topic, message): |
| 76 | + # This method is called when a topic the client is subscribed to |
| 77 | + # has a new message. |
59 | 78 | print('New message on topic {0}: {1}'.format(topic, message)) |
60 | 79 |
|
| 80 | + |
61 | 81 | # Connect to WiFi |
62 | 82 | wifi.connect() |
63 | 83 |
|
64 | 84 | # Set up a MiniMQTT Client |
65 | 85 | mqtt_client = MQTT(socket, |
66 | | - broker = secrets['broker'], |
67 | | - username = secrets['user'], |
68 | | - password = secrets['pass'], |
69 | | - network_manager = wifi) |
70 | | - |
71 | | -# Attach on_message method to the MQTT Client |
72 | | -mqtt_client.on_message = on_message |
73 | | - |
74 | | -# Initialize the MQTT Client |
| 86 | + broker='io.adafruit.com', |
| 87 | + username=secrets['aio_username'], |
| 88 | + password=secrets['aio_key'], |
| 89 | + network_manager=wifi) |
| 90 | + |
| 91 | +# Setup the callback methods above |
| 92 | +mqtt_client.on_connect = connected |
| 93 | +mqtt_client.on_disconnect = disconnected |
| 94 | +mqtt_client.on_message = message |
| 95 | + |
| 96 | +# Connect the client to the MQTT broker. |
| 97 | +print('Connecting to Adafruit IO...') |
75 | 98 | mqtt_client.connect() |
76 | 99 |
|
77 | | -# Subscribe the client to topic aio_subscribe_feed |
78 | | -mqtt_client.subscribe(aio_subscribe_feed) |
79 | | - |
80 | 100 | photocell_val = 0 |
81 | 101 | while True: |
82 | 102 | # Poll the message queue |
83 | 103 | mqtt_client.loop() |
84 | 104 |
|
85 | | - print('Sending photocell value: %d'%photocell_val) |
86 | | - mqtt_client.publish(aio_publish_feed, photocell_val) |
| 105 | + # Send a new message |
| 106 | + print('Sending photocell value: %d...' % photocell_val) |
| 107 | + mqtt_client.publish(photocell_feed, photocell_val) |
| 108 | + print('Sent!') |
87 | 109 | photocell_val += 1 |
88 | | - time.sleep(0.5) |
| 110 | + time.sleep(1) |
0 commit comments