Skip to content

Commit 7f202f3

Browse files
author
brentru
committed
add adafruitio_ethernet mmqt example
1 parent bad50b9 commit 7f202f3

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

examples/minimqtt_adafruitio_eth.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Adafruit MiniMQTT Pub/Sub Example
2+
# Written by Tony DiCola for Adafruit Industries
3+
# Modified by Brent Rubell for Adafruit Industries
4+
import time
5+
import board
6+
import busio
7+
from digitalio import DigitalInOut
8+
9+
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
10+
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
11+
12+
import adafruit_minimqtt as MQTT
13+
14+
# Get Adafruit IO details and more from a secrets.py file
15+
try:
16+
from secrets import secrets
17+
except ImportError:
18+
print("Adafruit IO secrets are kept in secrets.py, please add them there!")
19+
raise
20+
21+
cs = DigitalInOut(board.D10)
22+
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
23+
24+
# Initialize ethernet interface with DHCP
25+
eth = WIZNET5K(spi_bus, cs)
26+
27+
### Feeds ###
28+
29+
# Setup a feed named 'photocell' for publishing to a feed
30+
photocell_feed = secrets['aio_username'] + '/feeds/photocell'
31+
32+
# Setup a feed named 'onoff' for subscribing to changes
33+
onoff_feed = secrets['aio_username'] + '/feeds/onoff'
34+
35+
### Code ###
36+
37+
# Define callback methods which are called when events occur
38+
# pylint: disable=unused-argument, redefined-outer-name
39+
def connected(client, userdata, flags, rc):
40+
# This function will be called when the client is connected
41+
# successfully to the broker.
42+
print('Connected to Adafruit IO! Listening for topic changes on %s' % onoff_feed)
43+
# Subscribe to all changes on the onoff_feed.
44+
client.subscribe(onoff_feed)
45+
46+
47+
def disconnected(client, userdata, rc):
48+
# This method is called when the client is disconnected
49+
print('Disconnected from Adafruit IO!')
50+
51+
52+
def message(client, topic, message):
53+
# This method is called when a topic the client is subscribed to
54+
# has a new message.
55+
print('New message on topic {0}: {1}'.format(topic, message))
56+
57+
58+
# Initialize MQTT interface with the ethernet interface
59+
MQTT.set_socket(socket, eth)
60+
61+
# Set up a MiniMQTT Client
62+
# NOTE: We'll need to connect insecurely for ethernet configurations.
63+
mqtt_client = MQTT.MQTT(broker = 'io.adafruit.com',
64+
username = secrets['aio_username'],
65+
password = secrets['aio_key'],
66+
is_ssl = False)
67+
68+
# Setup the callback methods above
69+
mqtt_client.on_connect = connected
70+
mqtt_client.on_disconnect = disconnected
71+
mqtt_client.on_message = message
72+
73+
# Connect the client to the MQTT broker.
74+
print('Connecting to Adafruit IO...')
75+
mqtt_client.connect()
76+
77+
photocell_val = 0
78+
while True:
79+
# Poll the message queue
80+
mqtt_client.loop()
81+
82+
# Send a new message
83+
print('Sending photocell value: %d...' % photocell_val)
84+
mqtt_client.publish(photocell_feed, photocell_val)
85+
print('Sent!')
86+
photocell_val += 1
87+
time.sleep(5)

0 commit comments

Comments
 (0)