Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

nickNetduino

Member Since 08 Nov 2012
Offline Last Active Dec 24 2012 12:54 PM
-----

#39060 Netduino Plus 2 Native OneWire Temp Sensor

Posted by nickNetduino on 11 November 2012 - 11:00 PM

Ok folks, I've spent a day hacking this together but it appears to finally work. However, I was hoping some of the more experienced coders here might be able to critique this code to see what I may have not done very elegantly. Basically what the code should do is check the temperatures of 2 sensors ( DS18B20 ) every 10 seconds and store it in a global variable that may be read by a web client or logged. Thanks much in advance.

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
}



home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.