我想使用运行windows iot核心的覆盆子pi模型b远程切换继电器,Rasberry pi必须连接azure iot集线器,最初我可以通过互联网浏览器访问ui来打开/关闭继电器,
更好的方法(c#,windows iot核心上的node.js),链接到相关文章将不胜感激。
答案 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门户调用直接方法: