@@ -14,7 +14,8 @@ const static int thingIdSlot = 12;
14
14
15
15
ArduinoCloudClass::ArduinoCloudClass () :
16
16
_bearSslClient(NULL ),
17
- _mqttClient(256 )
17
+ // Size of the receive buffer
18
+ _mqttClient(MQTT_BUFFER_SIZE)
18
19
{
19
20
}
20
21
@@ -58,17 +59,31 @@ int ArduinoCloudClass::begin(Client& net)
58
59
_bearSslClient = new BearSSLClient (net);
59
60
_bearSslClient->setEccSlot (keySlot, ECCX08Cert.bytes (), ECCX08Cert.length ());
60
61
61
- _mqttClient.onMessageAdvanced (ArduinoCloudClass::onMessage);
62
- _mqttClient.begin (server, 8883 , *_bearSslClient);
63
-
64
- _stdoutTopic = " $aws/things/" + _id + " /stdout" ;
65
- _stdinTopic = " $aws/things/" + _id + " /stdin" ;
62
+ // Begin function for the MQTTClient
63
+ mqttClientBegin (*_bearSslClient);
66
64
67
65
return 1 ;
68
66
}
69
67
68
+ // private class method used to initialize mqttClient class member. (called in the begin class method)
69
+ void ArduinoCloudClass::mqttClientBegin (Client& net)
70
+ {
71
+ // MQTT topics definition
72
+ _stdoutTopic = " /a/d/" + _id + " /s/o" ;
73
+ _stdinTopic = " /a/d/" + _id + " /s/i" ;
74
+
75
+ // use onMessage as callback for received mqtt messages
76
+ _mqttClient.onMessageAdvanced (ArduinoCloudClass::onMessage);
77
+ _mqttClient.begin (server, 8883 , net);
78
+
79
+ // Set MQTT connection options
80
+ _mqttClient.setOptions (mqttOpt.keepAlive , mqttOpt.cleanSession , mqttOpt.timeout );
81
+ }
82
+
70
83
int ArduinoCloudClass::connect ()
71
84
{
85
+ // Username: device id
86
+ // Password: empty
72
87
if (!_mqttClient.connect (_id.c_str ())) {
73
88
return 0 ;
74
89
}
@@ -78,11 +93,62 @@ int ArduinoCloudClass::connect()
78
93
return 1 ;
79
94
}
80
95
96
+ bool ArduinoCloudClass::disconnect ()
97
+ {
98
+ return _mqttClient.disconnect ();
99
+ }
100
+
81
101
void ArduinoCloudClass::poll ()
82
102
{
103
+ // If user call poll() without parameters use the default ones
104
+ poll (MAX_RETRIES, RECONNECTION_TIMEOUT);
105
+ }
106
+
107
+ bool ArduinoCloudClass::mqttReconnect (int maxRetries, int timeout)
108
+ {
109
+ // Counter for reconnection retries
110
+ int retries = 0 ;
111
+ unsigned long start = millis ();
112
+
113
+ // Check for MQTT broker connection, of if maxReties limit is reached
114
+ // if MQTTClient is connected , simply do nothing and retun true
115
+ while (!_mqttClient.connected () && (retries++ < maxRetries) && (millis () - start < timeout)) {
116
+
117
+ // Get last MTTQClient error, (a common error may be a buffer overflow)
118
+ lwmqtt_err_t err = _mqttClient.lastError ();
119
+
120
+ // try establish the MQTT broker connection
121
+ connect ();
122
+ }
123
+
124
+ // It was impossible to establish a connection, return
125
+ if ((retries == maxRetries) || (millis () - start >= timeout))
126
+ return false ;
127
+
128
+ return true ;
129
+ }
130
+
131
+ void ArduinoCloudClass::poll (int reconnectionMaxRetries, int reconnectionTimeoutMs)
132
+ {
133
+ // Method's argument controls
134
+ int maxRetries = (reconnectionMaxRetries > 0 ) ? reconnectionMaxRetries : MAX_RETRIES;
135
+ int timeout = (reconnectionTimeoutMs > 0 ) ? reconnectionTimeoutMs : RECONNECTION_TIMEOUT;
136
+
137
+ // If the reconnect() culd not establish the connection, return the control to the user sketch
138
+ if (!mqttReconnect (maxRetries, timeout))
139
+ return ;
140
+
141
+ // MTTQClient connected!, poll() used to retrieve data from MQTT broker
83
142
_mqttClient.loop ();
84
143
}
85
144
145
+ void ArduinoCloudClass::reconnect (Client& net)
146
+ {
147
+ // Initialize again the MQTTClient, otherwise it would not be able to receive messages through its callback
148
+ mqttClientBegin (net);
149
+ connect ();
150
+ }
151
+
86
152
void ArduinoCloudClass::onGetTime (unsigned long (*callback)(void ))
87
153
{
88
154
ArduinoBearSSL.onGetTime (callback);
0 commit comments