@@ -11,12 +11,17 @@ const static int thingIdSlot = 12;
11
11
ArduinoIoTCloudClass::ArduinoIoTCloudClass () :
12
12
_thing_id (" " ),
13
13
_bearSslClient(NULL ),
14
- _mqttClient (MQTT_RECEIVE_BUFFER_SIZE )
14
+ _mqttClient (NULL )
15
15
{
16
16
}
17
17
18
18
ArduinoIoTCloudClass::~ArduinoIoTCloudClass ()
19
19
{
20
+ if (_mqttClient) {
21
+ delete _mqttClient;
22
+ _mqttClient = NULL ;
23
+ }
24
+
20
25
if (_bearSslClient) {
21
26
delete _bearSslClient;
22
27
_bearSslClient = NULL ;
@@ -58,17 +63,18 @@ int ArduinoIoTCloudClass::begin(Client& net, String brokerAddress)
58
63
}
59
64
_bearSslClient = new BearSSLClient (net);
60
65
_bearSslClient->setEccSlot (keySlot, ECCX08Cert.bytes (), ECCX08Cert.length ());
66
+ _mqttClient = new MQTTClient (*_bearSslClient);
61
67
62
68
// Begin function for the MQTTClient
63
- mqttClientBegin (*_bearSslClient );
69
+ mqttClientBegin ();
64
70
65
71
Thing.begin ();
66
72
67
73
return 1 ;
68
74
}
69
75
70
76
// private class method used to initialize mqttClient class member. (called in the begin class method)
71
- void ArduinoIoTCloudClass::mqttClientBegin (Client& net )
77
+ void ArduinoIoTCloudClass::mqttClientBegin ()
72
78
{
73
79
// MQTT topics definition
74
80
_stdoutTopic = " /a/d/" + _id + " /s/o" ;
@@ -83,29 +89,30 @@ void ArduinoIoTCloudClass::mqttClientBegin(Client& net)
83
89
}
84
90
85
91
// use onMessage as callback for received mqtt messages
86
- _mqttClient.onMessageAdvanced (ArduinoIoTCloudClass::onMessage);
87
- _mqttClient.begin (_brokerAddress.c_str (), 8883 , net);
88
-
89
- // Set MQTT connection options
90
- _mqttClient.setOptions (mqttOpt.keepAlive , mqttOpt.cleanSession , mqttOpt.timeout );
92
+ _mqttClient->onMessage (ArduinoIoTCloudClass::onMessage);
93
+ _mqttClient->setKeepAliveInterval (30 * 1000 );
94
+ _mqttClient->setConnectionTimeout (1500 );
95
+ _mqttClient->setId (_id.c_str ());
91
96
}
92
97
93
98
int ArduinoIoTCloudClass::connect ()
94
99
{
95
100
// Username: device id
96
101
// Password: empty
97
- if (!_mqttClient. connect (_id .c_str ())) {
102
+ if (!_mqttClient-> connect (_brokerAddress .c_str (), 8883 )) {
98
103
return 0 ;
99
104
}
100
- _mqttClient. subscribe (_stdinTopic);
101
- _mqttClient. subscribe (_dataTopicIn);
105
+ _mqttClient-> subscribe (_stdinTopic);
106
+ _mqttClient-> subscribe (_dataTopicIn);
102
107
103
108
return 1 ;
104
109
}
105
110
106
111
bool ArduinoIoTCloudClass::disconnect ()
107
112
{
108
- return _mqttClient.disconnect ();
113
+ _mqttClient->stop ();
114
+
115
+ return true ;
109
116
}
110
117
111
118
void ArduinoIoTCloudClass::poll ()
@@ -127,10 +134,8 @@ bool ArduinoIoTCloudClass::mqttReconnect(int const maxRetries, int const timeout
127
134
128
135
// Check for MQTT broker connection, of if maxReties limit is reached
129
136
// if MQTTClient is connected , simply do nothing and retun true
130
- while (!_mqttClient.connected () && (retries++ < maxRetries) && (millis () - start < timeout)) {
131
-
132
- // Get last MTTQClient error, (a common error may be a buffer overflow)
133
- lwmqtt_err_t err = _mqttClient.lastError ();
137
+ while (!_mqttClient->connected () && (retries++ < maxRetries) && (millis () - start < timeout)) {
138
+ // int connectError = _mqttClient->connectError();
134
139
135
140
// try establish the MQTT broker connection
136
141
connect ();
@@ -154,31 +159,22 @@ void ArduinoIoTCloudClass::update(int const reconnectionMaxRetries, int const re
154
159
return ;
155
160
156
161
// MTTQClient connected!, poll() used to retrieve data from MQTT broker
157
- _mqttClient. loop ();
162
+ _mqttClient-> poll ();
158
163
159
- uint8_t data[MQTT_RECEIVE_BUFFER_SIZE ];
164
+ uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE ];
160
165
int const length = Thing.encode (data, sizeof (data));
161
166
if (length > 0 ) {
162
167
writeProperties (data, length);
163
168
}
164
169
}
165
170
166
- int ArduinoIoTCloudClass::reconnect (Client& net)
171
+ int ArduinoIoTCloudClass::reconnect (Client& /* net*/ )
167
172
{
168
173
// check if MQTT client is still connected
169
- if (_mqttClient. connected ()) {
170
- while (! _mqttClient. disconnect () );
174
+ if (_mqttClient-> connected ()) {
175
+ _mqttClient-> stop ( );
171
176
}
172
177
173
- // Re-initialize _bearSslClient
174
- if (_bearSslClient) {
175
- delete _bearSslClient;
176
- }
177
- _bearSslClient = new BearSSLClient (net);
178
- _bearSslClient->setEccSlot (keySlot, ECCX08Cert.bytes (), ECCX08Cert.length ());
179
-
180
- // Initialize again the MQTTClient, otherwise it would not be able to receive messages through its callback
181
- mqttClientBegin (*_bearSslClient);
182
178
// Connect to the broker
183
179
return connect ();
184
180
}
@@ -190,26 +186,59 @@ void ArduinoIoTCloudClass::onGetTime(unsigned long(*callback)(void))
190
186
191
187
int ArduinoIoTCloudClass::connected ()
192
188
{
193
- return _mqttClient. connected ();
189
+ return _mqttClient-> connected ();
194
190
}
195
191
196
- int ArduinoIoTCloudClass::writeProperties (const byte data[], int const length)
192
+ int ArduinoIoTCloudClass::writeProperties (const byte data[], int length)
197
193
{
198
- return _mqttClient.publish (_dataTopicOut.c_str (), (const char *)data, length);
194
+ if (!_mqttClient->beginMessage (_dataTopicOut, length, false , 0 )) {
195
+ return 0 ;
196
+ }
197
+
198
+ if (!_mqttClient->write (data, length)) {
199
+ return 0 ;
200
+ }
201
+
202
+ if (!_mqttClient->endMessage ()) {
203
+ return 0 ;
204
+ }
205
+
206
+ return 1 ;
199
207
}
200
208
201
- int ArduinoIoTCloudClass::writeStdout (const byte data[], int const length)
209
+ int ArduinoIoTCloudClass::writeStdout (const byte data[], int length)
202
210
{
203
- return _mqttClient.publish (_stdoutTopic.c_str (), (const char *)data, length);
211
+ if (!_mqttClient->beginMessage (_stdoutTopic, length, false , 0 )) {
212
+ return 0 ;
213
+ }
214
+
215
+ if (!_mqttClient->write (data, length)) {
216
+ return 0 ;
217
+ }
218
+
219
+ if (!_mqttClient->endMessage ()) {
220
+ return 0 ;
221
+ }
222
+
223
+ return 1 ;
204
224
}
205
225
206
- void ArduinoIoTCloudClass::onMessage (MQTTClient* /* client */ , char topic[], char bytes[], int const length)
226
+ void ArduinoIoTCloudClass::onMessage (int length)
207
227
{
208
- ArduinoCloud.handleMessage (topic, bytes, length);
228
+ ArduinoCloud.handleMessage (length);
209
229
}
210
230
211
- void ArduinoIoTCloudClass::handleMessage (char topic[], char bytes[], int const length)
231
+ void ArduinoIoTCloudClass::handleMessage (int length)
212
232
{
233
+ String topic = _mqttClient->messageTopic ();
234
+
235
+ byte bytes[length];
236
+ int index = 0 ;
237
+
238
+ for (int i = 0 ; i < length; i++) {
239
+ bytes[i] = _mqttClient->read ();
240
+ }
241
+
213
242
if (_stdinTopic == topic) {
214
243
CloudSerial.appendStdin ((uint8_t *)bytes, length);
215
244
}
0 commit comments