Skip to content

Commit b4c0850

Browse files
author
brentru
committed
_eth simpletest
1 parent 3da4080 commit b4c0850

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

examples/minimqtt_simpletest_eth.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import board
2+
import busio
3+
from digitalio import DigitalInOut
4+
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
5+
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
6+
7+
import adafruit_minimqtt as MQTT
8+
9+
# Get MQTT details and more from a secrets.py file
10+
try:
11+
from secrets import secrets
12+
except ImportError:
13+
print("MQTT secrets are kept in secrets.py, please add them there!")
14+
raise
15+
16+
cs = DigitalInOut(board.D10)
17+
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
18+
19+
# Initialize ethernet interface with DHCP
20+
eth = WIZNET5K(spi_bus, cs)
21+
### Topic Setup ###
22+
23+
# MQTT Topic
24+
# Use this topic if you'd like to connect to a standard MQTT broker
25+
mqtt_topic = 'test/topic'
26+
27+
# Adafruit IO-style Topic
28+
# Use this topic if you'd like to connect to io.adafruit.com
29+
# mqtt_topic = 'aio_user/feeds/temperature'
30+
31+
### Code ###
32+
33+
# Define callback methods which are called when events occur
34+
# pylint: disable=unused-argument, redefined-outer-name
35+
def connect(client, userdata, flags, rc):
36+
# This function will be called when the client is connected
37+
# successfully to the broker.
38+
print('Connected to MQTT Broker!')
39+
print('Flags: {0}\n RC: {1}'.format(flags, rc))
40+
41+
def disconnect(client, userdata, rc):
42+
# This method is called when the client disconnects
43+
# from the broker.
44+
print('Disconnected from MQTT Broker!')
45+
46+
def subscribe(client, userdata, topic, granted_qos):
47+
# This method is called when the client subscribes to a new feed.
48+
print('Subscribed to {0} with QOS level {1}'.format(topic, granted_qos))
49+
50+
def unsubscribe(client, userdata, topic, pid):
51+
# This method is called when the client unsubscribes from a feed.
52+
print('Unsubscribed from {0} with PID {1}'.format(topic, pid))
53+
54+
def publish(client, userdata, topic, pid):
55+
# This method is called when the client publishes data to a feed.
56+
print('Published to {0} with PID {1}'.format(topic, pid))
57+
58+
# Initialize MQTT interface with the ethernet interface
59+
MQTT.set_socket(socket, eth)
60+
61+
# Set up a MiniMQTT Client
62+
client = MQTT.MQTT(broker = secrets['broker'],
63+
username = secrets['user'],
64+
password = secrets['pass'])
65+
66+
# Connect callback handlers to client
67+
client.on_connect = connect
68+
client.on_disconnect = disconnect
69+
client.on_subscribe = subscribe
70+
client.on_unsubscribe = unsubscribe
71+
client.on_publish = publish
72+
73+
print('Attempting to connect to %s'%client.broker)
74+
client.connect()
75+
76+
print('Subscribing to %s'%mqtt_topic)
77+
client.subscribe(mqtt_topic)
78+
79+
print('Publishing to %s'%mqtt_topic)
80+
client.publish(mqtt_topic, 'Hello Broker!')
81+
82+
print('Unsubscribing from %s'%mqtt_topic)
83+
client.unsubscribe(mqtt_topic)
84+
85+
print('Disconnecting from %s'%client.broker)
86+
client.disconnect()

0 commit comments

Comments
 (0)