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
-----

Topics I've Started

Netduino Plus 2 Native OneWire Temp Sensor

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
}

Problem with Native OneWire on Netduino Plus 2

11 November 2012 - 04:41 PM

I've been able to natively read the temp from a single DS18B20 on my new Netduino Plus 2 using non-parasite power. My trouble comes with trying to read multiple DS18B20's using parasite power. I can see the devices ok (populated into "deviceList") however I am getting erroneous readings. I'm sure my writebyte's/readbyte's must be off or something. Here is the pertinent code and a link to the datasheet (using page 18, example 1 as a reference). Please let me know if you have any insight. Thank you so much!

http://datasheets.ma.../ds/DS18B20.pdf

static float getOWTempByAddress(OneWire owBus)
        {
            ArrayList deviceList = owBus.FindAllDevices();

            foreach (byte[] device in deviceList)
            {
                Debug.Print("Found ID: " + bytesToHexString(device, true));
            }

            byte[] deviceToCheck = (byte[])deviceList[0]; //lets just look at the first device as a test

            float TemperatureC = 0;
            int tempLow = 0;
            int tempHigh = 0;
            int temptemp;

            owBus.TouchReset();
            owBus.WriteByte(0x55); //match rom
            foreach (byte theByte in deviceToCheck)
            {
                  owBus.WriteByte(theByte);
            }
            owBus.WriteByte(0x44); //start temp conversion
            while (owBus.ReadByte() == 0) ; //wait while busy
            owBus.TouchReset();
            owBus.WriteByte(0x55); //match rom
            foreach (byte theByte in deviceToCheck)
            {
                  owBus.WriteByte(theByte);
            }
            owBus.WriteByte(0xBE); //read scratchpad
            tempLow = owBus.ReadByte(); //LSB
            tempHigh = owBus.ReadByte(); //MSB
            temptemp = (((int)(tempHigh) << 8) | tempLow);
            TemperatureC = temptemp * 0.0625f;

       
            }
            return (float)((TemperatureC * 9.00 / 5.00) + 32.00);
        }

Reading 1 Wire Digital Temp Sensor

09 November 2012 - 01:55 PM

I'm trying to use a netduino plus to monitor temperature from a DS18B20 . I've read in the forums about a not-so-recent custom firmware (CWP) that might do the trick? Is this still advised? If it is, can someone point me to the most recent version to download said firmware? It's not clear when scrolling through the forums where I can download it and if it is the current build. PLEASE let me know if I don't actually need to downgrade to that custom firmware and if I can read the temp with what I have (4.2.0.1). Thanks so much! -Nick

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.