Sample Debug Output:
11/11/2012 19:03:25 Temp Inside: 67.8 Temp Outside: 44.4
11/11/2012 19:03:35 Temp Inside: 67.9 Temp Outside: 44.4
11/11/2012 19:03:45 Temp Inside: 67.9 Temp Outside: 44.5
11/11/2012 19:03:55 Temp Inside: 67.9 Temp Outside: 44.5
11/11/2012 19:04:05 Temp Inside: 67.9 Temp Outside: 44.5
public class Program { static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); static OutputPort owPort0 = new OutputPort(Pins.GPIO_PIN_D0, false); static OneWire owBus0 = new OneWire(owPort0); static byte[] owSensor1 = new byte[8] { 40, 168, 176, 219, 3, 0, 0, 221 }; //sensor 1 inside building static byte[] owSensor2 = new byte[8] { 40, 172, 164, 219, 3, 0, 0, 98 }; //sensor 2 outside building static float owSensor1Temp = -99; static float owSensor2Temp = -99; static DateTime tempReadingTimestamp = DateTime.Now; public static void Main() { //set ip and get time from a time server Microsoft.SPOT.Net.NetworkInformation.NetworkInterface ni = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0]; ni.EnableStaticIP("10.10.40.80", "255.255.255.0", "10.10.40.1"); SetTime(); //create a web server WebServer server = new WebServer(80); server.CommandReceived += new WebServer.CommandReceivedHandler(server_CommandReceived); server.AllowedCommands.Add(new WebCommand("getTemp", 1)); server.Start(); //thread to blink the led every 250ms Thread ledBlinkThread = new Thread(BlinkLed_Delegate); ledBlinkThread.Start(); //thread to read temps every 10 seconds Thread readTempsThread = new Thread(ReadTemps_Delegate); readTempsThread.Start(); //loop while (true) { //stay alive } } private static void ReadTemps_Delegate() { Timer readTempsTimer = new Timer(new TimerCallback(ReadTemps_Tick), null, 0, 10000); } private static void ReadTemps_Tick(Object obj) { tempReadingTimestamp = DateTime.Now; owSensor1Temp = GetOneWireTempByByteArray(owSensor1); owSensor2Temp = GetOneWireTempByByteArray(owSensor2); string debugString = tempReadingTimestamp.ToString(); debugString += " Temp Inside: " + owSensor1Temp.ToString("N1"); debugString += " Temp Outside: " + owSensor2Temp.ToString("N1"); Debug.Print(debugString); } static float GetOneWireTempByByteArray(byte[] sensor) { float TemperatureC = 0; int tempLow = 0; int tempHigh = 0; int temptemp; owBus0.TouchReset(); //reset owBus0.TouchByte(0x55); //match rom foreach (byte b in sensor) //send the whole byte array { owBus0.TouchByte(B); } owBus0.TouchByte(0x44); //start temp conversion while (owBus0.ReadByte() == 0) ; //wait while busy owBus0.TouchReset(); //reset owBus0.TouchByte(0x55); //match rom foreach (byte b in sensor) //send the whole byte array { owBus0.TouchByte(B); } owBus0.TouchByte(0xBE); //read scratchpad tempLow = owBus0.ReadByte(); //LSB tempHigh = owBus0.ReadByte(); //MSB temptemp = (((int)(tempHigh) << 8) | tempLow); TemperatureC = temptemp * 0.0625f; // temperature is negative if sign bit set if (!((tempHigh & 0x80) == 0)) { short t = 0; float result = 0f; // construct complete temp value t = (short)((tempHigh << 8) + tempLow); // if negative, take two's complement to get absolute value t = (short)-t; // get integer part if any result = (float)(t >> 8); // remove zero bits and add fractional part if any result += ((t >> 3) & 0x1f) * 0.03125f; // return value with correct sign TemperatureC = -result; } return (float)((TemperatureC * 9.00 / 5.00) + 32.00); } //left out some non essential code }