需要指导更好的覆盆子pi approch

时间:2018-03-26 08:06:21

标签: raspberry-pi azure-iot-hub windows-iot-core-10 home-automation

我想使用运行windows iot核心的覆盆子pi模型b远程切换继电器,Rasberry pi必须连接azure iot集线器,最初我可以通过互联网浏览器访问ui来打开/关闭继电器,

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

1 个答案:

答案 0 :(得分:0)

要使用Azure IoT Hub,您可以使用direct method

  • 设备端

对于Windows IoT Core,您可以从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));
        }
    }
}

您需要在您的UWP应用中安装 NuGet microsoft.azure.devices.client Here是您可以参考的.NET控制台应用程序的详细教程。

  • 云端:

您可以像这样从Azure门户调用直接方法:

enter image description here