DHT11/22 sensor managed driver - Page 3 - Project Showcase - Netduino Forums
   
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.
Photo

DHT11/22 sensor managed driver


  • Please log in to reply
59 replies to this topic

#41 Leif

Leif

    New Member

  • Members
  • Pip
  • 1 posts

Posted 04 May 2013 - 08:40 PM

I have the same issue besides the code above does not seem to be for a DHT11, so I get funny reading:

 

 

DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor data timeout.
DHT sensor data timeout.
DHT sensor Read() ok, RH = 998.4%, Temp = 588.8°C 1091.8°F
DHT sensor Read() ok, RH = 998.4%, Temp = 588.8°C 1091.8°F
DHT sensor Read() ok, RH = 998.4%, Temp = 588.8°C 1091.8°F
DHT sensor Read() ok, RH = 998.4%, Temp = 588.8°C 1091.8°F
DHT sensor Read() ok, RH = 998.4%, Temp = 588.8°C 1091.8°F
DHT sensor Read() ok, RH = 998.4%, Temp = 588.8°C 1091.8°F

 

I don't seem to be able to find out where I have to change some speed setting or so to make sure the DHT11 is correctly read. Also this issue with unpluging the one wire. Did anybod get that out of the way.

 

Thank you very much in advance.

 

Leif


  • stewsterl likes this

#42 Laurelle

Laurelle

    Member

  • Members
  • PipPip
  • 16 posts
  • LocationFrance

Posted 31 May 2013 - 04:41 PM

I tried the [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;] [/color]NetduinoPlus2RHT03.zip[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]  [/color][color=rgb(119,119,119);font-family:helvetica, arial, sans-serif;font-size:12px;]94.78K[/color][color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]  [/color][color=rgb(164,164,164);font-family:helvetica, arial, sans-serif;font-size:12px;]2 downloads[/color]

 

[color=rgb(164,164,164);font-family:helvetica, arial, sans-serif;font-size:12px;]It reads:[/color]

The thread '<No Name>' (0x2) has exited with code 0 (0x0).

DHT sensor data timeout.
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 0.0%, etc etc etc
untill i unplug and replug pin D1, while running!
And then it reads:
DHT sensor data timeout.
DHT sensor Read() ok, RH = 30.0%, Temp = 23.6°C 74.5°F
DHT sensor Read() ok, RH = 30.0%, Temp = 23.6°C 74.5°F
DHT sensor Read() ok, RH = 30.2%, Temp = 23.6°C 74.5°F
DHT sensor Read() ok, RH = 30.2%, etc etc
Can hardly do that every run:)
 

Does anyone have any clue what that's about?

I have exactly the same issue. I had to change the <<42 to <<41 and I needed to unplug the D1 and replug it to get a value.

Another way to make it work is to add a breakpoint, release it and continue.

I've tried a lot of things but nothing else is working.

Any idea?


Doing dev is like doing sport. It's healthy :-)

#43 GillesLondon

GillesLondon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 08 June 2013 - 11:15 AM

Thank you so much to CW2 and Giuliano for their source code, and others for participating to this topic.

 

I am also running the source code from Giuliano (NetduinoPlus2RTH03) and it only works after unplugging/plugging D1.

Anyone fixed this problem?

 

For info I am using the Netduino Plus 2 with latest Firmware 4.3.



#44 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 11 June 2013 - 04:56 PM

Hi GillesLondon,

 

I am not using the Firmware 4.3 yet but just wanted to make sure you are connecting D0 and D1 to the same wire that goes to the DATA signal (Pin 2) in the RHT03. Also, are you powering with 3.3V or 5V?

 

Let me know.



#45 GillesLondon

GillesLondon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 11 June 2013 - 08:20 PM

I am not using the Firmware 4.3 yet but just wanted to make sure you are connecting D0 and D1 to the same wire that goes to the DATA signal (Pin 2) in the RHT03. Also, are you powering with 3.3V or 5V?

 

Hi Giuliano,

Yes my wiring is correct, I can get valid data but only after unplugging/plugging the wire.

I tried with both 3.3 and 5V, the result is the same.

Leif and Laurelle have the same problem, that would be great if they could confirm which firmware they are using.



#46 Laurelle

Laurelle

    Member

  • Members
  • PipPip
  • 16 posts
  • LocationFrance

Posted 11 June 2013 - 08:26 PM

Yes, I confirm, same setup, same firmware, same behavior. I need to unplug and plug it again to make it works.
Doing dev is like doing sport. It's healthy :-)

#47 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 11 June 2013 - 08:28 PM

Hi GillesLondon and Laurelle,

 

Based on the info provided all I can say that it maybe related to the new Firmware 4.3 but I have to try it out.

 

Is there any way for you guys to flash the N+2 with 4.2 and give it a shot?

 

Giuliano



#48 GillesLondon

GillesLondon

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 June 2013 - 11:06 PM

Hi GillesLondon and Laurelle,

 

Based on the info provided all I can say that it maybe related to the new Firmware 4.3 but I have to try it out.

 

Is there any way for you guys to flash the N+2 with 4.2 and give it a shot?

 

Giuliano

 

Hi Giuliano,

 

I installed Visual Studio 2012 on another machine with SDK 4.2 and downgraded my N+2 to Firmware 4.2.2

I downloaded your source code (Feb. 20th), deployed and ran it but unfortunately I still have the same problem.

The sensor returns correct values only after unplugging/plugging PIN 0 (or other wires). I tried with 3.3 and 5V.

 

The problem is not extremely important when we know the manual "workaround" but I am having other weird problems with some other sensors and the problem might be the same as this one.

 

Any idea?

Thank you for your help.



#49 Sambo

Sambo

    New Member

  • Members
  • Pip
  • 6 posts

Posted 25 February 2014 - 05:37 PM

I managed to fix the consistent failures of CheckPins() as follows:
 

        private bool CheckPins()
        {
            Microsoft.SPOT.Debug.Assert(portIn != null, "ERROR: DHT Input port should not be null.");
            Microsoft.SPOT.Debug.Assert(portOut != null, "ERROR: DHT Output port should not be null.");
            Microsoft.SPOT.Debug.Assert(!portOut.Active, "ERROR: DHT Output port should not be active.");

            bool expectedTrue = portIn.Read();
            portOut.Active = true;  // Switch to output
            //portOut.Write(false); // already had false latched in
            bool expectedFalse = portIn.Read();
            portOut.Active = false; // Switch to input
            //bool expectedTrue = portIn.Read(); // BUGBUG: for some reason this is always reading false even w/ delay inserted before;
                                                 // so I moved it to be read bofore outputting a low by activating the o/p pin
            return (expectedTrue && !expectedFalse);
        }

Cheers!



#50 stewlg

stewlg

    New Member

  • Members
  • Pip
  • 6 posts

Posted 09 March 2014 - 12:09 AM

I was getting the
 
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
 
behavior with my DHT22 and a Netduino Plus 2 until I disconnected and reconnected a pin. Then I tried Fred Werneck's previous suggestion and changed DhtSensor.cs's Read() function to this:

        public bool Read()
        {
            if (disposed)
            {
                throw new ObjectDisposedException();
            }
            // The 'bitMask' also serves as edge counter: data bit edges plus
            // extra ones at the beginning of the communication (presence pulse).
            bitMask = 1L << 41;

            data = 0;
            // lastTicks = 0; // This is not really needed, we measure duration
            // between edges and the first three values are ignored anyway.

            // Initiate communication
            portOut.Active = true;
            portOut.Write(false);       // Pull bus low
            Thread.Sleep(StartDelay);
            portIn.EnableInterrupt();   // Turn on the receiver
            portOut.Active = false;     // Release bus

            bool dataValid = false;

            // Now the interrupt handler is getting called on each falling edge.
            // The communication takes up to 5 ms, but the interrupt handler managed
            // code takes longer to execute than is the duration of sensor pulse
            // (interrupts are queued), so we must wait for the last one to finish
            // and signal completion. 20 ms should be enough, 50 ms is safe.
            if (dataReceived.WaitOne(50, false))
            {
                // TODO: Use two short-s ?
                bytes[0] = (byte)((data >> 32) & 0xFF);
                bytes[1] = (byte)((data >> 24) & 0xFF);
                bytes[2] = (byte)((data >> 16) & 0xFF);
                bytes[3] = (byte)((data >> 8) & 0xFF);

                byte checksum = (byte)(bytes[0] + bytes[1] + bytes[2] + bytes[3]);
                if (checksum == (byte)(data & 0xFF))
                {
                    dataValid = true;
                    if (bytes[0] == 0)
                    {
                        portIn.DisableInterrupt();
                    }
                    else
                    {
                        Convert(bytes);
                    }
                }
                else
                {
                    Debug.Print("DHT sensor data has invalid checksum.");
                }
            }
            else
            {
                portIn.DisableInterrupt();  // Stop receiver
                Debug.Print("DHT sensor data timeout.");  // TODO: TimeoutException?
            }
            return dataValid;
        }

Now I get this when the demo app starts up:

 

DHT sensor data timeout.
DHT sensor Read() ok, RH = 0.0%, Temp = 0.0°C 32.0°F
DHT sensor Read() ok, RH = 53.2%, Temp = 24.2°C 75.6°F
DHT sensor Read() ok, RH = 53.0%, Temp = 24.2°C 75.6°F
DHT sensor Read() ok, RH = 52.9%, Temp = 24.2°C 75.6°F
DHT sensor Read() ok, RH = 52.9%, Temp = 24.2°C 75.6°F
DHT sensor Read() ok, RH = 52.8%, Temp = 24.2°C 75.6°F

 

A timeout, one bad read, and then good ones from that point forward. I'm sure with a bit of work we could eliminate that first bad read as well, I haven't tried yet.

 

To save someone else time, I'm attaching my working project. I'm targeting the 4.3 framework successfully in it.

Attached Files


  • 366cobra likes this

#51 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 11 March 2014 - 06:05 AM

To get this working with a DHT11 sensor you just need to update/replace RHT03.cs with the following. I've added a DHT11 class that doesn't do any of the calculations that are required for the DHT22. Obviously you need to change the declaration of the sensor to dht11sensor too

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using CW.NETMF;
using CW.NETMF.Sensors;

namespace NetduinoPlus2RHT03
{
    /// <summary>
    /// Represents an instance of the DHT22 sensor.
    /// </summary>
    /// <remarks>
    /// Humidity measurement range 0 - 100%, accuracy ±2-5%.
    /// Temperature measurement range -40 - 80°C, accuracy ±0.5°C.
    /// </remarks>
    public class Dht22Sensor : DhtSensor
    {
        /// <summary>
        /// Initialize a new instance of the <see cref="Dht22Sensor"/> class.
        /// </summary>
        /// <param name="pin1">The identifier for the sensor's data bus port.</param>
        /// <param name="pin2">The identifier for the sensor's data bus port.</param>
        /// <param name="pullUp">The pull-up resistor type.</param>
        /// <remarks>
        /// The ports identified by <paramref name="pin1"/> and <paramref name="pin2"/>
        /// must be wired together.
        /// </remarks>
        public Dht22Sensor(Cpu.Pin pin1, Cpu.Pin pin2, PullUpResistor pullUp)
            : base(pin1, pin2, pullUp)
        {
            // This constructor is intentionally left blank.
        }

        protected override int StartDelay
        {
            get
            {
                return 2; // At least 1 ms
            }
        }

        protected override void Convert(byte[] data)
        {
            Debug.Assert(data != null);
            Debug.Assert(data.Length == 4);

            //DHT22 Code
            // The first byte is integral, the second decimal part
            //Humidity = ((data[0] << 8) | data[1]) * 0.1F;

            //DHT11 Code
            Humidity = data[0];

            // DHT22 code
            //var temp = (((data[2] & 0x7F) << 8) | data[3]) * 0.1F;
            //Temperature = (data[2] & 0x80) == 0 ? temp : -temp; // MSB = 1 means negative

            // DHT11 Code
            Temperature = data[2];
        }
    }
    public class Dht11Sensor : DhtSensor
    {
        /// <summary>
        /// Initialize a new instance of the <see cref="Dht11Sensor"/> class.
        /// </summary>
        /// <param name="pin1">The identifier for the sensor's data bus port.</param>
        /// <param name="pin2">The identifier for the sensor's data bus port.</param>
        /// <param name="pullUp">The pull-up resistor type.</param>
        /// <remarks>
        /// The ports identified by <paramref name="pin1"/> and <paramref name="pin2"/>
        /// must be wired together.
        /// </remarks>
        public Dht11Sensor(Cpu.Pin pin1, Cpu.Pin pin2, PullUpResistor pullUp)
            : base(pin1, pin2, pullUp)
        {
            // This constructor is intentionally left blank.
        }

        protected override int StartDelay
        {
            get
            {
                return 2; // At least 1 ms
            }
        }

        protected override void Convert(byte[] data)
        {
            Debug.Assert(data != null);
            Debug.Assert(data.Length == 4);

            Humidity = data[0];

            Temperature = data[2];
        }
    }
}

 



#52 netdude

netdude

    New Member

  • Members
  • Pip
  • 1 posts

Posted 18 May 2014 - 05:05 PM

Thanks for posting this, it worked great.  I was expecting to spend hours figuring out how to use the DHT11 but had it up and running in about 15 minutes with your code.  My refrigerator went out last week and i had an old small fridge in the garage with a bad thermostat.  So, what else would a techie do but hook up a DHT11 with the netduino, a switch, relay and presto!  A working thermostat for the little fridge. The only changes i made to the code was to add a branch to test the temp and set GPIO_PIN_D7 hi or low to turn on or off the relay.  Since timing isn't important in this application I handled false reads by testing for the humidity above 0 along with the lower temp setting since the humidity only reads 0 on a false read.  I know, not real elegant but it was quick and easy.  Setting the sleep to 15 secs seemed to help also, it lets things settle between readings. "FrankenFridge" has been up and running for 2 days now with no issues, repair man is coming tomorrow to fix the big box.  Thanks again, it worked great! Attached File  IMAG0513.jpg   83.96KB   3 downloads



#53 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 20 May 2014 - 05:46 PM

Hi stewlf, how is it working with wendo's suggestion? Do you still get timeouts or 32°F/0 humidity values? how often do you read from the sensor?



#54 stewlg

stewlg

    New Member

  • Members
  • Pip
  • 6 posts

Posted 10 June 2014 - 02:20 PM

Hi stewlf, how is it working with wendo's suggestion? Do you still get timeouts or 32°F/0 humidity values? how often do you read from the sensor?

 

I am not going to be trying his code any time soon; my relevant Netduino project is installed and functioning adequately, and I'm not inclined to take it apart right now:

 

http://www.skyscratc...r-for-netduino/

 

To answer your question, after startup, I read from the sensor about once every 1-2 minutes.



#55 DanielK

DanielK

    Advanced Member

  • Members
  • PipPipPip
  • 32 posts

Posted 25 June 2014 - 05:51 PM

the netxt thing that isn´t working on my N+2. I tried several modifications (and of course  the original code). I get always "DHT sensor data timeout." After so much trouble getting my GPS module up and running on the N+2 and getting a file written on a SD card I´m about giving up the Netduino. Maybe I´m just too stupid... but I´m working as a SW developer for years now and never had such trouble with other solutions.

Sorry, but this is very(!) unsatisfying. Looks like I should switch back to the good old Arduino. That guy works very robust w/o all this trouble..

 

:angry: :(

 

Daniel



#56 lennygran

lennygran

    New Member

  • Members
  • Pip
  • 2 posts

Posted 27 September 2014 - 08:34 PM

I did 2 steps to fix the issue mentioned above: remove the reference to SecretLabs.NETMF.Hardware.Netduino and add another one to SecretLabs.NETMF.Hardware.NetduinoPlus; then make an appropriate change to the "usage" statement in the Program.cs file as below:

 

    using SecretLabs.NETMF.Hardware.NetduinoPlus;
 
This resolves the problem and all N+2 functionality as well as sensor is working correctly.


#57 FourT2

FourT2

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationSeattle, WA

Posted 28 September 2014 - 05:46 AM

I was able to get the code from stewlg and wendo to work with my N2+ and DHT11. First read fails, then all 0s, then appears to work, with a timeout from time to time. 

 

I have doubts about accuracy though. I plugged the very same DHT sensor into my Arduino and I am getting RH:40 and TempF:80.6 while on N2+ I am getting RH:35 and TempF: 77-78. I am not entirely sure which is right. I realize that DHT11 is not the most precise sensor out there. 



#58 wendo

wendo

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 29 September 2014 - 01:51 AM

When playing with these sensors I notice they take quite a while to stabilize after any changes. It may be as simple as moving between 3.3v and 5v VCC and they need some time to settle.



#59 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 361 posts
  • LocationSimi Valley, CA

Posted 30 September 2014 - 03:59 PM

From my experience with these sensors, they are like that, not very reliable but this one seems to works better:

 

https://www.sparkfun.../products/10167

 

Which is about the same price range. It is more reliable but I have to mention that mine broke in my backyard for being outdoors, I don't think it is designed for outdoors even though I put it underneath something that protects it from sunlight and rain (it doesn't really rain here :) )

 

Someone recommended me a good sensor that was very reliable but it was in the order of $30+



#60 FourT2

FourT2

    Advanced Member

  • Members
  • PipPipPip
  • 63 posts
  • LocationSeattle, WA

Posted 01 October 2014 - 04:42 AM

Yeah I also have this DHT21/22 in a self-contained package: http://www.ebay.com/...=STRK:MEBIDX:IT

 

it does seem to be a bit more accurate than DHT11.

 

I may try this one next: 

https://www.sparkfun.../products/11295


  • Giuliano likes this




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users

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.