张高兴的 .NET Core IoT 入门指南:(一)环境配置、Blink、部署

<link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">css

如何在 Raspberry Pi 的 Raspbian 上构建使用 GPIO 引脚的 IoT 程序?你可能会回答使用 C++ 或 Python 去访问 Raspberry Pi 的引脚。如今,C# 程序员可使用 .NET Core 在 Linux 上构建 IoT 应用程序。只须要引入 System.Device.Gpio NuGet 包便可。linux

<div style="display: block;position: relative;border-radius: 8px;padding: 1rem;background-color: #d2f9d2;color: #094409;margin: 10px"> <p style="margin-top:0;font-weight: bold"><i class="fa fa-lightbulb-o" aria-hidden="true"></i>&nbsp;&nbsp;提示</p> <p>由于 .NET Core JIT 依赖于 ARMv7 指令集,所以处理器架构新于 ARMv7 的 Linux 开发板均可以使用此包进行硬件操做。固然,一些特殊的硬件操做除外,好比对 GPIO 引脚进行上拉,这须要对处理器的寄存器进行访问,而 System.Device.Gpio 对不支持的硬件仅实现了通用操做。</p> </div>git

若要继续阅读下面的内容,你须要准备:程序员

  1. 安装有 Linux 的 Raspberry Pi 2B/3B/3A+/3B+
  2. Visual Studio 2019
  3. 用于构建程序的 .NET Core SDK (版本大于 2.1)

环境配置

  1. 首先获取 Raspberry Pi 的硬件接口的访问权限。github

<div style="display: block;position: relative;border-radius: 8px;padding: 1rem;background-color: #d2f9d2;color: #094409;margin: 10px"> <p style="margin-top:0;font-weight: bold"><i class="fa fa-lightbulb-o" aria-hidden="true"></i>&nbsp;&nbsp;提示</p> <p><span>远程访问 Raspbian 可使用 putty 经过 SSH 进行访问,也可使用 apt 安装 xrdp ,经过 Windows 远程桌面进行访问。对于没有桌面环境的 Raspbian Lite,能够经过执行 sudo raspi-config 进行配置。</span></p> </div>docker

  1. 使用二进制文件安装 .NET Core 运行时架构

    1. 下载
      wget https://download.visualstudio.microsoft.com/download/pr/4f9988da-8a62-4e01-9978-d9f1dd4fc386/3acb243f96e8e20b6774c64694d478ce/dotnet-runtime-2.1.13-linux-arm.tar.gz
    2. 解压
      mkdir ~/dotnet21 && tar -xvf dotnet-runtime-2.1.13-linux-arm.tar.gz -C ~/dotnet21
    3. 建立连接
      sudo ln -s ~/dotnet21/dotnet /usr/bin/dotnet

Blink

熟悉 Arduino 的朋友都知道,Blink 是默认烧写进 Arduino 的初始程序,控制板载链接 13 号引脚的 LED 闪烁,是一种相似于“Hello World”的存在。这里咱们将 LED 小灯链接至 Raspberry Pi 的 GPIO 17 引脚。app

硬件需求

名称 数量
LED 小灯 x1
220 Ω 电阻 x1
杜邦线 若干
  • LED 正极 - GPIO 17 (Pin 11)
  • LED 负极 - GND

电路

使用 Docker 运行示例

示例地址:https://github.com/ZhangGaoxing/dotnet-core-iot-demo/tree/master/src/Blink工具

docker build -t iot-blink -f Dockerfile .
docker run --rm -it --device /dev/gpiomem iot-blink

代码

  1. 打开 Visual Studio ,新建一个 .NET Core 控制台应用程序,项目名称为“Blink”。ui

  2. 打开 “工具”——“NuGet包管理器”——“程序包管理器控制台”,运行以下命令,以获取程序包。

    PM> Install-Package System.Device.Gpio
  3. 在 Program.cs 中,替换以下代码:

    using System;
    using System.Device.Gpio;
    using System.Threading;
    
    namespace Blink
    {
        class Program
        {
            static void Main(string[] args)
            {
                // 定义引脚
                int pinNumber = 17;
                // 定义延迟时间
                int delayTime = 1000;
    
                // 获取 GPIO 控制器
                using (GpioController controller = new GpioController(PinNumberingScheme.Logical))
                {
                    // 打开引脚 17
                    controller.OpenPin(pinNumber, PinMode.Output);
    
                    // 循环
                    while (true)
                    {
                        Console.WriteLine($"Light for {delayTime}ms");
                        // 打开 LED
                        controller.Write(pinNumber, PinValue.High);
                        // 等待 1s
                        Thread.Sleep(delayTime);
    
                        Console.WriteLine($"Dim for {delayTime}ms");
                        // 关闭 LED
                        controller.Write(pinNumber, PinValue.Low);
                        // 等待 1s
                        Thread.Sleep(delayTime);
                    }
                }
            }
        }
    }

部署

  1. 在“程序包管理器控制台”运行发布命令:

    dotnet publish -c release -r linux-arm

<div style="display: block;position: relative;border-radius: 8px;padding: 1rem;background-color: #d2f9d2;color: #094409;margin: 10px"> <p style="margin-top:0;font-weight: bold"><i class="fa fa-lightbulb-o" aria-hidden="true"></i>&nbsp;&nbsp;提示</p> <p><span>默认的发布路径是在 “\Blink\bin\Release\netcoreappXXX\win10-arm\publish”。你也可使用 -o 来指定发布路径,如:-o D:\BlinkPublish ,这将会发布在 D 盘的 BlinkPublish 文件夹下。</span></p> </div>

  1. 使用 FTP 工具将生成的发布文件夹复制到 Raspberry Pi 上,这里使用的是 WinSCP 。

<div style="display: block;position: relative;border-radius: 8px;padding: 1rem;background-color: #d2f9d2;color: #094409;margin: 10px"> <p style="margin-top:0;font-weight: bold"><i class="fa fa-lightbulb-o" aria-hidden="true"></i>&nbsp;&nbsp;提示</p> <p><span>Raspbian 使用 FTP 服务,请使用 apt 安装 vsftpd 。</span></p> </div>

  1. 更改程序权限。使用 cd 命令切换到发布的文件夹,运行:

    chmod 755 ./Blink

    或使用 FTP 工具进行变动

  2. 执行 ./Blink 运行程序,此时 LED 小灯应该一闪一闪的了。

供参考

  1. dotnet/iot Documentation:https://github.com/dotnet/iot/blob/master/Documentation/README.md
  2. .NET Core on Raspberry Pi:https://github.com/dotnet/core/blob/master/samples/RaspberryPiInstructions.md
相关文章
相关标签/搜索