21
21
import logging
22
22
23
23
import paho .mqtt .client as mqtt
24
-
24
+ import sys
25
+ from .errors import MQTTError , RequestError
25
26
26
27
# How long to wait before sending a keep alive (paho-mqtt configuration).
27
28
KEEP_ALIVE_SEC = 60 # One minute
@@ -34,23 +35,29 @@ class MQTTClient(object):
34
35
using the MQTT protocol.
35
36
"""
36
37
37
- def __init__ (self , username , key , service_host = 'io.adafruit.com' , service_port = 1883 ):
38
+ def __init__ (self , username , key , service_host = 'io.adafruit.com' , secure = True ):
38
39
"""Create instance of MQTT client.
39
40
40
- Required parameters:
41
- - username: The Adafruit.IO username for your account (found on the
42
- accounts site https://accounts.adafruit.com/).
43
- - key: The Adafruit.IO access key for your account.
41
+ :param username: Adafruit.IO Username for your account.
42
+ :param key: Adafruit IO access key (AIO Key) for your account.
43
+ :param secure: (optional, boolean) Switches secure/insecure connections
44
44
"""
45
45
self ._username = username
46
46
self ._service_host = service_host
47
- self ._service_port = service_port
47
+ if secure :
48
+ self ._service_port = 8883
49
+ elif not secure :
50
+ self ._service_port = 1883
48
51
# Initialize event callbacks to be None so they don't fire.
49
52
self .on_connect = None
50
53
self .on_disconnect = None
51
54
self .on_message = None
52
55
# Initialize MQTT client.
53
56
self ._client = mqtt .Client ()
57
+ if secure :
58
+ self ._client .tls_set_context ()
59
+ elif not secure :
60
+ print ('**THIS CONNECTION IS INSECURE** SSL/TLS not supported for this platform' )
54
61
self ._client .username_pw_set (username , key )
55
62
self ._client .on_connect = self ._mqtt_connect
56
63
self ._client .on_disconnect = self ._mqtt_disconnect
@@ -62,11 +69,12 @@ def _mqtt_connect(self, client, userdata, flags, rc):
62
69
# Check if the result code is success (0) or some error (non-zero) and
63
70
# raise an exception if failed.
64
71
if rc == 0 :
72
+ #raise RequestError(rc)
65
73
self ._connected = True
74
+ print ('Connected to Adafruit IO!' )
66
75
else :
67
- # TODO: Make explicit exception classes for these failures:
68
- # 0: Connection successful 1: Connection refused - incorrect protocol version 2: Connection refused - invalid client identifier 3: Connection refused - server unavailable 4: Connection refused - bad username or password 5: Connection refused - not authorised 6-255: Currently unused.
69
- raise RuntimeError ('Error connecting to Adafruit IO with rc: {0}' .format (rc ))
76
+ # handle RC errors within `errors.py`'s MQTTError class
77
+ raise MQTTError (rc )
70
78
# Call the on_connect callback if available.
71
79
if self .on_connect is not None :
72
80
self .on_connect (self )
@@ -78,7 +86,8 @@ def _mqtt_disconnect(self, client, userdata, rc):
78
86
# log the RC as an error. Continue on to call any disconnect handler
79
87
# so clients can potentially recover gracefully.
80
88
if rc != 0 :
81
- logger .debug ('Unexpected disconnect with rc: {0}' .format (rc ))
89
+ raise MQTTError (rc )
90
+ print ('Disconnected from Adafruit IO!' )
82
91
# Call the on_disconnect callback if available.
83
92
if self .on_disconnect is not None :
84
93
self .on_disconnect (self )
0 commit comments