An example can go a long way, so rather than a 2 hour presentation with nothing but slides, I decided that the NxtGenUG Christmas special should include an end-to-end .NET Gadgeteer example. The example below includes the steps to build your own data logger to log both temperature and humidity. The coding examples were purposely kept short and do not include all the error checking that should be included, the aim was to show a wide variety of .NET Gadgeteer capabilities resulting in a final working prototype – within the 2 hour limit (including all coding and hardware building)!
[Note: I assume you have the latest SDK’s installed and that the firmware has been updated. I am using Visual Studio Ultimate, but the following will also work in the free version of VS.]
1) Lets start with a new project..
I have updated to the latest SDK/Drivers and firmware so we are currently targeting v4.2
2) There are currently a wide variety of sensors, actuators and displays available (over 130 different modules)- Lets start with a simple temperature sensor, this module uses the SHT10 temp and humidity sensor. To create a basic prototype using this we will need a mainboard ( GHI Spider) and a power module. This is achieved by dragging the modules from the toolbox on the right into the designer (main panel). By right clicking and selecting connect all modules you should end up with the following in the designer.
3) To begin with we will code the bare minimum so we can see the development cycle and get a feel for the environment. Notice that dragging modules on to the designer instantiates them, so they are ready to be used in your code. First we add some code to the MeasurementCompleted event and then put the sensor into continuous measurement mode. Each time the sensor acquires a new measurement our code will just print the readings out to the debug console. (Remember to turn this on if you are using VS Express – Debug-> Windows-> Output, See http://wp.me/p2pktY-3q). As you type VS will prompt you with intellisense and auto complete assistance, be sure to use these as it saves time.
4) Next we will deploy the solution (hit F5/ Green arrow) this will compile the code and flash your mainboard with the solution. Note we are using the USB cable to power hardware as well as debug inside VS. Once the program has been deployed it will start and you can see the debug output in VS.
5) This is all very well but the Debug output is not a great place to view data, so let us add a display. There are a few to choose from but lets start with the T35 from GHI. Dragging and dropping the display in the designer and selecting connect all modules will result in the layout below.
6)I prefer to have a separate method to configure the display but feel free to have the code inside the program started method. We will need a font, one of the two supplied with the template. There are 2 modes of operation for the display, WPF and simple graphics. Many examples include the simple graphics option so we will use the WPF as a demo, suffice to say that this is a very basic interface and WPF is very powerful, so worth looking at further. The hardware display module has a WPFWindow property, this is the root of all the graphics that we will add. First we will add some text to the main canvas.
7) Each time the sensor returns a new reading we can use it to change the text content on the canvas. Note that we do not need to refresh the text, we are using the same text object.
8) This text is a bit long, and has a few too many decimal places, so lets shorten the text.
9) The text is also rather close to the top of the screen, so we can nudge the canvas down a bit by adding a margin.
10) If all went well we are left with a user interface that updates, but is perhaps not beautiful.
11) Imagine that you are happy with this prototype and show it to friends/colleagues, who like the prototype but think the display is not very good and suggest adding a larger display. (Substituting hardware is something that is often required but can be very difficult to achieve, particularly when working with embedded systems). Lets go to the designer and deleted the T35 display and drag and drop the CP7 display into the solution. Connecting up the new display is the same as the old one and hitting build will result in an error. The error occurs because the display variable names are different (I just used the default names) – correcting this will let the solution compile and deploy.
12) With minimal effort the new display should look like the picture below. (Still not a fantastic interface) Remember that you need to build different prototype versions, experiment with hardware and test your ideas. The quicker you can develop new versions the quicker you can test you ideas and bring them into reality.
13) The next step is to do something about this interface, it is lacking a bit of readability. The first step is to make the font bigger. This is not as simple as you imagine as there are just 2 default fonts in the template and they do not come in different sizes. Basically you would not want to waste all your precious embedded device memory on fonts! So you can add just the fonts (and character sets) that you need! There is a tool to import fonts called TFConvert which is command line driven (TFConvert more info: http://msdn.microsoft.com/en-us/library/cc533019.aspx). Like most things someone has written a tool to improve it :- http://informatix.miloush.net/microframework/Utilities/TinyFontTool.aspx
Once you have a new .tinyfnt file simply import is as a resource in VS (Add resource –> File-> *.tinyfnt) and it will become available in your code.
14) Lets just swap the font for the new font, intellisense will auto complete the FontResources, to show all the fonts you now have (should be 3).
15) Simply writing text to the display is a bit lazy, remember this is a WPFWindow, we have a canvas and a whole collection of WPF components that we could add. Most things in WPF end up in a stack panel of some sort, think of it as a bag that lets you throw things into and can display them either as a row or a column. I will add a stack panel to the canvas and then add the text for humidity and temperature as two separate text objects. Feel free to explore some of the methods at this point. For example you can catch touch events on WPF components such as a text object. I have added the complete code below as a reference.
16) Text that updates is lovely but sometimes you just need to draw something! This is the only code that I will not manually type but it is short and simple enough to read through and understand. There is a basic graphing class that will draw grid lines and plot data points. Source code copied from from http://gadgeteering.net/sites/default/files/users/nvillar/files/SimpleGraph.cs . Code listed below for completeness.
17) Import the simple graphing class and create a graph object.
Select the colours, size and grid lines. Then add the graph object to the stack panel that we created earlier.
Then every time the sensor event triggers add the data to the graph.
The result should be a nice stack of text , text, graph! (Axis, units, colour, background, buttons are still required!) Hopefully that demonstrates how quite sophisticated GUIs can be generated using .NET Gadgeteer and very few lines of code.
18) Clearly we are making a sensor logger here, assuming the display now meets all you requirements, lets log the data to an SD card. First step is to add an SD module to the designer. (Drag, drop)
19) First be sure to check you have an SD card, that it is formatted etc! We will just mount the storage that is in the SD module.
20) Every time the sensor gets some data, append it to the end of a file. Be sure to consider if this is a wise move, especially if you are polling the sensor frequently. ( I will add a fs.flush() for the demo to show that data is written). There is also a storage FileWrite method that is worth considering, I did not use it here as I plan to flush the FileStream, to show that data ends up on the card without having to wait for a flush.
21) We have a data display, and logger, but nothing is complete without some sort of internet capability. Lets make our sensor available across the internet! .NET Gadgeteer easily lets you set up a web server. Lets add an Ethernet connection in the designer:
You could just as easily add a WiFi module:
Note: If you use a WiFi module here is a handy way to setup an adhoc network on your Win8 laptop/PC
Setup wireless network (from http://www.addictivetips.com/windows-tips/how-to-create-wireless-ad-hoc-internet-connection-in-windows-8/)
> netsh wlan set hostednetwork mode=allow ssid=MyNetwork key=MyNetwork
> netsh wlan start hostednetwork
(Be sure to run as admin)
Note: Here is a good webserver example http://mikedodaro.net/2011/10/15/gadgeteer-web-service-surveillance-camera/
22) Be sure to add more error checking than is shown here, to your networking capabilities.
23) Lets start a webserver when we get an IP address. First register to get notifications when the IP changes, enable DHCP and then open the network.
As an aside I noticed that it can take the WiFi module a while to start-up so I added the initialisation into a delayed timer.
24) Again it is wise to add some sanity checks here, but we will assume that our networking is perfect , that we get an IP address and simply start a webserver on port 80. The great thing about the webserver is that you can setup events on the url, here we add an event that will be triggered when the url http://<IP>/Temp is requested. (Simple eh!)
It is always polite to return a response, we can simply return a webpage (basic!)
In just 2 hours we have built an example data logger, with a graphical frontend, storage capabilities as well as making the datalogger internet capable! Aside from the SimpleGraph capabilities we have assembled all the hardware and typed all the code to make the prototype work, including importing a custom font. Hopefully this demonstrates the power of .NET Gadgeteer for rapid prototype creations! A few shortcuts were taken and it is highly recommended to add some error checking.
Note: Throughout this presentation I have purposely iterated through the development cycle to simulate the process you will encounter developing a prototype. Each change resulted in a build, compile and deploy (F5 in VS) and then a simple test to show the expected output. We will have also experienced the debug breakpoint feature which is very powerful. It is important to realise that we can halt execution on the actual hardware and inspect the objects that are in scope, this is probably one of the most valuable debug features and is a fantastic capability for embedded devices!