簡體   English   中英

將設備雙屬性從天藍色物聯網中心發送到設備

[英]Send device twin property from azure iot central to device

我正在嘗試將Stm32發現套件IoT節點與Azure IoT中心連接。

我在IoT Central中創建了設備模板和設備,並創建了連接字符串。

使用虛擬設備,我確認可以正常發送數據...開發板雖然無法連接..看起來它正在等待IoT中心向其發送內容以更新設備雙胞胎或所需的屬性...但我不知道如何通過Azure IoT中心實現此目的。 我嘗試在屬性選項卡上更改屬性並保存,但是什么也沒發生...如何將某些東西從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);
  }
}

我認為您可能在找錯地方了。 是的,可以對IoT Central UI中的“雲” 屬性進行編輯,但是不會將其推回設備。 您應該將它們視為僅從雲/非邊緣角度來看很重要的元數據。 如果您想通過設備孿生“推送”屬性,那就是通過IoT Central 設置的概念。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM