简体   繁体   English

将设备双属性从天蓝色物联网中心发送到设备

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

I'm trying to connect an Stm32 discovery kit IoT Node with Azure IoT Central. 我正在尝试将Stm32发现套件IoT节点与Azure IoT中心连接。

I created a device template and device in IoT Central and have created a connection string. 我在IoT Central中创建了设备模板和设备,并创建了连接字符串。

With a virtual device I verified that I can send data OK... the dev board won't connect though.. it looks like it is waiting for IoT central to send something to it to update the device twin or desired properties...but I can't figure out how to do this through Azure IoT Central. 使用虚拟设备,我确认可以正常发送数据...开发板虽然无法连接..看起来它正在等待IoT中心向其发送内容以更新设备双胞胎或所需的属性...但我不知道如何通过Azure IoT中心实现此目的。 I tried changing properties on the properties tab and saving it, but nothing happens...How can I send something from IoT Central to my device? 我尝试在属性选项卡上更改属性并保存,但是什么也没发生...如何将某些东西从IoT Central发送到我的设备?

The callback function that sets the flag it is waiting on is below (written by STM) The flag it is waiting on is ReceivedDeviceTwinProperty 设置正在等待的标志的回调函数在下面(由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);
  }
}

I think you may be looking in the wrong place. 我认为您可能在找错地方了。 The "cloud" properties in the IoT Central UI are meant to be edited, yes, but they don't get pushed back to the device. 是的,可以对IoT Central UI中的“云” 属性进行编辑,但是不会将其推回设备。 You should think of them as metadata that only matters from a cloud / non-edge perspective. 您应该将它们视为仅从云/非边缘角度来看很重要的元数据。 If you want to "push" properties through the device twin, that would be through the concept of IoT Central settings . 如果您想通过设备孪生“推送”属性,那就是通过IoT Central 设置的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure IOT PnP 数字孪生 API/SDK,用于检索设备/孪生的属性、命令和遥测定义 - Azure IOT PnP Digital Twin API/SDK to retrieve Property, Command and Telemetry definitions for Device/Twin Azure IoT - 重置和清除设备孪生 - Azure IoT - resetting and clearing device twin Azure IoT Hub MQTT 设备和设备孪生更改 - Azure IoT Hub MQTT device and device twin changes 如何使Azure IoT中心设备孪生与设备更改同步? - How to syncronize Azure IoT Hub device twin with device changes? Azure IoT 中心 - 从使用报告的设备孪生消息充斥中心的设备中恢复 - Azure IoT Hub - Recovering from a device flooding the hub with reported device twin messages 用于从设备孪生获取所需属性的 Azure 物联网集线器设备与服务 SDK? - Azure Iot hub Device vs. Service SDK for getting desired properties from Device twin? Azure IoT Central:从 rest 请求创建设备模板 - Azure IoT Central: Create device template from rest request C#如何更新Azure IoT中心设备的所需双属性 - C# How to update desired twin property of an Azure IoT Hub device Azure IoT中心设备孪生的报告属性中的8字节数字自动转换为float - 8-byte number in reported property of Azure IoT hub device twin automatically converted to float Azure 适用于 Raspberry Pi 4 的 IoT Central 设备模板 - Azure IoT Central Device Template for Raspberry Pi 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM