Netduino Board

Netduino Temperature Logger with Universal Windows app

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest

In this previous post I described how to discover Netduinos on your network without knowing their IP’s. Building on that, this post describes how to make a Netduino log temperature data, and then how to build a Universal Windows 10 app that displays that data.

Graph of the temperature of ice cubes melting into water.Why

I’ve always been pretty fascinated by the Mpemba Effect, which states that hot water can freeze faster than cold water. At first glance you may think that means that hot water just decreases in temperature at a faster rate than cold water. However, as odd as it sounds, it really means that if you make ice cubes from hot and cold water, you’ll be sipping whiskey off of the hot-water ones first.

I wanted to test this myself, so I build this little Netduino temperature logger, hooked it up to my Xiaomi 10000mAh powerbank, and shoved it all in the deep-freeze. I was going to test by freezing 4 different jugs of water (each 800ml): boiling water, hot tap water, cold tap water, cold fridge water.

Long-story-short, it turns out that I suck at sciencing. I had assumed that I could just record the time it took each to get to zero degrees (real degrees, not your silly Fahrenheits). Turns out that when you put water in the freezer it will drop to ~0 degrees, and then sit there for a fair amount of time until it really freezes. That means that in pure graph-form it is impossible to tell when the water actually freezes. I did try stabbing the jug periodically with a knife to see if there was liquid water under the frozen surface – but decided that that would likely skew the results so much it wasn’t worth it.

Anyway, I only ended up doing boiling water and cold fridge water before giving up. Here is a graph comparing the boiling water and cold fridge water down to zero.
Absolute Temperature Graph
The boiling water takes 195 minutes to get from 93° to 0°. That is 0.48° per minute.
The cold water takes 66 minutes to get from 9° to 0°. That is 0.14° per minute.

Netduino Hardware

The hardware setup is pretty minimal.

  • 4.7k resistor
  • Netduino with network (I used a Netduino 3 WiFi)
  • DS18B20 waterproof temperature sensor

The DS18B20 waterproof temperature sensor is a really cheap, really useful piece of kit to have. You can get it for $10 from SparkFun, $3 from BangGood, or as low as $1 from AliExpress.(the latter two are if you’re willing to wait a couple weeks for shipping from China).

DS18B20 temperature sensor

The temperature sensor is powered through GND and 5V direct from the Netduino, and the signal wire (white or yellow usually) goes to Digital pin 0. Finally there is a 4.7k resistor between 5V and signal as a pull-up.Fritzing diagram of the electronics setup

And here is a photo of the real setup.The Netduino board wired up

Netduino Code

In this example, you’ll notice that the sensor data is never persisted to an SD card (or other storage) – that is for simplicity, plus isn’t needed for now. We keep all the values in memory, and return those to whatever client app requests them. This does mean that if you lose power, you’ll lose the current values too. But for short tests like this it is fine. In terms of running out of memory – you should be good for a week or so of data, which is way more than we need here.

The temperature sensor is a OneWire sensor, so we need a couple helper classes to read from it. Create a folder called Sensor, and add:



And OneWireBus.cs

Next we need a small class to store our sensor readings. Create SensorReading.cs.

And then the UDPNetworkDiscoveryServer from the previous post.

Now head to the main Program.cs and change it to the following.

Getting the reading from the sensor is as easy as

And then the rest of the code is setting up a TCP server on port 80 to listen for requests from the app. When a request comes in it loops over the data and returns it.

Notice that we’re able to have the UDP network discovery server plus the TCP server running at the same time, both on port 80. You can also change the port to something else if you’d prefer.

Universal Windows 10 App

To keep the Windows app nice and clean, we’ll use the MVVM pattern – which you should be doing in pretty much every app you make. Specifically, I like using MVVMLight.
For the graph, we’ll use the one in the WinRT XAML Toolkit.

So go ahead and install MVVMLight and WinRTXAMLToolkit.Controls.DataVisualization from Nuget.

In MainPage.XAML, we’ve got a pretty basic UI, bound to the main VM.

Before we add code to download the data from the device, we need a class to store it in. Create SensorReading.cs and add the following code (in my case I put it in a folder called Model).

The MainViewModel.cs is more complicated, but I’ve put lots of comments to explain what is going on. Essentially, the user hits the Refresh button in the UI, which then goes and discovers devices and picks the first one, then it downloads the data from the first device and converts it from text into proper types, then sets that into an ObservableCollection which the XAML chart is bound to.

The exporting code just takes the data and converts it to a comma separated value string and saves to a file that the user selects.


Battery Life

I was pleasantly surprised at the battery usage of this. Without doing anything special to save battery, it lasted about 3 days before the 10,000 mAh battery got down to 50% (I didn’t test longer than that).

Freezing Stuff / Disclaimer

When I was doing my tests I put the Netduino and powerbank together in a zip-lock bag with a small gap for the temperature sensor cable to come out. This probably isn’t a good idea. Although technically most of these components are able to handle below zero temperatures, they’ll get tons of moisture on them when they start heating up and condensation forms. I also think it is probably pretty bad for the batteries.


Let me know in the comments or Twitter if you’re having any problems.

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest