39
39
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT
40
40
"""
41
41
import time
42
- from adafruit_minimqtt import MQTT as MQTTClient
43
42
#from adafruit_io.adafruit_io_errors import AdafruitIO_RequestError, AdafruitIO_ThrottleError
44
43
45
44
__version__ = "0.0.0-auto.0"
49
48
'User-Agent' : 'AIO-CircuitPython/{0}' .format (__version__ )
50
49
}
51
50
52
- forecast_types = ["current" , "forecast_minutes_5" ,
53
- "forecast_minutes_30" , "forecast_hours_1" ,
54
- "forecast_hours_2" , "forecast_hours_6" ,
55
- "forecast_hours_24" , "forecast_days_1" ,
56
- "forecast_days_2" , "forecast_days_5" ,]
57
-
58
- class MQTT ():
51
+ class MQTT_API ():
59
52
"""
60
53
Client for interacting with the Adafruit IO MQTT API. The client establishes
61
54
a secure connection to Adafruit IO by default.
62
- :param str aio_username: Adafruit.io account username.
63
- :param str aio_key: Adafruit.io active key.
64
- :param network_manager: NetworkManager object, such as WiFiManager from ESPSPI_WiFiManager.
65
- :param bool secure: Enables SSL/TLS connection.
55
+ :param MiniMQTT mqtt_client: MiniMQTT Client object.
56
+ :param bool secure: Enables a secure SSL/TLS connection with Adafruit IO.
66
57
"""
67
- def __init__ (self , aio_username , aio_key , network_manager , socket , secure = True ):
58
+ def __init__ (self , mqtt_client , secure = True ):
68
59
self ._user = aio_username
69
60
self ._key = aio_key
70
- # Network interface hardware detection
71
- manager_type = str (type (network_manager ))
72
- if ('ESPSPI_WiFiManager' in manager_type or 'ESPAT_WiFiManager' in manager_type ):
73
- self ._network_manager = network_manager
74
- else :
75
- raise TypeError ("This library requires a NetworkManager object." )
76
- # Set up a MiniMQTT Client
77
- self ._client = MQTTClient (socket ,
78
- 'io.adafruit.com' ,
79
- port = 8883 ,
80
- username = self ._user ,
81
- password = self ._key ,
82
- network_manager = self ._network_manager )
61
+ # MiniMQTT Object
62
+ print (type (mqtt_client ))
63
+ print ('MQTT CLIENT: ' , mqtt_client )
64
+ self ._client = mqtt_client
83
65
# User-defined MQTT callback methods need to be init'd to none
84
66
self .on_connect = None
85
67
self .on_disconnect = None
@@ -98,9 +80,7 @@ def __init__(self, aio_username, aio_key, network_manager, socket, secure=True):
98
80
99
81
@property
100
82
def is_connected (self ):
101
- """Returns True if is connected to the to Adafruit IO
102
- MQTT Broker.
103
- """
83
+ """Returns if connected to Adafruit IO MQTT Broker."""
104
84
return self ._connected
105
85
106
86
def connect (self ):
@@ -142,30 +122,35 @@ def _on_disconnect_mqtt(self, client, userdata, rc):
142
122
if self .on_disconnect is not None :
143
123
self .on_disconnect (self )
144
124
145
- def _on_message_mqtt (self , client , topic , message ):
125
+ def _on_message_mqtt (self , client , topic , payload ):
146
126
"""Runs when the on_message callback is run from code.
147
- Performs parsing based on username/feed/feed-key .
127
+ Parses incoming data from special Adafruit IO feeds .
148
128
:param MQTT client: A MQTT Client Instance.
149
129
:param str topic: MQTT topic response from Adafruit IO.
150
- :param str message : MQTT message data response from Adafruit IO.
130
+ :param str payload : MQTT payload data response from Adafruit IO.
151
131
"""
152
132
if self ._logger :
153
133
self ._client ._logger .debug ('Client called on_message.' )
154
134
if self .on_message is not None :
155
135
# Parse the MQTT topic string
156
136
topic_name = topic .split ('/' )
157
137
if topic_name [1 ] == "groups" :
158
- print (message )
159
- message = eval (message )
160
- for feed in message ['feeds' ]:
161
- topic_name = feed # TODO: change this to topic_name
162
- message = message ['feeds' ][topic ]
163
- print (topic , message )
164
- topic_name = topic
138
+ # Adafruit IO Group Feed Parsing
139
+ # May have rx'd more than one feed - parse them.
140
+ feeds = []
141
+ messages = []
142
+ payload = eval (payload )
143
+ for feed in payload ['feeds' ]:
144
+ feeds .append (feed )
145
+ for msg in feeds :
146
+ payload = payload ['feeds' ][msg ]
147
+ messages .append (payload )
148
+ topic_name = feeds
149
+ message = messages
165
150
else :
166
151
topic_name = topic_name [2 ]
167
- # parse the message
168
- message = '' if message is None else message
152
+ # parse the payload
153
+ message = '' if payload is None else message
169
154
else :
170
155
raise ValueError ('You must define an on_message method before calling this callback.' )
171
156
self .on_message (self , topic_name , message )
@@ -214,12 +199,17 @@ def subscribe(self, feed_key=None, group_key=None, shared_user=None):
214
199
raise AdafruitIO_MQTTError ('Must provide a feed_key or group_key.' )
215
200
216
201
def subscribe_to_throttling (self ):
217
- """Subscribes to the throttle feed to notify you
218
- if your Adafruit IO rate limit has been exceeded.
202
+ """Subscribes to your personal Adafruit IO /throttle feed.
219
203
https://io.adafruit.com/api/docs/mqtt.html#mqtt-api-rate-limiting
220
204
"""
221
205
self ._client .subscribe ('%s/throttle' % self ._user )
222
206
207
+ def subscribe_to_errors (self ):
208
+ """Subscribes to your personal Adafruit IO /errors feed.
209
+ Notifies you of errors relating to publish/subscribe calls.
210
+ """
211
+ self ._client .subscribe ('%s/errors' % self ._user )
212
+
223
213
def subscribe_to_randomizer (self , randomizer_id ):
224
214
"""Subscribes to a random data stream created by the Adafruit IO Words service.
225
215
:param int randomizer_id: Random word record you want data for.
0 commit comments