Skip to content

This is a toolkit for quickly creating IoT Plug and Play compliant control software for Azure IoT Hub-connected devices using .NET.

License

Notifications You must be signed in to change notification settings

jcoliz/AzDevice.IoTHubWorker

Repository files navigation

AzDevice IoT Hub Worker

This is a toolkit for quickly creating IoT Plug and Play compliant control software for Azure IoT Hub-connected devices using .NET.

Running on Raspberry Pi

Why?

The basic work of connecting with Azure IoT Hub, following the IoT Plug and Play conventions, formatting messages, receiving commands or property updates--this all tends to look the same in each solution. My goal with the IoT Hub Worker is to write all that stuff just once. This leaves the application to focus on only the solution-specific work of implementing the telemetry, properties, and commands of a particular interface.

Ultimately this makes it much faster to bring up a new proof of concept connected to Azure IoT.

Basic Idea

The IoT Hub Worker is an Inversion of Control framework, which handles the communication between the device and IoT Hub. It will provision the device with DPS, then handle the initial IoT Hub setup and ongoing communication.

You set this up by defining a Program.cs which looks like the example below. The ControllerModel class in this example contains the application-specific logic, which will be called by the IoT Hub Worker as needed. The IRootModel and IComponentModel interfaces define the points of interaction where the application-specific logic can expect control.

IHost host = Host.CreateDefaultBuilder(args)
    .UseSystemd() 
    .ConfigureServices(services =>
    {
        services.AddHostedService<IoTHubWorker>();
        services.AddSingleton<IRootModel,ControllerModel>();
    })
    .ConfigureAppConfiguration(config =>
    {
        config.AddTomlFile("config.toml", optional: true, reloadOnChange: true);
    })
    .Build();

host.Run();

To take control of the inner loop, while still taking advantage of the individual functions of the base class,
implement a derived class and override ExecuteAsync(). The individual components called from there could be invidually used by a derived class.

Feature Set

  • Automatically provision device using Device Provisioning Service
  • Make connection with Azure IoT Hub
  • Read initial configuration state from standard .NET configuration sources
  • Send telemetry automatically on a configurable period
  • Report actual properties automatically at increasingly longer intervals
  • Receive desired property updates, passing them through to component implementation
  • Receive commands from IoT Hub, passing them through to component implementation
  • Follows IoT Plug and Play conventions to enable a model-based solution
  • Automatically supply a standard DeviceInformation model
  • Detailed multi-level logging to help with troubleshooting
  • Integrates with systemd to run as a background service on Linux
  • Copious examples showing usage with simulated and phyiscal devices on a variety of busses

How To...

Examples

There are some examples which help show how to use the IoT Hub Worker.

  • TemperatureContoller. Canonical example, simulates a "dtmi:com:example:Thermostat;2" model, with all telemetry, properties, and commands.
  • ClimateMontitor. Another example of implementing an existing model with simulated data. This one simulates "dtmi:com:example:climatemonitor;1".
  • I2cTempHumidityMonitor. This example is targeted toward a real physical sensor running on a Raspberry Pi. For easy testing, you can run this on a Windows PC and generate simulated data.
  • ModbusTempHumidityMonitor. This example shows how to connect to a sensor via Modbus. It will run on Windows or Linux, as you can easily use a USB-to-RS485 dongle to make the physical connection on any machine.

About

This is a toolkit for quickly creating IoT Plug and Play compliant control software for Azure IoT Hub-connected devices using .NET.

Resources

License

Stars

Watchers

Forks