Connect to an electrical relay from .Net Core Windows Universal App

  • .Net Core
  • Raspberry Pi
  • Windows Universal App
  • .Net Core Push Notification
  • Blazor
  • Mehdi Mohseni
  • October 4, 2020
  • Home
  • Blog
  • Connect to an electrical relay from .Net Core Windows Universal App

Introduction

For many years, writing an application which connects to a hardware and controls its was very hard. You even needed to have tool to burn your code on hardware! .Net Core provides a very handy way to write your application and deploy it into a Raspberry Pi. The .Net Core cross-platform possibility makes you able to host your application in Raspberry Pi OS and now, you can also install Windows for Internet of Things and then deploy your Windows Universal Application into that. And yes! you will have GUI with that.

General Idea

I am new to both Windows IoT and .Net Core IoT. I started a few weeks ago and I was thinking of coming up with a funny idea to create an application. My purpose was to see how to connect some technologies. And finally, I found this, a Morse messenger. A light that connects to the electrical really and the electrical relay is controlling by and Windows Univeral App. The app is hosted on a Windows IoT in a Raspberry Pi 3. Then I created a simple user-interface based on the Blazor. The Blazor app sends a message to .Net Core WebApi and then the WebApi application sends the push notification to the Windows Univeral App on the Raspberry Pi. And finally, the Windows Universal Application translates the string the Morse code and turn on and off the light.

Raspberry Pi Gpio Pins

The first step in learning how to connect a Raspberry Pi to hardware is learning Gpio Pins. Gpio Pins are the gateway to all external hardware. I can guid you to the official document in RaspberryPi.org about the GPIOs pins.

For controlling an electrical relay you need to have 5v circuit and know how to cut and connect the circuit. For my circuit, I use the Pin 2 for the 5 power, Pin 6 for the ground, and Pin 31 as a GPIO controller pin. A GPIO Controller pin is a pin that you can control it from via your software.
In the software, you can specify what is the usage purpose of the GPIO Controller Pin. For example, in this app, we are using the GPOI 6 (Pin 31) for the simplest usage (Output). I said the simplest usage because using the GPIO for input or other purpose is a little harder.

Windows 10 for IoT

As I mentioned I am using Windows 10 for IoT to host my application in the Raspberry Pi. I can say that for my application which just provides a gateway and listens to push notification from the API, the Windows 10 for IoT is not the best solution. Because I don't need to provide a Graphic User-Interface on the Raspberry Pi device.

How to install "Windows 10 for IoT" the Raspberry Pi

For installing the "Windows 10 for IoT" on the Raspberry Pi you need to install the Windows 10 IoT Dashboard on your Windows. When you install the IoT Dashboard it is so simple to select your device type and the OS and then flush it on an SD Card.

Universal Windows Application

What's a Universal Windows Platform (UWP) app?

UWP is one of many ways to create client applications for Windows. UWP apps use WinRT APIs to provide powerful UI and advanced asynchronous features that are ideal for internet-connected devices.

Features of a UWP app

A UWP app is:

  • Secure: UWP apps declare which device resources and data they access. The user must authorize that access.
  • Able to use a common API on all devices that run Windows 10.
  • Able to use device specific capabilities and adapt the UI to different device screen sizes, resolutions, and DPI.
  • Available from the Microsoft Store on all devices (or only those that you specify) that run on Windows 10. The Microsoft Store provides multiple ways to make money on your app.
  • Able to be installed and uninstalled without risk to the machine or incurring "machine rot".
  • Engaging: use live tiles, push notifications, and user activities that interact with Windows Timeline and Cortana's Pick Up Where I Left Off, to engage users.
  • Programmable in C#, C++, Visual Basic, and Javascript. For UI, use WinUI, XAML, HTML, or DirectX.

Create a Universal Windows Platform Application on the Visual Studio

In your Visual Studio, you just need to go the File -> New -> Project and then,

If you are familiar with WPF, then you can create a User-Interface and the XAML for the Windows Universal Platform application easily. Here is my simple (Super Simple) UI for the application. It shows the new messaged on the Listbox when the Api sends the user message via push notification:

Connect to GPIO in the .Net Application C# code

Here, you can see how to connect and initialize the GPIO Pins on the .Net Core code. in the First step, you need to instantiate a Gpio Controller GpioController.GetDefault(); and then in the .Net Core it is so easy to connect to a pin and then specify what is the reason for opening the Pin. With _bluePin.SetDriveMode(GpioPinDriveMode.Output);, you can specify what is the reason for initializing the pin. Then with the Write method, you can send Low and High to the pin.

Subscribe for Push Notification

For subscribing with Push Notification Server, you need to create a HubConnectionBuilder and specifies the Url. Please take care of the WithAutomaticReconnect to handle the reconnection in case of the server is off.

After that, you need to listen to the specific method and specify an action to call when receiving the push notification. Then you just need to listen. SO EASY!!

UI and WebApi

For the rest of the solution I don't got into the details. As I mentioned in the title, I used the Blazor font-end application for the UI and .Net Core WebApi for the RESTful Api.
You can see and download the code from my GitHub Repository, dotnet-talk-morse
And, please send a message to me in the UI here Morse Messenger and turn a light on my apartment.

Sources:

Related Post