Windows 10 IoT Core, UWP and Raspberry Pi 2–The Switched Blinking Light Demo

I thought that I’d written some of this up before but it looks like I must have dreamt it because I can’t find the blog post that I thought I’d written and so I’ll start from scratch.

I’ve had a Raspberry PI V2 for a little while now. I bought it from RS Online;

Raspberry Pi 2 Model B

and I also bought a little case for it as well;

Clear Raspberry Pi 2 B, Raspberry Pi B+ Development Board Case

and that was enough for me to follow the instructions up on the Windows IoT site and get Windows IoT Core to boot from (in my case) a 32GB SD Card. Those instructions are here;

Setup your Device 

and I found that they worked fine for me as did the instructions for setting up the development tools;

Setup your PC

and I then ran through the instructions on this page to make sure that I could talk to my device via PowerShell;

Using PowerShell to connect and configure a device running Windows IoT Core

and that all worked fine with the exception that I’ve yet to get the IoT Core Watcher application to do anything other than crash for me but that hasn’t stopped me deploying bits to the PI and I found it easy enough to set up remote debugging on the PI.

I took the application that I built up in this recent blog post and deployed it down to the Raspberry PI and that’s shown in this little piece of video here;

and that all works out fine although you’ll notice that in the video the array of large images doesn’t load very nicely on the PI and that’s really testament to the fact that I should be paying more attention to decoded image sizes in the my code rather than just assuming that every device the code runs on is going to have a tonne of memory to decode bitmaps. If I fixed that code, it would improve things for all platforms so that’d be a good thing to do.

With that done, I wanted to get so that I was actually controlling something on the PI and I went and bought myself a starter kit from Amazon;

Sunfounder Project Super Starter Kit for Raspberry Pi Model B+ w/ 40-Pin GPIO Extension Board, GPIO Cable, H-Bridge L293D, ADXL335, DC Motor, 7-Segment, Dot Matrix Display

and that allowed me to try and build up a simple example of switching on/off a flashing LED which seems to be fast becoming the default thing to do on these devices.

At this point, I should say that it’s a long time since I did anything even vaguely electrical and I’ve forgotten how to read values from a resistor and I’ve forgotten which end of an LED is +ve/-ve and those kinds of things and, frankly, I was never much good at those things in the first place.

Regardless. Between a combination of the little instruction book that comes with the Sunfounder kit and these great blog posts written by Paul Winstanley;

Getting Started

Set up the PC

Making a Light Blink

and this YouTube video from Sunfounder I quickly found that I could make some kind of setup where I had a switch controlling an LED.

The main ‘snag’ I hit in trying to get this going was to make sure that I had the map of the pins on the Raspberry Pi 2 correct and I found the Sunfounder Extension Board to be a really big help there (because it is nicely labelled Smile) as web searches for “Raspberry Pi pins” kept coming up with different images.

My little bit of electronic wizardry (ahem) looked like this;

image 

and I wrote a little bit of a UWP app with some code to attempt to flash the LED whenever someone pressed the switch and until they pressed it again to turn off the flashing light.

What I found most ‘interesting’ about this is that writing the code was fairly trivial and I did it purely by discovering the API as I went. I hadn’t attempted to read any docs or code prior to trying it out but it seemed to just follow very naturally once I’d added the extension SDK for Windows IoT.

Then again, I am only listening to an on/off value from a switch and then turning a light on and off Smile

I connected my LED to GPIO number 5 and I connected my switch up to GPIO number 6 and that let me write code behind a simple (blank) XAML page;

namespace App115
{
  using System.Threading;
  using System.Threading.Tasks;
  using Windows.Devices.Gpio;
  using Windows.UI.Xaml;
  using Windows.UI.Xaml.Controls;

  public sealed partial class MainPage : Page
  {
    public MainPage()
    {
      this.InitializeComponent();
      this.resetEvent = new ManualResetEvent(false);
      this.isFlashing = false;
      this.Loaded += OnLoaded;
    }
    async void OnLoaded(object sender, RoutedEventArgs e)
    {
      this.controller = GpioController.GetDefault();

      if (this.controller != null)
      {
        var pin = controller.OpenPin(SWITCH_GPIO, GpioSharingMode.SharedReadOnly);
        pin.ValueChanged += OnValueChanged;
      }

      Task.Run(() =>
      {
        var ledPin = this.controller.OpenPin(LED_GPIO);
        ledPin.SetDriveMode(GpioPinDriveMode.Output);

        while (true)
        {
          // block.
          this.resetEvent.WaitOne();

          ledPin.Write(GpioPinValue.Low);

          // block.
          Task.Delay(LED_FLASH_INTERVAL_MS / 2).Wait();

          ledPin.Write(GpioPinValue.High);

          // block.
          Task.Delay(LED_FLASH_INTERVAL_MS / 2).Wait();
        }
      });
    }
    void OnValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
      if (args.Edge == GpioPinEdge.RisingEdge)
      {
        if (!this.isFlashing)
        {
          this.resetEvent.Set();
        }
        else
        {
          this.resetEvent.Reset();
        }
        this.isFlashing = !this.isFlashing;
      }
    }
    bool isFlashing;
    ManualResetEvent resetEvent;
    static readonly int LED_FLASH_INTERVAL_MS = 500;
    static readonly int SWITCH_GPIO = 6;
    static readonly int LED_GPIO = 5;
    GpioController controller;
  }
}

and so here I just have a task which spins around a loop flashing the LED on and off whenever the ManualResetEvent that it is controlled by allows it to. I also have an event handler for when the GPIO pin 6 changes value which I can then use to control that ManualResetEvent.

So, as a ‘hello world’ it’s all pretty simple and I spent more time trying to read the resistance value off my resistor than I did trying to write the code to control the input/output Smile

As an aside, I had an additional ‘cunning plan’ for this demo which didn’t work so I’ll outline that there. I planned to;

  • Register a background task to run on a raw push notification.
  • Have that background task pick up data from the push notification of the format [GPIONUMBER: High/Low] and have the background task then send a high/low value down to the GPIO pin in question.
  • Register a push notification channel at start up and use an Azure Notification Hub to deliver raw push notifications via the WNS service.

I’m not sure that this is a very ‘real world’ scenario but I thought it might be fun to send a raw notification containing [5:Low] or similar to turn my light on/off.

However…I wasn’t entirely surprised when the constructor to a PushNotificationTrigger threw an exception at me saying “the notifications platform is not supported” and so this cunning plan has had to be sidelined for the moment.

I’m not sure whether there’s a plan to make notifications like this work on Windows IoT Core but (as far as I know) to date the notifications platform has been about delivering notifications to a particular user of a particular app on a particular device and it seems to me that the “user” part of that would be missing on Windows IoT Core and so maybe it’s not going to be a possibility.