In my previous posts on Modding my Rowing Machine, I got started with an Arduino, and started collecting speed sensor data. 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 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:
Then you can just include the necessary header file and connect to the network:
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:
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:
Then, in my Arduino, I put the following code in a method to send data to my application:
I just want to briefly mention one part of the above code, where I’m preparing body data to send.
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!
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!
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.
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:
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:
There’s one more thing that I don’t love about this though…
TCP Packet Fragmentation
So, what’s left to look at?
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:
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:
Once uploaded, let’s look at the new WireShark trace:
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.
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!
Next up in the ‘Modding my Rowing Machine’ series, I’ll be taking this speed data and generating a real-time graph in my browser, that updates continuously! Stay tuned…