我在 Azure IoT Central 中有一个自定义应用程序。我已经创建了我的设备,并且可以使用基本数据类型(bool、float、int)发送数据。
从 C api 看起来有一种机制来声明一个结构,将其添加到模型中,然后发送该数据。所以我做到了,我看到确认我的消息正在发送和接收,但没有数据显示......所以我猜你不能真正声明一个结构并发送它,或者我没有它在 Azure 端正确配置,但我没有找到任何可以帮助的文档。
BEGIN_NAMESPACE(AzureModel);
DECLARE_STRUCT(MyData,
int, num1,
float, num2,
int, num3);
DECLARE_MODEL(Azure1_t,
WITH_DATA(ascii_char_ptr, deviceId),
WITH_DATA(int, messageId),
WITH_DATA(MyData, myData),
WITH_DATA(EDM_DATE_TIME_OFFSET, ts),
/* Methods */
WITH_METHOD(Reboot),
WITH_METHOD(Quit),
WITH_METHOD(FirmwareUpdate, ascii_char_ptr, FwPackageUri),
/* Register Direct Methods with IoT Hub */
WITH_REPORTED_PROPERTY(ascii_char_ptr_no_quotes, SupportedMethods),
/* Telemetry Interval in Seconds... value from 1 to: 0xFFFF/2000 = About 30 Seconds */
WITH_REPORTED_PROPERTY(int,TelemetryInterval),
WITH_REPORTED_PROPERTY(ascii_char_ptr,AzureStatus),
WITH_REPORTED_PROPERTY(ascii_char_ptr, AzureFwVersion)
);
END_NAMESPACE(AzureModel);
Azure1_t *Azure1;
static void SendData(void)
{
EVENT_INSTANCE *messages;
unsigned char* destination;
size_t destinationSize;
time_t now;
/* Read the Time from RTC */
now = TimingSystemGetSystemTime();
/* Time Stamp */
Azure1->ts = GetDateTimeOffset(now);
messages = (EVENT_INSTANCE *) calloc(1,sizeof(EVENT_INSTANCE));
if(messages==NULL) {
AZURE_PRINTF("Err: Allocating Memory for messages to IoT Hub\r\n");
HAL_NVIC_SystemReset();
} else {
messages->this = (void *)messages;
}
SentMessagesCount++;
Azure1->messageId = messages->messageTrackingId = SentMessagesCount;
if (SERIALIZE(&destination, &destinationSize,
Azure1->deviceId,
Azure1->messageId,
Azure1->myData,
Azure1->ts) != CODEFIRST_OK){
AZURE_PRINTF("Err: Failed to serialize\r\n");
}
else
{
/* Only for Debug */
//AZURE_PRINTF("MessageToSend=%.*s\r\n",destinationSize,destination);
if ((messages->messageHandle = IoTHubMessage_CreateFromByteArray(destination, destinationSize)) == NULL) {
AZURE_PRINTF("Err: iotHubMessageHandle is NULL!\r\n");
} else {
char msgText[32];
MAP_HANDLE propMap = IoTHubMessage_Properties(messages->messageHandle);
sprintf_s(msgText, sizeof(msgText), "PropMsg_%zu", SentMessagesCount);
if (Map_AddOrUpdate(propMap, "PropName", msgText) != MAP_OK){
AZURE_PRINTF("Err: Map_AddOrUpdate Failed!\r\n");
}
if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages->messageHandle, SendConfirmationCallback, messages) != IOTHUB_CLIENT_OK) {
AZURE_PRINTF("Err: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
} else {
AZURE_PRINTF("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", SentMessagesCount);
}
free(destination);
}
IoTHubMessage_Destroy(messages->messageHandle);
}
}