Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CodeFest 2018. Raffaele Rialdi (Vevy Europe) — Writing IoT applications with NetCore 2 on the Raspberry PI

CodeFest 2018. Raffaele Rialdi (Vevy Europe) — Writing IoT applications with NetCore 2 on the Raspberry PI

Посмотрите выступление Raffaele: https://2018.codefest.ru/lecture/1245/

NetCore is the new cross-platform flavor of the .NET Framework which can smoothly run on Linux machines and ARM devices too.

During this session we will dig into this exciting world to see what is necessary to build applications leveraging the power of sensors and peripherals the Raspberry PI offers. In particular we will see a real-life scenario leveraging an ASP.NET Core application with database and authentication whose same codebase can run both on Windows and the PI.

During the session I will also show a tool that makes the development lifecycle much easier by allowing the continuous deployment on the device.

CodeFest

April 05, 2018
Tweet

More Decks by CodeFest

Other Decks in Programming

Transcript

  1. Writing IoT applications with NetCore 2 on the Raspberry PI

    Raffaele Rialdi Senior Software Architect Vevy Europe @raffaeler [email protected]
  2. Who am I? • Raffaele Rialdi, Senior Software Architect in

    Vevy Europe – Italy @raffaeler also known as "Raf" • Consultant in many industries Manufacturing, racing, healthcare, financial, … • Speaker and Trainer around the globe (development and security) Italy, Romania, Bulgaria, Russia (CodeFest, DotNext), USA, … • And proud member of the great Microsoft MVP family since 2003
  3. What is Net Core • Technically a «fork» of the

    .NET Framework • Looks the same CLR and Base Class Library but improved • It works Cross Platform (Linux, Mac, Windows) + x86, x64, ARM • Scenarios taking advantace from Net Core (currently) are: • ASP.NET Core: new ASP.NET stack, re-written from scratch • Universal Windows Platform (Windows Store Apps) • Cloud Applications: Applications and Microservices running on Azure • Console Application: the best way to start testing Net Core • The wat to go for containerized applications / docker ready
  4. Raspberry Peripherals 01 05 09 13 17 21 25 29

    33 37 02 06 10 14 18 22 26 30 34 38 3.3V SCL.1 0V GPIO.2 3.3V MISO 0V GPIO.21 GPIO.23 GPIO.25 5V 0V RxD 0V GPIO.5 GPIO.6 CE1 0V 0V GPIO.28 - 9 - 2 - 13 - 21 23 25 - - 16 - 5 6 11 - - 28 - 3 - 27 - 9 - 5 13 26 - - 15 - 24 25 7 - - 20
  5. Getting started • Device information: https://www.raspberrypi.org/ • Required Hardware: RPi2,

    RPi3 • RPi Zero does not work because it is based on the ARMv6 instructions • NetCore binaries for ARM are in Preview • The official status is currently "Not Supported" ... but stay tuned! • True for both Linux and Windows • One repository with all the info you need: • https://github.com/raffaeler/raspberrypi • Tutorials, materials, resources, GPIO pin maps and more
  6. Setting up Raspbian image • TL;DR 1. Flash the SD

    Card with the latest «Raspbian Stretch» 2. Boot and configure 3. Install these packages • sudo apt-get install libunwind8 libunwind8-dev gettext libicu-dev liblttng-ust-dev libcurl4-openssl- dev libssl-dev uuid-dev unzip • Optional • Install the Runtime for NetCore 2.0 • https://www.microsoft.com/net/download/linux • New! NetCore 2.1 SDK is in progress (and the preview is available now)
  7. Useful tools • SSH client • You need to enable

    SSHD on the device via raspi-config utility • Get a SSH client for Windows (Putty, Bitvise, …) to use the terminal • Get an SCP client (WinSCP, Bitvise, …) to copy files from/to the device • DeployTool by Raf (me) • A tool to ease deployment to a Linux machine (Continuous Deployment) • https://github.com/raffaeler/DeployTool
  8. Creating a Console App • Use the default template for

    a NetCore 2.0 Console app • Two local peripherals "netstandard" libraries currently available: • Unosquare.Raspberry.IO by Unosquare Labs • Pi.IO by Peter Marcu • If there are ARM specific resources, you may need to add the RuntimeIdentifier in the csproj <PropertyGroup> <OutputType>Exe</OutputType> <RuntimeIdentifier>linux-arm</RuntimeIdentifier> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup>
  9. Deploying the App • From the Windows command line: •

    If the runtime has been installed on the RPi dotnet publish -c Release -r linux-arm --self-contained=false or • To fully deploy the app and the CLR dotnet publish -c Release -r linux-arm --self-contained • On the Raspberry PI • Copy the files in the bin/release/linux-arm/publish using the SSH client • Grant eXecutable permissions: chmod +x myapp • Run it: ./myapp
  10. Continuous Deployment (CD) SSHDeploy is a tool created by me

    (Raf) • https://github.com/raffaeler/DeployTool 1. Write a configuration file 2. Run "dotnet-deploy interact" 3. Use the menu to run the config { "Description": "Raspberry", "Ssh": { "Host": "pi-atd01", "Username": "pi", "EncryptedPassword": "…" }, "Actions": [ { "ActionName": "DotnetPublishAction", "OutputFolder": "release-linux-arm", "RuntimeIdentifier": "linux-arm", "Configuration": "Release", "IsSelfContained": true }, { "ActionName": "SshCopyToRemoteAction", "LocalItems": [ "$(publishdir)", "db.sqlite", "pi1-cert.pfx" ], "RemoteFolder": "/home/pi/$(projectname)", "Recurse": true } ] }
  11. A full-featured ASP.NET Core App • MVC and WebApi work

    smoothly • Websockets • nuget package: Microsoft.AspNetCore.WebSockets • Entity Framework Core • It works with SQLite too • Requires: SQLitePCLRaw.lib.e_sqlite3.linux nuget package • HTTPS channel • The secure channel can be set up on the .NET Kestrel web server
  12. Takeaways • Check out the material on GitHub • https://github.com/raffaeler/raspberrypi

    • Tutorial to start using a Rapsberry PI • Setting up .NET Core 2.0 • Using the peripherals, PINs layout and C# libraries • Working with Docker on the Raspberry PI • Embrace the new .NET era • Create new libraries as "nestandard" • Read the latest: https://blogs.msdn.microsoft.com/dotnet/ • Continue the conversation later today, on Github or on Twitter @raffaeler