Skip to content

Commit 1d78c0f

Browse files
author
brentru
committed
add cpython example for AIO, tested
1 parent cb322ed commit 1d78c0f

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 socket
6+
import adafruit_minimqtt.adafruit_minimqtt as MQTT
7+
8+
### Secrets File Setup ###
9+
10+
try:
11+
from secrets import secrets
12+
except ImportError:
13+
print("Connection secrets are kept in secrets.py, please add them there!")
14+
raise
15+
16+
### Feeds ###
17+
18+
# Setup a feed named 'photocell' for publishing to a feed
19+
photocell_feed = secrets["aio_username"] + "/feeds/photocell"
20+
21+
# Setup a feed named 'onoff' for subscribing to changes
22+
onoff_feed = secrets["aio_username"] + "/feeds/onoff"
23+
24+
### Code ###
25+
26+
# Define callback methods which are called when events occur
27+
# pylint: disable=unused-argument, redefined-outer-name
28+
def connected(client, userdata, flags, rc):
29+
# This function will be called when the client is connected
30+
# successfully to the broker.
31+
print("Connected to Adafruit IO! Listening for topic changes on %s" % onoff_feed)
32+
# Subscribe to all changes on the onoff_feed.
33+
client.subscribe(onoff_feed)
34+
35+
36+
def disconnected(client, userdata, rc):
37+
# This method is called when the client is disconnected
38+
print("Disconnected from Adafruit IO!")
39+
40+
41+
def message(client, topic, message):
42+
# This method is called when a topic the client is subscribed to
43+
# has a new message.
44+
print("New message on topic {0}: {1}".format(topic, message))
45+
46+
47+
# Set up a MiniMQTT Client
48+
mqtt_client = MQTT.MQTT(
49+
broker=secrets['broker'],
50+
port=secrets['port'],
51+
username=secrets['aio_username'],
52+
password=secrets['aio_key'],
53+
socket_pool=socket
54+
)
55+
56+
# Setup the callback methods above
57+
mqtt_client.on_connect = connected
58+
mqtt_client.on_disconnect = disconnected
59+
mqtt_client.on_message = message
60+
61+
# Connect the client to the MQTT broker.
62+
print("Connecting to Adafruit IO...")
63+
mqtt_client.connect()
64+
65+
photocell_val = 0
66+
while True:
67+
# Poll the message queue
68+
mqtt_client.loop()
69+
70+
# Send a new message
71+
print("Sending photocell value: %d..." % photocell_val)
72+
mqtt_client.publish(photocell_feed, photocell_val)
73+
print("Sent!")
74+
photocell_val += 1
75+
time.sleep(1)

examples/minimqtt_simpletest_cpython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import socket
22
import adafruit_minimqtt.adafruit_minimqtt as MQTT
33

4-
### Secrets Setup ###
4+
### Secrets File Setup ###
55

66
try:
77
from secrets import secrets

0 commit comments

Comments
 (0)