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

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

  提示

因为 .NET Core JIT 依赖于 ARMv7 指令集,因此处理器架构新于 ARMv7 的 Linux 开发板都可以使用此包进行硬件操作。当然,一些特殊的硬件操作除外,比如对 GPIO 引脚进行上拉,这需要对处理器的寄存器进行访问,而 System.Device.Gpio 对不支持的硬件仅实现了通用操作。

若要继续阅读下面的内容,你需要准备:

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

环境配置

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

  提示

远程访问 Raspbian 可以使用 putty 通过 SSH 进行访问,也可以使用 apt 安装 xrdp ,通过 Windows 远程桌面进行访问。对于没有桌面环境的 Raspbian Lite,可以通过执行 sudo raspi-config 进行配置。

  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
      

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

硬件需求

名称 数量
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”。

  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
    

  提示

默认的发布路径是在 “\Blink\bin\Release\netcoreappXXX\win10-arm\publish”。你也可以使用 -o 来指定发布路径,如:-o D:\BlinkPublish ,这将会发布在 D 盘的 BlinkPublish 文件夹下。

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

  提示

Raspbian 使用 FTP 服务,请使用 apt 安装 vsftpd 。

  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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章