Building Connected Things with Windows 10 IoT and Microsoft Azure

Lab 01: Hello, Windows IoT!

In this lab, you will create a simple 'Thing' using Windows 10 IoT Core.

Table of Contents

In this lab, you will create a simple Thing using a Windows 10 IoT device and the Universal Windows Platform.

Bill of Materials

What you will need:

  1. Raspberry Pi 2
  2. From the GrovePi+ Starter Kit for Raspberry Pi
    • GrovePi shield
    • Grove LED (any color) × 1 + connector cable
  3. 5V (2A to 3A) Switching Power Supply w/ MicroUSB Cable
  4. A Wi-Fi Adapter (choose one from the list here)
  5. 8GB micro SD card - class 10 or better. Microsoft suggests one of the following:

If you haven’t already done so, follow the setup instructions at ‘Setting Up Your Raspberry Pi 2’.

Connect the Grove Parts (Shield and LED)

The Raspberry Pi 2 connects to the physical world through the GPIO pins. GPIO stands for General Purpose Input/Output and refers to the two rows of pins on Raspberry Pi 2. The GPIO pins are a physical interface between the Raspberry Pi 2 and the physical world. Through your app, you can designate pins to either receive input or send output. The inputs can be from switches, sensors or other devices. The outputs can be LEDs, servos, motors or countless other devices. Twenty-six of the 40 pins are GPIO pins; the others are power, ground, or reserved pins.

RPi2 Pin Map

The GrovePi Shield simplifies accessing the pins by providing connectors that you can simply plug sensors and devices into. The GrovePi Shield exposes GPIO pins (labeled D2-D8 for digital and A0-2 for analog), I2C (pronounced Eye-Squared-See) and Serial/SPI connections.

GrovePi Shield

Note: The Raspberry Pi 2 doesn’t have analog GPIO. The GrovePi shield includes an Analog-Digital Converter enabling you to connect analog devices to the A0-A2 connectors.

You can read more about how the GrovePi Shield works here.

Connect the GrovePi shield to the Raspberry Pi 2.

Connect the GrovePi Shield

Connect the LED to the D4 jack.

Connect the LED to D4

Create the Device Application

A Universal Windows app is a Windows experience that is built upon the Universal Windows Platform (UWP), which was first introduced in Windows 8 as the Windows Runtime. The UWP enables you to write an app that targets a device family, such as IoT devices. In fact, the Universal app that you write may be able to run on multiple device families, depending on the device characteristics that it takes advantage of. In this lab, you will create a Universal app targeting IoT devices running Windows 10. In theory this could be nearly any device, such as a phone, a tablet or a Raspberry Pi 2. The Universal app you will write, however, will access the General Purpose Input/Output (GPIO) of the device, so the app won’t be compatible with devices that don’t have a GPIO.

Create a New Background Task Project

  1. Launch Visual Studio and start a new Background Application (IoT) (found in the Templates -> C# -> Windows -> Windows IoT Core node).
  2. Name the application HelloWindowsIoT.

Create a new IoT Background application

Add a Reference to the GrovePi Libraries

The GrovePi libraries provide an abstraction over the Windows 10 IoT Core GPIO, I2C and SPI interfaces expose the sensor and device functionality as objects. The GrovePi libraries are easy to install using the Package Manager Console.

  1. To install GrovePi for Windows IoT, run the following command from the Package Manager Console
Install-Package GrovePi
  1. Verify that GrovePi v1.0.7 was installed by reading the log in the Package Manager Console.

Define the Libraries the App Requires

This application will run as a background task on a Windows 10 IoT device. Unlike a graphical (or headed) UWP application, where the Windows IoT OS is limited to running only one app at a time, Windows 10 IoT Core (and better) can run multiple BackGround Task applications at once. These are headless applications. That is to say, there is no UI. You will write code that performs the necessary functions for the application, but there will not be any UI. This example will demonstrate how to blink and LED on and off.

  1. Open the StartupTask.cs file. Add the following to the using statements at the top of the file.
// Add using statements to the GrovePi libraries
using GrovePi;
using GrovePi.Sensors;
using Windows.System.Threading;

Define the Class-level Variables

There are three class-level variables you will use in this application:

  • timer - A ThreadPoolTimer instance that will control the LED blinking interval.
  • deferral - A BackgroundTaskDeferral instance that will allow the application to continue to run even after the Run() method has completed.
  • led - A GrovePi.ILed instance that represents the Grove LED sensor.
  1. Add the following class-level variable definitions inside the public sealed class StartupTask : IBackgroundTask class definition:
namespace HelloWindowsIoT
{
    public sealed class StartupTask : IBackgroundTask
    {
        ThreadPoolTimer timer;
        BackgroundTaskDeferral deferral;
        ILed led;

        public void Run(IBackgroundTaskInstance taskInstance)
        {
            
        }
    }
}

Instantiate the LED Sensor and a Timer to Control It

For this application, you will use a ThreadPoolTimer to raise an event at a predefined interval. (In this example you will raise events once per second.)

  1. Inside the public void Run(IBackgroundTaskInstance taskInstance) function create a new object instance using the led variable, and a ThreadPoolTimer that will raise an event every one-second.
public void Run(IBackgroundTaskInstance taskInstance)
{
    deferral = taskInstance.GetDeferral();

    // Connect the LED to digital port 4
    led = DeviceFactory.Build.Led(Pin.DigitalPin4);

    // Create a timer that will 'tick' every one-second
    timer = ThreadPoolTimer.CreatePeriodicTimer(this.Timer_Tick, TimeSpan.FromSeconds(1));
}

Handle the Timer_Tick Event

In the previous section, you defined an event handler that will be invoked each time the timer ticks off using the ThreadPoolTimer.CreatePeriodicTimer(callback, TimeSpan)) method. Now you will add the callback event (aka the event handler). When the Timer_Tick event handler is invoked it will check the current state of the LED (on or off) and switch it to the opposite state.

  1. Using the Visual Studio refactoring tools, you can generate the method stub for the Timer_Tick event handler. Hover over the Timer_Tick text until a light bulb appears. Click the down arrow and select Generate method ‘StartupTask.Timer_Tick’

Generate Timer_Tick Event Handler

  1. Add the following code for the Timer_Tick event handler.
private void Timer_Tick(ThreadPoolTimer timer)
{
    try
    {
        led.ChangeState( (led.CurrentState == SensorStatus.Off) ? SensorStatus.On : SensorStatus.Off );
    }
    catch (Exception ex)
    {
        // Swallow the exception
    }
}

If you want to compare your code with the master lab code, you can find it in the ThingLabs - Thingy4Windows Github repo here.

Run the App on a Raspberry Pi 2

To run the application you will build it locally and then deploy it to the Raspberry Pi 2 and open a remote debugging session. Fortunately, this is all encapsulated in a simple gesture once you have it configured.

  1. Ensure ARM is selected in the Solution Platforms drop-down list in the toolbar
  2. Select Remote Machine from the Device list in the toolbar.

Select ARM

  1. You will be prompted with the Remote Connections dialog. You can select your device in one of two ways:
  • Select your device from the list of Auto Detected devices, OR
  • Type in the device name or IP address into the Manual Configuration text box (set the Authentication Mode to Universal (Unencrypted Protocol)) and click Select.

Select your device

  1. Now press F5 to run the application and you should see (in the Output windows) it building locally and then deploying on the Raspberry Pi 2. You will see the LED blink once per second.

NOTE: You can verify or modify these values by navigating to the project properties. This can be accomplished by double-clicking the Properties node in Solution Explorer and clicking on the Debug tab on the left.

Conclusion & Next Steps

Congratulations! You have built a Windows 10 IoT application that controlled a device connected to a Raspberry Pi 2. The core concepts you’ve learned are:

  1. Building a Windows 10 IoT Background Task application that can run on a Windows 10 IoT device.
  2. Using BackgroundTaskDeferral to enable an application to run even after the Run() method completes.
  3. Controlling the state of a device.

In the next lab, you will build a more complicated Thing that uses both input sensors and output devices.

Go to ‘Lab 02: Nightlight’ ›