詳説 .NET 5 のIoTサポート

>100 Views

December 19, 20

スライド概要

2020/12/19 【オンライン】.NET 5 技術セミナー!最新の技術情報を共有しよう!# .NET Conf2020
https://algyan.connpass.com/event/190970/

profile-image

SeeedKKの中の人。Microsoft MVP for Internet of Things。

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

詳説? .NET 5 のIoTサポート 2020/12/19 Takashi Matsuoka

2.

Takashi Matsuoka (@matsujirushi12) 2017~ MVP for Windows Development 2020~ for Microsoft Azure 「e」3つ

3.

.NET 5 とは、どんなモノなのでしょうか。 IoT で使えるのでしょうか。 #ALGYAN

4.

https://docs.microsoft.com/ja-jp/archive/msdn-magazine/2019/july/csharp-net-reunified-microsoft%E2%80%99s-plans-for-net-5

5.

.NET 5 Windows, Linux, macOS MCU MT3620 MPU (Azure Sphere) Cortex-M Cortex-A7 + M4 Cortex-A, Celeron Flash内蔵 (e.g. 2MB) Flash内蔵 (16MB) Flash外付 (e.g. 64GB eMMC) RAM内蔵 (e.g. 256KB) RAM内蔵 (4MB) RAM外付 (e.g. 8GB) ベアメタル, RTOS 小さいLinux Windows, Linux US$1~10 US$10 US$30~

6.

Network Connectivity System.Net OS Windows, Linux, macOS .NET 5 Architectures x86, x64, Arm32, Arm64 .NET 5 - Supported OS versions Hardware Interfaces <UART> System.IO.Ports ( .NET Platform Extensions 5.0 ) <GPIO, PWM, I2C, SPI> System.Device.Gpio Iot.Device ( .NET Core IoT Libraries )

7.

.NET Core IoT Libraries https://github.com/dotnet/iot/blob/master/Documentation/roadmap.md

8.

.NET 5 VSCode ODYSSEY - X86 (+Ubuntu 18.04) LED

10.

Feature request: support Linux ARM / ARM64

11.
[beta]
DigitalWrite
Shell

$ git clone https://github.com/dotnet/iot
$ dotnet new console -o DigitalWrite
$ cd DigitalWrite
$ dotnet add package System.Device.Gpio
or
$ dotnet add reference ../iot/src/System.Device.Gpio/System.Device.Gpio.csproj

Program.cs

using System;
using System.Device.Gpio;
using System.Threading;

const int pin = 337;

C# 9 :
Top-level statements

// Pin7

using var controller = new GpioController();
controller.OpenPin(pin, PinMode.Output);
bool ledOn = true;
while (true)
{
ledOn = !ledOn;
controller.Write(pin, ledOn ? PinValue.High : PinValue.Low);
Thread.Sleep(100);
}

12.

DEMO

13.

DigitalWrite - Thread.Sleep(100)

14.

DigitalWrite - Thread.Sleep(100)

15.

DigitalWrite – Python(sysfs)

16.

"dotnet" is not a file ターミナル プロセスが起動に失敗しました: Path to shell executable "dotnet" is not a file of a symlink。 tasks.json ... { "label": "build", "command": “/usr/bin/dotnet", "type": "process", "args": [ "build", "${workspaceFolder}/DigitalWrite.csproj", "/property:GenerateFullPaths=true", "/consoleloggerparameters:NoSummary" ], "problemMatcher": "$msCompile" }, ...

17.

UnauthorizedAccessException

18.

UnauthorizedAccessException $ wget https://aka.ms/getvsdbgsh -O - 2>/dev/null | /bin/sh /dev/stdin -v latest -l ~/vsdbg $ chmod 700 ~/.ssh $ chmod 600 ~/.ssh/id_rsa $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys $ sudo vi /etc/sudoers ubuntu ALL=NOPASSWD: ALL ← 最下行に追加

19.
[beta]
UnauthorizedAccessException
launch.json
...
{
"name": ".NET Core Remote Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net5.0/DigitalWrite.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "ssh",
"pipeArgs": [
"-T",
"-i", "~/.ssh/id_rsa",
"ubuntu@localhost"
],
"debuggerPath": "sudo ~/vsdbg/vsdbg",
"quoteArgs": false
}
},
...

20.

ODYSSEY - X86 (+Ubuntu 18.04) Azure IoT Hub

21.
[beta]
SendTelemetry
Shell

Program.cs

$ git clone https://github.com/dotnet/iot

using
using
using
using
using

$ dotnet new console -o SendTelemetry
$ cd SendTelemetry
$ dotnet add package Microsoft.Azure.Devices.Client

System;
System.Text;
System.Text.Json;
System.Threading.Tasks;
Microsoft.Azure.Devices.Client;

CONNECT

const string CONNECTION_STRING = "HostName=matsujirushi-iothub.azuredevices.net;DeviceId=sim;SharedAccessKey=...";
Console.WriteLine("Create DeviceClient");
var client = DeviceClient.CreateFromConnectionString(CONNECTION_STRING, TransportType.Mqtt);

var rand = new Random();
while (true)
{
double currentTemperature = 20 + rand.NextDouble() * 15;
double currentHumidity = 60 + rand.NextDouble() * 20;
Console.WriteLine("Create telemetry message");
var messageBody = JsonSerializer.Serialize(new { temperature = currentTemperature, humidity = curre
ntHumidity, });
using var message = new Message(Encoding.ASCII.GetBytes(messageBody))
{
ContentType = "application/json",
ContentEncoding = "utf-8",
};

CREATE
MESSSGE

Console.WriteLine($"Send telemetry message {DateTime.Now}");
await client.SendEventAsync(message);
await Task.Delay(1000);
}

SEND

22.

DEMO

23.

.NET 5 とは、どんなモノなのでしょうか。 IoT で使えるのでしょうか。 • OS / Architectures • Network Connectivity • Hardware Interfaces • Development Environment

24.

少しだけDive!

25.

“As a way to try out the new release ourselves, a few of us decided to update the dotnet/iot repo to use new C# 9 syntax and target .NET 5.0. The changes resulted in removing >2k lines of code, just by adopting new syntax. It uses top-level programs, records, patterns, and switch expressions.” https://devblogs.microsoft.com/dotnet/announcing-net-5-0/

26.

GPIO - .NET Core IoT Libraries GpioController (.cs) partial GpioController (.Linux.cs) GpioController (.Windows.cs) Create instance GpioDriver UnixDriver RaspberryPi3Driver (.cs) RaspberryPi3Driver (.Linux.cs) RaspberryPi3Driver (.Windows.cs) LibGpiodDriver (.Linux.cs) SysFsDriver (.Linux.cs) Windows10Driver (.Windows.cs)

27.

GPIO - .NET Core IoT Libraries https://github.com/dotnet/iot/tree/master/src/System.Device.Gpio

28.

GPIO - .NET Core IoT Libraries

29.

GPIO - .NET Core IoT Libraries

30.

GPIO - .NET Core IoT Libraries

31.

GPIO - .NET Core IoT Libraries GpioController (.cs) partial GpioController (.Linux.cs) GpioController (.Windows.cs) Create instance GpioDriver UnixDriver (.cs) RaspberryPi3Driver (.cs) RaspberryPi3Driver (.Linux.cs) RaspberryPi3Driver (.Windows.cs) RaspberryPi3 LinuxDriver (.cs) RaspberryPiCM3 Driver (.cs) Windowsのときの 振る舞いは 割愛♪ LibGpiodDriver (.cs) SysFsDriver (.cs)

32.

sysfs is dead! long live libgpiod!

34.

[DllImport("libgpiod")] # export LD_DEBUG=files # ./DigitalWrite 2780: 2780: 2780: file=/usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/libgpiod.so [0]; dynamically loaded by /usr/sh file=/home/ubuntu/dotnet/DigitalWrite/bin/Debug/net5.0/libgpiod.so [0]; dynamically loaded by /usr/share file=libgpiod.so [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/libcore 2780: 2780: 2780: file=/usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/liblibgpiod.so [0]; dynamically loaded by /usr file=/home/ubuntu/dotnet/DigitalWrite/bin/Debug/net5.0/liblibgpiod.so [0]; dynamically loaded by /usr/sh file=liblibgpiod.so [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/libc 2780: 2780: 2780: file=/usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/libgpiod [0]; dynamically loaded by /usr/share file=/home/ubuntu/dotnet/DigitalWrite/bin/Debug/net5.0/libgpiod [0]; dynamically loaded by /usr/share/do file=libgpiod [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/libcoreclr 2780: 2780: 2780: file=/usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/liblibgpiod [0]; dynamically loaded by /usr/sh file=/home/ubuntu/dotnet/DigitalWrite/bin/Debug/net5.0/liblibgpiod [0]; dynamically loaded by /usr/share file=liblibgpiod [0]; dynamically loaded by /usr/share/dotnet/shared/Microsoft.NETCore.App/5.0.0/libcore