In this tutorial we will be learning how to control a four channel relay module with the help of AllJoyn Device System Bridge(DSB) on Windows 10 IoT core. Microsoft recently released a Visual Studio project template for AllJoyn DSB applications. We will take help of this template and build a producer application which can control four relays with four different interfaces.
- AllJoyn Explorer
- Visual Studio 2015
- A Raspberry Pi running with Windows 10 IoT core
Download and install AllJoyn Device System Bridge Template from here. After installing the template create a new project. Make sure you select the AllJoyn Device System Bridge template from the Templates section.
Next, we will add a reference to the AdapterLib project to use the Windows IoT Extension SDK. This is needed in order to use the Windows.Devices.Gpio API.
Now we need to make some changes in Adapter.cs class file in AdapterLib project. First, add using Windows.Devices.Gpio; and then make the following changes:
We have declared certain attributes which will be used to expose the GPIO pins to the AllJoyn bus. Next, we will edit the Adapter() constructor.
We’ll now make some changes in the Initialize() function.
We have declared four different AdapterDevice instances for four different relays. Each of these instance represents an independent interface on the AllJoyn bus. We have also declared four AdapterProperty instances which will be used to add a Gpio pin as a property to each of the AdapterDevice instances.
Next, we make some changes in GetPropertyValue() and SetPropertyValue() function to retrieve and change GPIO pin value for each of the declared Adapter Devices respectively.
We’re done with the code part. But before deploying to your Raspberry Pi let’s connect the relays to the GPIO pins.
As mentioned in code above we are using pin number 5, 6, 12, 16, GND and 5V. Connect these pins as shown in the diagram.
Now connect the Pi to power and network(to which your PC is connected) and deploy the app onto it.
Open AllJoyn Explorer and you will find four Switch Devices that we have added in our code. Explore these devices and you’ll find the Pin Properties too.
Go ahead and change the value of PinValue(Switch 1→Pin5→interface_1) on any of these devices from 1 to 0, this will switch the particular relay on.
Find the project on Github here.
Now we have our AllJoyn producer ready. In our next tutorial we’ll see how to make universal app which can run on our Windows PC and Phones and control these switches.