Netduino and Windows Phone Bitcoin tracker on Azure

TwitterFacebookRedditGoogle+EmailPinterest

My intern and I were asked to do a talk at the Durban University of Technology to the 3rd year IT students to excite them about the possibility of using Netduinos or/and Windows Phones in their final year projects.

We didn’t want to just show them a blinking LED, but rather something that would be relevant to a real-world scenario – so we came up with this:

Sorry for the horrible GIF. What you can probably tell is that it is a time-lapse of the Bitcoin price changing. The graph represents that change, and the LED glows green when it goes up, and red when it goes down. (There is a typo on the screen – USD should be BTC).

WP_20140213_10_14_46_Pro - Copy

Azure

The obvious first step is to work out where we’re going to get the data from. A quick and easy API to use is the one run by mtgox (note: with all the controversy around them recently, it would probably be wise to use something like Bitstamp in a production environment). The problem is that a lot of other data comes along with the latest BUY price which will create unnecessary data for the Netduino/phone to download and process. The solution here is to create an intermediary service between the device and the API which strips out the stuff we don’t need. With Azure, this is REALLY easy.

Create a new custom website on the Azure portal:
image

Enter whatever URL you want for it and leave it to create while you open up Visual Studio.

Create a new ASP.NET MVC 4 Web Application :image

We are going to download the latest price from the mtgox API and pull out a single value. To do that we will need to deserialize the JSON result into .NET objects. Head over to the API and copy the JSON result. Then create a new .cs code file in the Models folder called MtGoxTicker.cs. Remove the class in that file so you’re just left with the usings and namespace definition, then press Edit > Paste Special > Paste JSON as classes. A whole bunch of classes should be created representing the API structure. Rename RootObject to MtGoxTicker.

 

In the Controllers folder set up a new WebAPI controller, or use one of the ones that is already there. You can remove all the methods except Get. All this method has to do is download the latest price from the API and return a value of the single BUY price we need.

Change the return type of Get to double, then make your method look like this:

 

Basically, when something requests this Get method it will download the price from the API, deserialize it, and return it without any of the other data we don’t need.

The last thing we need to do is force our API to always return the response in JSON. Head to the last line of the Application_Start method in Global.asax.cs and add this after the lines that are already there:

 

By now the Azure site would have been created, so you can go ahead and deploy the site from VS. The easiest way is to right click on the project in the Solution Explorer and hit Publish. Then press Import which will guide you through a login process and will pull down your Azure details (this is a lot nicer in VS2013 than 2012). Publishing should take a minute or two and then you you can visit your live site!

To test out what it should look like, here is mine: http://bitcoinpusher.azurewebsites.net/api/Price
(NOTE: I can’t guarantee that the link will be online forever).

Hardware

- Netduino Plus (or Plus 2)

- Nokia 5110 LCD ($4 at DX)

- RGB LED - we used an SMD 5050 with a custom breakout board that we etched, but any RGB LED will work.

Netduino

The Netduino will download the latest price from the Azure service, display it, and draw out a graph. It will do this in an endless loop and will refresh as fast as it can (based on how fast the internet is).

To write to the Nokia 5110 LCD we use a library that a Netduino community member developed which you can read more about here. Please note that there is a flaw in the line drawing algorithm in that library so you will need to download the source (right at the bottom) to use my version which is fixed.

To simplify the network calls we use the HTTP Client from the .NET MF Toolbox.

Instead of explaining the code bit by bit, I’ve pasted the entire Program.cs code below, and have added comments in-line. But it is mostly quite straight-forward:

And here is how to wire it all up:

BitcoinPusher_bb

WP_20140128_16_08_32_Pro - Copy

I’ve left this overnight a few times and it has worked fine. Unplugging the network cable and replugging it in also gets handled fine.

Windows Phone

The Windows Phone app does almost exactly the same thing as the Netduino: It downloads the price, displays it, then draws a graph of it over time.

The UI is just a TextBlock to display the price, and a Grid which will contain the lines.

wp_ss_20140202_0001

And below is the code. Note that because the code is entirely based on the Netduino code above, I haven’t commented on this one as much. Please refer back to the Netduino code if something is confusing.

That’s everything. Let me know if you have trouble in the comments or on twitter.

You can grab the source files HERE!

TwitterFacebookRedditGoogle+EmailPinterest