Weather station (using Lightning Providers)

This sample demonstrates how to communicate with an I2c device using the Micorosot.IoT.Providers.Lightning.I2cProvider.

It is based on the Weather Station sample and shares the same setup steps and most of the code.

Using Default Provider (Default Controller Driver/Inbox Driver)

The original sample used the default I2C provider for obtaining the I2C sensor device as follows:

// Initializing the sensor device in the original Weather Station sample code

I2cDevice sensor;

public async void Run(IBackgroundTaskInstance taskInstance)
{
    deferral = taskInstance.GetDeferral();

    String aqs = I2cDevice.GetDeviceSelector("I2C1");
    IReadOnlyList<DeviceInformation> dis = await DeviceInformation.FindAllAsync(aqs);
    //Ox40 was determined by looking at the datasheet for the device
    sensor = await I2cDevice.FromIdAsync(dis[0].Id, new I2cConnectionSettings(0x40));

    timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(1000));
}

The sample code using the default I2C provider, Weather Station sample.

Using the Lightning provider

Now, to enable the use of the Lightning provider, the I2C initialization code needs to be modified to set the Lightning provider as the default provider.

In the initialization code below, if the Lightning driver is enabled on the target device, the Lightning provider will be used. Otherwise, the app falls back to using the default provider.

using Microsoft.IoT.Lightning.Providers;

I2cDevice sensor;

public async void Run(IBackgroundTaskInstance taskInstance)
{
    deferral = taskInstance.GetDeferral();

    // Set the Lightning Provider as the default if Lightning driver is enabled on the target device
    if (LightningProvider.IsLightningEnabled)
    {
        LowLevelDevicesController.DefaultProvider = LightningProvider.GetAggregateProvider();
    }

    // This code works the same regardless of the current default provider
    I2cController controller = await I2cController.GetDefaultAsync();
    //Ox40 was determined by looking at the datasheet for the device
    sensor = controller.GetDevice(new I2cConnectionSettings(0x40));

    timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(1000));
}

Building and deploying the sample

  1. Enable Lightning on your Raspberry Pi2 or MBM device using the the steps outlined in the Lightning Setup Guide.

  2. Ensure the correct version of the Windows SDK is installed on your development machine. The required Windows SDK can be installed from here.

  3. Clone the source for the WeatherStation sample for Lightning from Github to a local folder.

  4. To setup the connections between the device and the weather shield, refer to the original sample.

  5. Load the solution in Visual Studio.

  6. Select “Weather Station (Universal Windows)” as the Startup Project.

  7. Build the app.

  8. Press F5 to run and deploy the app to your device.
    You can use the Deploying an App with Visual Studio for guidance.

  9. Open the Debug Output pane in Visual Studio (Debug->Windows->Output) and you should be able to watch the sensor data as it’s being read by your application.

Weather Station Debug Output

Sample code

Language: C#/C++

Tags: c#, c++, intermediate, lightning, i2c

Verified to work with:
Windows 10 IoT Core: Version 10.0.10586.0
Visual Studio 2015 update 2
Windows SDK: Version 10586
(Included with Visual Studio Update 2)


Edit this page on GitHub


Was this page helpful?
Additional feedback?
Thank you! We appreciate your feedback.
Follow us