Skip to content

Commit 566b6cf

Browse files
author
brentru
committed
add time topic example
1 parent 581dcbc commit 566b6cf

File tree

2 files changed

+126
-7
lines changed

2 files changed

+126
-7
lines changed

examples/mqtt/adafruit_io_mqtt_simpletest.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,28 @@ def message(client, feed_id, payload):
7979
# Connect to WiFi
8080
wifi.connect()
8181

82-
# Initialize a new MQTT Client
82+
# Initialize a new MQTT Client object
8383
client = MQTT(
8484
socket=socket,
8585
broker="io.adafruit.com",
8686
username=secrets["aio_user"],
8787
password=secrets["aio_key"],
8888
network_manager=wifi,
89+
log=True,
8990
)
9091

91-
# Setup the callback functions defined above.
92-
client.on_connect = connected
93-
client.on_disconnect = disconnected
94-
client.on_message = message
95-
9692
# Initialize an Adafruit IO MQTT Client
9793
io = IO_MQTT(client)
9894

95+
# Connect the callback methods defined above to Adafruit IO
96+
io.on_connect = connected
97+
io.on_disconnect = disconnected
98+
io.on_message = message
99+
99100
# Connect to Adafruit IO
100101
io.connect()
101102

102-
# You can call the message loop every X seconds
103+
# Below is an example of manually publishing a new value to Adafruit IO.
103104
last = 0
104105
print("Publishing a new message every 10 seconds...")
105106
while True:
@@ -112,6 +113,7 @@ def message(client, feed_id, payload):
112113
io.publish("DemoFeed", value)
113114
last = time.monotonic()
114115

116+
115117
# You can also call loop_blocking if you only want to receive values.
116118
# NOTE: If uncommented, no code below this line will run.
117119
# io.loop_blocking()
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Adafruit IO provides some built-in MQTT topics
2+
# for obtaining the current server time, if you don't have
3+
# access to a RTC module.
4+
5+
import time
6+
from random import randint
7+
import board
8+
import neopixel
9+
import busio
10+
from digitalio import DigitalInOut
11+
from adafruit_esp32spi import adafruit_esp32spi
12+
from adafruit_esp32spi import adafruit_esp32spi_wifimanager
13+
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
14+
15+
from adafruit_minimqtt import MQTT
16+
from adafruit_io.adafruit_io import IO_MQTT
17+
18+
### WiFi ###
19+
20+
# Get wifi details and more from a secrets.py file
21+
try:
22+
from secrets import secrets
23+
except ImportError:
24+
print("WiFi secrets are kept in secrets.py, please add them there!")
25+
raise
26+
27+
# If you are using a board with pre-defined ESP32 Pins:
28+
esp32_cs = DigitalInOut(board.ESP_CS)
29+
esp32_ready = DigitalInOut(board.ESP_BUSY)
30+
esp32_reset = DigitalInOut(board.ESP_RESET)
31+
32+
# If you have an externally connected ESP32:
33+
# esp32_cs = DigitalInOut(board.D9)
34+
# esp32_ready = DigitalInOut(board.D10)
35+
# esp32_reset = DigitalInOut(board.D5)
36+
37+
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
38+
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
39+
"""Use below for Most Boards"""
40+
status_light = neopixel.NeoPixel(
41+
board.NEOPIXEL, 1, brightness=0.2
42+
) # Uncomment for Most Boards
43+
"""Uncomment below for ItsyBitsy M4"""
44+
# status_light = dotstar.DotStar(board.APA102_SCK, board.APA102_MOSI, 1, brightness=0.2)
45+
# Uncomment below for an externally defined RGB LED
46+
# import adafruit_rgbled
47+
# from adafruit_esp32spi import PWMOut
48+
# RED_LED = PWMOut.PWMOut(esp, 26)
49+
# GREEN_LED = PWMOut.PWMOut(esp, 27)
50+
# BLUE_LED = PWMOut.PWMOut(esp, 25)
51+
# status_light = adafruit_rgbled.RGBLED(RED_LED, BLUE_LED, GREEN_LED)
52+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light)
53+
54+
# Define callback functions which will be called when certain events happen.
55+
def connected(client):
56+
# Connected function will be called when the client is connected to Adafruit IO.
57+
# This is a good place to subscribe to feed changes. The client parameter
58+
# passed to this function is the Adafruit IO MQTT client so you can make
59+
# calls against it easily.
60+
print("Connected to Adafruit IO!")
61+
62+
# Subscribe to time/seconds topic
63+
# https://io.adafruit.com/api/docs/mqtt.html#time-seconds
64+
io.subscribe_to_time('seconds')
65+
66+
# Subscribe to time/millis topic
67+
# https://io.adafruit.com/api/docs/mqtt.html#time-millis
68+
io.subscribe_to_time('millis')
69+
70+
# Subscribe to time/ISO-8601 topic
71+
# https://io.adafruit.com/api/docs/mqtt.html#time-iso-8601
72+
io.subscribe_to_time('iso')
73+
74+
# Subscribe to time/hours topic
75+
# NOTE: This topic only publishes once every hour.
76+
# https://io.adafruit.com/api/docs/mqtt.html#adafruit-io-monitor
77+
io.subscribe_to_time('hours')
78+
79+
80+
def disconnected(client):
81+
# Disconnected function will be called when the client disconnects.
82+
print("Disconnected from Adafruit IO!")
83+
84+
85+
def message(client, feed_id, payload):
86+
# Message function will be called when a subscribed feed has a new value.
87+
# The feed_id parameter identifies the feed, and the payload parameter has
88+
# the new value.
89+
print("Feed {0} received new value: {1}".format(feed_id, payload))
90+
91+
92+
# Connect to WiFi
93+
wifi.connect()
94+
95+
# Initialize a new MQTT Client object
96+
client = MQTT(
97+
socket=socket,
98+
broker="io.adafruit.com",
99+
username=secrets["aio_user"],
100+
password=secrets["aio_key"],
101+
network_manager=wifi,
102+
log=True
103+
)
104+
105+
# Initialize an Adafruit IO MQTT Client
106+
io = IO_MQTT(client)
107+
108+
# Connect the callback methods defined above to Adafruit IO
109+
io.on_connect = connected
110+
io.on_disconnect = disconnected
111+
io.on_message = message
112+
113+
# Connect to Adafruit IO
114+
io.connect()
115+
116+
# Listen forever...
117+
io.loop_blocking()

0 commit comments

Comments
 (0)