IainWoolley wrote on October 18, 2019:
I’m using v3.0.1 of the IoT SDK and v8.2.0 of FreeRTOS. This is because I’m using Espressif’s ESP-IDF, and v8.2.0 is the base version of FreeRTOS they’ve used which supports SMP.
Below is some pseudocode to illustrate my initial approach, which only triggers the delta callback after the vTaskDelay() call:
ShadowInitParameters_t shadowInitParams = ShadowInitParametersDefault;
ShadowConnectParameters_t shadowConnectParams = ShadowConnectParametersDefault;
jsonStruct_t shadowParam2, shadowParam2, shadowParam3;
void shadowInit()
{
shadowInitParams = // host address, port, certs, etc.
shadowConnectParams = // thing name, client id, etc.
// set up shadowParam, includng delta callback function
shadowParam1.cb = cbShadowDelta;
shadowParam1.pData = ¶meter_1;
shadowParam1.pKey = "parameter_1";
shadowParam1.type = SHADOW_JSON_FLOAT;
shadowParam1.dataLength = sizeof(float_t);
// and the same for shadowParam2, shadowParam3
// create a task to run the shadow
xTaskCreatePinnedToCore(&shadowTask, "shadow_task", 5120, NULL, 5, NULL, 1);
}
void shadowTask(void *param)
{
// initialise the shadow connection
aws_iot_shadow_init(&mqttClient, shadowInitParams);
// connect the shadow
aws_iot_shadow_connect(&mqttClient, &shadowConnectParams);
// register the delta callbacks
aws_iot_shadow_register_delta(&mqttClient, &shadowParam1);
aws_iot_shadow_register_delta(&mqttClient, &shadowParam2);
aws_iot_shadow_register_delta(&mqttClient, &shadowParam3);
while(networkConnected == true)
{
// wait for reporting period to elapse
vTaskDelay(reportingPeriod);
// yield to let MQTT receive message
aws_iot_shadow_yield(&mqttClient, 250);
// initialise the shadow document
aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
// add the parameters to the shadow document
aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &shadowParam1, &shadowParam2, &shadowParam3);
// finalise the shadow document
aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
// update the shadow document
aws_iot_shadow_update(&mqttClient, clientId, JsonDocumentBuffer, cbShadowUpdateStatus, NULL, 5, true);
}
}
void cbShadowDelta(const char *pJsonString, uint32_t valueLength, jsonStruct_t *pContext)
{
// react to the new desired state
}
void cbShadowUpdateStatus(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, const char *pReceivedJsonDocument, void *pContextData)
{
if(SHADOW_ACK_ACCEPTED == status)
{
// it's all good
}
}