DEV Community

Dave Glover for Microsoft Azure

Posted on

Build Raspberry Pi .NET Core IoT Applications running on Raspberry Pi OS or Ubuntu 20.04

.net core loves single board computers


Follow me on Twitter @dglover


#JulyOT

This is part of the #JulyOT IoT Tech Community series, a collection of blog posts, hands-on-labs, and videos designed to demonstrate and teach developers how to build projects with Azure Internet of Things (IoT) services. Please also follow #JulyOT on Twitter.


Operating Systems and ARM architectures supported

This tutorial has been tested with .NET Core applications running on Raspberry Pi OS and Ubuntu 20.04 (including Ubuntu Mate 20.04) for both 32bit (ARM32) and 64bit (ARM64). The projects also include build tasks for Debug and Release configurations.

Source Code

The source and the samples for this tutorial can be found here.

Examples

The examples are found in the samples director of this repo and cover the following .NET Core scenarios.

  1. Simple hello world: dotnet.core.iot.csharp
  2. Azure IoT Hub: dotnet.core.iot.csharp.iothub
  3. .NET Core Web API: dotnet.core.iot.csharp.webapi
  4. .NET Core Web App: dotnet.core.iot.csharp.webapp
  5. Azure IoT Hub or IoT Central with DPS (Device Provisioning Service): dotnet.core.iot.csharp.dps.iot
  6. Azure IoT device twins: dotnet.core.iot.csharp.dps.iot.device-twins
  7. Azure IoT Hub device twins, and Pi Sense HAT: dotnet.core.iot.csharp.dps.iot.device-twins.sense.hat
  8. Simple Pi Sense HAT Sample: dotnet.core.iot.csharp.sense.hat 9: F# and .NET Core: dotnet.core.iot.fsharp

Tips and Tricks for setting up Ubuntu 20.04 on a Raspberry Pi

Check out the following Raspberry Pi Tips and Tricks to boot Ubuntu from USB3 SSD, how to overclock, enable WiFi, and support for the Raspberry Pi Sense HAT.


Introduction

The .NET Core IoT Library connects your applications to hardware. In this tutorial you will learn how to:

  1. Develop a C# .NET Core IoT application from your Linux, macOS or Windows 10 computer,
  2. Streamline the develop, deploy, and debug process using Visual Studio Code,
  3. Use the .NET Core IoT library to read the CPU temperature. The CPU temperature will be used to represent the room temperature in this lab scenario,
  4. Stream temperature telemetry to Azure IoT Hub,
  5. Control a remote HVAC system.

Raspberry Pi Hardware

.Net Core requires an AMR32v7 processor and above, so anything Raspberry Pi 2 or better and you are good to go. Note, Raspberry Pi Zero is an ARM32v6 processor, and is not supported.

The Raspberry Pi 3a Plus is a great device for .NET Core.

I'm super happy with my Raspberry Pi 4B 4GB and 8GB devices seen here dress with a heatsink case.

I like to use Raspberry Pi OS Lite as it takes less resources than the full Raspberry Pi Desktop version and I do all my Raspberry Pi development from my computer.

If you've not set up a Raspberry Pi before then this is a great guide. "Setting up a Headless Pi". Be sure to use the WiFi network as your development computer.

If you are not comfortable setting up Raspberry Pi OS Lite (Headless), then follow the Setting up your Raspberry Pi guide to set up the full Raspberry Pi OS Desktop version.

Optional Hardware

The Raspberry Pi Sense HAT has builtin sensors and a LED panel. If you have one then great, one of the labs uses this HAT.


Why .NET Core

It used by millions of developers, it is mature, fast, supports multiple programming languages (C#, F#, and VB.NET), runs on multiple platforms (Linux, macOS, and Windows), and is supported across multiple processor architectures. It is used to build device, cloud, and IoT applications.

.NET Core is an open-source, general-purpose development platform maintained by Microsoft and the .NET community on GitHub.


Learning C#

There are lots of great resources for learning C#. Check out the following:

  1. C# official documentation
  2. C# 101 Series with Scott Hanselman and Kendra Havens
  3. Full C# Tutorial Path for Beginners and Everyone Else

The .NET Core IoT Libraries Open Source Project

The Microsoft .NET Core team along with the developer community are building support for IoT scenarios. The .NET Core IoT Library is supported on Linux, and Windows IoT Core, across ARM and Intel processor architectures. See the .NET Core IoT Library Roadmap for more information.

System.Device.Gpio

The System.Device.Gpio package supports general-purpose I/O (GPIO) pins, PWM, I2C, SPI and related interfaces for interacting with low-level hardware pins to control hardware sensors, displays and input devices on single-board-computers; Raspberry Pi, BeagleBoard, HummingBoard, ODROID, and other single-board-computers that are supported by Linux and Windows 10 IoT Core.

Iot.Device.Bindings

The .NET Core IoT Repository contains IoT.Device.Bindings, a growing set of community-maintained device bindings for IoT components that you can use with your .NET Core applications. If you can't find what you need then porting your own C/C++ driver libraries to .NET Core and C# is pretty straight forward too.

The drivers in the repository include sample code along with wiring diagrams. For example the BMx280 - Digital Pressure Sensors BMP280/BME280.


Raspberry Pi .NET Core Developer Learning Path


Have fun and stay safe and be sure to follow us on #JulyOT.

Top comments (0)