Streaming real-time sensor data to an ASP.NET Core app from an Arduino
Tags: arduino, csharp, networks, performance
Published: 2019-05-27 12:47:10

---
title: Streaming real-time sensor data to an ASP.NET Core app from an Arduino
published: true
tags: arduino, csharp, networks, performance
canonical_url: https://alistairevans.co.uk/2019/05/27/streaming-real-time-sensor-data-to-an-asp-net-core-app-from-an-arduino/
---

In my previous posts on Modding my Rowing Machine, [I got started with an Arduino](https://dev.to/alistairjevans/modding-my-rowing-machine-with-an-arduino-part-1-arduino-basics-21c5), and started [collecting speed sensor data](https://dev.to/alistairjevans/modding-my-rowing-machine-with-an-arduino-part-2-reading-the-speed-sensor-b58). The goal of this post is to connect to the WiFi network and upload sensor data to a server application I’ve got running on my laptop in as close to real-time as I can make it.

## Connecting to WiFi

My [Arduino Uno WiFi Rev 2](https://store.arduino.cc/arduino-uno-wifi-rev2) board has got a built-in WiFi module; it was considerably easier than I expected to get everything connected.

I first needed to install the necessary library to support the board, the WiFiNINA library:

[![](https://alistairevans.files.wordpress.com/2019/05/image-13.png?w=585&h=240)](https://alistairevans.files.wordpress.com/2019/05/image-13.png)

Then you can just include the necessary header file and connect to the network:

{% gist https://gist.github.com/alistairjevans/78e609666868874c42bc612f9fb74554 %}

To be honest, that code probably isn’t going to cut it, because WiFi networks don’t work that nicely. You need a retry mechanism with timeouts to keep trying to connect. Let’s take a look at the full example:

{% gist https://gist.github.com/alistairjevans/5968b5ae79e2d91912c1c192efb1d2d4 %}

## The Server

To receive the data from the Arduino, I created a light-weight ASP.NET Core 3.0 web application with a single controller endpoint to handle incoming data, taking a timestamp and the speed:

{% gist https://gist.github.com/alistairjevans/7f0d74b80e7a20b115af3330bc3834bf %}

Then, in my Arduino, I put the following code in a method to send data to my application:

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

I just want to briefly mention one part of the above code, where I’m preparing body data to send.

{% gist https://gist.github.com/alistairjevans/552db05d558d07fdf31e2d0c47b6f914 %}

The Arduino libraries do not support the `%f` specifier (for a float) in the `sprintf` method, so I can’t just add the speed as an argument there. Instead, you have to use the `dtostrf` method to insert a double into the string, specifying the number of decimal points you want.

Also, if you specify `%d` (int) instead of `%lu` (unsigned long) for the timestamp, the sprintf method treats the value as a signed int and you get very strange numbers being sent through for the timestamp.

Once that was uploaded, I started getting requests through!

[![](https://alistairevans.files.wordpress.com/2019/05/image-14.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-14.png)

## Performance

We now have HTTP requests from the Arduino to our ASP.NET Core app. But I’m not thrilled with the amount of time it takes to execute a single request.

If we take a look at the WireShark trace (I love WireShark), you can see that each request from start to finish is taking in the order of 100ms!

[![](https://alistairevans.files.wordpress.com/2019/05/image-15.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-15.png)

This is loads, and I can’t have my Arduino sitting there for that long.

#### ASP.NET Core Performance

You can see in the above trace that the web app handling the request is taking 20ms to return the response, which is a lot. I know that ASP.NET Core can do better than that.

Turns out this problem was actually due to the fact I had console logging switched on. Due to the synchronisation that takes place when writing to the console, it can add a lot of time to requests to print all that information-level data.

Once I turned the logging down from Information to Warning in my appsettings.json file, it got way better.

[![](https://alistairevans.files.wordpress.com/2019/05/image-16.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-16.png)<figcaption>That’s better!</figcaption>

That actually gives us sub-millisecond response times from the server, which is awesome.

#### TCP Handshake Overhead

Annoyingly, each request is still taking up to 100ms from start of connection to the end. How come?

If you look at those WireShark traces, we spend a lot of time in the TCP handshaking process. Opening a TCP connection does generally come with lots of network overhead, and that call to `client.connect(SERVER, SERVERPORT)` in my code blocks until the TCP connection is open; I don’t want to sit there waiting for that every time I want to send a sample.

The simple solution to this is to make the connection stay open between samples, so we can just repeatedly sent data on the same connection, only needing to do the handshake once.

Let’s rework our previous `sendData` code on the Arduino to keep the connection open:

{% gist https://gist.github.com/alistairjevans/038fece8f6cd00c31fe94fb1c0330b19 %}

In this version, we ask the server to leave the connection open after the request, and only open the connection if it is closed. I’m also not blocking waiting for a response.

This gives us way better behaviour, and we’re now down to about 40ms total:

[![](https://alistairevans.files.wordpress.com/2019/05/image-17.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-17.png)

There’s one more thing that I don’t love about this though…

#### TCP Packet Fragmentation

So, what’s left to look at?

[![](https://alistairevans.files.wordpress.com/2019/05/image-18.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-18.png)<figcaption>TCP segments</figcaption>

I’ve got a packet preceding each of my POST requests, that seems to hold things up by around 40ms. What’s going on here? Let’s look at the content of that packet:

[![](https://alistairevans.files.wordpress.com/2019/05/image-19.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-19.png)<figcaption>Wireshark data view</figcaption>

What I can tell from this is that rather than wait for my HTTP request data, the Arduino is not buffering for long enough, and is just sending what it has after the first `println` call containing `POST /data/providereading HTTP/1.1`. This packet fragmentation slows everything up because the Arduino has to wait for an ACK from the server before it continues.

> I just wanted to point out that it looks like the software in the Arduino libraries isn’t responsible for the fragmentation; it looks all the TCP behaviour is handled by the hardware WiFi module, that’s what is splitting my packets.

To stop this packet fragmentation, let’s adjust the sending code to prepare the entire request and send it all at once:

{% gist https://gist.github.com/alistairjevans/61b19f11eb78e88c2b06b1d5cc2d3161 %}

Once uploaded, let’s look at the new WireShark trace:

[![](https://alistairevans.files.wordpress.com/2019/05/image-21.png?w=1100)](https://alistairevans.files.wordpress.com/2019/05/image-21.png)<figcaption>No TCP Fragmentation</figcaption>

There we go! Sub-millisecond responses from the server, and precisely hitting my desired 50ms window between each sample send.

There’s still ACKs going on obviously, but they aren’t blocking packet issuing, which is the important thing.

## Summary

It’s always good to look at the WireShark trace for your requests to see if you’re getting the performance you want, and don’t dismiss the overhead of opening a new TCP connection each time!