上海建设网站平台,微信 微网站开发,免费建个网站,商业网站首页怎么做点击上方蓝字关注“汪宇杰博客”导语3年前#xff0c;我写过一篇《Windows 10 IoT Core Azure 远程控制LED》#xff0c;实现了《生活大爆炸》中的注孤生实验#xff0c;让信号从家里出发#xff0c;绕地球转一圈#xff0c;经过微软美国数据中心#xff0c;返回家里点亮… 点击上方蓝字关注“汪宇杰博客”导语3年前我写过一篇《Windows 10 IoT Core Azure 远程控制LED》实现了《生活大爆炸》中的注孤生实验让信号从家里出发绕地球转一圈经过微软美国数据中心返回家里点亮树莓派上连接的一个 LED 灯泡。然而3年后的现在Windows 10 IoT Core 以及UWP 已经冰冰凉透心凉甚至微软至今也没有支持树莓派4的 Windows 版本。我只能苟且偷生委曲求全开荤 Linux使用 .NET Core 重现了这个实验。微软和社区对于 .NET Core IoT 非常积极提供了比 UWP 好用不少的 IoT 基础库让我这个项目迁移非常方便。.NET Core IoT 全家桶https://github.com/dotnet/iot在开始之前如果你还没有在树莓派上配置.NET Core环境可以参考我之前写的在树莓派4上安装 .NET Core 3.0 运行时及 SDK“自启动”树莓派上的 .NET Core 3.0 环境基本原理我们要从自己电脑上发送信号到 Azure IoT Hub树莓派上的.NET Core程序会监听消息并控制LED开关。创建 Azure IoT Hub请根据微软文档创建 IoT Hub。最后记下你的连接字符串看起来就像这样HostNameYour Hub Name.azure-devices.net;SharedAccessKeyNameiothubowner;SharedAccessKeyYour Key微软文档https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-dotnet?WT.mc_idAZ-MVP-5002809下载并安装 Azure Device Explorer https://aka.ms/aziotdevexp把连接字符串粘贴到 Configuration / Connection Information里点击 Update 按钮。切换到 Management 选项卡点击 Create 输入你的设备名称勾选 Auto Generate Keys 树莓派物理连接将一个LED连接到树莓派长脚连接到 GPIO 17短脚连接到接地GROUND附树莓派4 GPIO 全家桶图来自网络复制粘贴创建一个 .NET Core 3.0 控制台程序。添加 Microsoft.Azure.Devices.Client 以及 System.Device.Gpio NuGet 包。前者用于和Azure IoT Hub通讯后者用于控制 GPIO 来开关LED灯泡。Project SdkMicrosoft.NET.Sdk PropertyGroup OutputTypeExe/OutputType TargetFrameworknetcoreapp3.0/TargetFramework /PropertyGroup ItemGroup PackageReference IncludeMicrosoft.Azure.Devices.Client Version1.21.1 / PackageReference IncludeSystem.Device.Gpio Version1.0.0 / /ItemGroup/ProjectProgram.cs 最终代码如下。记得把里面的字符串常量换成你自己的 Azure IoT Hub 信息。using System;using System.Device.Gpio;using System.Text;using System.Threading;using System.Threading.Tasks;using Microsoft.Azure.Devices.Client;using Microsoft.Azure.Devices.Client.Exceptions;namespace AzureLightControl{ class Program { private const string IotHubUri Your Hub Name.azure-devices.net; private const string DeviceKey Your Key; private const string DeviceId Your Device ID; private const int Pin 17; private static CancellationToken _ct; static async Task Main(string[] args) { Console.WriteLine(------------------------------); Console.WriteLine( Azure IoT Hub Light Control); Console.WriteLine(------------------------------); var cts new CancellationTokenSource(); _ct cts.Token; Console.CancelKeyPress (sender, eventArgs) { Console.WriteLine(${DateTime.Now} Cancelling...); cts.Cancel(); eventArgs.Cancel true; }; try { var t Task.Run(Run, cts.Token); await t; } catch (IotHubCommunicationException) { Console.WriteLine(${DateTime.Now} Operation has been canceled.); } catch (OperationCanceledException) { Console.WriteLine(${DateTime.Now} Operation has been canceled.); } finally { cts.Dispose(); } Console.ReadKey(); } private static async Task Run() { using var deviceClient DeviceClient.Create(IotHubUri, new DeviceAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey)); using var controller new GpioController(); controller.OpenPin(Pin, PinMode.Output); Console.WriteLine(${DateTime.Now} Connected to the best cloud on the planet.); Console.WriteLine($Azure IoT Hub: {IotHubUri}); Console.WriteLine($Device ID: {DeviceId}); Console.WriteLine(${DateTime.Now} GPIO pin enabled for use: {Pin}); while (!_ct.IsCancellationRequested) { Console.WriteLine(${DateTime.Now} Waiting new message from Azure...); var receivedMessage await deviceClient.ReceiveAsync(_ct); if (receivedMessage null) continue; var msg Encoding.ASCII.GetString(receivedMessage.GetBytes()); Console.WriteLine(${DateTime.Now} Received message: {msg}); switch (msg) { case on: Console.WriteLine(${DateTime.Now} Turn on the light.); controller.Write(Pin, PinValue.High); break; case off: Console.WriteLine(${DateTime.Now} Turn off the light.); controller.Write(Pin, PinValue.Low); break; default: Console.WriteLine($Unknown command: {msg}); break; } await deviceClient.CompleteAsync(receivedMessage, _ct); } } }}这里面deviceClient.ReceiveAsync() 负责监听 Azure IoT Hub 有没有发来新消息并处理收到的消息完事后调用 CompleteAsync() 告诉 Azure 这个消息已经处理好了这样的话设备再次连接到 Azure 就不会重复处理这条消息。处理消息十分直接读取消息内容为字符串如果写着on就向GPIO 17输出高电位即点亮灯泡。如果是off就输出低电位关闭灯泡。能跑就行将源代码或者发布后的dll全家桶复制到树莓派。然后在树莓派上用 .NET CLI 启动程序。在 PC 上通过 Device Explorer 向设备发送 on 或 off 消息。现在你学会了通过互联网控制家里灯泡开关的魔术