我正在尝试将 Stm32 发现工具包 IoT 节点与 Azure IoT Central 连接。
我在 IoT Central 中创建了一个设备模板和设备,并创建了一个连接字符串。
使用虚拟设备,我验证我可以发送数据正常......虽然开发板无法连接......看起来它正在等待物联网中心向它发送一些东西以更新设备孪生或所需的属性......但我不知道如何通过 Azure IoT Central 做到这一点。我尝试更改属性选项卡上的属性并将其保存,但没有任何反应...如何将某些内容从 IoT Central 发送到我的设备?
设置它正在等待的标志的回调函数如下(由STM编写)它正在等待的标志是ReceivedDeviceTwinProperty
**
* @brief CallBack received at the beginning on when there is a change on desired properties
* @param DEVICE_TWIN_UPDATE_STATE status_code Status received
* @param unsigned char* payload Payload received (json)
* @param size_t size size of the Payload received
* @param void* userContextCallback pointer to the model instance
* @retval None
*/
static void DeviceTwinCallbackStatus(DEVICE_TWIN_UPDATE_STATE status_code, const unsigned char* payload, size_t size, void* userContextCallback)
{
/* Only for Debug */
AZURE_PRINTF("DeviceTwinCallbackStatus Method payload: %.*s\r\nStatus_code = %d\r\n", size,(const char*)payload,status_code); //sabol
/* query the DeviceTwin Status */
{
JSON_DECODER_RESULT JSONDec;
MULTITREE_HANDLE multiTreeHandle;
char* temp = malloc(size + 1);
if (temp == NULL) {
AZURE_PRINTF("Err: failed to malloc\r\n");
return;
}
/* We need to add the missing termination char */
(void)memcpy(temp, payload, size);
temp[size] = '\0';
if((JSONDec = JSONDecoder_JSON_To_MultiTree(temp,&multiTreeHandle))!=JSON_DECODER_OK) {
AZURE_PRINTF("Err: Decoding JSON Code=%d\r\n",JSONDec);
free(temp);
return;
} else {
AZURE_PRINTF("JSON Decoded\r\n");
}
/* Search the Reported Properties Only at the beginning */
if(ReceivedDeviceTwinProperty==0){
MULTITREE_HANDLE childHandle;
ReceivedDeviceTwinProperty=1;
if(MultiTree_GetChildByName(multiTreeHandle, "reported", &childHandle)== MULTITREE_OK) {
MULTITREE_HANDLE childHandle2;
AZURE_PRINTF("Reported Property Found\r\n");
if(MultiTree_GetChildByName(childHandle, "AzureStatus", &childHandle2) != MULTITREE_OK) {
AZURE_PRINTF("AzureStatus Reported Property not Found\r\n");
} else {
void const *ResultStatus;
//AZURE_PRINTF("AzureStatus reported Property Found\r\n");
if(MultiTree_GetValue(childHandle2, &ResultStatus)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the AzureStatus Reported Property value\r\n");
} else {
//AZURE_PRINTF("AzureStatus reported Property value=%s\r\n",(unsigned char *)ResultStatus);
/* We need to avoid to check the \" because the property is "Applying" or "Downloading" */
if(!strncmp(ENUM_TO_STRING(FIRMWARE_UPDATE_STATUS,Applying),((char *)ResultStatus)+1,strlen(ENUM_TO_STRING(FIRMWARE_UPDATE_STATUS,Applying))-1)) {
/* This for cleanning the Supported Methods/Commands list */
Azure1->SupportedMethods = "null";
Azure1->SupportedCommands = "null";
ReportState(ApplyComplete);
} else if (!strncmp(ENUM_TO_STRING(FIRMWARE_UPDATE_STATUS,Downloading),((char *)ResultStatus)+1,strlen(ENUM_TO_STRING(FIRMWARE_UPDATE_STATUS,Downloading))-1)) {
ReportState(DownloadFailed);
}
}
}
}
/* Check also the Desired Properties */
if(MultiTree_GetChildByName(multiTreeHandle, "desired", &childHandle)== MULTITREE_OK) {
MULTITREE_HANDLE childHandle2;
#ifdef AZURE_IOT_CENTRAL
MULTITREE_HANDLE childHandleVersion;
#endif /* AZURE_IOT_CENTRAL */
AZURE_PRINTF("Desired Property Found\r\n");
/* Search the Version Number */
#ifdef AZURE_IOT_CENTRAL
if(MultiTree_GetChildByName(childHandle, "$version", &childHandleVersion) != MULTITREE_OK) {
AZURE_PRINTF("$version Desired Property not Found\r\n");
} else {
void const *ResultInterval;
if(MultiTree_GetValue(childHandleVersion, &ResultInterval)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the $version Desired Property value\r\n");
} else {
DesiredVersion = atoi((char *) ResultInterval);
AZURE_PRINTF("Desired property $version= %d\r\n",DesiredVersion);
}
}
#endif /* AZURE_IOT_CENTRAL */
if(MultiTree_GetChildByName(childHandle, "DesiredTelemetryInterval", &childHandle2) != MULTITREE_OK) {
AZURE_PRINTF("DesiredTelemetryInterval Desired Property not Found\r\n");
} else {
void const *ResultInterval;
//AZURE_PRINTF("DesiredTelemetryInterval Desired Property Found\r\n");
#ifndef AZURE_IOT_CENTRAL
if(MultiTree_GetValue(childHandle2, &ResultInterval)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the DesiredTelemetryInterval Desired Property value\r\n");
} else {
Azure1_t *Azure = userContextCallback;
Azure->DesiredTelemetryInterval= atoi(((char *) ResultInterval));
AZURE_PRINTF("Desired Telemetry Interval= %d\r\n", Azure->DesiredTelemetryInterval);
ChangeTelemetryInterval(userContextCallback);
}
#else /* AZURE_IOT_CENTRAL */
MULTITREE_HANDLE childHandle3;
if(MultiTree_GetChildByName(childHandle2, "value", &childHandle3) != MULTITREE_OK) {
AZURE_PRINTF("value for Desired DesiredTelemetryInterval Property not Found\r\n");
} else {
if(MultiTree_GetValue(childHandle3, &ResultInterval)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the DesiredTelemetryInterval Desired Property value\r\n");
} else {
Azure1_t *Azure = userContextCallback;
DesiredTelemetryInterval= atoi(((char *) ResultInterval));
AZURE_PRINTF("Desired Telemetry Interval= %d\r\n", DesiredTelemetryInterval);
ChangeTelemetryInterval(userContextCallback);
}
}
#endif /* AZURE_IOT_CENTRAL */
}
#ifndef USE_STM32L475E_IOT01
if(MultiTree_GetChildByName(childHandle, "DesiredHWMode", &childHandle2) != MULTITREE_OK) {
AZURE_PRINTF("DesiredHWMode Desired Property not Found\r\n");
} else {
void const *ResultHWMode;
//AZURE_PRINTF("DesiredHWMode Desired Property Found\r\n");
if(MultiTree_GetValue(childHandle2, &ResultHWMode)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the DesiredHWMode Desired Property value\r\n");
} else {
Azure1_t *Azure = userContextCallback;
Azure->DesiredHWMode= atoi(((char *) ResultHWMode));
AZURE_PRINTF("Desired HW Mode= %d\r\n", Azure->DesiredHWMode);
ChangeHWMode(userContextCallback);
}
}
#endif /* USE_STM32L475E_IOT01 */
}
} else {
/* if we are not at the beginning... there is a change on the Desided properties */
/* Search the Version Number */
#ifdef AZURE_IOT_CENTRAL
MULTITREE_HANDLE childHandleVersion;
if(MultiTree_GetChildByName(multiTreeHandle, "$version", &childHandleVersion) != MULTITREE_OK) {
AZURE_PRINTF("$version Desired Property not Found\r\n");
} else {
void const *ResultInterval;
if(MultiTree_GetValue(childHandleVersion, &ResultInterval)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the $version Desired Property value\r\n");
} else {
DesiredVersion = atoi((char *) ResultInterval);
AZURE_PRINTF("Desired property $version= %d\r\n",DesiredVersion);
}
}
#endif /* AZURE_IOT_CENTRAL */
MULTITREE_HANDLE childHandle;
if(MultiTree_GetChildByName(multiTreeHandle, "DesiredTelemetryInterval", &childHandle) != MULTITREE_OK) {
AZURE_PRINTF("DesiredTelemetryInterval Desired Property not Found\r\n");
} else {
void const *ResultInterval;
#ifndef AZURE_IOT_CENTRAL
if(MultiTree_GetValue(childHandle, &ResultInterval)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the DesiredTelemetryInterval Desired Property value\r\n");
} else {
Azure1_t *Azure = userContextCallback;
Azure->DesiredTelemetryInterval= atoi(((char *) ResultInterval));
AZURE_PRINTF("Desired Telemetry Interval= %d\r\n", Azure->DesiredTelemetryInterval);
ChangeTelemetryInterval(userContextCallback);
}
#else /* AZURE_IOT_CENTRAL */
MULTITREE_HANDLE childHandle2;
if(MultiTree_GetChildByName(childHandle, "value", &childHandle2) != MULTITREE_OK) {
AZURE_PRINTF("value for Desired DesiredTelemetryInterval Property not Found\r\n");
} else {
if(MultiTree_GetValue(childHandle2, &ResultInterval)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the DesiredTelemetryInterval Desired Property value\r\n");
} else {
Azure1_t *Azure = userContextCallback;
DesiredTelemetryInterval= atoi(((char *) ResultInterval));
AZURE_PRINTF("Desired Telemetry Interval= %d\r\n", DesiredTelemetryInterval);
ChangeTelemetryInterval(userContextCallback);
}
}
#endif /* AZURE_IOT_CENTRAL */
}
#ifndef USE_STM32L475E_IOT01
if(MultiTree_GetChildByName(multiTreeHandle, "DesiredHWMode", &childHandle) != MULTITREE_OK) {
AZURE_PRINTF("DesiredHWMode Desired Property not Found\r\n");
} else {
void const *ResultHWMode;
if(MultiTree_GetValue(childHandle, &ResultHWMode)!= MULTITREE_OK) {
AZURE_PRINTF("Err: Reading the DesiredHWMode Desired Property value\r\n");
} else {
Azure1_t *Azure = userContextCallback;
Azure->DesiredHWMode= atoi(((char *) ResultHWMode));
AZURE_PRINTF("Desired HW Mode= %d\r\n", Azure->DesiredHWMode);
ChangeHWMode(userContextCallback);
}
}
#endif /* USE_STM32L475E_IOT01 */
}
MultiTree_Destroy(multiTreeHandle);
free(temp);
}
}