@@ -63,7 +63,6 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
63
63
, _connection_attempt(0 ,0 )
64
64
, _message_stream(std::bind(&ArduinoIoTCloudTCP::sendMessage, this , std::placeholders::_1))
65
65
, _thing(&_message_stream)
66
- , _thing_id_property{nullptr }
67
66
, _device(&_message_stream)
68
67
, _mqtt_data_buf{0 }
69
68
, _mqtt_data_len{0 }
@@ -77,6 +76,8 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
77
76
, _mqttClient{nullptr }
78
77
, _deviceTopicOut(" " )
79
78
, _deviceTopicIn(" " )
79
+ , _messageTopicOut(" " )
80
+ , _messageTopicIn(" " )
80
81
, _dataTopicOut(" " )
81
82
, _dataTopicIn(" " )
82
83
#if OTA_ENABLED
@@ -319,10 +320,16 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
319
320
{
320
321
if (_mqttClient.connect (_brokerAddress.c_str (), _brokerPort))
321
322
{
323
+ /* Subscribe to message topic to receive commands */
322
324
_mqttClient.subscribe (_messageTopicIn);
323
- DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s connected to %s:%d" , __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
325
+
326
+ /* Temoporarly subsribe to device topic to receive OTA properties */
327
+ _mqttClient.subscribe (_deviceTopicIn);
328
+
324
329
/* Reconfigure timers for next state */
325
330
_connection_attempt.begin (AIOT_CONFIG_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms, AIOT_CONFIG_MAX_DEVICE_TOPIC_SUBSCRIBE_RETRY_DELAY_ms);
331
+
332
+ DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s connected to %s:%d" , __FUNCTION__, _brokerAddress.c_str (), _brokerPort);
326
333
return State::Connected;
327
334
}
328
335
@@ -339,6 +346,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected()
339
346
{
340
347
if (!_mqttClient.connected () || !_thing.connected () || !_device.connected ())
341
348
{
349
+ Serial.println (" State::Disconnect" );
342
350
return State::Disconnect;
343
351
}
344
352
@@ -437,28 +445,6 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
437
445
/* Topic for OTA properties and device configuration */
438
446
if (_deviceTopicIn == topic) {
439
447
CBORDecoder::decode (_device.getPropertyContainer (), (uint8_t *)bytes, length);
440
-
441
- /* Temporary check to avoid flooding device state machine with usless messages */
442
- if (_thing_id_property->isDifferentFromCloud ()) {
443
- _thing_id_property->fromCloudToLocal ();
444
-
445
- Message message;
446
- /* If we are attached we need first to detach */
447
- if (_device.isAttached ()) {
448
- detachThing ();
449
- message = { DeviceDetachedCmdId };
450
- }
451
- /* If received thing id is valid attach to the new thing */
452
- if (_thing_id.length ()) {
453
- attachThing ();
454
- message = { DeviceAttachedCmdId };
455
- } else {
456
- /* Send message to device state machine to inform we have received a null thing-id */
457
- _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
458
- message = { DeviceRegisteredCmdId };
459
- }
460
- _device.handleMessage (&message);
461
- }
462
448
}
463
449
464
450
/* Topic for user input data */
@@ -479,9 +465,26 @@ void ArduinoIoTCloudTCP::handleMessage(int length)
479
465
{
480
466
case CommandId::ThingUpdateCmdId:
481
467
{
482
- _thing_id = String (command.thingBeginCmd .params .thing_id );
483
468
DEBUG_VERBOSE (" ArduinoIoTCloudTCP::%s [%d] device configuration received" , __FUNCTION__, millis ());
484
- _device.handleMessage ((Message*)&command);
469
+ if ( _thing_id != String (command.thingBeginCmd .params .thing_id )) {
470
+ _thing_id = String (command.thingBeginCmd .params .thing_id );
471
+ Message message;
472
+ /* If we are attached we need first to detach */
473
+ if (_device.isAttached ()) {
474
+ detachThing ();
475
+ message = { DeviceDetachedCmdId };
476
+ }
477
+ /* If received thing id is valid attach to the new thing */
478
+ if (_thing_id.length ()) {
479
+ attachThing ();
480
+ message = { DeviceAttachedCmdId };
481
+ } else {
482
+ /* Send message to device state machine to inform we have received a null thing-id */
483
+ _thing_id = " xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ;
484
+ message = { DeviceRegisteredCmdId };
485
+ }
486
+ _device.handleMessage (&message);
487
+ }
485
488
}
486
489
break ;
487
490
@@ -516,6 +519,25 @@ void ArduinoIoTCloudTCP::sendMessage(Message * msg)
516
519
size_t bytes_encoded = sizeof (data);
517
520
CBORMessageEncoder encoder;
518
521
522
+ switch (msg->id ) {
523
+ case PropertiesUpdateCmdId:
524
+ return sendPropertyContainerToCloud (_dataTopicOut,
525
+ _thing.getPropertyContainer (),
526
+ _thing.getPropertyContainerIndex ());
527
+ break ;
528
+
529
+ #if OTA_ENABLED
530
+ case DeviceBeginCmdId:
531
+ sendDevicePropertyToCloud (" OTA_CAP" );
532
+ sendDevicePropertyToCloud (" OTA_ERROR" );
533
+ sendDevicePropertyToCloud (" OTA_SHA256" );
534
+ break ;
535
+ #endif
536
+
537
+ default :
538
+ break ;
539
+ }
540
+
519
541
if (encoder.encode (msg, data, bytes_encoded) == Encoder::Status::Complete &&
520
542
bytes_encoded > 0 ) {
521
543
write (_messageTopicOut, data, bytes_encoded);
@@ -544,11 +566,6 @@ void ArduinoIoTCloudTCP::sendPropertyContainerToCloud(String const topic, Proper
544
566
}
545
567
}
546
568
547
- void ArduinoIoTCloudTCP::sendThingPropertiesToCloud ()
548
- {
549
- sendPropertyContainerToCloud (_dataTopicOut, _thing.getPropertyContainer (), _thing.getPropertyContainerIndex ());
550
- }
551
-
552
569
#if OTA_ENABLED
553
570
void ArduinoIoTCloudTCP::sendDevicePropertyToCloud (String const name)
554
571
{
0 commit comments