Displaying Real-time Sensor Data in the Browser with SignalR and ChartJS
Tags: arduino, aspnetcore, csharp, signalr
Published: 2019-06-09 11:14:27

---
title: Displaying Real-time Sensor Data in the Browser with SignalR and ChartJS
published: true
tags: arduino,aspnetcore,csharp,signalr
canonical_url: https://alistairevans.co.uk/2019/06/08/displaying-real-time-sensor-data-in-the-browser-with-signalr-and-chartjs/
cover_image: https://alistairevans.files.wordpress.com/2019/06/rowergraph.png?w=1000
---

In my previous posts on Modding My Rowing Machine, I [wired up an Arduino to my rowing machine](https://dev.to/alistairjevans/modding-my-rowing-machine-with-an-arduino-part-2-reading-the-speed-sensor-b58), and streamed the [speed sensor data to an ASP.NET core application.](https://dev.to/alistairjevans/streaming-real-time-sensor-data-to-an-asp-net-core-app-from-an-arduino-3ilo)

In this post, I’m going to show you how to take sensor and counter data, push it to a browser as it arrives, and display it in a real-time chart.

> If you want to skip ahead, I’ve uploaded all the code for the Arduino and ASP.NET components to a github repo at [https://github.com/alistairjevans/rower-mod](https://github.com/alistairjevans/rower-mod).
> 
> I’m using Visual Studio 2019 with the ASP.NET Core 3.0 Preview for all the server-side components, but the latest stable release of ASP.NET Core will work just fine, I’m not using any of the new features.

### Pushing Data to the Browser

So, you will probably have heard of [SignalR](https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-3.0), the ASP.NET technology that can be used to push data to the browser from the server, and generally establish a closer relationship between the two.

I’m going to use it send data to the browser whenever new sensor data arrives, and also to let the browser request that the count be reset.

The overall component layout looks like this:

![](https://alistairevans.files.wordpress.com/2019/06/image.png?w=536&h=331)

### Setting up SignalR

This bit is pretty easy; first up, head over to the Startup.cs file in your ASP.NET app project, and in the `ConfigureServices` method, add SignalR:

{%gist https://gist.github.com/alistairjevans/5c37b2fb7db6f9e0312f38a49dae6209 %}

Next, create a SignalR Hub. This is effectively the endpoint your clients will connect to, and will contain any methods a client needs to invoke on the server.

{%gist https://gist.github.com/alistairjevans/d1d6fdb451d056af714e59fe85522867 %}

SignalR Hubs are just classes that derive from the Hub class. I’ve got just the one method in mine at the moment, for resetting my counter.

Before that Hub will work, you need to register it in your Startup class’ `Configure `method:

{%gist https://gist.github.com/alistairjevans/0c82b2c599cbc615ed33aabb8549cfb8 %}

You’re also going to want to add the necessary SignalR javascript to your project. I did it using the “Manage Client-Side Libraries” feature in Visual Studio; you can find my entire libman.json file (which defines which libraries I’m using) [on my github repo](https://github.com/alistairjevans/rower-mod/blob/master/app/aspnet-rower/libman.json)

### Sending Data to the Client

In the MVC Controller where the data arrives from the Arduino, I’m going to push the sensor data to all clients connected to the hub.

The way you access the clients of a hub from outside the hub (i.e. an MVC Controller) is by resolving an `IHubContext<THubType>`, and then accessing the Clients property.

{%gist https://gist.github.com/alistairjevans/67ddd2cd4d59378bf3e4205fcdc9d574 %}

> Pro tip:   
> Got multiple IO operations to do in a single request, that don’t depend on each other? Don’t just await one, then await the other; use Task.WhenAll, and the operations will run in parallel.
> 
> In my example above I’m writing to a file and to SignalR clients at the same time, and only continuing when both are done.

### Browser

Ok, so we’ve got the set-up to push data to the browser, but no HTML just yet. I don’t actually need any MVC Controller functionality, so I’m just going to create a [Razor Page](https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-3.0&tabs=visual-studio), which still gives me a Razor template, but without having to write the controller behind it.

If I put an ‘Index.cshtml’ file under a new ‘Pages’ folder in my project, and put the following content in it, that becomes the landing page of my app:

{% gist https://gist.github.com/alistairjevans/e74c22f1bf2e370dc8a2771baccc2371 %}

In my site.js file, I’m just going to open a connection to the SignalR hub and attach a callback for data being given to me:

{% gist https://gist.github.com/alistairjevans/3a0fbcda65e799f5b182573e22dd6a49 %}

That’s actually all we need to get data flowing down to the browser, and displaying the current speed and counter values!

I want something a little more visual though….

## Displaying the Chart

I’m going to use the [ChartJS](https://www.chartjs.org/) library to render a chart, plus a handy plugin for ChartJS that helps with streaming live data and rendering it, the [chartjs-plugin-streaming](https://github.com/nagix/chartjs-plugin-streaming) plugin.

First off, add the two libraries to your project (and your HTML file), plus [MomentJS](https://momentjs.com/), which ChartJS requires to function.

Next, let’s set up our chart, by defining it’s configuration and attaching it to the 2d context of the canvas object:

{% gist https://gist.github.com/alistairjevans/dbe0d7d23575d89193b1383035b34566 %}

Finally, let’s make our chart display new sensor data as it arrives:

{% gist https://gist.github.com/alistairjevans/c7ce67ab961974ffbd75792a75c74f6b %}

With all that together, let’s see what we get!

![](https://alistairevans.files.wordpress.com/2019/06/completedgraph.gif?w=1100)

Awesome, a real-time graph of my rowing!

> As an aside, I used the excellent tool by [@sarah\_edo](https://twitter.com/sarah_edo) to generate a CSS grid really quickly, so thanks for that! You can find it at [https://cssgrid-generator.netlify.com/](https://cssgrid-generator.netlify.com/)

You can check out the entire solution, including all the code for the Arduino and the ASP.NET app, on the github repo at [https://github.com/alistairjevans/rower-mod](https://github.com/alistairjevans/rower-mod).

Next up for the rowing machine project, I want to put some form of gamification, achievements or progress tracking into the app, but not sure exactly how it will look yet.