Main Site Documentation

Fez Panda dht11 error


#1

Hello,
I am using the fez connect shield with the fez panda to use the dht11 sensor.
The driver that I am using is:http://code.tinyclr.com/project/289/dht11—temperature-and-humidity-sensor/. This is the code I am using.

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;

namespace dht11_test
{
    public class Program
    {
        public static void Main()
        {
            DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);

            while (true)
            {
                if (MyDHT11.ReadSensor())
                {
                    Debug.Print("Temperature = " + MyDHT11.Temperature.ToString() + "°C");
                    Debug.Print("Humidity    = " + MyDHT11.Humidity.ToString() + "%");
                }
                else Debug.Print("DHT11 Error : " + MyDHT11.LastError);

                Thread.Sleep(10000);
            }
        }

    }
}

The error is in the image.


#2

I think you need to get rid of the cpu.pin duplicate name mapping; the “using Microsoft.SPOT.Hardware;” line should have done that but it seems you have something else coming through overriding that. When I hover over cpu.pin in the default new project, I get the tooltip of “class Microsoft.SPOT.Hardware”, what do you get?

I have just done the following and it all works fine. Started a new project, without adding extra references. Copied the class definition from the driver you linked to below the [quote]public class Program[/quote] section. Added the statement in main() [quote] DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7); [/quote]

TheDHT11 declaration resolved correctly, all fine.


#3

This is a really strange error!


#4

I get


#5

Try doing exactly what I did and see if you get a different result - just paste the driver content into the program.cs file created when you start a new project


#6

Looks like you are missing a using of the namespace of the DHT11 class ?


#7

Ok I tried adding the namspace

using dht11---temperature-and-humidity-sensor;

And I get even more errors. I also tried putting the driver code in the program .cs.
Can some email the project so I can compare. My email is sidy.ndiongue@ hotmail.com
Thanks


#8

That namespace looks very odd. If you got it from http://code.tinyclr.com/project/289/dht11—temperature-and-humidity-sensor, you may be confused about what a namespace in c# is. It is not a URI namespace (like those used in defining XML schemas).


#9

You’ll need someday to look at the beginner’s book to understand about namespace. Don’t worry, that’s easy !

But for now,

  • Just get rid of your project file dht11—temperature-and-humidity-sensor
  • Copy and paste the dht11 class directly inside your program.cs, just after the public class program

Give it a try ?

Have fun :smiley:


#10

Should the code look like this:

using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;



namespace dht11_test
{
    public class Program
    {
        /// <summary>
        /// Class to read the single wire Humitidy/Temperature DHT11 sensor
        /// It uses 2 interrupt pins connected together to access the sensor quick enough
        /// </summary>
        public class DHT11 : IDisposable
        {

            /// <summary>
            /// Temperature in celcius degres
            /// </summary>
            public float Temperature { get; private set; }

            /// <summary>
            /// Humidity in percents
            /// </summary>
            public float Humidity { get; private set; }

            /// <summary>
            /// If not empty, gives the last error that occured
            /// </summary>
            public string LastError { get; private set; }

            private TristatePort _dht11out;
            private PinCapture _dht11in;

            /// <summary>
            /// Constructor. Needs to interrupt pins to be provided and linked together in Hardware.
            /// Blocking call for 1s to give sensor time to initialize.
            /// </summary>
            public DHT11(Cpu.Pin In, Cpu.Pin Out)
            {
                _dht11out = new TristatePort(Out, false, false, Port.ResistorMode.PullUp);
                _dht11in = new PinCapture(In, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
                if (_dht11out.Active == false) _dht11out.Active = true; // Make tristateport "output" 
                _dht11out.Write(true);   //"high up" (standby state)
                Thread.Sleep(1000); // 1s to pass the "unstable status" as per the documentation
            }

            #region IDisposable Members

            public void Dispose()
            {
                _dht11out.Dispose();
                _dht11in.Dispose();
            }

            #endregion

            /// <summary>
            /// Access the sensor. Returns true if successful, false if it fails.
            /// If false, please check the LastError value for reason.
            /// </summary>
            public bool ReadSensor()
            {
                uint[] buffer = new uint[90];
                int nb, i;

                // Testing if the 2 pins are connected together
                bool rt = _dht11in.InternalPort.Read();  // Should be true
                _dht11out.Write(false);  // "low down" : initiate transmission
                bool rf = _dht11in.InternalPort.Read();  // Should be false
                if (!rt || rf)
                {
                    LastError = "The 2 pins are not hardwired together !";
                    _dht11out.Write(true);   //"high up" (standby state)
                    return false;
                }
                Thread.Sleep(20);       // For "at least 18ms" as per the documentation
                _dht11out.Write(true);   //"high up" then listen
                nb = _dht11in.Read(false, buffer, 0, 90, 10);  // get the sensor answer
                if (nb < 81)
                {
                    LastError = "Did not receive enough data from the sensor";
                    return false;
                }
                nb -= 2; // skip last 50us down          
                byte checksum = 0, decimalT = 0, integralT = 0, decimalRH = 0, integralRH = 0;
                for (i = 0; i < 8; i++, nb -= 2) checksum |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) decimalT |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) integralT |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) decimalRH |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) integralRH |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                if (((integralRH + decimalRH + integralT + decimalT) & 0xFF) != checksum)
                {
                    LastError = "Checksum Error";
                    return false;
                }
                Temperature = ((float)integralT) + ((float)decimalT) / 10;
                Humidity = ((float)integralRH) + ((float)decimalRH) / 10;
                LastError = "";
                return true;
            }
        }
        public static void Main()
        {
            DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);

            while (true)
            {
                if (MyDHT11.ReadSensor())
                {
                    Debug.Print("Temperature = " + MyDHT11.Temperature.ToString() + "°C");
                    Debug.Print("Humidity    = " + MyDHT11.Humidity.ToString() + "%");
                }
                else Debug.Print("DHT11 Error : " + MyDHT11.LastError);

                Thread.Sleep(10000);
            }
        }

    }
}


#11

no, move the class DHT11 after the class Program, not inside it.


#12

I don’t get it can I see your code?


#13

Try that way… the DHT11 class AFTER the Program class…

using System;
using System.Threading;
 
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
 
using GHIElectronics.NETMF.FEZ;
 
 
 
namespace dht11_test
{
    public class Program
    {
        
        public static void Main()
        {
            DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);
 
            while (true)
            {
                if (MyDHT11.ReadSensor())
                {
                    Debug.Print("Temperature = " + MyDHT11.Temperature.ToString() + "°C");
                    Debug.Print("Humidity    = " + MyDHT11.Humidity.ToString() + "%");
                }
                else Debug.Print("DHT11 Error : " + MyDHT11.LastError);
 
                Thread.Sleep(10000);
            }
        }
 
    }

/// <summary>
        /// Class to read the single wire Humitidy/Temperature DHT11 sensor
        /// It uses 2 interrupt pins connected together to access the sensor quick enough
        /// </summary>
        public class DHT11 : IDisposable
        {
 
            /// <summary>
            /// Temperature in celcius degres
            /// </summary>
            public float Temperature { get; private set; }
 
            /// <summary>
            /// Humidity in percents
            /// </summary>
            public float Humidity { get; private set; }
 
            /// <summary>
            /// If not empty, gives the last error that occured
            /// </summary>
            public string LastError { get; private set; }
 
            private TristatePort _dht11out;
            private PinCapture _dht11in;
 
            /// <summary>
            /// Constructor. Needs to interrupt pins to be provided and linked together in Hardware.
            /// Blocking call for 1s to give sensor time to initialize.
            /// </summary>
            public DHT11(Cpu.Pin In, Cpu.Pin Out)
            {
                _dht11out = new TristatePort(Out, false, false, Port.ResistorMode.PullUp);
                _dht11in = new PinCapture(In, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
                if (_dht11out.Active == false) _dht11out.Active = true; // Make tristateport "output" 
                _dht11out.Write(true);   //"high up" (standby state)
                Thread.Sleep(1000); // 1s to pass the "unstable status" as per the documentation
            }
 
            #region IDisposable Members
 
            public void Dispose()
            {
                _dht11out.Dispose();
                _dht11in.Dispose();
            }
 
            #endregion
 
            /// <summary>
            /// Access the sensor. Returns true if successful, false if it fails.
            /// If false, please check the LastError value for reason.
            /// </summary>
            public bool ReadSensor()
            {
                uint[] buffer = new uint[90];
                int nb, i;
 
                // Testing if the 2 pins are connected together
                bool rt = _dht11in.InternalPort.Read();  // Should be true
                _dht11out.Write(false);  // "low down" : initiate transmission
                bool rf = _dht11in.InternalPort.Read();  // Should be false
                if (!rt || rf)
                {
                    LastError = "The 2 pins are not hardwired together !";
                    _dht11out.Write(true);   //"high up" (standby state)
                    return false;
                }
                Thread.Sleep(20);       // For "at least 18ms" as per the documentation
                _dht11out.Write(true);   //"high up" then listen
                nb = _dht11in.Read(false, buffer, 0, 90, 10);  // get the sensor answer
                if (nb < 81)
                {
                    LastError = "Did not receive enough data from the sensor";
                    return false;
                }
                nb -= 2; // skip last 50us down          
                byte checksum = 0, decimalT = 0, integralT = 0, decimalRH = 0, integralRH = 0;
                for (i = 0; i < 8; i++, nb -= 2) checksum |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) decimalT |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) integralT |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) decimalRH |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                for (i = 0; i < 8; i++, nb -= 2) integralRH |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
                if (((integralRH + decimalRH + integralT + decimalT) & 0xFF) != checksum)
                {
                    LastError = "Checksum Error";
                    return false;
                }
                Temperature = ((float)integralT) + ((float)decimalT) / 10;
                Humidity = ((float)integralRH) + ((float)decimalRH) / 10;
                LastError = "";
                return true;
            }
        }
}

#14

It’s not that hard…

namespace dht11_test
{
    public class Program
    {
main()
{
......
}
}


  /// <summary>
        /// Class to read the single wire Humitidy/Temperature DHT11 sensor
        /// It uses 2 interrupt pins connected together to access the sensor quick enough
        /// </summary>
        public class DHT11 : IDisposable
        {
 
.....etc
}

Which is what I said to do earlier…


#15

Nicolas3
Which references are yoou using?
There still seems to be errors.


#16

Never mind it works.
Thank alot guys.

using System;
using System.Threading;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;



namespace dht11_test
{
    public class Program
    {

        public static void Main()
        {
            DHT11 MyDHT11 = new DHT11((Cpu.Pin)FEZ_Pin.Digital.Di6, (Cpu.Pin)FEZ_Pin.Digital.Di7);

            while (true)
            {
                if (MyDHT11.ReadSensor())
                {
                    Debug.Print("Temperature = " + MyDHT11.Temperature.ToString() + "°C");
                    Debug.Print("Humidity    = " + MyDHT11.Humidity.ToString() + "%");
                }
                else Debug.Print("DHT11 Error : " + MyDHT11.LastError);

                Thread.Sleep(10000);
            }
        }

    }

    /// <summary>
    /// Class to read the single wire Humitidy/Temperature DHT11 sensor
    /// It uses 2 interrupt pins connected together to access the sensor quick enough
    /// </summary>
    public class DHT11 : IDisposable
    {

        /// <summary>
        /// Temperature in celcius degres
        /// </summary>
        public float Temperature { get; private set; }

        /// <summary>
        /// Humidity in percents
        /// </summary>
        public float Humidity { get; private set; }

        /// <summary>
        /// If not empty, gives the last error that occured
        /// </summary>
        public string LastError { get; private set; }

        private TristatePort _dht11out;
        private PinCapture _dht11in;

        /// <summary>
        /// Constructor. Needs to interrupt pins to be provided and linked together in Hardware.
        /// Blocking call for 1s to give sensor time to initialize.
        /// </summary>
        public DHT11(Cpu.Pin In, Cpu.Pin Out)
        {
            _dht11out = new TristatePort(Out, false, false, Port.ResistorMode.PullUp);
            _dht11in = new PinCapture(In, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
            if (_dht11out.Active == false) _dht11out.Active = true; // Make tristateport "output" 
            _dht11out.Write(true);   //"high up" (standby state)
            Thread.Sleep(1000); // 1s to pass the "unstable status" as per the documentation
        }

        #region IDisposable Members

        public void Dispose()
        {
            _dht11out.Dispose();
            _dht11in.Dispose();
        }

        #endregion

        /// <summary>
        /// Access the sensor. Returns true if successful, false if it fails.
        /// If false, please check the LastError value for reason.
        /// </summary>
        public bool ReadSensor()
        {
            uint[] buffer = new uint[90];
            int nb, i;

            // Testing if the 2 pins are connected together
            bool rt = _dht11in.InternalPort.Read();  // Should be true
            _dht11out.Write(false);  // "low down" : initiate transmission
            bool rf = _dht11in.InternalPort.Read();  // Should be false
            if (!rt || rf)
            {
                LastError = "The 2 pins are not hardwired together !";
                _dht11out.Write(true);   //"high up" (standby state)
                return false;
            }
            Thread.Sleep(20);       // For "at least 18ms" as per the documentation
            _dht11out.Write(true);   //"high up" then listen
            nb = _dht11in.Read(false, buffer, 0, 90, 10);  // get the sensor answer
            if (nb < 81)
            {
                LastError = "Did not receive enough data from the sensor";
                return false;
            }
            nb -= 2; // skip last 50us down          
            byte checksum = 0, decimalT = 0, integralT = 0, decimalRH = 0, integralRH = 0;
            for (i = 0; i < 8; i++, nb -= 2) checksum |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
            for (i = 0; i < 8; i++, nb -= 2) decimalT |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
            for (i = 0; i < 8; i++, nb -= 2) integralT |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
            for (i = 0; i < 8; i++, nb -= 2) decimalRH |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
            for (i = 0; i < 8; i++, nb -= 2) integralRH |= (byte)(buffer[nb] > 50 ? 1 << i : 0);
            if (((integralRH + decimalRH + integralT + decimalT) & 0xFF) != checksum)
            {
                LastError = "Checksum Error";
                return false;
            }
            Temperature = ((float)integralT) + ((float)decimalT) / 10;
            Humidity = ((float)integralRH) + ((float)decimalRH) / 10;
            LastError = "";
            return true;
        }
    }
}


#17

Now if you want to make it work as you tried to do first (dht11 class in a separate file, part of the project):

  1. Insert the DHT11 class inside a namespace in your class file, example:

namespace mysensors
{
public class DHT11 : IDisposable
    {
    ///...
    }
}

  1. Then, add the beginning of your main program file, add a line:

using mysensors;

I am interested to know about your tests with the DHT11. My own test worked fine at the begining, with reasonable accuracy, but with time (several months) the accuracy “drifter”. Now I am using DHT22 instead, which seem more stable in time and accept negative temperatures as well…

Good luck :wink: !


#18

I can’t get the first way to work. For now I will use the second way. For the sensor my JST wires should be flipped.
Thanks