简体   繁体   中英

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.

I created a device template and device in IoT Central and have created a connection string.

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. 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?

The callback function that sets the flag it is waiting on is below (written by STM) The flag it is waiting on is 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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM