Hi.
I’m designing a product based on the shadow example and the document that is sent from the aws iot object is the one in the example like this:
{
"desired": {
"powerOn": 1
},
"reported": {
"powerOn": 1
}
}
This works correctly as long as “powerOn” has a value from 0 to 9, but I need to send more values, up to the number 17, which implies two bytes, for example if I send the number 15, the first byte is 1 and the second is 5 .
I have modified the code in the aws_iot_demo_shadow.c file for this case. In the shadowDeltaCallback function to get the second byte I do the following:
deltaFound = _getDelta( pCallbackParam->u.callback.pDocument,
pCallbackParam->u.callback.documentLength,
"powerOn",
&pDelta,
&deltaLength );
if( deltaFound == true )
{
if (deltaLength > 1) //If two bytes arrive, the length is 2
{
iDelta = *pDelta - '0'; //In the example of 15, here would be 1
varInt = *(pDelta + 1) - '0'; //here will be 5
iDelta = 10*iDelta+varInt;//here the number 15 is created, 1 * 10 + 5
}
else
{
iDelta = *pDelta - '0'; //here comes a number from 0 to 9
}
...
}
To generate the Shadow document and report its status I do the following:
if (deltaLength > 1)
{
updateDocument.u.update.pUpdateDocument = pUpdateDocument2;
updateDocument.u.update.updateDocumentLength = EXPECTED_REPORTED_JSON_2_SIZE;
updateDocumentLength = snprintf( pUpdateDocument2,
EXPECTED_REPORTED_JSON_2_SIZE + 1,
SHADOW_REPORTED_JSON_2,
( int ) currentState,
( long unsigned ) ( IotClock_GetTimeMs() % 1000000 ) );
varInt = EXPECTED_REPORTED_JSON_2_SIZE;
}
else
{
updateDocument.u.update.pUpdateDocument = pUpdateDocument;
updateDocument.u.update.updateDocumentLength = EXPECTED_REPORTED_JSON_SIZE;
updateDocumentLength = snprintf( pUpdateDocument,
EXPECTED_REPORTED_JSON_SIZE + 1,
SHADOW_REPORTED_JSON,
( int ) currentState,
( long unsigned ) ( IotClock_GetTimeMs() % 1000000 ) );
varInt = EXPECTED_REPORTED_JSON_SIZE;
}
if( ( size_t ) updateDocumentLength != varInt)//EXPECTED_REPORTED_JSON_SIZE )
{
IotLogError( "Failed to generate reported state document for Shadow update." );
}
else
{
/* Send the Shadow update. Its result is not checked, as the Shadow updated
* callback will report if the Shadow was successfully updated. Because the
* Shadow is constantly updated in this demo, the "Keep Subscriptions" flag
* is passed to this function. */
updateStatus = AwsIotShadow_Update( pCallbackParam->mqttConnection,
&updateDocument,
AWS_IOT_SHADOW_FLAG_KEEP_SUBSCRIPTIONS,
NULL,
NULL );
if( updateStatus != AWS_IOT_SHADOW_STATUS_PENDING )
{
IotLogWarn( "%.*s failed to report new state.",
pCallbackParam->thingNameLength,
pCallbackParam->pThingName );
}
else
{
IotLogInfo( "%.*s sent new state report.",
pCallbackParam->thingNameLength,
pCallbackParam->pThingName );
}
}
I have two strings that represent the JSON one for 1 byte (SHADOW_REPORTED_JSON) and another for two bytes (SHADOW_REPORTED_JSON_2)
#define SHADOW_REPORTED_JSON \
"{" \
"\"state\":{" \
"\"reported\":{" \
"\"powerOn\":%01d" \
"}" \
"}," \
"\"clientToken\":\"%06lu\"" \
"}"
#define SHADOW_REPORTED_JSON_2 \
"{" \
"\"state\":{" \
"\"reported\":{" \
"\"powerOn\":%02d" \
"}" \
"}," \
"\"clientToken\":\"%06lu\"" \
"}"
/**
* @brief The expected size of #SHADOW_REPORTED_JSON.
*
* Because all the format specifiers in #SHADOW_REPORTED_JSON include a length,
* its full size is known at compile-time.
*/
#define EXPECTED_REPORTED_JSON_SIZE ( sizeof( SHADOW_REPORTED_JSON ) - 3 )
#define EXPECTED_REPORTED_JSON_2_SIZE ( sizeof( SHADOW_REPORTED_JSON_2 ) - 3 )
All of this apparently works very well with the microcontroller, until it stops responding to commands from the aws iot object (or from a lambda function associated with an alexa ability) after a few seconds.
I decided to connect the RS232 console to see if there was a type of error, and the microcontroller only arrived to send the last message which is “29 3382 [IP-task] Socket 29603 → 34004522ip: 8883 State eCONNECT_SYN-> eESTABLISHED”
16 3298 [iot_thread] [INFO ][DEMO][3298] ---------STARTING DEMO---------
17 3298 [iot_thread] [INFO ][INIT][3298] SDK successfully initialized.
18 3298 [iot_thread] [INFO ][DEMO][3298] Successfully initialized the demo. Network type for the demo: 4
19 3298 [iot_thread] [INFO ][MQTT][3298] MQTT library successfully initialized.
20 3298 [iot_thread] [INFO ][Shadow][3298] Shadow library successfully initialized.
21 3298 [iot_thread] [INFO ][DEMO][3298] Shadow Thing Name is XXXXXXXXX (length 12).
22 3336 [iot_thread] prvProcessDNSCache: add: 'axxxxxx-xxx.iot.us-east-1.amazonaws.com' @ 34004522ip
23 3336 [iot_thread] DNS[0xF357]: The answer to 'axxxxxxxxxxx-xxx.iot.us-east-1.amazonaws.com' (34004522ip) will be stored
24 3336 [iot_thread] FreeRTOS_connect: 29603 to 34004522ip:8883
25 3336 [iot_thread] Socket 29603 -> 34004522ip:8883 State eCLOSED->eCONNECT_SYN
26 3336 [IP-task] prvSocketSetMSS: 1400 bytes for 34004522ip:8883
27 3336 [IP-task] Connect[34004522ip:8883]: next timeout 1: 3000 ms
28 3382 [IP-task] MSS change 1400 -> 1460
29 3382 [IP-task] Socket 29603 -> 34004522ip:8883 State eCONNECT_SYN->eESTABLISHED
After that there are no more messages, although the Shadow document is modified and the hardware activates and deactivates terminals according to “powerOn”, these changes are not displayed.
When the network cable is disconnected or connected, the respective messages do appear (“MAC link: OFF!” Or “MAC link: ON!”)
After a while, it no longer responds to changes in the shadow document and the only option is to reboot the hardware.
But if I undo all the changes above, it works very well, the hardware always reports what happens through the Rs232 console.
Any comments or suggestions are welcome