Fun times with SignalR Part 2: Windows Store and Lego Mindstorms
- Chris Wallace
- Thu Jan 07 2016
The next part of my christmas project, pushing my SignalR endpoint to more client platforms. I've proved that SignalR works between JavaScript and Console Applications, what's next?
So you may recall my previous post (if not you can check out the related links). That over the christmas period I was experimenting with SignalR hubs to tracking activity on my site. Nothing spectacular just a series of JavaScript and console application clients (characteristically called Randalls) which dispatch messages and events when someone clicks a link (not all that useful or scalable in business but for something small like this site it's quite neat).
The beauty of SignalR is the platform support, most of the libraries are portable and can be used across Console Applications, WPF, Windows Forms and Windows Store Apps (which opens up a lot of opportunities thanks to Windows 10). So the next part of my project was to port part of the code over to a Windows Store App.
The app itself is fairly simple (and not that different from a WPF solution). There's a simple Xaml listview with some connect buttons and text fields. This allows me to connect to a specific 'context' (i.e. a chatroom or a 'channel'), give myself a name and join as a 'Finster' user (remember Finsters can send/receive all messages/events to/from all clients aka 'Randalls').
The underlying SignalR code isn't that different from a console application, the handling and sending is just refactored into more ideal places (and has to deal with async because of the UI layer).
So I have a (slightly) nicer looking UI to view events. So what? Well, the next part is the real reason I wanted to do this!
A couple of years ago, Santa sent me a lego Mindstorms EV3 robotics kit for Christmas (if you see where I'm going with this, you might as well scroll to the end!) Unlike previous versions, the latest version of the lego Mindstorms brick has Wifi support and (more importantly) class libraries for use with .NET!
The libraries for the EV3 were a few years old now and intended for the original WinRT, but still work perfectly fine with 'file -> new' Windows 10 apps. The libraries themselves allow you to talk to your creation either using the built-in Bluetooth connection or over a LAN Wifi connection (if you plug in a supported USB WiFi dongle).
Again using little extra code I was able to establish a connection with my Lego Robot (sadly on the shelf gathering dust) and get it moving around the floor. After a few attempts at controlling the motors (and working out how to stop them!) I had a little dancing robot!
The final part was to hook up the integration between the SignalR messages/events and the EV3 brick. Here are the results!
So what's happening?
Nothing that different from my console application really:
- A user visits a page on the site (either entering the address directly or clicking a link)
- JavaScript within my React/Flux application sends a message and event to my SignalR hub in Azure
- The SignalR hub relays the message/event to the clients (either other JavaScript clients, console applications or my new Windows Store App).
- The Windows Store App logs the message/event to a listview
- The Windows Store App tells the connected EV3 brick to run the leg motors on the robot for 6 seconds at 100%/-100% power (making it spin).
And that's it! Again just a fun example of integrating different devices altogether. There's still more devices I want to explore with this SignalR project but it depends on how many other SignalR clients libraries I can find out in the wild!
More information on the Lego Mindstorms EV3 API can be found here and the libraries can be found here (if you want to have some fun with .NET then I really suggest buying the EV3 kit!)