3. About Alon Fliess
3
Chief Architect & Founder – CodeValue
Microsoft Regional Director
Microsoft Azure MVP
alonf@codevalue.net
@alon_fliess
http://alonfliess.me
http://codevalue.net
4. IoT 101
var sensorData = await _bmp180.GetSensorDataAsync(Bmp180.UltraHighResolution);
var messageString = JsonConvert.SerializeObject(sensorData);
var message = new
Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString));
await deviceClient.SendEventAsync(message);
5. .NET IoT Libraries
System.Device.Gpio
Hardware + OS
Raspberry Pi, BeagleBoard, HummingBoard, ODROID,
and other single-board-computers that are supported
by Linux and Windows 10 IoT Core OS
Wraps hardware access libraries on both Windows
and Linux
Provides API for:
General-purpose I/O (GPIO)
Inter-Integrated Circuit (I2C)
Serial Peripheral Interface (SPI)
Pulse Width Modulation (PWM)
Serial port
Device Binding
Built on top of System.Device.Gpio
Cross-Platform hardware driver classes
Sensors, Displays, HMI devices and anything else that
requires software to control
Cross-targeting .NET Standard 2.0 and up
Full Framework, 3.1, 5, 6, mono
5
7. What is a source generator?
It lets you generate source code
What does “generate source” mean?
Why isn’t he saying any of these bullet points?
So “source” I guess means source code
And “generate” means create
Wait.. Are source generators going to take our jobs?!
8. Your .cs
files
The C# Compiler
Project.dll
Parse
Source
Generator
Compile Emit
CST
AST
CST
Compilation IL
Source
(string)
CST
10. A Simplified Flow
Initialize Register for a syntax node notification
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver());
Syntax node notification gather code generation information
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
//collect information
…
}
Execute Generate source files and add them to the compilation
10
Your Implementation
11. IoTHubClientGenerator
Build an IoT Device client program in seconds
Handle configuration in multiple ways
Use IoT Device Connection String, or Device Provisioning Service
Support most of Azure IoT Hub protocols and protocols’ security
Support Device Twin, Sending Telemetry, Direct Method and Cloud to Device
messages
Support device connection status changed notification
11
12. Demo
Show me an
example!
• Raspberry Pi
• .NET 6.0 + .NET IoT
Libraries
• C# Source Code
Generator
• Azure IoT Hub + Azure
IoT Client SDK for C#
• VS Code
13. Summary & Takeaways
.NET IoT Libraries – cross platform IoT foundation
Azure IoT Hub – cloud scale IoT Platform
C# + Source generators (IoTHubClientGenerator) – The easiest way to
develop Azure connected IoT device
13
16. Appendix – Demo Code
1. using System;
2. using System.Device.I2c;
3. using System.Device.Gpio;
4. using System.Threading;
5. using Iot.Device.Tcs3472x;
6. using System.Threading.Tasks;
7. using IoTHubClientGeneratorSDK;
8. using Microsoft.Azure.Devices.Client;
9. using Microsoft.Azure.Devices.Client.Exceptions;
10. using Microsoft.Azure.Devices.Client.Transport.Mqtt;
11. using System.Text;
12.
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 OS can be used with .NET Core and System.Device.Gpio. On Windows 10 IoT Core OS, the library wraps the Windows.Devices.Gpio.dll assembly. On Linux, the library supports three driver modes: libgpiod for fast full-featured GPIO access on all Linux distros since version 4.8 of the Linux kernel; slower and limited-functionality GPIO access via the deprecated Sysfs interface (/sys/class/gpio) when running on older Linux distro versions with a Linux kernel older than version 4.8; and lastly board-specific Linux drivers that access GPIO addresses in /dev/mem for fasted performance at the trade-off of being able to run on very specific versions of single-board-computers. In the future, the board-specific Linux drivers may be removed in favor of only supporting libgpiod and sysfs Linux interfaces. In addition to System.Device.Gpio, the optional IoT.Device.Bindings NuGet package contains device bindings for many sensors, displays, and input devices that can be used with System.Device.Gpio.
Microsoft Azure IoT Hub provides capabilities for securely connecting, provisioning, updating and sending commands to devices. IoT Hub enables companies to control millions of IoT assets running on a broad set of operating systems and protocols to jumpstart their Internet of Things projects.
IoT Hub enables companies to:
Establish reliable bi-directional communication with IoT assets, even if they are intermittently connected, so companies can analyze incoming telemetry data and send commands and notifications as needed.
Enhance security of IoT solutions by leveraging per-device authentication to communicate with devices with the appropriate credentials.
Revoke access rights to specific devices, if needed, to maintain the integrity of the system.
A Source Generator is a .NET Standard 2.0
CST- Concrete Syntax Tree
https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/
A Source Generator is a new kind of component that C# developers can write that lets you do two major things:
Retrieve a Compilation object that represents all user code that is being compiled. This object can be inspected and you can write code that works with the syntax and semantic models for the code being compiled, just like with analyzers today.
Generate C# source files that can be added to a Compilation object during the course of compilation. In other words, you can provide additional source code as input to a compilation while the code is being compiled.
Demonstrate using source code generator from NuGet