.Net Core Pi - a tasty alternative for IoT devices.

So, I’ve been working on upcycling this old Ferguson Radio I bought on eBay. It doesn’t work, in as much as nobody is broadcasting in analogue anymore, so I’m going to turn it into a digital speaker.

But I want to tune it, I want to scroll between familiar radio stations (Spotify Playlists), I want to switch between bands (Applications), I want to interact with this beautiful wooden box, I want a nostalgic experience. So let’s rip everything out, put an Arduino and a Raspberry Pi in and fake it!

The diagrams above illustrate we have a lot to do. We have a kiosk app running on an HDMI Screen, but no touch interface. We have 4 different types of inputs, 4 music applications to start, stop and control… and that after we’ve lit the thing up and connected it to the internet and the cloud.

How does one go about this, we’re running Linux so your mind’s turned to python… yeah, mine too, but I don’t like python and people who use it build ugly applications. There’s also a lot to do here, a lot to do at once… Web Server for the Kiosk, Web Socket Server to pass real-time sensor data to it, a Serial Port connection with the Arduino, a Cloud Connection and an API Client. That’s on top of the Spotify Connect and AirPlay software we need to install and consume…

If I was doing this on Windows I’d just build a .Net Application that ran each of these requirements in its own thread, that passed messages around.

So let’s do that on Linux!

One app to rule them all...

Linux has long been the dark realm of Tolkienesque creatures, frantically tapping secret commands into terminal windows. Let's see if we can leave the gates to Mordor open, or at least slightly ajar… gatekeepers, look the other way for a moment.

3 years ago, Microsoft seemed to drop the ball on IoT devices… at least they dropped the Windows IoT Core and UWP ball and started playing with Azure IoT Edge and containers. I mean, I get it, they'd admitted defeat on phones, the whole UWP dream was dead… but then they port .Net Core to ARM Processors, without an operating system or graphical interface… I have about 80% of what I need to build devices, if they’d finished the job I’d be nowhere near a terminal window right now.

However, .Net Core is still my first choice, it's fast and powerful and I've designed an application architecture that will handle pretty much anything I throw at it, embedded device wise. It will run like a bullet on a Raspberry Pi, and could easily be re-engineered into a bunch of Azure IoT Edge modules for a more 'Enterprise' application.

The two images above illustrate what I need to build and how we structure that in a .Net Core application. Kiosk represents our .Net Core Web App and the four smaller orange boxes represent services we need to run concurrently in the background. Bash sends commands to the system and monitors Linux Pipes, HTTP sends requests to API’s and deals with the responses, Cloud connects to Cloud Services such as Azure IoT Hub and Serial communicates with the Arduino. They inherit the Hosted Service class and listen to thread-safe queues located in the Message Broker.

The Web Sockets and Message Broker are used by other areas of the application so are created as Singletons and can be injected into classes. Web Socket also injects into the Pipeline as middleware to intercept Web Socket requests to the server. I wrote about Web Sockets in .Net Core for my MSc.

It's not lost on me that the work I’m doing here with Web Sockets to pass backend commands to a user interface in real-time is exactly what Blazor is for, but by doing it this way, the actual Web App could be developed in React or any other front-end technology.

So let's get started...

This project became module three of my MSc so I've gone into loads more detail, all be it 'slightly' more formally.