使用IoT DevKit和Azure IoT中心将云发送到设备消息-设备代码 [英] Sending Cloud to Device Messages using IoT DevKit and Azure IoT Hub - Device Code

查看:122
本文介绍了使用IoT DevKit和Azure IoT中心将云发送到设备消息-设备代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从IoT中心向DevKit设备发送一条消息.基于 https://docs. microsoft.com/en-au/azure/iot-hub/iot-hub-devguide-c2d-guidance 我想发送直接方法,因为我需要管理一组继电器.

I need to send a message from the IoT Hub to the DevKit Device. Based on https://docs.microsoft.com/en-au/azure/iot-hub/iot-hub-devguide-c2d-guidance I want to send a Direct Method as I need to manage a bank of relays.

我有一个IoT DevKit,并且已经成功配置了它,并且能够将设备发送到IoT中心消息,但是我正在寻找一个样本来以其他方式执行此操作.我目前只能找到设置设备孪生属性的示例,而不能发送直接方法.在服务器端,我相信我会使用Microsoft.Azure.Devices.ServiceClient向设备发送一条消息(很高兴被纠正是不正确的).

I have an IoT DevKit and have successfully configured it and are able to send device to IoT Hub messages but am looking for a sample to do this the other way. I currently can only find samples that set the device twin properties, not send direct methods. On the server-side I believe I would use Microsoft.Azure.Devices.ServiceClient to SendAsync a message to the device (happy to be corrected is incorrect).

在我认为(???)的设备上,我需要使用SetDeviceMethodCallback,但是我不知道如何初始化它并接收消息.理想情况下,该示例还应包括如何发送确认消息已被接收并已采取行动的确认.

On the device I think (???) I need to use SetDeviceMethodCallback but I have no idea how to initialise it and receive messages. Ideally, the sample would also include how to send an acknowledgement that the message was received and actioned.

即使是让我知道自己在正确的道路上,任何帮助也将不胜感激.预先感谢.

Any help would be appreciated even if just to let me know I am on the right track here. Thanks in advance.

推荐答案

以下是我之前在设备端使用IoT DevKit(= Mxchip)的一些示例:

Here is some sample that I used before with the IoT DevKit (=Mxchip) on the device side:

static int  DeviceMethodCallback(const char *methodName, const unsigned char *payload, int size, unsigned char **response, int *response_size)
{
  LogInfo("Try to invoke method %s", methodName);
  const char *responseMessage = "\"Successfully invoke device method\"";
  int result = 200;

  if (strcmp(methodName, "start") == 0)
  {
    DoSomething();
  }
  else if (strcmp(methodName, "stop") == 0)
  {
    DoSomethingElse();
  }
  else
  {
    LogInfo("No method %s found", methodName);
    responseMessage = "\"No method found\"";
    result = 404;
  }

  *response_size = strlen(responseMessage) + 1;
  *response = (unsigned char *)strdup(responseMessage);

  return result;
}

DevKitMQTTClient_SetDeviceMethodCallback(DeviceMethodCallback);

在服务端(执行方法调用的地方),这里是一些C#示例

On the services side (where you do the method invocation) here is some C# example

ServiceClient _iothubServiceClient = ServiceClient.CreateFromConnectionString(config["iothubowner_cs"]);

var result = await _iothubServiceClient.InvokeDeviceMethodAsync(deviceid, "start");
var status = result.Status;

这篇关于使用IoT DevKit和Azure IoT中心将云发送到设备消息-设备代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆