[Domotica | Win IoT | .NET Core 2.1] UWP Host Controller with Z-Wave USB Interface

To make it possible to run an application on Windows IoT it needs to be an ARM application and therefore I need to create a new UWP application. While doing this I choose to support the Windows 10 Creators Update (Build 15063).

With the new project there are 2 things I need to implement:

  • Connect to the Z-Wave USB Interface
  • Connect to the SignalR host

This topic is describing the first step together with small testcase to make sure that the implementation of the Z-Wave USB Interface is done before I continue. In short: I just want to make sure it works and I want to have the easiest solution possible 🙂

Before I plug the Z-Wave USB Interface (z-stick for short) in the RPi I need to pair the Aeon Labs Smart Switch 6 (node for short). There is a manual available of how to do this in the package of the z-stick or Switch, but you can find it also online.

When the z-stick is plugged in the RPi, it will automatically install and after a couple of minutes the lights on the z-stick with shows a variety of colors which means that it is ready to use.

To communicate with the z-stick we need to send byte arrays so we need to figure out which bunch of commands we can execute and how to deliver them to the z-stick. Next to that, we also need to keep the handshake in mind as the z-stick will not fire a command to a node when the sender is actually not allowed to do so.

Instead of creating these commands myself I can also choose to use the Zwave4Net project of Rob Lans. This project supports a lot of things like; it supports around 9 different manufactures; there are more then 15 command classes implemented and people are still contributing these days to this project. Another important reason for me to use this package is that it supports Universal Windows: uap10.0! By including the nuget package in my UAP project I can focus on the stuff I really want to do; executing a command to the node via the z-stick and not bugging around with the way of communication.

The code

As at startup of the UAP application the OnNavigatedTo method on the Mainpage.xaml will be executed, the easiest way is to start there.

    private static ZWaveController _controller;

    public MainPage()

    protected override void OnNavigatedTo(NavigationEventArgs e)

        _controller = new ZWaveController(vendorId: 0x0658, productId: 0x0200);

The above code is all that is needed to setup the connection to the z-stick. Note the vendorId and productId. Normally when I plug the z-stick in my own laptop or PC I can communicate via the selected COM port. However, for Win IoT it is a bit different: opening the serialport by name fails on Windows 10 IoT, therefore I need to use the USB vendorId and productId instead.

Next thing I want to achieve is to test the commands. What better test case can I have to build a never ending loop and toggle the switch every 5 seconds.

    var set = false;

    while (true)
        set = !set;
        var currentNode = _controller.GetNodes().Result[1];
        var switchBinary = new SwitchBinary(currentNode);
        await switchBinary.Set(set);

The code above is straight forward, except for 1 method. The response of the _controller.GetNodes() method will return a list of registered nodes on the z-stick. The order of this listed is based on when each node has been paired on the z-stick. So keep up your attention when you have multiple nodes registered together with the above code sample.

Deploying using the debugger in Visual Studio

To try out my first application I can deploy the UWP app on the RPi by simply using the debugging on a remote device. First I need to make sure that I have the ‘ARM’ selected as ‘Solution Platform’. Next, I select the ‘Remote Machine’ option as the debug target.

Debug target
Debug target

I will be prompt with a popup asking which connection I like to use. If the RPi is set up correctly, the RPi should be visible in the list of ‘Auto Detect’.

Remote Connections - Auto detect.
Remote Connections – Auto detect.

Once selected this option and login with my credentials, it gives me a successful deploy. I am working via WiFi so deployment will take a bit longer but eventually the node turns on and off.

Now that I have the proof that it works with a really simple setup I can start thinking about how the user interface of my panel will look like. But before I jump into that I will explain in the next article how to setup a SignalR Hub.

Leave a Reply

Your email address will not be published. Required fields are marked *