@@ -14,7 +14,8 @@ const static int thingIdSlot = 12;
1414
1515ArduinoCloudClass::ArduinoCloudClass () :
1616 _bearSslClient(NULL ),
17- _mqttClient(256 )
17+ // Size of the receive buffer
18+ _mqttClient(MQTT_BUFFER_SIZE)
1819{
1920}
2021
@@ -58,17 +59,31 @@ int ArduinoCloudClass::begin(Client& net)
5859 _bearSslClient = new BearSSLClient (net);
5960 _bearSslClient->setEccSlot (keySlot, ECCX08Cert.bytes (), ECCX08Cert.length ());
6061
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);
6664
6765 return 1 ;
6866}
6967
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+
7083int ArduinoCloudClass::connect ()
7184{
85+ // Username: device id
86+ // Password: empty
7287 if (!_mqttClient.connect (_id.c_str ())) {
7388 return 0 ;
7489 }
@@ -78,11 +93,62 @@ int ArduinoCloudClass::connect()
7893 return 1 ;
7994}
8095
96+ bool ArduinoCloudClass::disconnect ()
97+ {
98+ return _mqttClient.disconnect ();
99+ }
100+
81101void ArduinoCloudClass::poll ()
82102{
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
83142 _mqttClient.loop ();
84143}
85144
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+
86152void ArduinoCloudClass::onGetTime (unsigned long (*callback)(void ))
87153{
88154 ArduinoBearSSL.onGetTime (callback);
0 commit comments