Join Michael Lehman for an in-depth discussion in this video Create a headless app, part of IoT Development with Windows 10 IoT Core 1: Installation and Basic Connection.
- [Instructor] Okay, for our last trick we're going to create a headless app, one that does not interact with the HDMI display. It could run if there was no display on the device at all. To do that, we're going to go ahead and recreate Blinky but this time as a headless app. So first we'll verify we have our background template installed, that V6 we installed earlier. Then we'll create a new app, we'll create the run loop, which is how the mechanism works so you've got things that'll operate periodically, and finally inside the code for the callbacks for that run loop, we'll toggle the D5 IO port again to make the light flash on and off.
So let's get to Visual Studio and get started. So our first step was to verify the template's there, and the easiest way to do that is to start to create our project, so shift + ctrl + N, and there we are under Windows, Windows IoT core, there's our background application. So we're going to call this thing Blinky V2 and click okay. As we did before, we'll make sure that the target and minimum version are set to the Windows 10 Anniversary 14393 build. Click okay, and here have an empty background app. IF we just take a quick look at it, you can see that basically this sets up a background task for the run method.
Now, if your run method, if you just want to do a while true and then some kind of delay, you could do that in here, but the easiest way is to use the background task deferral method, so that's what we're going to do. Alright, we'll begin by creating some variables that we can use to to talk to the background task deferral mechanism and our GPIO, so BackgroundTaskaskDeferral, deferral. Then we need a value to say whether our LED is on or off. And there we go, we can see that these things require some additional using, so we'll just hover over there, show potential fixes and pick windows.devices.gpio.
That resolves both of those. Now we'll create a constant for our pin and then we'll create a pin for our pin so we can talk to it. And finally a timer, so we can toggle our IO on and off to make the light blink. So once again we have to come over here and show potential fixes, in this case we want windows.system.threading. Alright, now in order to make this work, what you do is you ask for a deferral, initialize your GPIO, and create a timer to do your callbacks. So we say deferral, which we already defined up there, equals taskInstance.GetDeferral.
That keeps our application alive, then we'll define a method called InitGpio, and then we'll call it up here. And then finally we'll set up our timer. ThreadPoolTimer.CreatePeriodicTimer. And the value we're going to we're going to pass it in here is first of all our tick method, Timer_Tick which we'll create in a moment, and then finally we need a timespan to say how quickly we want to be called back by this timer. So Timespan.From Milliseconds, and we'll say 200 milliseconds, make it blink pretty rapidly. Okay, so now all we have to do is to implement GPIO and Timer_Tick.
And Timer_Tick looks like this, private void Timer_Tick And it gets passed a ThreadPoolTimer. Alright, in our InitGpio, we need to say that this particular pin is an output pin, and set it to be the value we want, so we'll say pin equals GpioControll1er.GetDefault().OpenPin and then we use D5 as the pin we want to open. Now that we've got access to the pin, we can set its drive mode. pin.SetDriveMode to output, GpioPinDriveMode.Output and finally we can write to it.
pin.Write (GpioPinValue.High) so we'll turn it on to start with. Alright, there we go, cause that matches our value. Now inside the Timer_Tick, what we're going to do is go look at the value of the pin and then write the opposite value of that pin to the pin so that the light goes on and off in a toggling fashion. So we'll start out by asking if our current value is high, then we will set it to Low. Otherwise we'll set it to high.
There, that makes it toggle back and forth and then finally we'll write it out to our pin. pin.Write(value). Alright, so let's take another quick look at this and then let's run it. So we created a deferral so we could keep alive. We created a value object so we could remember what state the LED is in. We created a constant to say which pin the LED is attached to. We created a pin variable in order to be able to access that pin, and finally we've created a timer so that we could get called back periodically to toggle the value. The run method is pretty standard, we ask for a deferral, we init our GPIO and then we start the timer.
InitGpio is very simple, open the pin, set it to output, and then set its current value to High. And then finally in the timer, we look and see if it's currently High, we set it to Low, otherwise we set it to High and write it back out again. So let's go ahead and build this and deploy it to our Raspberry Pi and we should see Blinky version 2 running the light. Let's make sure that we have done the setup we've done before, so this is set up for debug arm, so let's make sure we're talking to the right machine. Project, Properties, Debug, Remote Machine here.
And then Find, there's our device with the LED connected up. Close that, and we'll give this a whirl. Well here's an interesting thing happened. You see under there, Diagnostic Tools, it says "user action required, reboot the target device." Well the target device is actually working kind of nicely. The light is blinking. But let's go ahead and reboot the target device so we can make sure the full-on debugging capability is working. And we can easily do that by coming down to our IoT explorer, right-click on this and say restart.
Put in our password, tell it to remember so we don't have to do that every time, and there we go. IoT device is restarted, and once that restart is complete we'll run it again. Hopefully that will allow Visual Studio to properly synchronize its tooling with what's running on the Raspberry Pi. So you can cut all this stuff out, and I'll tell you when we begin again. Right now we're waiting for the reboot. Okay, our device is rebooted. Let's give it a whirl. Go ahead and make sure that our properties are set up, the device is actually there. Okay, click select, close this, and we'll run it again.
And that's it, you can now take a look at your Raspberry Pi and you can see that LED flashing on the breadboard. You can look at your device info on the screen and see that we're not interacting with the screen, so you've now built a headless windows 10 IoT core app.
- Reviewing Windows 10 IoT in action
- Selecting a hardware platform
- Choosing sensors and effectors
- Installing the IoT Core Dashboard and Windows 10 IoT
- Configuring Raspberry Pi hardware
- Installing Sense HAT, an add-on board for Raspberry Pi
- Creating a UX with XAML
- Prototyping a product