Raspberry pi 需要关于树莓皮的更好方法的指导吗

Raspberry pi 需要关于树莓皮的更好方法的指导吗,raspberry-pi,azure-iot-hub,windows-iot-core-10,home-automation,Raspberry Pi,Azure Iot Hub,Windows Iot Core 10,Home Automation,我想使用运行windows iot core的raspberry pi model b远程切换继电器开/关,raspberry pi必须连接azure iot hub,最初我可以通过internet上的浏览器访问ui来切换继电器开/关 更好的方法(c#,windows iot core上的node.js),相关文章的链接将不胜感激 对于使用Azure IoT Hub,您可以利用 设备端: 例如,您可以从UWP应用程序开始 以下是在设备上实现直接方法的简单示例: using Microsoft

我想使用运行windows iot core的raspberry pi model b远程切换继电器开/关,raspberry pi必须连接azure iot hub,最初我可以通过internet上的浏览器访问ui来切换继电器开/关


更好的方法(c#,windows iot core上的node.js),相关文章的链接将不胜感激

对于使用Azure IoT Hub,您可以利用

  • 设备端
例如,您可以从UWP应用程序开始

以下是在设备上实现直接方法的简单示例:

using Microsoft.Azure.Devices.Client;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;


namespace App1
{
    public sealed partial class MainPage : Page
    {
        private string connectionStr = "HostName=[YOUR HUB NAME].azure-devices.net;DeviceId=[YOUR DEVICE ID];SharedAccessKey=[SHARED ACCESS KEY]";
        private DeviceClient deviceClient;

        public MainPage()
        {
            this.InitializeComponent();
            AddDirectMethod();
        }

        private async void AddDirectMethod()
        {
            deviceClient = DeviceClient.CreateFromConnectionString(connectionStr, TransportType.Mqtt);
            await deviceClient.SetMethodHandlerAsync("TurnOn", new MethodCallback(TurnOnRelay), null);
            await deviceClient.SetMethodHandlerAsync("TurnOff", new MethodCallback(TurnOffRelay), null);
        }

        private Task<MethodResponse> TurnOffRelay(MethodRequest methodRequest, object userContext)
        {
            Debug.WriteLine("Direct method name:" + methodRequest.Name);

            // Put Relay toggle code here.
            // ...

            string result = "{\"Relay Status\":\"The Relay is OFF.\"}";
            return Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 200));
        }

        private Task<MethodResponse> TurnOnRelay(MethodRequest methodRequest, object userContext)
        {
            Debug.WriteLine("Direct method name:" + methodRequest.Name);

            // Put Relay toggle code here.
            // ...

            string result = "{\"Relay Status\":\"The Relay is ON.\"}";
            return Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 200));
        }
    }
}
使用Microsoft.Azure.Devices.Client;
使用系统诊断;
使用系统文本;
使用System.Threading.Tasks;
使用Windows.UI.Xaml.Controls;
名称空间App1
{
公共密封部分类主页面:第页
{
私有字符串connectionStr=“主机名=[您的集线器名称]。azure设备.net;设备ID=[您的设备ID];SharedAccessKey=[共享访问密钥]”;
专用设备客户机设备客户机;
公共主页()
{
this.InitializeComponent();
AddDirectMethod();
}
私有异步void AddDirectMethod()
{
deviceClient=deviceClient.CreateFromConnectionString(connectionStr,TransportType.Mqtt);
等待deviceClient.SetMethodHandlerAsync(“开启”,新方法回调(开启中继),null);
等待deviceClient.SetMethodHandlerAsync(“关闭”,新方法回调(关闭继电器),null);
}
专用任务关闭中继(MethodRequest MethodRequest,对象userContext)
{
Debug.WriteLine(“直接方法名称:”+methodRequest.name);
//在这里输入继电器切换代码。
// ...
字符串结果=“{\”继电器状态\:\”继电器关闭。\“}”;
返回Task.FromResult(newmethodResponse(Encoding.UTF8.GetBytes(result),200));
}
私有任务TurnOnRelay(MethodRequest MethodRequest,对象userContext)
{
Debug.WriteLine(“直接方法名称:”+methodRequest.name);
//在这里输入继电器切换代码。
// ...
字符串结果=“{\”中继状态\”:“中继已打开。\”;
返回Task.FromResult(newmethodResponse(Encoding.UTF8.GetBytes(result),200));
}
}
}
您需要将NuGet软件包microsoft.azure.devices.client安装到您的UWP应用程序。是一个你可以参考的.NET控制台应用程序的详细教程

  • 云端:
您可以像这样从Azure门户调用direct方法: