@@ -119,8 +119,10 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
119
119
_sslClient.setClient (_connection->getClient ());
120
120
_sslClient.setEccSlot (static_cast <int >(ECCX08Slot::Key), _eccx08_cert.bytes (), _eccx08_cert.length ());
121
121
#elif defined(BOARD_ESP)
122
+ #ifndef ESP32
122
123
_sslClient.setInsecure ();
123
124
#endif
125
+ #endif
124
126
125
127
_mqttClient.setClient (_sslClient);
126
128
#ifdef BOARD_ESP
@@ -147,11 +149,11 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort)
147
149
#if OTA_STORAGE_SNU
148
150
String const nina_fw_version = WiFi.firmwareVersion ();
149
151
if (nina_fw_version < " 1.4.1" ) {
150
- _ota_cap = false ;
151
- DBG_WARNING (F (" ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s" ), __FUNCTION__, nina_fw_version.c_str ());
152
+ _ota_cap = false ;
153
+ DBG_WARNING (F (" ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s" ), __FUNCTION__, nina_fw_version.c_str ());
152
154
}
153
155
else {
154
- _ota_cap = true ;
156
+ _ota_cap = true ;
155
157
}
156
158
#endif /* OTA_STORAGE_SNU */
157
159
@@ -175,7 +177,7 @@ void ArduinoIoTCloudTCP::update()
175
177
176
178
/* Check for new data from the MQTT client. */
177
179
if (_mqttClient.connected ())
178
- _mqttClient.poll ();
180
+ _mqttClient.poll ();
179
181
}
180
182
181
183
int ArduinoIoTCloudTCP::connected ()
@@ -198,9 +200,9 @@ void ArduinoIoTCloudTCP::printDebugInfo()
198
200
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy ()
199
201
{
200
202
if (_connection->check () == NetworkConnectionState::CONNECTED)
201
- return State::SyncTime;
203
+ return State::SyncTime;
202
204
else
203
- return State::ConnectPhy;
205
+ return State::ConnectPhy;
204
206
}
205
207
206
208
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime ()
@@ -213,7 +215,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
213
215
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker ()
214
216
{
215
217
if (_mqttClient.connect (_brokerAddress.c_str (), _brokerPort))
216
- return State::SubscribeMqttTopics;
218
+ return State::SubscribeMqttTopics;
217
219
218
220
DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not connect to %s:%d" ), __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
219
221
return State::ConnectPhy;
@@ -223,26 +225,26 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics()
223
225
{
224
226
if (!_mqttClient.subscribe (_dataTopicIn))
225
227
{
226
- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not subscribe to %s" ), __FUNCTION__, _dataTopicIn.c_str ());
227
- return State::SubscribeMqttTopics;
228
+ DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not subscribe to %s" ), __FUNCTION__, _dataTopicIn.c_str ());
229
+ return State::SubscribeMqttTopics;
228
230
}
229
231
230
232
if (_shadowTopicIn != " " )
231
233
{
232
- if (!_mqttClient.subscribe (_shadowTopicIn))
233
- {
234
- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not subscribe to %s" ), __FUNCTION__, _shadowTopicIn.c_str ());
235
- return State::SubscribeMqttTopics;
236
- }
234
+ if (!_mqttClient.subscribe (_shadowTopicIn))
235
+ {
236
+ DBG_ERROR (F (" ArduinoIoTCloudTCP::%s could not subscribe to %s" ), __FUNCTION__, _shadowTopicIn.c_str ());
237
+ return State::SubscribeMqttTopics;
238
+ }
237
239
}
238
240
239
241
DBG_INFO (F (" Connected to Arduino IoT Cloud" ));
240
242
execCloudEventCallback (ArduinoIoTCloudEvent::CONNECT);
241
243
242
244
if (_shadowTopicIn != " " )
243
- return State::RequestLastValues;
245
+ return State::RequestLastValues;
244
246
else
245
- return State::Connected;
247
+ return State::Connected;
246
248
}
247
249
248
250
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues ()
@@ -251,9 +253,9 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()
251
253
unsigned long const now = millis ();
252
254
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)
253
255
{
254
- DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s [%d] last values requested" ), __FUNCTION__, now);
255
- requestLastValue ();
256
- _lastSyncRequestTickTime = now;
256
+ DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s [%d] last values requested" ), __FUNCTION__, now);
257
+ requestLastValue ();
258
+ _lastSyncRequestTickTime = now;
257
259
}
258
260
259
261
return State::RequestLastValues;
@@ -263,62 +265,62 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
263
265
{
264
266
if (!_mqttClient.connected ())
265
267
{
266
- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s MQTT client connection lost" ), __FUNCTION__);
268
+ DBG_ERROR (F (" ArduinoIoTCloudTCP::%s MQTT client connection lost" ), __FUNCTION__);
267
269
268
- /* Forcefully disconnect MQTT client and trigger a reconnection. */
269
- _mqttClient.stop ();
270
+ /* Forcefully disconnect MQTT client and trigger a reconnection. */
271
+ _mqttClient.stop ();
270
272
271
- /* The last message was definitely lost, trigger a retransmit. */
272
- _mqtt_data_request_retransmit = true ;
273
+ /* The last message was definitely lost, trigger a retransmit. */
274
+ _mqtt_data_request_retransmit = true ;
273
275
274
- /* We are not connected anymore, trigger the callback for a disconnected event. */
275
- execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
276
+ /* We are not connected anymore, trigger the callback for a disconnected event. */
277
+ execCloudEventCallback (ArduinoIoTCloudEvent::DISCONNECT);
276
278
277
- return State::ConnectPhy;
279
+ return State::ConnectPhy;
278
280
}
279
281
/* We are connected so let's to our stuff here. */
280
282
else
281
283
{
282
- /* Check if a primitive property wrapper is locally changed.
283
- * This function requires an existing time service which in
284
- * turn requires an established connection. Not having that
285
- * leads to a wrong time set in the time service which inhibits
286
- * the connection from being established due to a wrong data
287
- * in the reconstructed certificate.
288
- */
289
- updateTimestampOnLocallyChangedProperties (_property_container);
290
-
291
- /* Retransmit data in case there was a lost transaction due
292
- * to phy layer or MQTT connectivity loss.
293
- */
294
- if (_mqtt_data_request_retransmit && (_mqtt_data_len > 0 )) {
295
- write (_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
296
- _mqtt_data_request_retransmit = false ;
297
- }
298
-
299
- /* Check if any properties need encoding and send them to
300
- * the cloud if necessary.
301
- */
302
- sendPropertiesToCloud ();
284
+ /* Check if a primitive property wrapper is locally changed.
285
+ * This function requires an existing time service which in
286
+ * turn requires an established connection. Not having that
287
+ * leads to a wrong time set in the time service which inhibits
288
+ * the connection from being established due to a wrong data
289
+ * in the reconstructed certificate.
290
+ */
291
+ updateTimestampOnLocallyChangedProperties (_property_container);
292
+
293
+ /* Retransmit data in case there was a lost transaction due
294
+ * to phy layer or MQTT connectivity loss.
295
+ */
296
+ if (_mqtt_data_request_retransmit && (_mqtt_data_len > 0 )) {
297
+ write (_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
298
+ _mqtt_data_request_retransmit = false ;
299
+ }
300
+
301
+ /* Check if any properties need encoding and send them to
302
+ * the cloud if necessary.
303
+ */
304
+ sendPropertiesToCloud ();
303
305
304
306
#if OTA_ENABLED
305
- /* Request a OTA download if the hidden property
306
- * OTA request has been set.
307
- */
308
- if (_ota_req)
309
- {
310
- /* Clear the error flag. */
311
- _ota_error = static_cast <int >(OTAError::None);
312
- /* Transmit the cleared error flag to the cloud. */
313
- sendPropertiesToCloud ();
314
- /* Clear the request flag. */
315
- _ota_req = false ;
316
- /* Call member function to handle OTA request. */
317
- onOTARequest ();
318
- }
307
+ /* Request a OTA download if the hidden property
308
+ * OTA request has been set.
309
+ */
310
+ if (_ota_req)
311
+ {
312
+ /* Clear the error flag. */
313
+ _ota_error = static_cast <int >(OTAError::None);
314
+ /* Transmit the cleared error flag to the cloud. */
315
+ sendPropertiesToCloud ();
316
+ /* Clear the request flag. */
317
+ _ota_req = false ;
318
+ /* Call member function to handle OTA request. */
319
+ onOTARequest ();
320
+ }
319
321
#endif /* OTA_ENABLED */
320
322
321
- return State::Connected;
323
+ return State::Connected;
322
324
}
323
325
}
324
326
@@ -334,20 +336,20 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
334
336
byte bytes[length];
335
337
336
338
for (int i = 0 ; i < length; i++) {
337
- bytes[i] = _mqttClient.read ();
339
+ bytes[i] = _mqttClient.read ();
338
340
}
339
341
340
342
if (_dataTopicIn == topic) {
341
- CBORDecoder::decode (_property_container, (uint8_t *)bytes, length);
343
+ CBORDecoder::decode (_property_container, (uint8_t *)bytes, length);
342
344
}
343
345
344
346
if ((_shadowTopicIn == topic) && (_state == State::RequestLastValues))
345
347
{
346
- DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s [%d] last values received" ), __FUNCTION__, millis ());
347
- CBORDecoder::decode (_property_container, (uint8_t *)bytes, length, true );
348
- sendPropertiesToCloud ();
349
- execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
350
- _state = State::Connected;
348
+ DBG_VERBOSE (F (" ArduinoIoTCloudTCP::%s [%d] last values received" ), __FUNCTION__, millis ());
349
+ CBORDecoder::decode (_property_container, (uint8_t *)bytes, length, true );
350
+ sendPropertiesToCloud ();
351
+ execCloudEventCallback (ArduinoIoTCloudEvent::SYNC);
352
+ _state = State::Connected;
351
353
}
352
354
}
353
355
@@ -357,16 +359,16 @@ void ArduinoIoTCloudTCP::sendPropertiesToCloud()
357
359
uint8_t data[MQTT_TRANSMIT_BUFFER_SIZE];
358
360
359
361
if (CBOREncoder::encode (_property_container, data, sizeof (data), bytes_encoded, false ) == CborNoError)
360
- if (bytes_encoded > 0 )
361
- {
362
- /* If properties have been encoded store them in the back-up buffer
363
- * in order to allow retransmission in case of failure.
364
- */
365
- _mqtt_data_len = bytes_encoded;
366
- memcpy (_mqtt_data_buf, data, _mqtt_data_len);
367
- /* Transmit the properties to the MQTT broker */
368
- write (_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
369
- }
362
+ if (bytes_encoded > 0 )
363
+ {
364
+ /* If properties have been encoded store them in the back-up buffer
365
+ * in order to allow retransmission in case of failure.
366
+ */
367
+ _mqtt_data_len = bytes_encoded;
368
+ memcpy (_mqtt_data_buf, data, _mqtt_data_len);
369
+ /* Transmit the properties to the MQTT broker */
370
+ write (_dataTopicOut, _mqtt_data_buf, _mqtt_data_len);
371
+ }
370
372
}
371
373
372
374
void ArduinoIoTCloudTCP::requestLastValue ()
@@ -381,11 +383,11 @@ void ArduinoIoTCloudTCP::requestLastValue()
381
383
int ArduinoIoTCloudTCP::write (String const topic, byte const data[], int const length)
382
384
{
383
385
if (_mqttClient.beginMessage (topic, length, false , 0 )) {
384
- if (_mqttClient.write (data, length)) {
385
- if (_mqttClient.endMessage ()) {
386
- return 1 ;
387
- }
388
- }
386
+ if (_mqttClient.write (data, length)) {
387
+ if (_mqttClient.endMessage ()) {
388
+ return 1 ;
389
+ }
390
+ }
389
391
}
390
392
return 0 ;
391
393
}
@@ -409,9 +411,9 @@ void ArduinoIoTCloudTCP::onOTARequest()
409
411
uint8_t nina_ota_err_code = 0 ;
410
412
if (!WiFiStorage.downloadOTA (_ota_url.c_str (), &nina_ota_err_code))
411
413
{
412
- DBG_ERROR (F (" ArduinoIoTCloudTCP::%s error download to nina: %d" ), __FUNCTION__, nina_ota_err_code);
413
- _ota_error = static_cast <int >(OTAError::DownloadFailed);
414
- return ;
414
+ DBG_ERROR (F (" ArduinoIoTCloudTCP::%s error download to nina: %d" ), __FUNCTION__, nina_ota_err_code);
415
+ _ota_error = static_cast <int >(OTAError::DownloadFailed);
416
+ return ;
415
417
}
416
418
417
419
/* The download was a success. */
@@ -420,7 +422,7 @@ void ArduinoIoTCloudTCP::onOTARequest()
420
422
421
423
/* Perform the reset to reboot to SxU. */
422
424
if (ota_download_success)
423
- NVIC_SystemReset ();
425
+ NVIC_SystemReset ();
424
426
}
425
427
#endif
426
428
0 commit comments