<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Laurent Ellerbach</title><link>http://blogs.msdn.com/b/laurelle/</link><description>
Ce blog est principalement destiné à publier des informations relatives à Microsoft, à ses technologies, aux outils Visual Studio et à ses versions Express notamment Visual Basic</description><dc:language>fr</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>Adding Bluetooth support to a NETMF board (.NET Microframework)</title><link>http://blogs.msdn.com/b/laurelle/archive/2013/04/29/adding-bluetooth-support-to-a-netmf-board-net-microframework.aspx</link><pubDate>Mon, 29 Apr 2013 09:59:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10414706</guid><dc:creator>Laurelle</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10414706</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2013/04/29/adding-bluetooth-support-to-a-netmf-board-net-microframework.aspx#comments</comments><description>&lt;p&gt;I recently bought a very cheap Bluetooth adaptor for my Netduino. I wanted to test how easy/hard it is to support Bluetooth. I see lots of advantages with Bluetooth for a near field communication like piloting easily a robot with a Phone without the need of other network or Infrared. Also Bluetooth is a secured communication with a peering.&lt;/p&gt;  &lt;p&gt;So I bought &lt;a href="http://dx.com/p/jy-mcu-arduino-bluetooth-wireless-serial-port-module-104299" target="_blank"&gt;this cheap Bluetooth&lt;/a&gt; adaptor for $8.20. It does expose itself to the world with a serial port on one side and as a normal Bluetooth device on the other side. Communication is supported with a serial port from one side to the other. On a PC, Phone or whatever device, it creates a serial port. So communication is basically very transparent and assimilated to a serial port from end to end.&lt;/p&gt;  &lt;p&gt;&lt;img alt="JY-MCU Arduino Bluetooth Wireless Serial Port Module" src="http://img.dxcdn.com/productimages/sku_104299_1.jpg" width="256" height="256" /&gt;&lt;img alt="JY-MCU Arduino Bluetooth Wireless Serial Port Module" src="http://img.dxcdn.com/productimages/sku_104299_3.jpg" width="245" height="245" /&gt;&lt;/p&gt;  &lt;p&gt;When I received it, I was impatient to test it. First step was to peer it with a PC. I looked at the documentation and found the default name for this device was “linvor” and found out the passkey was 1234. After cabling it with 3.3V (my board support 3.3V to 12V alimentation) and the ground, and approximately 1 minutes, I peered it!&lt;/p&gt;  &lt;p&gt;New step was to write a bit of code to test all this. I decided to do a very basic echo program. So whatever it will receive, it will send it back to the calling program. On the netduino board, I’ll use the COM1 (pins D0 and D1). I found also in less than 1 minute that the default configuration was 9600 bauds, 8 bits, no parity and 1 bit stop. So I wrote this very simple code for the test, very hard to do more basic than that:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Net;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Net.Sockets;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Threading;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.SPOT;
&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.SPOT.Hardware;
&lt;span class="kwrd"&gt;using&lt;/span&gt; SecretLabs.NETMF.Hardware;
&lt;span class="kwrd"&gt;using&lt;/span&gt; SecretLabs.NETMF.Hardware.Netduino;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text; 
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.IO.Ports;   

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; Bluetooth
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Program
    {
        &lt;span class="kwrd"&gt;static&lt;/span&gt; SerialPort serial;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Main()
        {
            &lt;span class="rem"&gt;// initialize the serial port for COM1 (pins D0 and D1)             &lt;/span&gt;
            serial = &lt;span class="kwrd"&gt;new&lt;/span&gt; SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);             
            &lt;span class="rem"&gt;// open the serial-port, so we can send and receive data             &lt;/span&gt;
            serial.Open();             
            &lt;span class="rem"&gt;// add an event-handler for handling incoming data             &lt;/span&gt;
            serial.DataReceived += &lt;span class="kwrd"&gt;new&lt;/span&gt; SerialDataReceivedEventHandler(serial_DataReceived);       
            &lt;span class="rem"&gt;//wait until the end of the Universe :-)&lt;/span&gt;
            
            Thread.Sleep(Timeout.Infinite);         
        }            
        &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; serial_DataReceived(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, SerialDataReceivedEventArgs e)         
        {             
            &lt;span class="rem"&gt;// create a single byte array             &lt;/span&gt;
            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] bytes = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[1];                
            &lt;span class="rem"&gt;// as long as there is data waiting to be read             &lt;/span&gt;
            &lt;span class="kwrd"&gt;while&lt;/span&gt; (serial.BytesToRead &amp;gt; 0)             
            {                 
                &lt;span class="rem"&gt;// read a single byte                 &lt;/span&gt;
                serial.Read(bytes, 0, bytes.Length);                 
                &lt;span class="rem"&gt;// send the same byte back                 &lt;/span&gt;
                serial.Write(bytes, 0, bytes.Length);             
            }         
        }       
        
    }
}
&lt;/pre&gt;


&lt;p&gt;I launch a simple serial port program like the old Hyper terminal on the PC where I peered the Bluetooth device and ran the test. Good surprise, I selected the port created on my PC (was port 6), open the port. The Bluetooth device went from a red blinking led to a always on led showing the device was correctly peered. Sounds good so far! So I typed “bonjour” and send it. instantly I get the “bonjour” back. &lt;/p&gt;

&lt;p&gt;So cool it’s working! I wanted to know more about the cheap and what can be setup, changes like the name of the device, the pin, the baud rate, etc. I used my preferred search engine Bing and quickly found out that it’s possible to change lots of things by sending couple of AT command. Those commands were used at the old age of modems &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7532.wlEmoticon_2D00_smile_5F00_780F0A3C.png" /&gt; It just remembered me that!&lt;/p&gt;

&lt;p&gt;Even if there are lots of cheap existing like the one I bought, most support exactly the same commands. I found a good documentation &lt;a href="http://www.cutedigi.com/pub/Bluetooth/BMX_Bluetooth_quanxin.pdf" target="_blank"&gt;there&lt;/a&gt;. It’s not the same cheap and the AT commands are a bit different but I quickly found out that most were working. So I’ve decided to test if it was working. All what you have to do is send the commands when the device is not peered. You can do it either with a USB to serial FTDI cheap or directly from the board. I did it directly from the Netduino by modifying&amp;#160; a bit the code to send the commands. I found the most interesting commands were the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;AT+NAMEnewname\r\n to change the device name, you get an answer&lt;/li&gt;

  &lt;li&gt;AT+PINxxxx\r\n to change the pin code, default is 1234&lt;/li&gt;

  &lt;li&gt;AT+BAUDx\r\n where X goes from 1 to 8 (1 = 1200 to 8 = 115200) to change the baud rate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;I send couple of commands to test and it worked just perfectly &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7532.wlEmoticon_2D00_smile_5F00_780F0A3C.png" /&gt; So I renamed the device to LaurelleBT instead of linvor. As the device was already peered, Windows did not had to reinstall drivers or cut the communication, it was just about changing the displayed name:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1072.image_5F00_1E70ED88.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/4212.image_5F00_thumb_5F00_37008AD8.png" width="438" height="64" /&gt;&amp;#160;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So that’s it! In 5 minutes I had a working Bluetooth module on my board. I was positively surprised and I’ll buy more for sure! Next step it to mount it on a robot and pilot it from a Windows Phone or Windows 8 device.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10414706" width="1" height="1"&gt;</description></item><item><title>.NET Microframework (NETMF) Web Server source code available</title><link>http://blogs.msdn.com/b/laurelle/archive/2013/04/07/net-microframework-netmf-web-server-source-code-available.aspx</link><pubDate>Sun, 07 Apr 2013 14:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10409149</guid><dc:creator>Laurelle</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10409149</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2013/04/07/net-microframework-netmf-web-server-source-code-available.aspx#comments</comments><description>&lt;p&gt;So as I got lots of asks to get the code of my Web Server, I’ve decided to create a Codeplex project. You’ll find the source here: &lt;a title="https://netmfwebserver.codeplex.com/" href="https://netmfwebserver.codeplex.com/"&gt;https://netmfwebserver.codeplex.com/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It does include the following features:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Start, stop, Pause the Web Server&lt;/li&gt;    &lt;li&gt;Creation of a Web Server on any port&lt;/li&gt;    &lt;li&gt;Fully functional multithread Web Server supporting GET only&lt;/li&gt;    &lt;li&gt;Downloading any file present on a SD (or any other physical storage)&lt;/li&gt;    &lt;li&gt;A full function to get all the parameters of a URL&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Examples of usage can be found on this blog. Working to publish a fully functional example.&lt;/p&gt;  &lt;p&gt;Feedback welcome!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10409149" width="1" height="1"&gt;</description></item><item><title>Web Server and CSS files in NETMF (.NET Microframework)</title><link>http://blogs.msdn.com/b/laurelle/archive/2013/04/05/web-server-and-css-files-in-netmf-net-microframework.aspx</link><pubDate>Fri, 05 Apr 2013 08:16:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10407866</guid><dc:creator>Laurelle</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10407866</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2013/04/05/web-server-and-css-files-in-netmf-net-microframework.aspx#comments</comments><description>&lt;p&gt;It’s been a long time I did not write anything on my blog. Not that I haven’t developed anything but just because I did not take the time to write proper articles. I’ve continue to add features to my Lego city by piloting the trains but also the switches. And I’ll try to write articles to explain how to do that for all the features.&lt;/p&gt;  &lt;p&gt;But I will start with the modification of my Web Server to support CSS file. I did couple of demonstration of my development and each time I show the interface people were telling to me I need to work with a designer. And that’s what I finally did &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/0447.wlEmoticon_2D00_smile_5F00_6AA2141C.png" /&gt; I worked with &lt;a href="http://blogs.msdn.com/b/designmichel/" target="_blank"&gt;Michel Rousseau&lt;/a&gt; who is designer at Microsoft in the French team. And I gave him a challenge: “Design this simple web page without changing the code too much and keep it less than couple of K without any image”. Michel is used to design Windows 8 and Windows Phone apps but not very very simple page like the one I had.&lt;/p&gt;  &lt;p&gt;And he has done an excellent job! Here is the view before and after:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6215.image_5F00_1103F768.png"&gt;&lt;img title="image" style="margin: 0px; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/3568.image_5F00_thumb_5F00_7C81389A.png" width="179" height="244" /&gt;&lt;/a&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/0116.image_5F00_050497E5.png"&gt;&lt;img title="image" style="margin: 0px; display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6470.image_5F00_thumb_5F00_7BAB6361.png" width="180" height="244" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Now I had to implement this in my code. As the brief was to have minimal effect on the code, I was expecting to implement this quickly. Reality was a bit different. It took me a bit more time than expected for the following reasons:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;I had to implement in &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/05/29/creating-an-efficient-http-web-server-for-net-microframework-netmf.aspx" target="_blank"&gt;my basic web server&lt;/a&gt; a function to be able to download a file (the CSS one)&lt;/li&gt;    &lt;li&gt;To read and download a file from an SD, you have to do it by chunk as the buffer size is limited (in the case of my &lt;a href="http://www.netduino.com/" target="_blank"&gt;Netduino&lt;/a&gt; 1024 bit)&lt;/li&gt;    &lt;li&gt;Modify the main code to care about downloaded file and also add the lines of code to support CSS&lt;/li&gt;    &lt;li&gt;But the main issue was that I’ve discovered that to be able to have a CSS file, you need to have the specific type “text/css”. This is to avoid cross domain fishing and other hacking&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So let see how to implement this step by step. So let start with the reading part of the file and how to send it. As explained in the last point, a CSS file has to have the correct mime type in the header. In fact, Internet Explorer and most of the other browsers such as Chrome and Firefox does not need the mime type to determine what kind of fire you are downloading. They do it with the mime type and/or with the extension. Most of the time, it’s just with the extension and reading the header of the file. But for security reason, it’s better if you have to determine correctly the type matching with the extension and the header of the file. And for CSS, it is forced like this to reinforce the security in Internet Explorer 8, 9 and 10. &lt;/p&gt;  &lt;p&gt;So as I had to implement this feature for CSS, I made a simple function to support some types I’ll use in other creation:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendFileOverHTTP(Socket response, &lt;span class="kwrd"&gt;string&lt;/span&gt; strFilePath)
        {
            &lt;span class="kwrd"&gt;string&lt;/span&gt; ContentType = &lt;span class="str"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;;
            &lt;span class="rem"&gt;//determine the type of file for the http header&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.cs&amp;quot;&lt;/span&gt;) != -1 ||
                strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.txt&amp;quot;&lt;/span&gt;) != -1 ||
                strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.csproj&amp;quot;&lt;/span&gt;) != -1
            )
            {
                ContentType = &lt;span class="str"&gt;&amp;quot;text/plain&amp;quot;&lt;/span&gt;;
            }

            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.jpg&amp;quot;&lt;/span&gt;) != -1 ||
                strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.bmp&amp;quot;&lt;/span&gt;) != -1 ||
                strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.jpeg&amp;quot;&lt;/span&gt;) != -1
              )
            {
                ContentType = &lt;span class="str"&gt;&amp;quot;image&amp;quot;&lt;/span&gt;;
            }

            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.htm&amp;quot;&lt;/span&gt;) != -1 ||
                strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.html&amp;quot;&lt;/span&gt;) != -1
              )
            {
                ContentType = &lt;span class="str"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;;
            }

            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.mp3&amp;quot;&lt;/span&gt;) != -1)
            {
                ContentType = &lt;span class="str"&gt;&amp;quot;audio/mpeg&amp;quot;&lt;/span&gt;;
            }
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.IndexOf(&lt;span class="str"&gt;&amp;quot;.css&amp;quot;&lt;/span&gt;) != -1)
            {
                ContentType = &lt;span class="str"&gt;&amp;quot;text/css&amp;quot;&lt;/span&gt;;
            }

            &lt;span class="kwrd"&gt;string&lt;/span&gt; strResp = &lt;span class="str"&gt;&amp;quot;HTTP/1.1 200 OK\r\nContent-Type: &amp;quot;&lt;/span&gt;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="str"&gt;&lt;/span&gt; + ContentType + &lt;span class="str"&gt;&amp;quot;; charset=UTF-8\r\nCache-Control: &lt;/span&gt;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="str"&gt;no-cache\r\nConnection: close\r\n\r\n&amp;quot;&lt;/span&gt;;
            OutPutStream(response, strResp);&lt;/pre&gt;


&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So very simple and straight forward code. I do determine the extension of the file I want to read and create a ContentType variable with the right format. Then I build the HTTP header and send the header. Very simple, efficient and straight forward code. We are doing Embedded code, it’s not the code I would do in a normal development of a real web server. But it does the job there!&lt;/p&gt;

&lt;p&gt;Next step is reading the file and outputting it to the Socket. And I do this in the same function, right after this first part:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            FileStream fileToServe = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
            &lt;span class="kwrd"&gt;try&lt;/span&gt;
            {
                fileToServe = &lt;span class="kwrd"&gt;new&lt;/span&gt; FileStream(strFilePath, &lt;/pre&gt;

&lt;pre class="csharpcode"&gt;FileMode.Open, FileAccess.Read);
                &lt;span class="kwrd"&gt;long&lt;/span&gt; fileLength = fileToServe.Length;
                &lt;span class="rem"&gt;// Now loops sending all the data.&lt;/span&gt;

                &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] buf = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[MAX_BUFF];
                &lt;span class="kwrd"&gt;for&lt;/span&gt; (&lt;span class="kwrd"&gt;long&lt;/span&gt; bytesSent = 0; bytesSent &amp;lt; fileLength; )
                {
                    &lt;span class="rem"&gt;// Determines amount of data left.&lt;/span&gt;
                    &lt;span class="kwrd"&gt;long&lt;/span&gt; bytesToRead = fileLength - bytesSent;
                    bytesToRead = bytesToRead &amp;lt; MAX_BUFF ? bytesToRead : MAX_BUFF;
                    &lt;span class="rem"&gt;// Reads the data.&lt;/span&gt;
                    fileToServe.Read(buf, 0, (&lt;span class="kwrd"&gt;int&lt;/span&gt;)bytesToRead);
                    &lt;span class="rem"&gt;// Writes data to browser&lt;/span&gt;
                    response.Send(buf, 0, (&lt;span class="kwrd"&gt;int&lt;/span&gt;)bytesToRead, SocketFlags.None);

                    System.Threading.Thread.Sleep(100);
                    &lt;span class="rem"&gt;// Updates bytes read.&lt;/span&gt;
                    bytesSent += bytesToRead;
                }
                fileToServe.Close();
            }
            &lt;span class="kwrd"&gt;catch&lt;/span&gt; (Exception e)
            {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (fileToServe != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
                {
                    fileToServe.Close();
                }
                &lt;span class="kwrd"&gt;throw&lt;/span&gt; e;
            }

        }&lt;/pre&gt;


&lt;p&gt;First step is to create a FileStream and create the Stream with the path of the file and read the length of the file. MAX_BUFF = 1024 and is the maximum size of a buffer. It depends on the .NET Microframework Platform. And we will start a loop to read part of the file and send it.&lt;/p&gt;

&lt;p&gt;The System.Threading.Thread.Sleep(100) is necessary to allow some time for the system and other tasks. If you don’t put it and have other tasks, the risk is that the memory will get full very quickly and you’ll block all the code.&lt;/p&gt;

&lt;p&gt;In my Web Server I have an event raised when an HTTP request is done. Here is an example of code you can place in your handling function to manage on one side the files to be downloaded from the SD and on the other side a page you’ll generate dynamically like in ASP, ASP.NET, PHP or any other dynamic language:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;            &lt;span class="rem"&gt;//PageCSS&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.Length &amp;gt;= pageCSS.Length)
            {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.Substring(0, pageCSS.Length).ToLower() == pageCSS)
                {
                    &lt;span class="kwrd"&gt;string&lt;/span&gt; strDefaultDir = &lt;span class="str"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (Microsoft.SPOT.Hardware.SystemInfo.IsEmulator)
                        strDefaultDir = &lt;span class="str"&gt;&amp;quot;WINFS&amp;quot;&lt;/span&gt;;
                    &lt;span class="kwrd"&gt;else&lt;/span&gt;
                        strDefaultDir = &lt;span class="str"&gt;&amp;quot;SD&amp;quot;&lt;/span&gt;;
                    WebServer.SendFileOverHTTP(response, strDefaultDir + &lt;span class="str"&gt;&amp;quot;\\&amp;quot; + pageCSS);
                    return;
                }
            }
            //HTTP header
            strResp = &amp;quot;&lt;/span&gt;HTTP/1.1 200 OK\r\nContent-Type: text/html; &lt;/pre&gt;

&lt;pre class="csharpcode"&gt;charset=utf-8\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n&amp;quot;;
            strResp = WebServer.OutPutStream(response, strResp);

            &lt;span class="rem"&gt;// Page util&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.Length &amp;gt;= pageUtil.Length)
            {
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (strFilePath.Substring(0, pageUtil.Length).ToLower() == pageUtil)
                {
                    ProcessUtil(response, strFilePath);
                    &lt;span class="kwrd"&gt;return&lt;/span&gt;;
                }
            }&lt;/pre&gt;


&lt;p&gt;Here PageCSS = the file name of the file you are looking for (including the path if in sub directory) so something like “page.css” and pageUtil = name of a page you will generate dynamically (including the path subdirectory) so something like “util.aspx”&lt;/p&gt;

&lt;p&gt;strFilePath = full URL including the parameters so something like “util.apsx?bd=3;tc=4”&lt;/p&gt;

&lt;p&gt;so code looks for the name of the page in the URL and brunch it to a either the SendFileOverHTTP function we’ve just look at or another function in the case of a dynamic page.&lt;/p&gt;

&lt;p&gt;You’ll note also that the is a case in the file part as the default directory is not the same if you are in the emulator or on a real board. The path depend also from the Platform. In the case of my Netduino, it’s CD. For emulator, it’s always WINFS.&lt;/p&gt;

&lt;p&gt;And please do all your file reading brunching before the HTTP header part. As for files, the header is already send out. Which is not the case for the dynamic generated page. As you can send whatever you want including images, text, binary files and you’ll need to set it up correctly.&lt;/p&gt;

&lt;p&gt;Now, lets have a look at the CSS file:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;@charset &amp;quot;utf-8&amp;quot;;
/* CSS Document */

body {
    font-family: &amp;quot;Lucida Console&amp;quot;, Monaco, monospace;
    font-size: 16px;
    color: #09C;
    text-align: center;
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}

a {
    font-family: &amp;quot;Lucida Console&amp;quot;, Monaco, monospace;
    font-size: 14px;
    color: #09F;
}
td {
    font-family: Arial, Helvetica, sans-serif;
    color: #004262;
    font-size: 14px;
}
input {
    font-family: &amp;quot;Lucida Console&amp;quot;, Monaco, monospace;
    font-size: 16px;
    color: #09F;
    background-color: #FFF;
    -webkit-transition: all 0s linear 0s;
    -moz-transition: all 0s linear 0s;
    -ms-transition: all 0s linear 0s;
    -o-transition: all 0s linear 0s;
    transition: all 0s linear 0s;
    border: 1px none #FFF;
}

h1 {
    font-family: Arial, Helvetica, sans-serif;
    color: #FFF;
    background-color: #006699;
    font-size: 24px;
    border: thick solid #006699;
}
td {
    font-family: Arial, Helvetica, sans-serif;
    text-align: left;
    font-size: 16px;
}
footer {
    color: #09C;
}
input:hover {
    background-color: #09F;
    color: #FFF;
}
input:active {
    background-color: #003;
}
&lt;/pre&gt;

&lt;p&gt;As you can see, it is very simple CSS that Michel did. It is just over righting the normal styles with colors and fonts. Nothing really complex but complicated to do something nice in only few lines of code! Well done Michel &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/0447.wlEmoticon_2D00_smile_5F00_6AA2141C.png" /&gt;&lt;/p&gt;

&lt;p&gt;Now implementation in the rest of the code and all the pages is quite straight forward and simple, here is an example:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="rem"&gt;// Start HTML document&lt;/span&gt;
strResp += &lt;span class="str"&gt;&amp;quot;&amp;lt;!DOCTYPE html PUBLIC \&amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN\&amp;quot; &lt;/span&gt;&lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="str"&gt;\&amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span class="str"&gt;&amp;quot;&amp;lt;html xmlns=\&amp;quot;http://www.w3.org/1999/xhtml\&amp;quot;&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Gestion des trains&amp;lt;/title&amp;gt;&amp;quot;&lt;/span&gt;;
&lt;span class="rem"&gt;//this is the css to make it nice :-)&lt;/span&gt;
strResp += &lt;span class="str"&gt;&amp;quot;&amp;lt;link href=\&amp;quot;&amp;quot;&lt;/span&gt; + pageCSS + &lt;span class="str"&gt;&amp;quot;?&amp;quot;&lt;/span&gt; + securityKey + &lt;span class="str"&gt;&amp;quot;\&amp;quot; rel=\&amp;quot;stylesheet\&amp;quot; type=\&amp;quot;text/css\&amp;quot; /&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span class="str"&gt;&amp;quot;&amp;lt;meta http-equiv=\&amp;quot;Content-Type\&amp;quot; content=\&amp;quot;text/html; charset=utf-8\&amp;quot;/&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span class="str"&gt;&amp;quot;&amp;lt;meta http-equiv=\&amp;quot;Cache-control\&amp;quot; content=\&amp;quot;no-cache\&amp;quot;/&amp;gt;&amp;quot;&lt;/span&gt;;
strResp = WebServer.OutPutStream(response, strResp);&lt;/pre&gt;


&lt;p&gt;The HTML page is build and the CSS page is added with a parameter (a security key). And this line including the CSS is the only line I have to add to go from the original design to the nice new one!&lt;/p&gt;

&lt;p&gt;So that’s it for this part. I’ll try to find some time to write additional examples.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10407866" width="1" height="1"&gt;</description></item><item><title>How software can solve hardware problems</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/07/30/how-software-can-solve-hardware-problems.aspx</link><pubDate>Mon, 30 Jul 2012 12:09:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10334740</guid><dc:creator>Laurelle</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10334740</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/07/30/how-software-can-solve-hardware-problems.aspx#comments</comments><description>&lt;p&gt;I’ve developed my own sprinkler system and my own humidity sensor. This sensor is extremely simple as you can read in my &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/07/18/a-soil-low-cost-humidity-sensor-moisture-and-net-micro-framework-netmf.aspx"&gt;article&lt;/a&gt;. The main problem of this sensor is that the data to be read is an analogic data and the length of the cable is very long. There are lots of problems implied with long cables. You can read an excellent article from Mario Vernari &lt;a href="http://highfieldtales.wordpress.com/2012/04/25/effect-of-long-wiring-on-digital-signals/"&gt;here&lt;/a&gt;. In his article, you can clearly see the impact of an alimentation, of other signals into a long distance cable.&lt;/p&gt;  &lt;p&gt;Of course my humidity sensor will follow the same problem. And I’ve logged couple of days of data every minute to have enough data to see how the data looks like.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/5531.image_5F00_2.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1715.image_5F00_thumb.png" width="538" height="335" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;As you can visually see the bleu line represents the data and with 19000 data, you can feel like it is very random. In fact those data are not random, they are just noisy. There are couple of solutions to avoid this problem. First solution is of course hardware, changing the hardware to remove a maximum of noise and clear the signal. But I will not do this for my example.&lt;/p&gt;  &lt;p&gt;I will use a pure software solution to remove this noise. As explained in Mario’s blog, most of the noise is kid of high frequency compare to the normal signal. And this apply to any scale. So in my scale, I will also have this issue. What I know is that my signal do not change much. Remember I’m measuring the soil humidity or moisture and this is not changing so quickly. It can change very quickly in less than couple of minutes in case of huge rain.&lt;/p&gt;  &lt;p&gt;So one of the solution is to do a mobile average. By doing this, you can remove the high frequency. The principle is simple, it’s about doing an average of N values that “moves” with the signal. And the effect is to mathematically remove the high frequencies. Let use an example with a period of 15 points. In my graph, that would say an average of a moving quarter. Applied to the previous graph, the black line does represent it:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/5100.image_5F00_4.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8737.image_5F00_thumb_5F00_1.png" width="544" height="339" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;and visually, you can see that it does remove lots of the randomness. Of course, if you do an average on more points, you’ll remove even more of the noise. The orange one does represent an average on 60 points so a mobile average of 1h:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/3823.image_5F00_6.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8787.image_5F00_thumb_5F00_2.png" width="549" height="342" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The period you have to choose depend on the quality of the signal and of course the change of data you still want to measure. The longest the period is and the less changes you can evaluate. So let use this last example with a 1 hour filter and let see if we can read anything on it. I know what was the weather and when I did sprinkler. Logically, each time it has rain, the signal should increase and decrease with no rain and high temperatures. And it should also increase if I did sprinkler. On this graph, I’ve put in red when it did rain and in green when I did sprinkler.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1348.image_5F00_12.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/2313.image_5F00_thumb_5F00_5.png" width="553" height="345" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So even if we have this 60 points mobile filter and this remove rapid change in the measurement, it is still possible to see the impact of a rain or of a sprinkler. Now, I’ve added a 600 points mobile average and I let the pic where they were, here is the result:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/0815.image_5F00_14.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7750.image_5F00_thumb_5F00_6.png" width="558" height="348" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;it is still readable but the impact of an event can be read later. And the pic are lower. I don’t want to enter in the theory but to be simple, it’s just because, you are using more data, so as explain, the average will lower the result as it is there to remove high frequencies. And you’ll need more “higher” data to see an impact. It’s like if you had a field of green grass and add couple of red flowers. Take a picture and do an average of the color. It will be green. Add couple of other red flours, take a picture again, do the average and continue like this. And you’ll need to have lots of red flours added to see a change while you’ve started adding flowers some time ago. This is exactly what is happening there. You will detect the phenomenon much later than it happened.&lt;/p&gt;  &lt;p&gt;So if you want to use those kind of filters, make sure you use them wisely and not with a too long period. In my case, I will probably use for production something between 15 and 60 points. I need to have an idea of the humidity, not a very precise value. And as shown in the previous graph, it is visible when there’s been some rain or if the sprinkler was on.&lt;/p&gt;  &lt;p&gt;And of course, I’ll need also to work on the isolation of the cable to try to remove some of the noise.&lt;/p&gt;  &lt;p&gt;You may Wonder how I was able to log 19000 points? Well, it is very simple and the code is there:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LogToFile
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;static public bool &lt;/span&gt;Print(&lt;span style="color: blue;"&gt;string &lt;/span&gt;StrFileName, &lt;span style="color: blue;"&gt;string &lt;/span&gt;strToLog)
    {
        &lt;span style="color: blue;"&gt;try
        &lt;/span&gt;{
            &lt;span style="color: rgb(43, 145, 175);"&gt;FileStream &lt;/span&gt;fileToWrite = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;FileStream&lt;/span&gt;(StrFileName, &lt;span style="color: rgb(43, 145, 175);"&gt;FileMode&lt;/span&gt;.OpenOrCreate, &lt;span style="color: rgb(43, 145, 175);"&gt;FileAccess&lt;/span&gt;.Write);
            &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] buff = &lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(strToLog);
            fileToWrite.Seek(fileToWrite.Length, 0);
            fileToWrite.Write(buff, 0, buff.Length);
            fileToWrite.Close();
            &lt;span style="color: blue;"&gt;return true&lt;/span&gt;; 
        }
        &lt;span style="color: blue;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Exception &lt;/span&gt;e)
        { 
            &lt;span style="color: blue;"&gt;return false&lt;/span&gt;;  
        }
    
    }
}
&lt;/pre&gt;

&lt;p&gt;It takes a simple file name as an input and something to write. The code is straight forward, it first create or open a file. Then encode the string to write in the buff variable. Seek to the end of the file, write the buff and close the file. So extremely simple code!&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;LogToFile&lt;/span&gt;.Print(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;WINFS\\out.txt&amp;quot;&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.ToString(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;MM/dd/yyyy HH:mm:ss&amp;quot;&lt;/span&gt;) + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;;&amp;quot; &lt;/span&gt;+ mSensor.Humidity + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\r\n&amp;quot;&lt;/span&gt;);
&lt;/pre&gt;

&lt;p&gt;And the usage is as simple as the code, one line to log data like in this example. and as a result, you get data like this in the text file:&lt;/p&gt;

&lt;p&gt;07/29/2012 13:34:46;86 
  &lt;br /&gt; 07/29/2012 13:34:47;93 

  &lt;br /&gt; 07/29/2012 13:34:48;91 

  &lt;br /&gt; 07/29/2012 13:34:49;93 

  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;For the next article, we will see how to implement the filter in the real code.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10334740" width="1" height="1"&gt;</description></item><item><title>A soil low cost humidity sensor (moisture) and .NET Micro Framework (NETMF)</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/07/18/a-soil-low-cost-humidity-sensor-moisture-and-net-micro-framework-netmf.aspx</link><pubDate>Wed, 18 Jul 2012 01:22:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10330920</guid><dc:creator>Laurelle</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10330920</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/07/18/a-soil-low-cost-humidity-sensor-moisture-and-net-micro-framework-netmf.aspx#comments</comments><description>&lt;p&gt;I’m working on my &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/05/07/managing-my-sprinklers-from-the-cloud.aspx"&gt;own sprinkler system&lt;/a&gt; which I can pilot thru Internet wherever I am. I can add programs and also open and close sprinklers when I want. I recently build a &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/06/24/a-low-cost-humidity-sensor-for-my-sprinkler-system.aspx"&gt;prototype of a humidity sensor&lt;/a&gt;. Now it’s time to implement this humidity sensor in pre production and see how it is working for real. Next phase will be an automatic piloting based on this humidity sensor. Thanks also to all the feedback I get which encourage me to continue those posts. I’m just a marketing guy doing hardware and software development &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt;&lt;/p&gt;  &lt;p&gt;Based on the work of the prototype, I’ve build a normal size sensor and put it for real in my garden:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/0511.WP_5F00_000846_5F00_2.jpg"&gt;&lt;img title="WP_000846" style="display: inline; background-image: none;" border="0" alt="WP_000846" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7851.WP_5F00_000846_5F00_thumb.jpg" width="324" height="430" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It is the same principle as the prototype, there are 2 coppers cables put in the soil (approx. 10 centimeters) and current arriving to those 2 cables. The potential is measured and interpreted to see how humid it is. To summarize, the more humid it is the lower the resistance of the soil is. So I need 3 cables only: one for ground, one for +3.3V and one for the measurement. I can chain sensors and will only need 1 additional cable per sensor. This solution is very simple and very easy to implement. There are smartest way to do this with a real serial bus self powered. But for the moment, I’m fine with this solution. I will explore the other solution when I will want to add temperature, luminosity and others like wind.&lt;/p&gt;  &lt;p&gt;Now, looking at the code, I wanted to make part of the code reusable and I’ve define a humidity sensor with simple Properties and a class to initialize the analogic input.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public struct &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySenrorParam
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;public int &lt;/span&gt;MinValue;
    &lt;span style="color: blue;"&gt;public int &lt;/span&gt;MaxValue;
    &lt;span style="color: blue;"&gt;public int &lt;/span&gt;DeclValue;
    &lt;span style="color: blue;"&gt;public int &lt;/span&gt;UpdateTime;
}

&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySensor
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;private &lt;/span&gt;SecretLabs.NETMF.Hardware.&lt;span style="color: rgb(43, 145, 175);"&gt;AnalogInput &lt;/span&gt;HumSensor;
    &lt;span style="color: blue;"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Timer &lt;/span&gt;MyTimer;
    &lt;span style="color: blue;"&gt;private int &lt;/span&gt;myDeclUpdate;
    &lt;span style="color: blue;"&gt;private int &lt;/span&gt;myHumidity;
    &lt;span style="color: blue;"&gt;private bool &lt;/span&gt;bhumid = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;
   
    &lt;span style="color: blue;"&gt;public &lt;/span&gt;HumiditySensor(&lt;span style="color: rgb(43, 145, 175);"&gt;Cpu&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;Pin &lt;/span&gt;HumidityPin, &lt;br /&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySenrorParam &lt;/span&gt;MyHumidityStruct)
    {
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(!Microsoft.SPOT.Hardware.&lt;span style="color: rgb(43, 145, 175);"&gt;SystemInfo&lt;/span&gt;.IsEmulator)
            HumSensor = &lt;br /&gt;&lt;span style="color: blue;"&gt;new &lt;/span&gt;SecretLabs.NETMF.Hardware.&lt;span style="color: rgb(43, 145, 175);"&gt;AnalogInput&lt;/span&gt;(HumidityPin);
        MyTimer = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Timer&lt;/span&gt;(&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;TimerCallback&lt;/span&gt;(ClockTimer_Tick), &lt;br /&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;, MyHumidityStruct.UpdateTime * 1000, 0);
        MinValue = MyHumidityStruct.MinValue;
        MaxValue = MyHumidityStruct.MaxValue;
        DeclValue = MyHumidityStruct.DeclValue;
        UpdateTime = MyHumidityStruct.UpdateTime;
    }
&lt;/pre&gt;

&lt;p&gt;The HumiditySensorParam class is used to pass the initialization value to the HumiditySensor class. Each class is using an analogic input on a specific Pin from the CPU. This value is also passed in the constructor.&lt;/p&gt;

&lt;p&gt;An analogic input on the netduino has 1024 points from 0 to 3.3V starting from 0 to 1023. But as explained in the previous article on the prototype, only part of this range is used. So I’m using 3 parameters: a minimum value, a maximum value and a value to determine the limit between dry and humid. All should be from 0 to 1023. It is not checked in the class but a good programmer will do &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt;&lt;/p&gt;

&lt;p&gt;Also, the analogic input has to be read in a regular timeframe. This is done by the UpdateTime value in seconds. A timer is raised every period to update the value of the humidity sensor.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public int &lt;/span&gt;MinValue
{ &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }

&lt;span style="color: blue;"&gt;public int &lt;/span&gt;MaxValue
{ &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }

&lt;span style="color: blue;"&gt;public int &lt;/span&gt;DeclValue
{ &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }

&lt;span style="color: blue;"&gt;public int &lt;/span&gt;UpdateTime
{
    &lt;span style="color: blue;"&gt;get &lt;/span&gt;{ &lt;span style="color: blue;"&gt;return &lt;/span&gt;myDeclUpdate; }
    &lt;span style="color: blue;"&gt;set
    &lt;/span&gt;{
        MyTimer.Change(0, &lt;span style="color: blue;"&gt;value &lt;/span&gt;* 1000);
        myDeclUpdate = &lt;span style="color: blue;"&gt;value&lt;/span&gt;; 
    }
}
&lt;/pre&gt;

&lt;p&gt;All 4 parameters are stored into Properties. The min, max and decl value are stored in normal Properties. The UpdateTime is a bit different. It has to change the value of the timer. Again, here, I do things in a simple way, but all those values should be checked before setting them. &lt;/p&gt;

&lt;p&gt;Now let have a look at the timer&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;static void &lt;/span&gt;ClockTimer_Tick(&lt;span style="color: blue;"&gt;object &lt;/span&gt;sender)
{
    &lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySensor &lt;/span&gt;mSensor = (&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySensor&lt;/span&gt;)sender;
    mSensor.Update();
    &lt;span style="color: green;"&gt;//Debug.Print(DateTime.Now.ToString(&amp;quot;MM/dd/yyyy HH:mm:ss&amp;quot;) &lt;br /&gt;+ &amp;quot; Humidity: &amp;quot; + mSensor.Humidity + &amp;quot; IsHumid: &amp;quot; + mSensor.IsHumid);
&lt;/span&gt;}
&lt;/pre&gt;

&lt;p&gt;The code is quite simple, it first get the object. This function must be a static one as it is called by .NETMF when it is time. The sender object is the humidity sensor itself as setup in the constructor. After the cast, it is possible to call the update method.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;Update()
{
    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(!Microsoft.SPOT.Hardware.&lt;span style="color: rgb(43, 145, 175);"&gt;SystemInfo&lt;/span&gt;.IsEmulator)
        Humidity = HumSensor.Read();
    &lt;span style="color: blue;"&gt;else
        &lt;/span&gt;Humidity = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Random&lt;/span&gt;().Next(MaxValue);
}

&lt;/pre&gt;

&lt;p&gt;I first check if we are in the emulator or on a real Platform. In the case I’m in the emulator, I just return a random number. This allow to test the software when I do development in planes like now on my way to Atlanta &lt;img class="wlEmoticon wlEmoticon-winkingsmile" alt="Clignement d&amp;#39;œil" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6886.wlEmoticon_2D00_winkingsmile_5F00_2.png" /&gt;&lt;/p&gt;

&lt;p&gt;In the other case, I read the analogic input and put the value into the Humidity property. &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public int &lt;/span&gt;Humidity
{ &lt;span style="color: blue;"&gt;get &lt;/span&gt;{
    &lt;span style="color: blue;"&gt;return &lt;/span&gt;myHumidity;
}
    &lt;span style="color: blue;"&gt;internal set &lt;/span&gt;{
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(&lt;span style="color: blue;"&gt;value &lt;/span&gt;&amp;lt; MinValue) &lt;span style="color: blue;"&gt;value &lt;/span&gt;= MinValue;
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(&lt;span style="color: blue;"&gt;value &lt;/span&gt;&amp;gt; MaxValue) &lt;span style="color: blue;"&gt;value &lt;/span&gt;= MaxValue;
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(&lt;span style="color: blue;"&gt;value &lt;/span&gt;&amp;gt;= DeclValue) bhumid = &lt;span style="color: blue;"&gt;true&lt;/span&gt;; &lt;span style="color: blue;"&gt;else &lt;/span&gt;bhumid = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;
        myHumidity = 100 * (&lt;span style="color: blue;"&gt;value &lt;/span&gt;- MinValue)/(MaxValue - MinValue);} 
}
&lt;/pre&gt;

&lt;p&gt;I allow to update this property only internally. I do the math with the Min, Max values to compute a percentage. And check if it is dry or humid.&lt;/p&gt;

&lt;p&gt;And I’ve also created 2 other Properties I can use later in the code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public bool &lt;/span&gt;IsHumid
{
    &lt;span style="color: blue;"&gt;get
    &lt;/span&gt;{ &lt;span style="color: blue;"&gt;return &lt;/span&gt;bhumid; }
}

&lt;span style="color: blue;"&gt;public int &lt;/span&gt;DeclHumidity
{ 
    &lt;span style="color: blue;"&gt;get &lt;/span&gt;{ &lt;span style="color: blue;"&gt;return &lt;/span&gt;(100 * (DeclValue - MinValue) / (MaxValue - MinValue)) ; }
}
&lt;/pre&gt;

&lt;p&gt;The IsHumid return true if it is humid compare to the level and the DeclHumidity return this level in percentage. &lt;/p&gt;

&lt;p&gt;Here it is, you have a humidity sensor and the code to read the values &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt; Now we will look at how to use it. For this, I display the humidity level in the main web page with the information regarding the sprinklers. And I’ve also build a page to be able to change dynamically the values of the Humidity sensor. This is useful when you need to calibrate it.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySenrorParam &lt;/span&gt;MyHumidityStruc = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySenrorParam&lt;/span&gt;();
MyHumidityStruc.MinValue = 0;
MyHumidityStruc.MaxValue = 1023;
MyHumidityStruc.DeclValue = 500;
MyHumidityStruc.UpdateTime = 60;
&lt;/pre&gt;

&lt;pre class="code"&gt;Sprinklers[0].HumiditySensor = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;HumiditySensor&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Pins&lt;/span&gt;.GPIO_PIN_A0, MyHumidityStruc);
&lt;/pre&gt;

&lt;p&gt;Nothing complex in this initialization phase. I’ve already explained in previous posts the Sprinkler class. I just added a HumiditySensor member. So I can have 1 humidity sensor per sprinkling zone. Looking at how it’s working in the industry. And I want to be flexible. So I can have a humidity sensor wherever I want. I’m using a parameter file which I read and interpret at boot time.&lt;/p&gt;

&lt;p&gt;The final result looks like this with 1 humidity sensor:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8831.image_5F00_2.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/2260.image_5F00_thumb.png" width="332" height="306" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And the code to build the page is easy:&lt;/p&gt;

&lt;pre class="code"&gt;strResp = &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;HTML&amp;gt;&amp;lt;BODY&amp;gt;netduino sprinkler&amp;lt;p&amp;gt;&amp;quot;&lt;/span&gt;;
&lt;span style="color: green;"&gt;// need to test!
&lt;/span&gt;strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);
&lt;span style="color: blue;"&gt;for &lt;/span&gt;(&lt;span style="color: blue;"&gt;int &lt;/span&gt;i = 0; i &amp;lt; NUMBER_SPRINKLERS; i++)
{
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Springler &amp;quot; &lt;/span&gt;+ Sprinklers[i].Name + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;: &amp;lt;a href='/&amp;quot; &lt;br /&gt;&lt;/span&gt;+ paramPageSprinkler + ParamStart + securityKey + ParamSeparator &lt;br /&gt;+ paramSpr + i + ParamEqual + !Sprinklers[i].Open + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'&amp;gt;&amp;quot; &lt;br /&gt;&lt;/span&gt;+ Sprinklers[i].Open + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;a href='/&amp;quot; &lt;/span&gt;+ paramPageCalendar + ParamStart &lt;br /&gt;+ securityKey + ParamSeparator + paramYear + ParamEqual + &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.Year &lt;br /&gt;+ ParamSeparator + paramMonth + ParamEqual + &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.Month &lt;br /&gt;+ ParamSeparator + paramSpr + ParamEqual + i + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'&amp;gt;Program Sprinkler &amp;quot; &lt;br /&gt;&lt;/span&gt;+ Sprinklers[i].Name + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;a href='/&amp;quot; &lt;/span&gt;+ paramPageListPrgm + ParamStart &lt;br /&gt;+ securityKey + ParamSeparator + paramSpr + ParamEqual + i &lt;br /&gt;+ &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'&amp;gt;List all programs for Sprinkler &amp;quot; &lt;/span&gt;+ Sprinklers[i].Name + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(Sprinklers[i].HumiditySensor != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
    {
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Humidity: &amp;quot; &lt;/span&gt;+ Sprinklers[i].HumiditySensor.Humidity;
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(Sprinklers[i].HumiditySensor.IsHumid)
            strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot; and it is humid&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
        &lt;span style="color: blue;"&gt;else
            &lt;/span&gt;strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot; and it is time to sprinkle!&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
    }
    strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);
}
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;p&amp;gt;&amp;lt;a href='/&amp;quot; &lt;/span&gt;+ paramPageUtil + ParamStart &lt;br /&gt;+ securityKey + ParamSeparator + paramClk + ParamEqual &lt;br /&gt;+ &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;1'&amp;gt;Update date and time&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;p&amp;gt;&amp;lt;a href='/&amp;quot; &lt;/span&gt;+ paramPageUtil + ParamStart &lt;br /&gt;+ securityKey + ParamSeparator + paramReboot + ParamEqual &lt;br /&gt;+ &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;1'&amp;gt;Reboot&amp;lt;/a&amp;gt;&amp;lt;br&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.ToString();
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/BODY&amp;gt;&amp;lt;/HTML&amp;gt;&amp;quot;&lt;/span&gt;;
strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);

&lt;/pre&gt;

&lt;p&gt;ParamStart = ‘?’, ParamSeparator = ‘&amp;amp;’, ParamEqual = ‘=’, the various paramPage are the name of the page (like “spr.aspx” for paramPageSprinkler). It is mainly about building and URL like spr.aspx?sec=seckey&amp;amp;spr0=True&lt;/p&gt;

&lt;p&gt;I display only the information regarding a humidity sensor if one has been setup. If not, I don’t display anything. And as you can see, it is really simple, the Humidity property return the humidity in percentage. and IsHumid allow to check if it is dry or humid so if it time to sprinkle or not &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt;&lt;/p&gt;

&lt;p&gt;You’ve read this blog post up to this point. So I’m sure you still have energy! I’ve build a page to be able to update the various parameters. The page look like this with one humidity sensor. With multiple humidity sensor, it will just display more forms on the same page:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8424.image_5F00_4.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8037.image_5F00_thumb_5F00_1.png" width="328" height="262" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When updates, the result is the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/2063.image_5F00_6.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6864.image_5F00_thumb_5F00_2.png" width="328" height="197" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My code allow also to get this information without and UI, just with the data. It allow also to just change one sensor and not all. I will not show all the code, I will just explain the core part with the forms:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;for &lt;/span&gt;(&lt;span style="color: blue;"&gt;int &lt;/span&gt;i = 0; i &amp;lt; NUMBER_SPRINKLERS; i++)
    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(Sprinklers[i].HumiditySensor != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
    {
        &lt;span style="color: green;"&gt;//display a form with all params to be modified
        &lt;/span&gt;strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;form method=\&amp;quot;get\&amp;quot; action=\&amp;quot;&amp;quot; &lt;br /&gt;&lt;/span&gt;+ paramPageHumidity + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot;&amp;gt;&amp;lt;p&amp;gt;Humidity sensor &amp;quot; &lt;br /&gt;&lt;/span&gt;+ Sprinklers[i].Name;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;br /&amp;gt;Humidity = &amp;quot; &lt;br /&gt;&lt;/span&gt;+ Sprinklers[i].HumiditySensor.Humidity + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot; and is Humid = &amp;quot; &lt;br /&gt;&lt;/span&gt;+ Sprinklers[i].HumiditySensor.IsHumid + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Min = &amp;lt;input type=\&amp;quot;text\&amp;quot; name=\&amp;quot;&amp;quot;&lt;br /&gt;&lt;/span&gt;+ paramHumidityMinValue +&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; value=\&amp;quot;&amp;quot; &lt;/span&gt;+ Sprinklers[i].HumiditySensor.MinValue &lt;br /&gt;+ &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; /&amp;gt;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Max = &amp;lt;input type=\&amp;quot;text\&amp;quot; name=\&amp;quot;&amp;quot;&lt;br /&gt;&lt;/span&gt;+ paramHumidityMaxValue +&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; value=\&amp;quot;&amp;quot; &lt;/span&gt;+ Sprinklers[i].HumiditySensor.MaxValue &lt;br /&gt;+ &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; /&amp;gt;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Decl = &amp;lt;input type=\&amp;quot;text\&amp;quot; name=\&amp;quot;&amp;quot;&lt;br /&gt;&lt;/span&gt;+ paramHumidityDeclValue +&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; value=\&amp;quot;&amp;quot; &lt;/span&gt;+ Sprinklers[i].HumiditySensor.DeclValue &lt;br /&gt;+ &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; /&amp;gt;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Time update (sec) = &amp;lt;input type=\&amp;quot;text\&amp;quot; name=\&amp;quot;&amp;quot;&lt;br /&gt;&lt;/span&gt;+ paramHumidityTimeUpdate +&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; value=\&amp;quot;&amp;quot; &lt;br /&gt;&lt;/span&gt;+ Sprinklers[i].HumiditySensor.UpdateTime + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; /&amp;gt;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&lt;br /&gt;&amp;lt;input id=\&amp;quot;Submit\&amp;quot; type=\&amp;quot;submit\&amp;quot; value=\&amp;quot;Update\&amp;quot; /&amp;gt;&amp;lt;/p&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;input type=\&amp;quot;hidden\&amp;quot; name=\&amp;quot;&amp;quot; &lt;br /&gt;&lt;/span&gt;+ paramSpr + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; value=\&amp;quot;&amp;quot; &lt;/span&gt;+ i + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;input type=\&amp;quot;hidden\&amp;quot; name=\&amp;quot;&amp;quot; &lt;br /&gt;&lt;/span&gt;+ paramSecurityKey + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot; value=\&amp;quot;&amp;quot; &lt;/span&gt;+ MySecurityKey + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;\&amp;quot;&amp;gt;&amp;lt;/form&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt;;
        strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);
    }
&lt;/pre&gt;

&lt;p&gt;Nothing really complicated here, it’s just building a HTML page by hands &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt; The form will be posted with the GET method. So all parameters will be passed thru the URL. I’ve used this method for all the development I’ve done and you’ll find examples in my previous posts. Including the web server itself.&lt;/p&gt;

&lt;p&gt;So enjoy this humidity sensor &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt; And I hope the weather will get dryer in Paris this summer so I’ll be able to calibrate it correctly, find the right level Under which it is interesting to sprinkle. And of course, next step is to automate all this! And let the system manage itself based on the soil humidity. And I will add other sensors like temperature! Stay tune &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/1172.wlEmoticon_2D00_smile_5F00_2.png" /&gt; and feedback welcome as usual. &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10330920" width="1" height="1"&gt;</description></item><item><title>A low cost humidity sensor for my sprinkler system</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/06/24/a-low-cost-humidity-sensor-for-my-sprinkler-system.aspx</link><pubDate>Sun, 24 Jun 2012 15:23:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10323326</guid><dc:creator>Laurelle</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10323326</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/06/24/a-low-cost-humidity-sensor-for-my-sprinkler-system.aspx#comments</comments><description>&lt;p&gt;I’ve &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/05/07/managing-my-sprinklers-from-the-cloud.aspx"&gt;developed my own sprinkler system&lt;/a&gt; which embed a web server and allow me to control it remotely where ever I am. I can program when and how long I will sprinkler on which circuit. I have 3 circuits but my system can work with a large number of circuits.&lt;/p&gt;  &lt;p&gt;What I want to do is to be able to add intelligence into my netduino board. This .NET Microframework (NETMF) board runs without any OS. So now Windows, no Linux, no Mac, nothing! Directly .NET on the chip. Not a full .NET of course but all what is necessary to be able to &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/02/17/using-basic-io-with-net-microframework.aspx"&gt;pilot IO&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/05/29/creating-an-efficient-http-web-server-for-net-microframework-netmf.aspx"&gt;have a web server&lt;/a&gt;, etc. All this in an embedded board smallest of the size of a credit card. &lt;/p&gt;  &lt;p&gt;Part of my project is to be able to measure the soil humidity. So I’ve decided to develop my own sensor. The basic idea is to measure the conductivity (or resistor) of the soil. Any object/material has it’s own resistance. The more conductive it is, the smallest the resistor is and the less conductive, the higher the resistor is. And it does apply to anything. Metals are usually excellent resistors with a very low resistance of less than 1 Ω. And something like a plastic will have more than 1 MΩ resistor. So if you apply a huge voltage, you’ll get a very small current.&lt;/p&gt;  &lt;p&gt;The rule you have to know to do some electronic is U = R x I where U is the voltage (tension in volt V), R is the resistor (in ohm Ω) and I is the intensity of the current (in ampere, A). So I will measure the resistor 'of the soil and I will determine if it is dry or humid.&lt;/p&gt;  &lt;p&gt;Let start wit a bit of theory there regarding soil conductivity. It is possible to measure the soil conductivity with a &lt;a href="http://en.wikipedia.org/wiki/Tellurometer"&gt;Tellurometer&lt;/a&gt;. Soil conductivity is measured by this specific sensor and the resistance of the soil is determined. In my case what will interest me is to be able to measure the difference of conductivity between a humid and a dry soil at the same place. It just need to have 2 stick of copper or any other metal put into the soil and have a current going thru one stick and measuring the difference of voltage from the other.&lt;/p&gt;  &lt;p&gt;When a soil is humid the resistor decrease and when it is dry, it does increase. So imagine I will build something like a voltmeter put into the soil and I will measure the resistance. As my netduino has an analogic input I will use it for this purpose. What I measure here, is a voltage so indirectly this variance or resistance. As per the &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/02/17/using-basic-io-with-net-microframework.aspx"&gt;light sensor&lt;/a&gt;, I’ll use the same principle:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/5518.image_5F00_6.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7652.image_5F00_thumb_5F00_2.png" width="283" height="396" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So I will measure the voltage of R3. R3 is a high value of 10K to do a pull down. It is a high resistor which will create a small current between the ground and A0. If I don’t put any resistor,I won’t be able to measure any intensity. And if I place A0 on the ogher side of my sensor and remove R3, I will use more current than in this design. It is possible to do the same as for the light sensor but in my case it will be a bit less efficient I guess.&lt;/p&gt;  &lt;p&gt;R1 is here to reduce a bit the current and I will have to adjust this value regarding of my current soil.&lt;/p&gt;  &lt;p&gt;The code is extremely simple:&lt;/p&gt;  &lt;pre class="code"&gt;SecretLabs.NETMF.Hardware.&lt;span style="color: rgb(43, 145, 175);"&gt;AnalogInput &lt;/span&gt;SoilSensor =&lt;br /&gt; &lt;span style="color: blue;"&gt;new &lt;/span&gt;SecretLabs.NETMF.Hardware.&lt;span style="color: rgb(43, 145, 175);"&gt;AnalogInput&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Pins&lt;/span&gt;.GPIO_PIN_A0);
&lt;span style="color: green;"&gt;//lightSensor.SetRange(0, 100);

&lt;/span&gt;&lt;span style="color: blue;"&gt;int &lt;/span&gt;lSoilSensorReading = 0;
&lt;span style="color: blue;"&gt;while &lt;/span&gt;(&lt;span style="color: blue;"&gt;true&lt;/span&gt;)
{
    SoilSensorReading = SoilSensor.Read();
    &lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Print(SoilSensorReading.ToString());
    &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;.Sleep(500);
}
&lt;/pre&gt;

&lt;p&gt;I create an analogic input on port A0. And then I read the value every 500 milliseconds. And that’s it!&lt;/p&gt;

&lt;p&gt;I’ve done the test with real soil, one is very humid, one a bit humid and one is very dry.&lt;/p&gt;

&lt;p&gt;I get the following results:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;very humid = 650&lt;/li&gt;

  &lt;li&gt;a bit humid = 630&lt;/li&gt;

  &lt;li&gt;very dry = 550&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And here is the picture of the prototype:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/2860.WP_5F00_000792_5F00_2.jpg"&gt;&lt;img title="WP_000792" style="display: inline; background-image: none;" border="0" alt="WP_000792" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/4604.WP_5F00_000792_5F00_thumb.jpg" width="514" height="387" /&gt;&amp;#160;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the analogic port has 1024 values going from 0 to 1023 on 3.3V, I have an amplitude of 100 values which represent a variance of approximately 0.32V.&lt;/p&gt;

&lt;p&gt;So with this prototype I have a difference of 0.32V between dry and very humid with this specific soil.&lt;/p&gt;

&lt;p&gt;I’m sure I can change a bit the sensitivity to use a broader range of the analogic input. I can do like for the light sensor an remove the R3 resistor and measure directly the tension between the sensor and the ground. I can also change R1 to a value close to the middle of the resistance of the soil. I can also change the alimentation value to 5V or so. &lt;/p&gt;

&lt;p&gt;That was just a first experiment! Just to prove it is working &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/4101.wlEmoticon_2D00_smile_5F00_2.png" /&gt; Now, I need to improve a bit the system and see how far I can go. Any feedback from an electronic guy welcome.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10323326" width="1" height="1"&gt;</description></item><item><title>Using XMLHttpRequest to pilot a Lego train dynamically in HTML 5</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/06/20/using-xmlhttprequest-to-pilot-a-lego-train-dynamically-in-html-5.aspx</link><pubDate>Wed, 20 Jun 2012 20:51:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10322348</guid><dc:creator>Laurelle</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10322348</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/06/20/using-xmlhttprequest-to-pilot-a-lego-train-dynamically-in-html-5.aspx#comments</comments><description>&lt;p&gt;It’s a long time I did not write a blog post. I was very busy and had no time to write and code anything in the last weeks. I still have a lot of work but I need an intellectual break for the evening. So I do not write this post from a plane but from an hotel room. In my past &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/04/26/using-netduino-and-net-microframework-to-pilot-any-lego-power-function-thru-infrared-part-3.aspx"&gt;blog posts&lt;/a&gt; I’ve explained how to pilot any Lego Power System with a &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/09/netduino-board-geek-tool-for-net-microframework.aspx"&gt;Netduino&lt;/a&gt; using .NET Microframework. &lt;/p&gt;  &lt;p&gt;In the &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/05/29/creating-an-efficient-http-web-server-for-net-microframework-netmf.aspx"&gt;HTTP Web server I’ve implemented&lt;/a&gt;, I command the train thru a URL with arguments. Those arguments are transformed into parameters which are given to a class. This class output a wave form into an infrared led amplified by a transistor. This is a simple and efficient way to command anything. I do the same for &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/05/07/managing-my-sprinklers-from-the-cloud.aspx"&gt;my sprinkler system&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;Now if you want to pilot in a web interface multiple trains, and click on buttons or pictures to get an action without opening a new web page or refreshing the page, you need to do some Scripting in your HTML page. I’m not a web developer, I don’t like Scripting languages as they are not strict enough to write correct code and imply too many errors. They drastically increase your development time! I truly prefer a good language like C#, VB or even Java and I can go up to C/C++ &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8030.wlEmoticon_2D00_smile_5F00_2.png" /&gt; Now, if I want to avoid any problem, I can jump into Eiffel &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8030.wlEmoticon_2D00_smile_5F00_2.png" /&gt; OK, I won’t go up to there, I’ll stay with java script in an HTML5 page.&lt;/p&gt;  &lt;p&gt;What I want is to call my command page in the background of the page and stay in the HTML page when I click on a button. There is a nice object in HTML which allow you to do that which is XMLHttpRequest. It is implemented in all decent browsers.&lt;/p&gt;  &lt;p&gt;Here is the code that I generate dynamically (I’ll show you the code later) and I’ll explain you how it works:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;html &lt;/span&gt;&lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;head&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;head&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;body&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;SCRIPT &lt;/span&gt;&lt;span style="color: red;"&gt;language&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;JavaScript&amp;quot;&amp;gt;
var &lt;/span&gt;xhr = &lt;span style="color: blue;"&gt;new &lt;/span&gt;XMLHttpRequest();
&lt;span style="color: blue;"&gt;function &lt;/span&gt;btnclicked(boxMSG, cmdSend) {
    boxMSG.innerHTML = &lt;span style="color: maroon;"&gt;&amp;quot;Waiting&amp;quot;&lt;/span&gt;;
    xhr.open(&lt;span style="color: maroon;"&gt;'GET'&lt;/span&gt;, &lt;span style="color: maroon;"&gt;'singlepwm.aspx?' &lt;/span&gt;+ cmdSend + &lt;span style="color: maroon;"&gt;'&amp;amp;sec='&lt;/span&gt;);
    xhr.send(&lt;span style="color: blue;"&gt;null&lt;/span&gt;);
    xhr.onreadystatechange = &lt;span style="color: blue;"&gt;function &lt;/span&gt;() {
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(xhr.readyState == 4)
        { boxMSG.innerHTML = xhr.responseText; } 
};
}
&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;SCRIPT&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TABLE &lt;/span&gt;&lt;span style="color: red;"&gt;BORDER&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;0&amp;quot;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TR&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;FORM&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Super train&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;INPUT &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;button&amp;quot; 
&lt;/span&gt;&lt;span style="color: red;"&gt;onClick&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;btnclicked(document.getElementById('train0'),
'pw=11&amp;amp;op=0&amp;amp;ch=254')&amp;quot; 
&lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;&amp;lt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;INPUT &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;button&amp;quot; 
&lt;/span&gt;&lt;span style="color: red;"&gt;onClick&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;btnclicked(document.getElementById('train0'),
'pw=8&amp;amp;op=0&amp;amp;ch=254')&amp;quot; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;Stop&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;INPUT &lt;/span&gt;&lt;span style="color: red;"&gt;type&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;button&amp;quot; 
&lt;/span&gt;&lt;span style="color: red;"&gt;onClick&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;btnclicked(document.getElementById('train0'),
'pw=5&amp;amp;op=0&amp;amp;ch=254')&amp;quot; &lt;/span&gt;&lt;span style="color: red;"&gt;value&lt;/span&gt;&lt;span style="color: blue;"&gt;=&amp;quot;&amp;gt;&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;span &lt;/span&gt;&lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;='train0'&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;span&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;FORM&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;TD&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;TR&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;

&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;In the script part of the page, I have created a simple script. Those lines of code is what is necessary to do a synchronous call of an HTTP page and display the result in the page. &lt;/p&gt;

&lt;p&gt;I create an XMLHttpRequest object which I call xhr. The function call btnclicked takes 2 arguments. the first one is the element of the page I will put the results of the request. And the second one is the command (the parameters) to pass to the URL which will pilot the infrared led as explain previously.&lt;/p&gt;

&lt;p&gt;The function is very simple. First, I put “Waiting” in the element. Then I open the XMLHttpRequest object. I open it with GET and pass the overall URL.&lt;/p&gt;

&lt;p&gt;The request is done when the send function is called. It is a synchronous call, so it get to the onreadystatechange when it is finished. &lt;/p&gt;

&lt;p&gt;Here, when you read the documentation, the readyState 4 mean that everything went well and you have your data back. My function return “OK” when everything is OK and “Problem” if there is a problem.&lt;/p&gt;

&lt;p&gt;Now let have a look at the rest of the HTML page. I decided to create a form with button input. Each button input has a onClick event. This event can be linked to a script. So I will call the fucntion describe before and give a span element (here train0) and the URL. The URL is different depending if you want to train to go forward, backward or stop. And I put all this in a nice table top be able to have multiple trains.&lt;/p&gt;

&lt;p&gt;The page with 4 trains looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/5807.image_5F00_2.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7360.image_5F00_thumb.png" width="371" height="282" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ve clicked on the forward button, “Wainting” is displayed in span. And as soon as the command will finish, it will either display OK or Problem. In my case, Problem will be displayed as in the Emulator, there is no SPI port!&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Start HTML document
&lt;/span&gt;strResp = &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;!DOCTYPE html PUBLIC \&amp;quot;-//W3C//DTD XHTML 1.0 Transitional//EN\&amp;quot;&lt;br /&gt; \&amp;quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;html xmlns=\&amp;quot;http://www.w3.org/1999/xhtml\&amp;quot;&amp;gt;&lt;br /&gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&amp;lt;body&amp;gt;&amp;quot;&lt;/span&gt;;
&lt;span style="color: green;"&gt;//creat the script part
&lt;/span&gt;strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;SCRIPT language=\&amp;quot;JavaScript\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;var xhr = new XMLHttpRequest(); function btnclicked(boxMSG, cmdSend) &lt;br /&gt;{ boxMSG.innerHTML=\&amp;quot;Waiting\&amp;quot;;&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;xhr.open('GET', 'singlepwm.aspx?' + cmdSend + '&amp;amp;&amp;quot; &lt;/span&gt;+ securityKey + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;');&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;xhr.send(null); xhr.onreadystatechange = function() &lt;br /&gt;{if (xhr.readyState == 4) {boxMSG.innerHTML=xhr.responseText;}};}&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/SCRIPT&amp;gt;&amp;quot;&lt;/span&gt;;
strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);
&lt;span style="color: green;"&gt;// Create one section for each train
&lt;/span&gt;strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;TABLE BORDER=\&amp;quot;0\&amp;quot;&amp;gt;&amp;quot;&lt;/span&gt;;
&lt;span style="color: blue;"&gt;for &lt;/span&gt;(&lt;span style="color: blue;"&gt;byte &lt;/span&gt;i = 0; i &amp;lt; myParamRail.NumberOfTrains; i++)
{
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;TR&amp;gt;&amp;lt;TD&amp;gt;&amp;lt;FORM&amp;gt;&amp;quot; &lt;/span&gt;+ myParamRail.Trains[i].TrainName + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/TD&amp;gt;&lt;br /&gt;&amp;lt;TD&amp;gt;&amp;lt;INPUT type=\&amp;quot;button\&amp;quot; &lt;br /&gt;onClick=\&amp;quot;btnclicked(document.getElementById('train&amp;quot; &lt;/span&gt;+ i + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;')&lt;br /&gt;,'pw=&amp;quot; &lt;/span&gt;+ (16 - myParamRail.Trains[i].Speed);
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;op=&amp;quot;&lt;/span&gt;+ myParamRail.Trains[i].RedBlue + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;ch=&amp;quot; &lt;/span&gt;+ &lt;br /&gt;(myParamRail.Trains[i].Channel - 1) + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;')\&amp;quot; value=\&amp;quot;&amp;lt;\&amp;quot;&amp;gt;&amp;lt;/TD&amp;gt;&amp;quot;&lt;/span&gt;;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;TD&amp;gt;&amp;lt;INPUT type=\&amp;quot;button\&amp;quot; &lt;br /&gt;onClick=\&amp;quot;btnclicked(document.getElementById('train&amp;quot; &lt;/span&gt;+ i + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'),'pw=8&amp;quot;&lt;/span&gt;;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;op=&amp;quot; &lt;/span&gt;+ myParamRail.Trains[i].RedBlue + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;ch=&amp;quot; &lt;/span&gt;+ &lt;br /&gt;(myParamRail.Trains[i].Channel - 1) + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;')\&amp;quot; value=\&amp;quot;Stop\&amp;quot;&amp;gt;&amp;lt;/TD&amp;gt;&amp;quot;&lt;/span&gt;;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;TD&amp;gt;&amp;lt;INPUT type=\&amp;quot;button\&amp;quot; &lt;br /&gt;onClick=\&amp;quot;btnclicked(document.getElementById('train&amp;quot; &lt;/span&gt;+ i + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'),&lt;br /&gt;'pw=&amp;quot; &lt;/span&gt;+ myParamRail.Trains[i].Speed;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;op=&amp;quot; &lt;/span&gt;+ myParamRail.Trains[i].RedBlue + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;amp;ch=&amp;quot; &lt;/span&gt;+ &lt;br /&gt;(myParamRail.Trains[i].Channel - 1) + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;')\&amp;quot; value=\&amp;quot;&amp;gt;\&amp;quot;&amp;gt;&amp;lt;/TD&amp;gt;&amp;quot;&lt;/span&gt;;
    strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;TD&amp;gt;&amp;lt;span id='train&amp;quot; &lt;/span&gt;+ i + &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/FORM&amp;gt;&amp;lt;/TD&amp;gt;&amp;lt;/TR&amp;gt;&amp;quot;&lt;/span&gt;; 
    strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);
}
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/TABLE&amp;gt;&amp;lt;br&amp;gt;&amp;lt;a href='all.aspx?&amp;quot; &lt;/span&gt;+ securityKey + &lt;br /&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;'&amp;gt;Display all page&amp;lt;/a&amp;gt;&amp;quot;&lt;/span&gt;;
strResp += &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&amp;quot;&lt;/span&gt;;
strResp = &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.OutPutStream(response, strResp);
&lt;/pre&gt;

&lt;p&gt;The code to generate the HMTL page is here. As you see it is manually generated. The table which contains the name of the train, the backward, stop and forward button is created totally dynamically. Each train may have a different speed and use different channels so the URL is generated dynamically. &lt;/p&gt;

&lt;p&gt;All this takes time to generate and to output in the Stream. So it’s much better to do it only 1 time and then call a simple and small function which will return just a bit of text.&lt;/p&gt;

&lt;p&gt;Last part, on the server header, you have to make sure you add “Cache-Control: no-cache” in the response header. If you don’t do it, only the first request will be send to the netduino board back. XMLHttpRequest will consider that the page will never expire. So I’ve modified the header code of my HTTP Server like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;header = &lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;HTTP/1.1 200 OK\r\nContent-Type: text/html; &lt;br /&gt;charset=utf-8\r\nCache-Control: no-cache\r\nConnection: &lt;br /&gt;close\r\n\r\n&amp;quot;&lt;/span&gt;;
connection.Send(&lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(header), header.Length, &lt;br /&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SocketFlags&lt;/span&gt;.None);

&lt;/pre&gt;

&lt;p&gt;What is interesting there is that I can now pilot up to 8 Lego trains using this very simple interface and without having to refresh the page.&lt;/p&gt;

&lt;p&gt;And the excellent news is that it is working from my Windows Phone so it’s even cool to pilot your Lego train from your Windows Phone. I haven’t tested from an Android or iPhone but I’m quite sure it will work the same way. Remember that all the code is sitting in a very small hardware with no OS, just .NET Microframework! As always, do not hesitate to send me your comments &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8030.wlEmoticon_2D00_smile_5F00_2.png" /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10322348" width="1" height="1"&gt;</description></item><item><title>Creating an efficient HTTP Web Server for .NET Microframework (NETMF)</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/05/29/creating-an-efficient-http-web-server-for-net-microframework-netmf.aspx</link><pubDate>Tue, 29 May 2012 16:36:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10311391</guid><dc:creator>Laurelle</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10311391</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/05/29/creating-an-efficient-http-web-server-for-net-microframework-netmf.aspx#comments</comments><description>&lt;p&gt;That’s not the first post I’m doing on incorporating a Web Server in .NET Microframework (NETMF). In some of &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/12/07/creating-dynamically-a-web-page-using-net-micro-framework.aspx"&gt;my previous posts&lt;/a&gt;, I’ve explain how to do it using the existing .NET classes for this. And it is working very well!&lt;/p&gt;  &lt;p&gt;The main concerns I have is that I’m using a &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/09/netduino-board-geek-tool-for-net-microframework.aspx"&gt;netduino&lt;/a&gt; board. This board is great, I love it. But there is a very limited amount of memory available and limited amount of space to store the programs. Total storage is 64Kb and what is left when the code is in is about 48K… So very little amount of memory. And the http .NET classes are great and use stream but they are intense in terms of memory and also the main http class is huge… &lt;/p&gt;  &lt;p&gt;So I had to find a solution and it was to redevelop a web server like IIS or Apache. OK, I can’t compare &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6862.wlEmoticon_2D00_smile_5F00_2.png"&gt; I just want a web server which handle GET answers and respond a simple web page. The other challenge I have is to be able to reuse the code I4ve already written for my Sprinkler, to pilot my Lego city and my Lego infrared receiver like my Lego trains…&lt;/p&gt;  &lt;p&gt;So I was searching for code to reuse on the Internet and found some code. So I did a mix of existing code and spend some time testing various solutions &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6862.wlEmoticon_2D00_smile_5F00_2.png"&gt; Most of the existing code is not really robust. It does fail if there is a network problem, if 2 requests arrive at the same time, etc. I’m not saying my code is perfect but it is working and working well for the last month with no problem at all.&lt;/p&gt;  &lt;p&gt;A web server is simple, it’s just a connection on a socket and a protocol of communication which is HTTP. It is also very simple as it is text based. What is interesting is to see all what you can do with such a simple protocol and such a simple markup language like HTML and some javascript.&lt;/p&gt;  &lt;p&gt;OK, so let start with what is necessary: a thread that will run all the time and handle socket requests. So we need also a socket. And a way to stop the thread.&lt;/p&gt;  &lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private bool &lt;/span&gt;cancel = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;
&lt;span style="color: blue;"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Thread &lt;/span&gt;serverThread = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public &lt;/span&gt;WebServer(&lt;span style="color: blue;"&gt;int &lt;/span&gt;port, &lt;span style="color: blue;"&gt;int &lt;/span&gt;timeout)
{
    &lt;span style="color: blue;"&gt;this&lt;/span&gt;.Timeout = timeout;
    &lt;span style="color: blue;"&gt;this&lt;/span&gt;.Port = port;
    &lt;span style="color: blue;"&gt;this&lt;/span&gt;.serverThread = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;(StartServer);
    &lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Print(&lt;span style="color: rgb(163, 21, 21);"&gt;"Web server started on port " &lt;/span&gt;+ port.ToString());
}
&lt;/pre&gt;&lt;p&gt;As you can see, it is quite simple, the WebServer object is initialize with a specific port and a timeout. By default, the http port is 80 but it can be anything. There is no limitation. And as it’s easy to implement, let make the code generic enough to be able to be use with different ports. And a new Thread is created to point on function StartServer. I will detail it later. I will explain also why we need a timeout later.&lt;/p&gt;

&lt;p&gt;Now we have this object initialize, let start the Webserver:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public bool &lt;/span&gt;Start()
{
    &lt;span style="color: blue;"&gt;bool &lt;/span&gt;bStarted = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;
    &lt;span style="color: green;"&gt;// start server           
    &lt;/span&gt;&lt;span style="color: blue;"&gt;try
    &lt;/span&gt;{
        cancel = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;
        serverThread.Start();
        &lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Print(&lt;span style="color: rgb(163, 21, 21);"&gt;"Started server in thread " &lt;/span&gt;+ serverThread.GetHashCode().ToString());
    }
    &lt;span style="color: blue;"&gt;catch
    &lt;/span&gt;{   &lt;span style="color: green;"&gt;//if there is a problem, maybe due to the fact we did not wait engouth
        &lt;/span&gt;cancel = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;
        bStarted = &lt;span style="color: blue;"&gt;false&lt;/span&gt;;
    }
    &lt;span style="color: blue;"&gt;return &lt;/span&gt;bStarted;
}
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;/pre&gt;

&lt;p&gt;That is where the fun being! We start listening and initialize a variable we will use later to stop the server if needed. The catch can contain something to retry to start, here, it just return if it is started or not. At this stage, it should work with no problem as it is only a thread starting. But who knows &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6862.wlEmoticon_2D00_smile_5F00_2.png"&gt;&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private void &lt;/span&gt;StartServer()
{
    &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Socket &lt;/span&gt;server = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Socket&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;AddressFamily&lt;/span&gt;.InterNetwork, &lt;span style="color: rgb(43, 145, 175);"&gt;SocketType&lt;/span&gt;.Stream, &lt;span style="color: rgb(43, 145, 175);"&gt;ProtocolType&lt;/span&gt;.Tcp))
    {
        &lt;span style="color: green;"&gt;//set a receive Timeout to avoid too long connection 
        &lt;/span&gt;server.ReceiveTimeout = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.Timeout;
        server.Bind(&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;IPEndPoint&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;IPAddress&lt;/span&gt;.Any, &lt;span style="color: blue;"&gt;this&lt;/span&gt;.Port));
        server.Listen(&lt;span style="color: blue;"&gt;int&lt;/span&gt;.MaxValue);
        &lt;span style="color: blue;"&gt;while &lt;/span&gt;(!cancel)
        {
            &lt;span style="color: blue;"&gt;try
            &lt;/span&gt;{

                &lt;span style="color: blue;"&gt;using &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Socket &lt;/span&gt;connection = server.Accept())
                {
                    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(connection.Poll(-1, &lt;span style="color: rgb(43, 145, 175);"&gt;SelectMode&lt;/span&gt;.SelectRead))
                    {
                        &lt;span style="color: green;"&gt;// Create buffer and receive raw bytes.
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] bytes = &lt;span style="color: blue;"&gt;new byte&lt;/span&gt;[connection.Available];
                        &lt;span style="color: blue;"&gt;int &lt;/span&gt;count = connection.Receive(bytes);
                        &lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Print(&lt;span style="color: rgb(163, 21, 21);"&gt;"Request received from " &lt;br&gt;&lt;/span&gt;+ connection.RemoteEndPoint.ToString() + &lt;span style="color: rgb(163, 21, 21);"&gt;" at " &lt;/span&gt;+ &lt;span style="color: rgb(43, 145, 175);"&gt;DateTime&lt;/span&gt;.Now.ToString(&lt;span style="color: rgb(163, 21, 21);"&gt;"dd MMM yyyy HH:mm:ss"&lt;/span&gt;));
                        &lt;span style="color: green;"&gt;//stup some time for send timeout as 10s.
                        //necessary to avoid any problem when multiple requests are done the same time.
                        &lt;/span&gt;connection.SendTimeout = &lt;span style="color: blue;"&gt;this&lt;/span&gt;.Timeout; ;
                        &lt;span style="color: green;"&gt;// Convert to string, will include HTTP headers.
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;rawData = &lt;span style="color: blue;"&gt;new string&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.UTF8.GetChars(bytes));
                        &lt;span style="color: blue;"&gt;string &lt;/span&gt;mURI;

                        &lt;span style="color: green;"&gt;// Remove GET + Space
                        // pull out uri and remove the first /
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(rawData.Length &amp;gt; 5)
                        {
                            &lt;span style="color: blue;"&gt;int &lt;/span&gt;uriStart = rawData.IndexOf(&lt;span style="color: rgb(163, 21, 21);"&gt;' '&lt;/span&gt;) + 2;
                            mURI = rawData.Substring(uriStart, rawData.IndexOf(&lt;span style="color: rgb(163, 21, 21);"&gt;' '&lt;/span&gt;, uriStart) - uriStart);
                        }
                        &lt;span style="color: blue;"&gt;else
                            &lt;/span&gt;mURI = &lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;;
                        &lt;span style="color: green;"&gt;// return a simple header
                        &lt;/span&gt;&lt;span style="color: blue;"&gt;string &lt;/span&gt;header = &lt;span style="color: rgb(163, 21, 21);"&gt;"HTTP/1.1 200 OK\r\nContent-Type: text/html&lt;br&gt;; charset=utf-8\r\nConnection: close\r\n\r\n"&lt;/span&gt;;
                        connection.Send(&lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(header), header.Length, &lt;span style="color: rgb(43, 145, 175);"&gt;SocketFlags&lt;/span&gt;.None);
                        &lt;span style="color: blue;"&gt;if &lt;/span&gt;(CommandReceived != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
                            CommandReceived(&lt;span style="color: blue;"&gt;this&lt;/span&gt;, &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebServerEventArgs&lt;/span&gt;(connection, mURI));
                    }
                }
            }
            &lt;span style="color: blue;"&gt;catch &lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;Exception &lt;/span&gt;e)
            {
                &lt;span style="color: green;"&gt;//this may be due to a bad IP address
                &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Print(e.Message);
            }
        }
    }
}
&lt;/pre&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;This function will run all the time in a thread. It’s in an infinite loop which can be break by the cancel variable. First, we need to initialize the Socket. We will use IPv4 with a stream and the TCP protocol. No timeout to receive the request. The, you’ll have to bind this socket to a physical IP address. In our case, we will use all IP address on the port initialized before. Any IP address mean all addresses and in our case only 1 IP address as we do have only 1 Ethernet interface.  We are using '”using” to make sure the server Socket will be closed and cleaned properly after usage.&lt;/p&gt;&lt;p&gt;The way it is working is not too complicated. Remember that we’ve open a Socket named Server, setup it to listen to port 80. This is running in a separate thread in this thread. So in order to analyze the information returned when a connection is accepted (so when a Browser ask for a page), we need to create another Socket pointing to the same Socket, here “using (Socket connection = server.Accept())”. In this case “using” allow the code to clean in the “proper way” when the thread will be finished or then the loop end or when it goes back to the initial loop. It’s thread in thread and if you don’t close things correctly, it can quickly let lots of objects in the memory, objects which will be seen as alive by the garbage collector.&lt;/p&gt;

&lt;p&gt;When there are bytes ready to read with connection.Poll, we just read them. The request is transformed into a string. An http request look like “GET /folder/name.ext?param1=foo&amp;amp;param2=bar HTTP/1.1”. Areal life example looks more like this: "GET /folder/name.ext?param1=foo&amp;amp;param2=bar HTTP/1.1\r\nAccept: text/html, application/xhtml+xml, */*\r\nAccept-Language: fr-FR,fr;q=0.8,en-US;q=0.5,en;q=0.3\r\nUser-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)\r\nAccept-Encoding: gzip, deflate, peerdist\r\nHost: localhost:81\r\nConnection: Keep-Alive\r\nX-P2P-PeerDist: Version=1.1\r\nX-P2P-PeerDistEx: MinContentInformation=1.0, MaxContentInformation=1.0\r\n\r\n"&lt;/p&gt;

&lt;p&gt;For a normal, full web server like IIS or Apache, you’ll analyze all those parameters, and there are lots, see the W3C protocol &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html"&gt;here&lt;/a&gt;. For our usage, the only thing that interest us is the full URL. And it is located between the 2 first spaces. And we will extract the URL, remove the first ‘/’ as I will not use it in the rest of the code.&lt;/p&gt;

&lt;p&gt;Now, the next step is to start answering the request. When someone ask you something, it’s polite to answer &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6862.wlEmoticon_2D00_smile_5F00_2.png"&gt; Like for the request, the response need to have couple of header information. And as my usage is extremely simple, I will always consider that it is OK, I’m only delivering text content and that the connection can be closed. By the way, whatever you put there, HTTP is a disconnected protocol, so you should never consider that you are always connected! It’s an error and can drive you to very bad behaviors.&lt;/p&gt;

&lt;p&gt;connection.Send return the first part of the message and then I call an event to tell the creator of the WebServer object that something happened. I send of course the connection object so that the caller will be able to create an HTML page and answer and also the URL so that it can analyze it. &lt;/p&gt;

&lt;p&gt;Last but not least, the try and catch is extremely important. With Sockets a problem can quickly arrive due to a network problem. And I’ve seen it happening on the netduino for no reason. Just capturing the problem and not doing anything makes the web server working for months! Even if you loose the network, the catch will capture the problem and the server will continue to work up to the point the network will work again. The other reason to use it is because of the timeout. If something happen between the client and our webserver, after the timeout, you’ll get in this catch and you’ll start a new socket and the process will go back to something normal. It can happen and happened to me with very long HTML pages I was generating. When I was interrupting the creation and ask for a new page, the socket went into a kind of infinite loop waiting for a request. There should be a smart way to check is something goes well or not but it’s an easy way. &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public delegate void &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetRequestHandler&lt;/span&gt;(&lt;span style="color: blue;"&gt;object &lt;/span&gt;obj, &lt;span style="color: rgb(43, 145, 175);"&gt;WebServerEventArgs &lt;/span&gt;e);
&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebServerEventArgs&lt;/span&gt;: &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;public &lt;/span&gt;WebServerEventArgs(&lt;span style="color: rgb(43, 145, 175);"&gt;Socket &lt;/span&gt;mresponse, &lt;span style="color: blue;"&gt;string &lt;/span&gt;mrawURL)
    {
        &lt;span style="color: blue;"&gt;this&lt;/span&gt;.response = mresponse;
        &lt;span style="color: blue;"&gt;this&lt;/span&gt;.rawURL = mrawURL;
    }
    &lt;span style="color: blue;"&gt;public &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Socket &lt;/span&gt;response { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;protected set&lt;/span&gt;;  }
    &lt;span style="color: blue;"&gt;public string &lt;/span&gt;rawURL { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;protected set&lt;/span&gt;; }

}

&lt;span style="color: blue;"&gt;public event &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;GetRequestHandler &lt;/span&gt;CommandReceived;
&lt;/pre&gt;

&lt;p&gt;Right after the header is sent back, an event is raised. The arguments are simple here, we do send the Socket object and the URL. If you want to enrich the web server, you can add other elements like the header element rather than sending them right away, the browser requesting the page, the IP address or whatever you want! Again, simple and efficient there.&lt;/p&gt;&lt;p&gt;Last but not least if you need to stop the Server, you’ll need a function to this and also to clean the code at the end:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private bool &lt;/span&gt;Restart()
{
    Stop();
    &lt;span style="color: blue;"&gt;return &lt;/span&gt;Start();
}
&lt;/pre&gt;&lt;/span&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;Stop()
{
    cancel = &lt;span style="color: blue;"&gt;true&lt;/span&gt;;
    &lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;.Sleep(100);
    serverThread.Suspend();
    &lt;span style="color: rgb(43, 145, 175);"&gt;Debug&lt;/span&gt;.Print(&lt;span style="color: rgb(163, 21, 21);"&gt;"Stoped server in thread "&lt;/span&gt;);
}
&lt;/pre&gt;&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;Dispose()
{
    Dispose(&lt;span style="color: blue;"&gt;true&lt;/span&gt;);
    &lt;span style="color: rgb(43, 145, 175);"&gt;GC&lt;/span&gt;.SuppressFinalize(&lt;span style="color: blue;"&gt;this&lt;/span&gt;);
}

&lt;span style="color: blue;"&gt;protected virtual void &lt;/span&gt;Dispose(&lt;span style="color: blue;"&gt;bool &lt;/span&gt;disposing)
{
    &lt;span style="color: blue;"&gt;if &lt;/span&gt;(disposing)
    {
        serverThread = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;
    }
}
&lt;/pre&gt;

&lt;p&gt;Nothing too complex there, it’s just about pausing the thread (remember, there are other tread attached in it), closing the other thread leaving in the Server object and cleaning everything. I hope it’s the good code to let it clean &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6862.wlEmoticon_2D00_smile_5F00_2.png"&gt; But at the end of the day, my only interest is to let this server running all the time. So I don not really care if it will stop correctly! &lt;/p&gt;

&lt;p&gt;Now, to use the server, easy:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebServer &lt;/span&gt;server;
&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Start the HTTP Server
&lt;/span&gt;&lt;pre class="code"&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebServer &lt;/span&gt;server = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;(80, 10000);
server.CommandReceived += &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;GetRequestHandler&lt;/span&gt;(ProcessClientGetRequest);
&lt;span style="color: green;"&gt;// Start the server.
&lt;/span&gt;server.Start();
&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;Declare a static WebServer if you want it to be unique. Technically, you can have multiple servers running on different port. In my case, no need for this. Then, it’s about creating the object, adding an event and starting the server!&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;private static void &lt;/span&gt;ProcessClientGetRequest(&lt;span style="color: blue;"&gt;object &lt;/span&gt;obj, &lt;span style="color: rgb(43, 145, 175);"&gt;WebServer&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;WebServerEventArgs &lt;/span&gt;e)
&lt;/pre&gt;

&lt;p&gt;And you are ready to do some treatment into this function. To return part of the answer, just use e.response.Send as for the header part and you’re done!&lt;/p&gt;

&lt;p&gt;To simplify the process, as it’s a function which you’ll have to call often, I’ve created a function to do this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public static string &lt;/span&gt;OutPutStream(&lt;span style="color: rgb(43, 145, 175);"&gt;Socket &lt;/span&gt;response, &lt;span style="color: blue;"&gt;string &lt;/span&gt;strResponse)
{
    &lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] messageBody = &lt;span style="color: rgb(43, 145, 175);"&gt;Encoding&lt;/span&gt;.UTF8.GetBytes(strResponse);
    response.Send(messageBody, 0, messageBody.Length, &lt;span style="color: rgb(43, 145, 175);"&gt;SocketFlags&lt;/span&gt;.None);
    &lt;span style="color: green;"&gt;//allow time to physically send the bits
    &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Thread&lt;/span&gt;.Sleep(10);
    &lt;span style="color: blue;"&gt;return &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;""&lt;/span&gt;;
}
&lt;/pre&gt;

&lt;p&gt;This can be a function you add in your main code or can add in the Web Server code. &lt;/p&gt;

&lt;p&gt;Now you have a fully functional simple Web Server. You can &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/12/implementing-a-simple-http-server-in-net-microframework.aspx"&gt;read the previous article&lt;/a&gt; on how to handle parameters and analyzing them. The code to manage the parameter is now in the WebServer class. &lt;/p&gt;&lt;p&gt;I’ll post the code in CodePlex so anyone will be able to use it as a helper.&lt;/p&gt;&lt;p&gt;Enjoy this code &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6862.wlEmoticon_2D00_smile_5F00_2.png"&gt; Again, I’m just a marketing director doing some code. And it’s the code running in my sprinkler management system for the last month without any problem! &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10311391" width="1" height="1"&gt;</description></item><item><title>Using a SPI device with netduino and .NET micro framework</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/05/22/using-a-spi-device-with-netduino-and-net-micro-framework.aspx</link><pubDate>Tue, 22 May 2012 18:27:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10309055</guid><dc:creator>Laurelle</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10309055</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/05/22/using-a-spi-device-with-netduino-and-net-micro-framework.aspx#comments</comments><description>&lt;p&gt;After playing with I2C, with various IO, I’ve decided to play a bit with SPI &lt;img class="wlEmoticon wlEmoticon-smile" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/43278.wlEmoticon_2D00_smile_5F00_2.png" /&gt; The real project behind using a SPI device is to be able to use it a a multiplexer/demultipler of IO.&lt;/p&gt;  &lt;p&gt;I want to pilot lights for my Lego train. The idea is to have the ability to switch a signal light to green or red. I want to be able to have 16 lights. The netduino board have only 14 digital IO and 6 analogic. And I’m already using some to pilot the Lego train thru infrared. &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/04/07/using-netduino-and-net-microframework-to-pilot-any-lego-power-function-thru-infrared-part-1.aspx"&gt;See previous article&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;So I have to find a multiplexing solution. And one is used by most of the netduino fan: SPI. In terms of electronic it’s simple, it is a serial to parallel (and vice versa as you can read also). The component the most used for this is &lt;a href="http://www.nxp.com/documents/data_sheet/74HC_HCT595.pdf"&gt;74HC595&lt;/a&gt;. There are couple of basics explanations on the &lt;a href="http://wiki.netduino.com/SPI-Configuration-Example-74HC595.ashx"&gt;netduino wiki&lt;/a&gt; and also good examples as always on &lt;a href="http://highfieldtales.wordpress.com/"&gt;Mario Vernari blog&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Each 74HC595 can have 8 output and they can be chained. Which mean that you can use 2 component to have 16 output. And you basically put them in series. One output of one 74HC595 to the input of the others.&lt;/p&gt;  &lt;p&gt;I spend quite a bit of time to understand how to plug the 74HC595 to the netduino. So here is what to do:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Pin 8 is ground&lt;/li&gt;    &lt;li&gt;Pin 16 is VCC (+5V recommended)&lt;/li&gt;    &lt;li&gt;Pin 14 to netduino pin D11 (called MOSI) for the first 74HC595 of the chain. For the next one it has to be linked to the Pin 9 of the previous 74HC595&lt;/li&gt;    &lt;li&gt;Pin 13 to ground (used to activate or not the 8 output)&lt;/li&gt;    &lt;li&gt;Pin 12 to one output port you’ll choose on the netduino. Let say pin D10. This one is used to select the 74HC595 when you have multiple 74HC595 used in parallel for different purposes (called SS)&lt;/li&gt;    &lt;li&gt;Pin 11 to netduino pin D13 (called SCLK)&lt;/li&gt;    &lt;li&gt;Pin 10 to +VSS (this one is used to reset if needed)&lt;/li&gt;    &lt;li&gt;Pin 15, 1, 2, 3, 4, 5, 6, 7 are the output pin&lt;/li&gt;    &lt;li&gt;Pin 9 has to be linked to the next 74HC595 of the chain if there are more than 1.&lt;/li&gt;  &lt;/ul&gt;  &lt;p&gt;And that’s it for the basics! Now what I want to do with the output is to be able to light one led green or switch it to red. So I’ll use the output of the 74HC595. When it will be 1, I’ll light the green one, when it will be 0, I’ll light the red one. Here is the hardware to schematic:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/7776.image_5F00_2.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/3073.image_5F00_thumb.png" width="417" height="183" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;When the signal will be 1, the current will go thru the green light light. And when it will be 0, the inverter will switch to 1 and the red light will light. It just need 3 cables per light and the current can be transported on long distance. The RS resistor can be adapted with a lower resistor to 100 or something like this in order to send a more intense current in the cable as there will be some loss if it is very long. by the way I recommend to read the excellent article on the length of cable and impact on signal noise from &lt;a href="http://highfieldtales.wordpress.com/2012/04/25/effect-of-long-wiring-on-digital-signals/"&gt;Mario&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Now, in terms of software, It’s not so difficult. In my project, I’m using the SPI for multiple purpose. So I need to use it in a smart way. SPI can be shared and the only thing to do is to change the configuration and write (and read) right after. So I’m using the MultiSPI class from the &lt;a href="http://www.netmftoolbox.com/"&gt;NETMF Toolbox&lt;/a&gt;. It’s an impressive toolbox containing lots of helpers. It goes from hardware to HTTP web client. There is a 74HC595 also but I found it more complex to use that just directly using the MultiSPI class.&lt;/p&gt;  &lt;p&gt;Here the example of class to switch to green (1) or red (0) a specific output of the 2 74HC595:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Signal
&lt;/span&gt;{
    &lt;span style="color: blue;"&gt;private byte &lt;/span&gt;mNumberSignal;
    &lt;span style="color: blue;"&gt;private bool&lt;/span&gt;[] mSignalStatus;
    &lt;span style="color: blue;"&gt;public const byte &lt;/span&gt;NUMBER_SIGNAL_MAX = 16;
    &lt;span style="color: blue;"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MultiSPI &lt;/span&gt;MySignal;

    &lt;span style="color: blue;"&gt;public &lt;/span&gt;Signal(&lt;span style="color: blue;"&gt;byte &lt;/span&gt;NumberOfSignal)
    {
        mNumberSignal = NumberOfSignal;
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;((mNumberSignal &amp;lt;= 0) &amp;amp;&amp;amp; (mNumberSignal &amp;gt; NUMBER_SIGNAL_MAX))
            &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Not correct number of Signals&amp;quot;&lt;/span&gt;);
        mSignalStatus = &lt;span style="color: blue;"&gt;new bool&lt;/span&gt;[mNumberSignal];
        &lt;span style="color: green;"&gt;// open a SPI
        &lt;/span&gt;MySignal = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;MultiSPI&lt;/span&gt;(&lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SPI&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;Configuration&lt;/span&gt;(
            &lt;span style="color: rgb(43, 145, 175);"&gt;Pins&lt;/span&gt;.GPIO_PIN_D10, &lt;span style="color: green;"&gt;// SS-pin
            &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;,             &lt;span style="color: green;"&gt;// SS-pin active state
            &lt;/span&gt;0,                 &lt;span style="color: green;"&gt;// The setup time for the SS port
            &lt;/span&gt;0,                 &lt;span style="color: green;"&gt;// The hold time for the SS port
            &lt;/span&gt;&lt;span style="color: blue;"&gt;false&lt;/span&gt;,              &lt;span style="color: green;"&gt;// The idle state of the clock
            &lt;/span&gt;&lt;span style="color: blue;"&gt;true&lt;/span&gt;,             &lt;span style="color: green;"&gt;// The sampling clock edge
            &lt;/span&gt;1000,              &lt;span style="color: green;"&gt;// The SPI clock rate in KHz
            &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SPI_Devices&lt;/span&gt;.SPI1));   &lt;span style="color: green;"&gt;// The used SPI bus (refers to a MOSI MISO and SCLK pinset)

        //initialise all signals to &amp;quot;false&amp;quot;
        &lt;/span&gt;&lt;span style="color: blue;"&gt;for &lt;/span&gt;(&lt;span style="color: blue;"&gt;byte &lt;/span&gt;i = 0; i &amp;lt; mNumberSignal; i++)
            ChangeSignal(i, &lt;span style="color: blue;"&gt;true&lt;/span&gt;);
    }

    &lt;span style="color: blue;"&gt;public byte &lt;/span&gt;NumberOfSignals
    { &lt;span style="color: blue;"&gt;get &lt;/span&gt;{ &lt;span style="color: blue;"&gt;return &lt;/span&gt;mNumberSignal; } }

    &lt;span style="color: blue;"&gt;public void &lt;/span&gt;ChangeSignal(&lt;span style="color: blue;"&gt;byte &lt;/span&gt;NumSignal, &lt;span style="color: blue;"&gt;bool &lt;/span&gt;value)
    {
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;((NumSignal &amp;lt;= 0) &amp;amp;&amp;amp; (NumSignal &amp;gt; mNumberSignal))
            &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Not correct number of Signals&amp;quot;&lt;/span&gt;);
        &lt;span style="color: green;"&gt;//need to convert to select the right Signal
        &lt;/span&gt;mSignalStatus[NumSignal] = value;
        &lt;span style="color: green;"&gt;// fill the buffer to be sent
        &lt;/span&gt;&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;[] mySign = &lt;span style="color: blue;"&gt;new ushort&lt;/span&gt;[1] { 0 };
        &lt;span style="color: blue;"&gt;for &lt;/span&gt;(&lt;span style="color: blue;"&gt;ushort &lt;/span&gt;i = 0; i &amp;lt; mNumberSignal; i++)
            &lt;span style="color: blue;"&gt;if &lt;/span&gt;(mSignalStatus[i])
                mySign[0] = (&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;)(mySign[0] | (&lt;span style="color: blue;"&gt;ushort&lt;/span&gt;)(1 &amp;lt;&amp;lt; i));
        &lt;span style="color: green;"&gt;//send the bytes
        &lt;/span&gt;MySignal.Write(mySign);
        

    }

    &lt;span style="color: blue;"&gt;public bool &lt;/span&gt;GetSignal(&lt;span style="color: blue;"&gt;byte &lt;/span&gt;NumSignal)
    {
        &lt;span style="color: blue;"&gt;if &lt;/span&gt;((NumSignal &amp;lt;= 0) &amp;amp;&amp;amp; (NumSignal &amp;gt; mNumberSignal))
            &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;Exception&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;&amp;quot;Not correct number of Signals&amp;quot;&lt;/span&gt;);
&lt;span style="color: green;"&gt;        &lt;/span&gt;&lt;span style="color: blue;"&gt;return &lt;/span&gt;mSignalStatus[NumSignal];
    }
}
&lt;/pre&gt;

&lt;p&gt;I just need to declare a MultiSPI. The SPI Configuration contains the necessary information to setup the 74HC595. The SS pin is set to D10. So I can use the SPI for other purposed to pilot my Lego infrared modules. And in the Lego module, I’ll have also to declare the SPI as MultiSPI.&lt;/p&gt;

&lt;p&gt;What the MultiSPI class is doing is very simple. It has a static SPI in its private variables. And a SPI.Configuration which is not static.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: gray;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;Reference to the SPI Device. All MultiSPI &lt;br /&gt;devices use the same SPI class from the NETMF, so this reference is static&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SPI &lt;/span&gt;_SPIDevice;
&lt;span style="color: gray;"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;SPI Configuration. Different for each device, so not a static reference&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;
&lt;/span&gt;&lt;span style="color: blue;"&gt;private &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SPI&lt;/span&gt;.&lt;span style="color: rgb(43, 145, 175);"&gt;Configuration &lt;/span&gt;_Configuration;
&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: green;"&gt;// Store the configuration file for each MultiSPI instance&lt;br /&gt;Sets the configuration in a local value
&lt;/span&gt;&lt;span style="color: blue;"&gt;this&lt;/span&gt;._Configuration = config;

&lt;span style="color: green;"&gt;// If no SPI Device exists yet, we create it's first instance
&lt;/span&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;(_SPIDevice == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)
{
    &lt;span style="color: green;"&gt;// Creates the SPI Device only 1 time as it is a static!
    &lt;/span&gt;_SPIDevice = &lt;span style="color: blue;"&gt;new &lt;/span&gt;&lt;span style="color: rgb(43, 145, 175);"&gt;SPI&lt;/span&gt;(&lt;span style="color: blue;"&gt;this&lt;/span&gt;._Configuration);
}
&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;public void &lt;/span&gt;Write(&lt;span style="color: blue;"&gt;byte&lt;/span&gt;[] WriteBuffer)
{
    _SPIDevice.Config = &lt;span style="color: blue;"&gt;this&lt;/span&gt;._Configuration;
    _SPIDevice.Write(WriteBuffer);
}
&lt;/pre&gt;

&lt;p&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So each time a program want to write in a SPI, the right configuration is selected and the write command is send. It’s working like this to read. The full class is a bit more complex but the main principle is there.&lt;/p&gt;

&lt;p&gt;The rest of my code is very simple and I’m sure I would have been to write it in a better way. I’m just storing the state of a signal and then output all the buffer to the SPI. &lt;/p&gt;

&lt;p&gt;So bottom line, it is very easy to use SPI as a multiplexer/demultipler for IO. That was what I needed. I did not tested yet how to read the data but it should be as simple!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10309055" width="1" height="1"&gt;</description></item><item><title>Managing my Sprinklers from the Cloud</title><link>http://blogs.msdn.com/b/laurelle/archive/2012/05/07/managing-my-sprinklers-from-the-cloud.aspx</link><pubDate>Mon, 07 May 2012 06:32:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10301697</guid><dc:creator>Laurelle</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://blogs.msdn.com/b/laurelle/rsscomments.aspx?WeblogPostID=10301697</wfw:commentRss><comments>http://blogs.msdn.com/b/laurelle/archive/2012/05/07/managing-my-sprinklers-from-the-cloud.aspx#comments</comments><description>&lt;p&gt;I’ve started a project to pilot my sprinklers remotely and make them smart almost &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/09/netduino-board-geek-tool-for-net-microframework.aspx"&gt;9 months ago&lt;/a&gt;. The idea is to be able to remotely open and close my sprinklers but also be able to launch automatic sprinkling cycles. &lt;/p&gt;  &lt;p&gt;After some software and hardware development, I now have a fully working solution:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/4721.WP_5F00_000611_5F00_2.jpg"&gt;&lt;img title="WP_000611" style="display: inline; background-image: none;" border="0" alt="WP_000611" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/8372.WP_5F00_000611_5F00_thumb.jpg" width="543" height="408" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You can see the netduino .NET Microframework board where all the smart is happening. I wrote couple of articles already to show &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/12/implementing-a-simple-http-server-in-net-microframework.aspx"&gt;how to implement a web server&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/27/program-a-date-time-and-duration-for-a-sprinkler-in-net-microframework-and-netduino.aspx"&gt;launch timers&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/20/displaying-a-calendar-in-a-web-page-using-net-microframework.aspx"&gt;create calendars&lt;/a&gt;, &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2011/09/13/setup-a-time-and-date-using-net-microframework.aspx"&gt;get the date and time&lt;/a&gt;. I’ve also wrote couple of articles on hardware. &lt;/p&gt;  &lt;p&gt;Recently, I’ve met with Alain, a Lego fan like me and I show him the way I can control any Lego Power Function element using .NET Microframework. And we discussed about my sprinkler project. I was about to implement the final solution with my existing 9V Gardena electro valves. And he challenged ma on this telling me that I will never know the status of the electro valve if I hacked the Gardena system. And he tell me to go for a 24V DC electro valve. The big advantage is that when you switch on the current, it open the valve, when it’s off, it close. So no unknown state like with the bistable&amp;#160; valve from Gardena. So I bought 3 24V DC electro valve for 22€ each plus couple of cents for the diode (never forget to add a return current diode on a solenoid &lt;img class="wlEmoticon wlEmoticon-smile" style="style" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6886.wlEmoticon_2D00_smile_5F00_2.png" /&gt;).&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6886.WP_5F00_000612_5F00_2.jpg"&gt;&lt;img title="WP_000612" style="display: inline; background-image: none;" border="0" alt="WP_000612" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/5807.WP_5F00_000612_5F00_thumb.jpg" width="544" height="409" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;And the display screen with the button you see is a Crouzet automate. It’s a gift from Alain and it’s really cool to have it. For my project, it can be replaced by 3 buttons, 3 leds and 3 transistors but it’s just more cool &lt;img class="wlEmoticon wlEmoticon-smile" style="style" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6886.wlEmoticon_2D00_smile_5F00_2.png" /&gt; The netduino enter in the DC input section and when the voltage is higher than 2V (normal high level is 3.3V), it just switch on an electro valve. So nothing complicated there.&lt;/p&gt;  &lt;p&gt;What has been long is to bring electricity when I wanted this box to be plus networking plus bring cables to all the places where I have electro valves. And as I have in front and rear of the house, I have lots of cables to install nicely in the basement. And just this took 1 full day with a friend who helped me. Thanks Thomas by the way &lt;img class="wlEmoticon wlEmoticon-smile" style="style" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6886.wlEmoticon_2D00_smile_5F00_2.png" /&gt;&lt;/p&gt;  &lt;p&gt;And now from my Windows Phone or a PC or any other device which support HTML, I can open, close and program my sprinklers:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/4743.image_5F00_2.png"&gt;&lt;img title="image" style="display: inline; background-image: none;" border="0" alt="image" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/5314.image_5F00_thumb.png" width="228" height="280" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I’m an happy geek &lt;img class="wlEmoticon wlEmoticon-smile" style="style" alt="Sourire" src="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-65-39-metablogapi/6886.wlEmoticon_2D00_smile_5F00_2.png" /&gt; And as there are always some improvement to do, next step is to add a &lt;a href="http://blogs.msdn.com/b/laurelle/archive/2012/02/21/using-one-temperature-sensor-with-i2c-protocol-and-net-micro-framework-on-netduino-board.aspx"&gt;I2C temperature sensor&lt;/a&gt; to get information on the temperature outside and implement automatic rules.&lt;/p&gt;  &lt;p&gt;Stay tune!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10301697" width="1" height="1"&gt;</description></item></channel></rss>