当前位置: 首页 > news >正文

上海建设网站平台微信 微网站开发

上海建设网站平台,微信 微网站开发,免费建个网站,商业网站首页怎么做点击上方蓝字关注“汪宇杰博客”导语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 消息。现在你学会了通过互联网控制家里灯泡开关的魔术
http://wiki.neutronadmin.com/news/336707/

相关文章:

  • 百度推广进入后是别的网站 说是服务器问题中国3.15诚信建设联盟网站
  • 招远 两学一做 网站昆明市建设厅网站
  • 电子商务网站建设的目标是企业网站建设感想
  • 南江红鱼洞水库建设管理局网站广州市市场监督管理局
  • 做蛋糕视频的网站富库网站建设
  • 温岭做网站公司企业网站建设需要提供什么内容
  • 基于微信的网站开发wordpress 手机首页
  • 做音乐的网站微信公众号平台开发文档
  • asp加dw做网站广西网站建设路
  • 烟台百度网站php网站建设文献综述
  • 国内知名网站太原市建设厅网站首页
  • 哪个网站有适合小学生做的题亿建联网站是谁做的
  • 只做瓶子包装设计的创意网站网上做效果图网站
  • asp网站实例wordpress中文破解主题下载
  • 一个网站做各种好玩的实验班级网站网页设计
  • 自己做网站需要做啥网站推广计划包括哪些
  • 南宁网站建设超薄网络珠海做网站优化
  • 百度上如何做优化网站上海市企业服务云网站
  • 网站建设与管理案例...免费咨询法律援助该打什么电话
  • 网站开发实战网络课个人网站html源码
  • 合肥做检查军大网站小程序是怎么开发的
  • 收费网站解决方案网站怎么设计制作
  • 用html做的网站加背景音乐企业运营公司
  • 网站建设公司的公司浙江省建设银行网站首页
  • 做静态网站d微信官方小程序开发工具
  • 手表网站西安做网站微信公司哪家好
  • 网站备案需要建设好网站吗建设电瓶车官方网站
  • 视频网站后台登陆wordpress简洁cms主题
  • 网站换稳定服务器做企业网站用drupal7
  • 和文化有关的吉网站建设模板企业网站网站建设公司