如何将IoT设备连接到远程监控预配置解决方案(Windows)?如何实现和指定物联网集线器设备的行为?

时间:2017-01-30 04:47:29

标签: azure azure-iot-hub

https://docs.microsoft.com/en-us/azure/iot-suite/iot-suite-connecting-devices#create-a-c-sample-solution-on-windows

添加以下在设备从IoT Hub接收SetTemperature和SetHumidity命令时执行的功能:

 EXECUTE_COMMAND_RESULT SetTemperature(Thermostat* thermostat, int temperature)
 {
   (void)printf("Received temperature %d\r\n", temperature);
   thermostat->Temperature = temperature;
   return EXECUTE_COMMAND_SUCCESS;
 }

 EXECUTE_COMMAND_RESULT SetHumidity(Thermostat* thermostat, int humidity)
 {
   (void)printf("Received humidity %d\r\n", humidity);
   thermostat->Humidity = humidity;
   return EXECUTE_COMMAND_SUCCESS;
 }

添加以下向IoT Hub发送消息的功能:

 static void sendMessage(IOTHUB_CLIENT_HANDLE iotHubClientHandle, const unsigned char* buffer, size_t size)
 {
   IOTHUB_MESSAGE_HANDLE messageHandle = IoTHubMessage_CreateFromByteArray(buffer, size);
   if (messageHandle == NULL)
   {
     printf("unable to create a new IoTHubMessage\r\n");
   }
   else
   {
     if (IoTHubClient_SendEventAsync(iotHubClientHandle, messageHandle, NULL, NULL) != IOTHUB_CLIENT_OK)
     {
       printf("failed to hand over the message to IoTHubClient");
     }
     else
     {
       printf("IoTHubClient accepted the message for delivery\r\n");
     }

 IoTHubMessage_Destroy(messageHandle);
   }
 free((void*)buffer);
 }

更多指定链接

1 个答案:

答案 0 :(得分:0)

您在此处引用的article向您展示了如何使用Visual Studio在Windows桌面计算机上构建和运行此示例代码。还有另外两篇等效文章向您展示了如何在Linux计算机或mbed设备上运行相同的代码。

如果您想了解如何使用其他硬件设备(如Raspberry Pi或英特尔Edison与Azure IoT Hub)的深入教程,请查看获取的IoT Hub教程集合。已开始文件夹here