<feed xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns="http://www.w3.org/2005/Atom" xml:lang="de-DE">
    <title>Zak McKracken's blog</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/ZakMcKracken/Atom.aspx" />
    <subtitle type="html">Home automation, retro stuff and .NET</subtitle>
    <id>http://geekswithblogs.net/ZakMcKracken/Default.aspx</id>
    <author>
        <name>Zak McKracken</name>
        <uri>http://geekswithblogs.net/ZakMcKracken/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2012-08-01T11:25:00Z</updated>
    <entry>
        <title>Weather station with .NET - Part 1</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ZakMcKracken/archive/2012/08/01/weather-station-with-.net---part-1.aspx" />
        <id>http://geekswithblogs.net/ZakMcKracken/archive/2012/08/01/weather-station-with-.net---part-1.aspx</id>
        <published>2012-08-01T11:25:0002:00:00</published>
        <updated>2012-08-01T11:25:00Z</updated>
        <content type="html">&lt;p&gt;I’ve always been a fan of those little ARM-boards that can be programmed with C#.&lt;/p&gt;  &lt;p&gt;So far, I was playing around with a &lt;a href="http://netduino.com/"&gt;Netduino-board&lt;/a&gt; in my office. I use it for automatic testing my software where I need to simulate user action like pressing a (physical) button on a device every minute. This is a very simple task which does not use the potential of this board.&lt;/p&gt;  &lt;p&gt;So, for my house automation I use a function, which I call “shield”: The shield function will lower or raise the blinds in certain rooms if temperature rises to high. I use this in the rooms to the south and to the west. It works noticeable and is cheaper than a climate control. This is very interesting to see and guests react sometimes strange, when the house does this on its own &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smiley" src="http://gwb.blob.core.windows.net/zakmckracken/Windows-Live-Writer/da16a2d6bdf7_B121/wlEmoticon-smile_2.png" /&gt;. &lt;/p&gt;  &lt;p&gt;To improve this function, I need some extra sensors. A good job for a FEZ Panda II board with an Ethernet shield! I use this board, because I ordered it for a project I didn’t realize some time ago. &lt;/p&gt;  &lt;p&gt;I want to know the brightness outside, because if the temperature in the room is high, but there is no sun, lowering the blinds will not help &lt;img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-winkingsmile" alt="Zwinkerndes Smiley" src="http://gwb.blob.core.windows.net/zakmckracken/Windows-Live-Writer/da16a2d6bdf7_B121/wlEmoticon-winkingsmile_2.png" /&gt;. &lt;/p&gt;  &lt;p&gt;Additionally I want to know the temperature on the south side of the house and (probably) expand it with a wind meter or a rain gauge in the future.&lt;/p&gt;  &lt;p&gt;So I ordered two sensors:&lt;/p&gt;  &lt;p&gt;- &lt;a href="http://www.adafruit.com/products/439"&gt;Adafruit TSl2561&lt;/a&gt; lux sensor&lt;/p&gt;  &lt;p&gt;- &lt;a href="http://www.ghielectronics.com/catalog/product/321"&gt;GHI Barometer module&lt;/a&gt; from .NET Gadgeteer platform&lt;/p&gt;  &lt;p&gt;Both sensors use the I2C bus. The barometer module from GHI is intended for use with the .NET Gadgeteer platform and uses an “I”-type socket. This is actually a standardized header, but technically it’s also an I2C bus. So I cut the cable and rewired it to the FEZ Panda shield. This works without any problems so far, but of course you can’t use the GHI libraries for the sensor. You need to build your own.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;I will provide the source code, when the project works (for now, the lux meter does not work).&lt;/strong&gt;&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;&lt;strong&gt;Step 0: Connect all things together&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I first connect the shield to the base FEZ Panda board (of course). For power input, I use the &lt;strong&gt;Vin&lt;/strong&gt; pin which is also available on the shield. &lt;/p&gt;  &lt;p&gt;The barometer module needs 3.3V, so do not connect it to 5V! Now I connect the SCL and SDA lines, which are available at Pin &lt;strong&gt;GPIO_Pin3&lt;/strong&gt; (SCL) and &lt;strong&gt;GPIO_Pin2&lt;/strong&gt; (SDA). In addition, one more pin needs to be connected and set to high in order to read the values from the module. I use &lt;strong&gt;GPIO_Pin5&lt;/strong&gt; for this.&lt;/p&gt;  &lt;p&gt;The communication over the I2C bus can be completely done over the Microsoft.SPOT.Hardware.I2CDevice class.&lt;/p&gt;  &lt;hr /&gt;  &lt;p&gt;&lt;strong&gt;Step 1: Build up some communication for the web server&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The Class Webserver is based on GHI’s Webserver class that will work with the Wiznet W5100 Ethernet libraries. I cut a lot of things out that I do not need (file access on SD card, file upload functions).&lt;/p&gt;  &lt;p&gt;Basically, the class sets up a web server and listens. If there’s a request, data is collected (only then, why should I collect the data, when nobody wants it and I do not want to store it on the device). Of course, you could change that behavior and poll data in a defined interval and store it on a SD card, if you want.&lt;/p&gt;  &lt;p&gt;I need three different answers from the web server:&lt;/p&gt;  &lt;p&gt;1. Answer for &lt;a href="http://{FEZ_IP_ADDRESS}"&gt;http://{FEZ_IP_ADDRESS}&lt;/a&gt;: This will bring up a HTML page with the measured data&lt;/p&gt;  &lt;p&gt;2. Answer for &lt;a href="http://{FEZ_IP_ADDRESS}/data.xml"&gt;http://{FEZ_IP_ADDRESS}/data.xml&lt;/a&gt;: This is used by my server software. Data is presented on a XML page that can be used without big parsing.&lt;/p&gt;  &lt;p&gt;3. Answer for all other calls: There’s a small 404 page (just for fun)&lt;/p&gt;  &lt;p&gt;The class looks like this:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.SPOT;
&lt;span class="kwrd"&gt;using&lt;/span&gt; GHIElectronics.NETMF.Net;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Threading;
&lt;span class="kwrd"&gt;using&lt;/span&gt; GHIElectronics.NETMF.FEZ;
&lt;span class="kwrd"&gt;using&lt;/span&gt; System;

&lt;span class="kwrd"&gt;namespace&lt;/span&gt; FezWeather
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyHttpServer
    {
        &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; StartServer()
        {
            Debug.Print(&lt;span class="str"&gt;"Starting HttpServer..."&lt;/span&gt;);
            Thread httpThread = &lt;span class="kwrd"&gt;new&lt;/span&gt; Thread((&lt;span class="kwrd"&gt;new&lt;/span&gt; PrefixKeeper(&lt;span class="str"&gt;"http"&lt;/span&gt;)).RunServerDelegate);
            httpThread.Start();
        }

        &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
        &lt;span class="rem"&gt;/// All this class does is keeps the prefix and provides &lt;/span&gt;
        &lt;span class="rem"&gt;/// RunServerDelegate to run in separate thread.&lt;/span&gt;
        &lt;span class="rem"&gt;/// RunServerDelegate calls HttpServerApp.RunServer with saved prefix.&lt;/span&gt;
        &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span class="kwrd"&gt;class&lt;/span&gt; PrefixKeeper
        {
            &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
            &lt;span class="rem"&gt;/// Keeps the prefix to start server.&lt;/span&gt;
            &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; m_prefix;

            &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
            &lt;span class="rem"&gt;/// Saves the prefix&lt;/span&gt;
            &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
            &lt;span class="rem"&gt;/// &amp;lt;param name="prefix"&amp;gt;Prefix&amp;lt;/param&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;internal&lt;/span&gt; PrefixKeeper(&lt;span class="kwrd"&gt;string&lt;/span&gt; prefix)
            {
                m_prefix = prefix;
            }

            &lt;span class="rem"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
            &lt;span class="rem"&gt;/// Delegate to run server in separate thread.&lt;/span&gt;
            &lt;span class="rem"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
            &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RunServerDelegate()
            {
                MyHttpServer.RunServer(m_prefix);
            }
        }

        &lt;span class="kwrd"&gt;internal&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; RunServer(&lt;span class="kwrd"&gt;string&lt;/span&gt; prefix)
        {
            HttpListener listener = &lt;span class="kwrd"&gt;new&lt;/span&gt; HttpListener(prefix, -1);

            listener.Start();
            Debug.Print(&lt;span class="str"&gt;"---o0o--- HttpListener is up and running ---o0o---"&lt;/span&gt;);

            &lt;span class="kwrd"&gt;while&lt;/span&gt; (&lt;span class="kwrd"&gt;true&lt;/span&gt;)
            {
                HttpListenerContext context = &lt;span class="kwrd"&gt;null&lt;/span&gt;;

                &lt;span class="kwrd"&gt;try&lt;/span&gt;
                {
                    context = listener.GetContext();
                    HttpListenerRequest request = context.Request;
                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (request.HttpMethod.ToUpper() == &lt;span class="str"&gt;"GET"&lt;/span&gt;)
                    {
                        Program.LEDAccess.StartBlinking(25, 25);

                        Program.GetAllData();
                        ProcessClientGetRequest(context);

                        Program.LEDAccess.StopBlinking();
                        Program.LEDAccess.ShutOff();
                    }
                    
                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (context != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
                    {
                        context.Close();
                        context = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
                    }
                }
                &lt;span class="kwrd"&gt;catch&lt;/span&gt;
                {
                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (context != &lt;span class="kwrd"&gt;null&lt;/span&gt;)
                    {
                        context.Close();
                        context = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
                    }
                }
            }
        }

        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; ProcessClientGetRequest(HttpListenerContext context)
        {
            HttpListenerResponse response = context.Response;

            Debug.Print(&lt;span class="str"&gt;"Query from IP: "&lt;/span&gt; + context.Request.RemoteEndPoint.Address.ToString() + &lt;span class="str"&gt;" @ "&lt;/span&gt; + DateTime.Now.ToString());
            
            response.StatusCode = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)HttpStatusCode.OK;

            &lt;span class="kwrd"&gt;string&lt;/span&gt; strResp = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Empty;

            &lt;span class="kwrd"&gt;switch&lt;/span&gt; (context.Request.RawUrl)
            {
                &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;"/data.xml"&lt;/span&gt;:
                    strResp = &lt;span class="str"&gt;"&amp;lt;?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?&amp;gt;\r\n"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;weather&amp;gt;\r\n"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;temperature&amp;gt;"&lt;/span&gt; + Program.Temperature + &lt;span class="str"&gt;"&amp;lt;/temperature&amp;gt;\r\n"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;brightness&amp;gt;"&lt;/span&gt; + Program.Brightness + &lt;span class="str"&gt;"&amp;lt;/brightness&amp;gt;\r\n"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;pressure&amp;gt;"&lt;/span&gt; + Program.Pressure + &lt;span class="str"&gt;"&amp;lt;/pressure&amp;gt;\r\n&amp;lt;/weather&amp;gt;\r\n"&lt;/span&gt;;

                    response.ContentType = &lt;span class="str"&gt;"application/xml"&lt;/span&gt;;
                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;

                &lt;span class="kwrd"&gt;case&lt;/span&gt; &lt;span class="str"&gt;"/"&lt;/span&gt;:
                    strResp = &lt;span class="str"&gt;"&amp;lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"&amp;gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;FEZWeather station&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;body style=\"background-color:#007CE2; font-family:Courier; color:White\"&amp;gt;    &amp;lt;h1&amp;gt;FEZ current weather values&amp;lt;/h1&amp;gt;&amp;lt;h3&amp;gt;Temperature:"&lt;/span&gt; + Program.Temperature + &lt;span class="str"&gt;" &amp;amp;deg;C&amp;lt;/h3&amp;gt;&amp;lt;h3&amp;gt;Pressure:"&lt;/span&gt; + Program.Pressure + &lt;span class="str"&gt;" hPa&amp;lt;/h3&amp;gt;&amp;lt;h3&amp;gt;Brightness:"&lt;/span&gt; + Program.Brightness + &lt;span class="str"&gt;" lux&amp;lt;/h3&amp;gt;"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;Data in XML-format? Look here: &amp;lt;a href=\"data.xml\"&amp;gt;data.xml&amp;lt;/a&amp;gt;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;"&lt;/span&gt;;

                    response.ContentType = &lt;span class="str"&gt;"text/html"&lt;/span&gt;;
                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;

                &lt;span class="kwrd"&gt;default&lt;/span&gt;:
                    strResp = &lt;span class="str"&gt;"&amp;lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"&amp;gt;&amp;lt;html&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;FEZWeather station&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;"&lt;/span&gt;;
                    strResp += &lt;span class="str"&gt;"&amp;lt;body style=\"background-color:#007CE2; font-family:Courier; color:White\"&amp;gt;    &amp;lt;h1&amp;gt;Not found&amp;lt;/h1&amp;gt;    &amp;lt;h3&amp;gt;sorry...&amp;lt;/h3&amp;gt;    &amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;"&lt;/span&gt;;

                    response.ContentType = &lt;span class="str"&gt;"text/html"&lt;/span&gt;;
                    &lt;span class="kwrd"&gt;break&lt;/span&gt;;

            }

            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] messageBody = Encoding.UTF8.GetBytes(strResp);
            response.OutputStream.Write(messageBody, 0, messageBody.Length);
        }
    }
}&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[






.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;To use this in my main program, I declare some static byte[] arrays to store the network information:

&lt;pre class="csharpcode"&gt;  &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] IP = { 192, 168, 0, 219 };                    &lt;span class="rem"&gt;//  IP address of FEZ Panda II ethernet shield&lt;/span&gt;
  &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] GW = { 192, 168, 0, 1 };                      &lt;span class="rem"&gt;//  Your gateway address&lt;/span&gt;
  &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] SUB = { 255, 255, 255, 0 };                   &lt;span class="rem"&gt;//  Your subnet mask&lt;/span&gt;
  &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] DNS = { 192, 168, 0, 1 };                     &lt;span class="rem"&gt;//  Your DNS&lt;/span&gt;
  &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] MAC = { 0x00, 0x26, 0x1C, 0x7B, 0x29, 0xE8 }; //  Your MAC address&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[






.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Although you could use DHCP, I decided to supply a manual IP address.. So, in Main() I call:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di9, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
NetworkInterface.EnableStaticIP(IP, SUB, GW, MAC);
NetworkInterface.EnableStaticDns(DNS);

MyHttpServer.StartServer();&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[






.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;That’s all to do for the web server part. The web server will query the modules if there’s a request and supply it to the user. &lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Talking to the barometer module (as this seems to be easier)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First of all, I need some objects for I2C communication. I use the built-in methods, which makes it very easy to access the bus.&lt;/p&gt;

&lt;p&gt;Most of the code is based in the Gadgeteer sample code found here: &lt;a title="http://gadgeteer.codeplex.com/SourceControl/changeset/view/19905#157772" href="http://gadgeteer.codeplex.com/SourceControl/changeset/view/19905#157772"&gt;http://gadgeteer.codeplex.com/SourceControl/changeset/view/19905#157772&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The barometer module has two addresses: One for the eeprom, where some coefficients are stored, the other is the module itself, where I can obtain the values.&lt;/p&gt;

&lt;p&gt;So, first some declarations:&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;string&lt;/span&gt; Temperature = &lt;span class="str"&gt;"0.0"&lt;/span&gt;;                   &lt;span class="rem"&gt;//  Temperature value in °C used for XML output&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Brightness = &lt;span class="str"&gt;"1.0"&lt;/span&gt;;                    &lt;span class="rem"&gt;//  LUX value used for XML output&lt;/span&gt;
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; Pressure = &lt;span class="str"&gt;"2.0"&lt;/span&gt;;                      &lt;span class="rem"&gt;//  Pressure value in hPa used for XML output&lt;/span&gt;

&lt;span class="kwrd"&gt;static&lt;/span&gt; I2CDevice FEZ_I2C;                                   &lt;span class="rem"&gt;//  I2C object&lt;/span&gt;
&lt;span class="kwrd"&gt;static&lt;/span&gt; OutputPort XCLR;                                     &lt;span class="rem"&gt;//  XCLR line. Needed to be set HIGH when reading temp/pressure&lt;/span&gt;

&lt;span class="kwrd"&gt;static&lt;/span&gt; I2CDevice.Configuration conDevBARO;              &lt;span class="rem"&gt;//  Config for barometer module&lt;/span&gt;
&lt;span class="kwrd"&gt;static&lt;/span&gt; I2CDevice.Configuration conDevBAROCfg;           &lt;span class="rem"&gt;//  Config for barometer module eeprom&lt;/span&gt;

&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt; BARO_ADDRESS = 0x77;                        &lt;span class="rem"&gt;//  Address of barometer&lt;/span&gt;
&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt; BARO_EEPROM = 0x50;                         &lt;span class="rem"&gt;//  Address of barometer eeprom&lt;/span&gt;
            
&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] _readBuffer144 = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[18];    &lt;span class="rem"&gt;//  Byte Array for storing config loaded from eeprom&lt;/span&gt;
&lt;span class="kwrd"&gt;static&lt;/span&gt; Coefficients Coeff;                      &lt;span class="rem"&gt;//  Coeffients for calulation (loaded from eeprom)&lt;/span&gt;

&lt;span class="kwrd"&gt;struct&lt;/span&gt; Coefficients
{
     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; C1, C2, C3, C4, C5, C6, C7;
     &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; A, B, C, D;
 }&lt;/pre&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;In Main() I need to read the coefficients from the eeprom first, so I open address 0x50 and create some transaction to get these values. I need them later for calculation. &lt;/p&gt;

&lt;pre class="csharpcode"&gt;Coeff = &lt;span class="kwrd"&gt;new&lt;/span&gt; Coefficients();

&lt;span class="rem"&gt;// Setup barometer sensor&lt;/span&gt;
XCLR = &lt;span class="kwrd"&gt;new&lt;/span&gt; OutputPort(Cpu.Pin.GPIO_Pin5, &lt;span class="kwrd"&gt;false&lt;/span&gt;);
conDevBAROCfg = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.Configuration(BARO_EEPROM, 400);
conDevBARO = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.Configuration(BARO_ADDRESS, 400);
FEZ_I2C = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice(conDevBAROCfg);

I2CDevice.I2CTransaction[] xActionsGetBaroCfg = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.I2CTransaction[2];
&lt;span class="kwrd"&gt;byte&lt;/span&gt;[] CallRegister = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[1] { 0x10 };
xActionsGetBaroCfg[0] = I2CDevice.CreateWriteTransaction(CallRegister);
xActionsGetBaroCfg[1] = I2CDevice.CreateReadTransaction(_readBuffer144);

FEZ_I2C.Execute(xActionsGetBaroCfg, 1000);

Coeff.C1 = (_readBuffer144[0] &amp;lt;&amp;lt; 8) + _readBuffer144[1];
Coeff.C2 = (_readBuffer144[2] &amp;lt;&amp;lt; 8) + _readBuffer144[3];
Coeff.C3 = (_readBuffer144[4] &amp;lt;&amp;lt; 8) + _readBuffer144[5];
Coeff.C4 = (_readBuffer144[6] &amp;lt;&amp;lt; 8) + _readBuffer144[7];
Coeff.C5 = (_readBuffer144[8] &amp;lt;&amp;lt; 8) + _readBuffer144[9];
Coeff.C6 = (_readBuffer144[10] &amp;lt;&amp;lt; 8) + _readBuffer144[11];
Coeff.C7 = (_readBuffer144[12] &amp;lt;&amp;lt; 8) + _readBuffer144[13];
Coeff.A = _readBuffer144[14];
Coeff.B = _readBuffer144[15];
Coeff.C = _readBuffer144[16];
Coeff.D = _readBuffer144[17];

conDevBAROCfg = &lt;span class="kwrd"&gt;null&lt;/span&gt;;
FEZ_I2C.Dispose();&lt;/pre&gt;

&lt;pre class="csharpcode"&gt; &lt;/pre&gt;

&lt;pre class="csharpcode"&gt; &lt;/pre&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;&lt;strong&gt;Remarks: Always set the Configuration objects to null and dispose the FEZ_I2C object, otherwise you can’t talk to another I2C module!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For getting the temperature and pressure value, I need to connect to address 0x77.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; GetDataTemp()
        {
            &lt;span class="kwrd"&gt;double&lt;/span&gt; dUT, OFF, SENS, X;
            &lt;span class="kwrd"&gt;double&lt;/span&gt; P, T;
            &lt;span class="kwrd"&gt;int&lt;/span&gt; D1, D2;

            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] _readBuffer16 = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[2];
            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] ReadByte = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[1] { 0xFD };

            XCLR.Write(&lt;span class="kwrd"&gt;true&lt;/span&gt;);

            &lt;span class="rem"&gt;// Get raw pressure value&lt;/span&gt;
            I2CDevice.I2CTransaction[] xWeatherAction = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.I2CTransaction[1];
            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] GetPressure = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[2] { 0xFF, 0xF0 };
            xWeatherAction[0] = I2CDevice.CreateWriteTransaction(GetPressure);
            FEZ_I2C.Execute(xWeatherAction, 1000);
            Thread.Sleep(50);

            xWeatherAction = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.I2CTransaction[2];
            xWeatherAction[0] = I2CDevice.CreateWriteTransaction(ReadByte);
            xWeatherAction[1] = I2CDevice.CreateReadTransaction(_readBuffer16);
            
            FEZ_I2C.Execute(xWeatherAction, 1000);

            D1 = (_readBuffer16[0] &amp;lt;&amp;lt; 8) | _readBuffer16[1];

            Debug.Print(&lt;span class="str"&gt;"RAW D1="&lt;/span&gt; + D1.ToString());

            &lt;span class="rem"&gt;// Get raw temperature value&lt;/span&gt;
            _readBuffer16 = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[2];
            &lt;span class="rem"&gt;//ReadByte = new byte[1] { 0xF0 };&lt;/span&gt;
            xWeatherAction = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.I2CTransaction[1];
            &lt;span class="kwrd"&gt;byte&lt;/span&gt;[] GetTemp = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;byte&lt;/span&gt;[2] { 0xFF, 0xE8 };
            xWeatherAction[0] = I2CDevice.CreateWriteTransaction(GetTemp);
            FEZ_I2C.Execute(xWeatherAction, 1000);
            Thread.Sleep(50);

            xWeatherAction = &lt;span class="kwrd"&gt;new&lt;/span&gt; I2CDevice.I2CTransaction[2];
            xWeatherAction[0] = I2CDevice.CreateWriteTransaction(ReadByte);
            xWeatherAction[1] = I2CDevice.CreateReadTransaction(_readBuffer16);

            FEZ_I2C.Execute(xWeatherAction, 1000);

            D2 = (_readBuffer16[0] &amp;lt;&amp;lt; 8) | _readBuffer16[1];
            Debug.Print(&lt;span class="str"&gt;"RAW D2="&lt;/span&gt; + D2.ToString());
            XCLR.Write(&lt;span class="kwrd"&gt;false&lt;/span&gt;);


            &lt;span class="rem"&gt;//////////////////////////////////////////////////////////////&lt;/span&gt;
            &lt;span class="rem"&gt;// Calculate temperature and pressure based on calibration data&lt;/span&gt;
            &lt;span class="rem"&gt;////////////////////////////////////////////////////////////////&lt;/span&gt;

            &lt;span class="rem"&gt;// Step 1. Get temperature value.&lt;/span&gt;

            &lt;span class="rem"&gt;// D2 &amp;gt;= C5 dUT= D2-C5 - ((D2-C5)/2^7) * ((D2-C5)/2^7) * A / 2^C&lt;/span&gt;
            &lt;span class="kwrd"&gt;if&lt;/span&gt; (D2 &amp;gt;= Coeff.C5)
            {
                dUT = D2 - Coeff.C5 - ((D2 - Coeff.C5) / System.Math.Pow(2, 7) * ((D2 - Coeff.C5) / System.Math.Pow(2, 7)) * Coeff.A / System.Math.Pow(2, Coeff.C));
            }
            &lt;span class="rem"&gt;// D2 &amp;lt;  C5 dUT= D2-C5 - ((D2-C5)/2^7) * ((D2-C5)/2^7) * B / 2^C&lt;/span&gt;
            &lt;span class="kwrd"&gt;else&lt;/span&gt;
            {
                dUT = D2 - Coeff.C5 - ((D2 - Coeff.C5) / System.Math.Pow(2, 7) * ((D2 - Coeff.C5) / System.Math.Pow(2, 7)) * Coeff.B / System.Math.Pow(2, Coeff.C));
            }

            &lt;span class="rem"&gt;// Step 2. Calculate offset, sensitivity and final pressure value.&lt;/span&gt;

            &lt;span class="rem"&gt;// OFF=(C2+(C4-1024)*dUT/2^14)*4&lt;/span&gt;
            OFF = (Coeff.C2 + (Coeff.C4 - 1024) * dUT / System.Math.Pow(2, 14)) * 4;
            &lt;span class="rem"&gt;// SENS = C1+ C3*dUT/2^10&lt;/span&gt;
            SENS = Coeff.C1 + Coeff.C3 * dUT / System.Math.Pow(2, 10);
            &lt;span class="rem"&gt;// X= SENS * (D1-7168)/2^14 - OFF&lt;/span&gt;
            X = SENS * (D1 - 7168) / System.Math.Pow(2, 14) - OFF;
            &lt;span class="rem"&gt;// P=X*10/2^5+C7&lt;/span&gt;
            P = X * 10 / System.Math.Pow(2, 5) + Coeff.C7;

            &lt;span class="rem"&gt;// Step 3. Calculate temperature&lt;/span&gt;

            &lt;span class="rem"&gt;// T = 250 + dUT * C6 / 2 ^ 16-dUT/2^D&lt;/span&gt;
            T = 250 + dUT * Coeff.C6 / System.Math.Pow(2, 16) - dUT / System.Math.Pow(2, Coeff.D);

            &lt;span class="kwrd"&gt;double&lt;/span&gt; Temp = T / 10;
            &lt;span class="kwrd"&gt;int&lt;/span&gt; Press = (&lt;span class="kwrd"&gt;int&lt;/span&gt;)P / 10;
            
            Debug.Print(Temp.ToString(&lt;span class="str"&gt;"F1"&lt;/span&gt;) + &lt;span class="str"&gt;" °C"&lt;/span&gt;);
            Debug.Print(Press.ToString() + &lt;span class="str"&gt;" hPa"&lt;/span&gt;);

            Temperature = Temp.ToString(&lt;span class="str"&gt;"F1"&lt;/span&gt;);
            Pressure = Press.ToString();
        }&lt;/pre&gt;

&lt;pre class="csharpcode"&gt; &lt;/pre&gt;

&lt;pre class="csharpcode"&gt;&lt;font color="#444444" face="Tahoma"&gt;This stores the measured values in the static strings declared above. The method is called by the web server to get the current values.&lt;/font&gt;&lt;/pre&gt;&lt;img src="http://geekswithblogs.net/ZakMcKracken/aggbug/150350.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ZakMcKracken/comments/150350.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ZakMcKracken/comments/commentRss/150350.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ZakMcKracken/services/trackbacks/150350.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Let's get started</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/ZakMcKracken/archive/2011/10/27/lets-get-started.aspx" />
        <id>http://geekswithblogs.net/ZakMcKracken/archive/2011/10/27/lets-get-started.aspx</id>
        <published>2011-10-27T15:35:4202:00:00</published>
        <updated>2011-10-27T15:35:42Z</updated>
        <content type="html">&lt;p&gt;Thank's for invitation, here I am :-)&lt;/p&gt;&lt;img src="http://geekswithblogs.net/ZakMcKracken/aggbug/147471.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/ZakMcKracken/comments/147471.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/ZakMcKracken/comments/commentRss/147471.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/ZakMcKracken/services/trackbacks/147471.aspx</trackback:ping>
    </entry>
</feed>