-
Notifications
You must be signed in to change notification settings - Fork 17
/
subscribeAdafruit.py
108 lines (94 loc) · 3.56 KB
/
subscribeAdafruit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# The MIT License (MIT)
# Copyright (c) 2019 Mike Teachman
# https://opensource.org/licenses/MIT
#
# Example MicroPython and CircuitPython code showing how to use the MQTT protocol to
# subscribe to an Adafruit IO feed
#
# Tested using the releases:
# ESP8266
# MicroPython 1.9.3
# MicroPython 1.9.4
# MicroPython 1.10
# CircuitPython 2.3.1 (needs addition of CircuitPython specific umqtt module)
# CircuitPython 3.0.0 (needs addition of CircuitPython specific umqtt module)
# ESP32
# MicroPython 1.9.4 (needs addition of MicroPython umqtt module)
# MicroPython 1.10
#
# Tested using the following boards:
# Adafruit Feather HUZZAH ESP8266
# Adafruit Feather HUZZAH ESP32
# WeMos D1 Mini
#
# User configuration parameters are indicated with "ENTER_".
import network
import time
from umqtt.robust import MQTTClient
import os
import sys
# the following function is the callback which is
# called when subscribed data is received
def cb(topic, msg):
print('Received Data: Topic = {}, Msg = {}'.format(topic, msg))
free_heap = int(str(msg,'utf-8'))
print('free heap size = {} bytes'.format(free_heap))
# WiFi connection information
WIFI_SSID = '<ENTER_WIFI_SSID>'
WIFI_PASSWORD = '<ENTER_WIFI_PASSWORD>'
# turn off the WiFi Access Point
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
# connect the device to the WiFi network
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
# wait until the device is connected to the WiFi network
MAX_ATTEMPTS = 20
attempt_count = 0
while not wifi.isconnected() and attempt_count < MAX_ATTEMPTS:
attempt_count += 1
time.sleep(1)
if attempt_count == MAX_ATTEMPTS:
print('could not connect to the WiFi network')
sys.exit()
# create a random MQTT clientID
random_num = int.from_bytes(os.urandom(3), 'little')
mqtt_client_id = bytes('client_'+str(random_num), 'utf-8')
# connect to Adafruit IO MQTT broker using unsecure TCP (port 1883)
#
# To use a secure connection (encrypted) with TLS:
# set MQTTClient initializer parameter to "ssl=True"
# Caveat: a secure connection uses about 9k bytes of the heap
# (about 1/4 of the micropython heap on the ESP8266 platform)
ADAFRUIT_IO_URL = b'io.adafruit.com'
ADAFRUIT_USERNAME = b'<ENTER_ADAFRUIT_USERNAME>'
ADAFRUIT_IO_KEY = b'<ENTER_ADAFRUIT_IO_KEY>'
ADAFRUIT_IO_FEEDNAME = b'freeHeap'
client = MQTTClient(client_id=mqtt_client_id,
server=ADAFRUIT_IO_URL,
user=ADAFRUIT_USERNAME,
password=ADAFRUIT_IO_KEY,
ssl=False)
try:
client.connect()
except Exception as e:
print('could not connect to MQTT server {}{}'.format(type(e).__name__, e))
sys.exit()
mqtt_feedname = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_IO_FEEDNAME), 'utf-8')
client.set_callback(cb)
client.subscribe(mqtt_feedname)
# following two lines is an Adafruit-specific implementation of the Publish "retain" feature
# which allows a Subscription to immediately receive the last Published value for a feed,
# even if that value was Published two hours ago.
# Described in the Adafruit IO blog, April 22, 2018: https://io.adafruit.com/blog/
mqtt_feedname_get = bytes('{:s}/get'.format(mqtt_feedname), 'utf-8')
client.publish(mqtt_feedname_get, '\0')
# wait until data has been Published to the Adafruit IO feed
while True:
try:
client.wait_msg()
except KeyboardInterrupt:
print('Ctrl-C pressed...exiting')
client.disconnect()
sys.exit()