Windows IoT Core applications fall into two categories. Headless applications, also called Background applications, and Headed applications which are applications that expose a user interface. The Windows IoT Core device as a whole can also be run in a headless or headed mode. You are still able to run any number of background applications on a device that is configured in headed mode. These background applications may also be deployed alongside a currently running headed application. It is important to note that there can only be one running UI application at a given time. Background applications can be thought of as a type of service. They can be run at startup, or be spun up on demand to perform their tasks.
Windows App Services
There may be times where it would be beneficial to have one or more applications on a device communicate with each other. Windows App Services provides a solution to this problem. App Services are instances of background applications that are spun up on demand to accomplish some type of work. In using App Services, applications also have the ability to take advantage of bi-directional communication.
In this article, we will create an IoT Background Application that will monitor the state of a door sensor. If a door is opened, an App Service will notify its Client that the perimeter has been breached. This behaves similar to a push notification. The App Service that we will create will also serve as a hardware service, the Client application can send messages to the Background Application requesting an LED in the circuit be turned on or off on demand.
Hardware
This project uses the following hardware:
- Adafruit Pi Cobbler Plus
- Magnetic Door Sensor
- Green LED
- 46 Ohm Resistor
- Your existing Windows IoT Core/Raspberry Pi Setup
Wiring Diagram
Wiring Details
- LED Anode to Pin 23
- One leg of resistor to LED Cathode
- Second leg of resistor to GND
- One wire of contact sensor to Pin 18
- Second wire of contact sensor to GND
Software
Let’s begin by creating a Background Application that exposes an App Service endpoint. Open Visual Studio 2015 and create a new Windows IoT Core Background Application. Name the project “BackgroundHeadlessApplication”, and name the solution “InterApplicationCommunication”.
Once the project is created, delete the StartupTask.cs file. Add a reference to the Windows IoT Extensions for UWP. Then, add a new class called “PerimeterBreachAppService”. Replace the code listing with the code for PerimeterBreachAppService.cs in the Code section.
In reviewing the code, this Background Task is responsible for monitoring the state of a magnetic door switch. If the door is opened, the task will use the App Service connection to notify the Client application that the perimeter has been breached, it will also notify the Client when the perimeter has been secured. This task is also responsible for turning the LED in the circuit on or off based off of messages sent through the App Service connection from the client.
Now we will need to set up the Background Application to expose an App Service. In order to do this, right click on the Package.appxmanifest document and select “View Code”. Scroll to the bottom of the document and remove the following XML element (if the StartupTask.cs still existed, this would prevent it from running at startup of the device):
<Extension Category="windows.backgroundTasks" EntryPoint="BackgroundHeadlessApplication.StartupTask">
<BackgroundTasks>
<iot:Task Type="startup" />
</BackgroundTasks>
</Extension>
In the same file, to expose the App Service, add the following element within the Extensions element of the document:
<uap:Extension Category="windows.appService" EntryPoint="BackgroundHeadlessApplication.PerimeterBreachAppService">
<uap:AppService Name="PerimeterBreachService" />
</uap:Extension>
What this does is creates an App Service identified by the name of “PerimeterBreachService”. The entry point of this service is defined with the fully qualified class name of the Background Task we just created: “BackgroundHeadlessApplication.PerimeterBreachAppService”. When this service is requested, it will spin up an instance of this class and execute its Run method.
It's time to deploy the Background Task application to the Windows IoT Core device by right-clicking the project, and selecting “Deploy”. Ensure the CPU selected is ARM and that you deploy the correct device by selecting your Windows IoT Core device as the remote machine (you can verify/change this by right-clicking the project and selecting “Properties” then choosing the Debug tab).
Let's move along to our client application. In this case we will add a new Blank Universal Application to our solution, name it “HeadedApplication”. After the project is generated, open MainPage.xaml and copy the XAML from the "UI XAML Snippet" item in the Code section and paste it into the Grid Element on the page.
The UI for this application is quite simple. It has a status textblock that will display messages to the user. There is also a couple buttons thrown in that will send commands to the App Service to control the LED light.
To add the functionality behind the UI, open MainPage.xaml.cs and replace the code with the code listing found in the Code section.
In reviewing this code, you can see that a connection is made to the PerimeterBreachService and adds a handler for messages received via the App Service. Remember that the Background Task we created earlier uses the App Service channel to send a message when the door sensor that it’s monitoring changes state (door open==perimeter breach, door closed==perimeter secure).
Now we are ready to run our HeadedApplication on our device:
Here is a video of our bi-directional communication between applications in action:
https://youtu.be/YFHoRPFSgLs
Comments