Author Topic: DHT22 and DHT11 Sensor Library  (Read 8751 times)

emmertex

  • Newbie
  • *
  • Posts: 4
    • View Profile
DHT22 and DHT11 Sensor Library
« on: September 23, 2013, 10:48:19 PM »
I have developed a DHT22 Sensor Library.

Unfortunately, due to timing issues, etc... no existent code worked, so I had to start from scratch.

Please note that this is under development.
I got it working well with 1 sensor, and recently complicated it for working with 2 sensors.

Grab it from Github.
https://github.com/emmertex/DHT22-Library

Please post any questions in this topic.
« Last Edit: February 11, 2014, 05:58:30 PM by modtro2 »

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: DHT22 Sensor Library
« Reply #1 on: February 11, 2014, 05:58:12 PM »
Hi, thanks for contribution! I have added full support for the DHT11 and DHT22 to the "Netcruzer Library". The code reads the sensor in the background at a configurable rate. Provides functions for checking when new data is available, and reading it. All contained in the nz_dhtSensor.h and nz_dhtSensor.c files in the "../src/netcruzer/lib" folder of the download.

I added an example to the "Webserver" project that displays the temperature and humidity on a web page. See screen shot below. For this page to work, the SBC66EC board must be programmed with the "DHT Sensor Demo". Currently contained in the V2.03 Beta download available here. The demo is part of the "Webserver" project, located in the "..src/projects/webserver" folder of the download. Open the Webserver project in MPLAB X, select the "DHT Sensor Demo" configuration, and build the project. You might want to change the port used for the sensor. For this demo, it is SBC66 Port Y6. To change this, simply replace 3 lines of code in the demo main.c file (located in ..src/projects/webserver/demos/Sens_DHT22_Demo) that contain PIN_Y6 and SBCPORT_ID_Y6 with the new port. For example PIN_X5 and SBCPORT_ID_X5 to use port X5.

To update the firmware with this demo, build the "DHT Sensor Demo", and update the firmware update the firmware.

This page shows how to display the temperature and humidity values obtained from a DHT11 or DHT22 sensor connected to any I/O port of the SBC66EC/SBC66ECL board. It uses "User Memory" on the SBC66 board to store the temperature and humidity values read by the sensor, and then displays these "User Memory" values on the web page using the "User Memory Tag". Using this method it is very easy displaying any user data on a web page. For details on the "User Memory" and other tags, click here.

« Last Edit: February 13, 2014, 05:16:05 PM by modtro2 »

dennis_h

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #2 on: February 12, 2014, 02:26:48 AM »
great work,

I have been able to compile the code with Mplabs and have also tried the hex file in the bin directory.

What I have not been able to do is find the webpages you developed to load to Webserver.

Are they in the web server default folder. I have been through all downloaded zip file, but think i am over looking the obvious.

Dennis

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #3 on: February 12, 2014, 05:21:46 PM »
Sorry, forgot to update zip build script to include it. I attached the new web pages for you. Will add them to the beta download now, are located in "../src/projects/webserver/webpages/DefaultNZ" folder.

To upload them to your SBC66EC board, use the "Webpage Generator":
http://www.netcruzer.com/webpagegen.

Shane

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #4 on: February 13, 2014, 11:55:21 AM »
Hi, I'm new to this and I want to add another sensor, RHT02, works the same as the DHT22 to the board. What is the best way to do this? I have created another variable "objRht02" of type DHT_SENSOR_INFO, and fbrTcbRHT02;I created the fiber, initialized the sensor using pin Y7, passed it into dhtSens_task(), and tried to store it in nzGlobals.userRam[4-7]. But all I'm getting is rubbish information for output.

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #5 on: February 13, 2014, 05:04:57 PM »
Hi, do you want to use 2 sensors simultaneously, or just one - the RHT02?
Hi, can you please post your code on the forum. Or, if you don't want to make it public, please use the Netcruzer "Contact Us" page:
http://www.netcruzer.com/contact-us.html
« Last Edit: February 13, 2014, 05:50:10 PM by modtro2 »

Shane

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #6 on: February 16, 2014, 11:40:42 AM »
Hi, I want to read up to 4 sensors simultaneously. Alternatively, I'm going to try to read them one at a time within a 10 second window. Attached is my modified main.c file in the "netcruzer_v203\src\projects\webserver\demos\Sens_DHT22_Demo" directory and modified dhtSens_task() function in nz_dhtsensor.c
« Last Edit: February 16, 2014, 11:50:19 AM by Shane »

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #7 on: February 17, 2014, 02:34:36 PM »
Hi Shane

This should be possible without modifying the library ( nz_dhtsensor.c). If not, I have to modify the library. Best is if you never modify the library, it makes porting to new firmware easier.

In main.c you forgot to configure your second sensor port as an "interrupt on change" interrupt:
Code: [Select]
intOnChangeEnablePort(SBCPORT_ID_Y6); //Enable "interrupt on change" for Channel A = Y6
intOnChangeEnablePort(SBCPORT_ID_Y7); //Enable "interrupt on change" for Channel A = Y7

In the processIO() function in main.c, you should call dhtSens_task() for each sensor you have. You should not modify dhtSens_task() (in  nz_dhtsensor.c) to take multiple objects. Try this code with the original  nz_dhtsensor.c:
Code: [Select]
dhtSens_task(&objDht22); //Call dhtSens_task() for DHT22
dhtSens_task(&objRht02); //Call dhtSens_task() for DHT02

All other modifications you did look good!

I will also test this code with multiple sensors using this method. Please let me know your results too.

Shane

  • Jr. Member
  • **
  • Posts: 8
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #8 on: February 18, 2014, 02:37:24 PM »
hey, the PIC can read 2 sensors now but there are a few problems. When the board is reset it sometimes won't start reading data until a sensor was disconnected and reconnected, I think this might have something to do with the pin's threshold for interrupt, maybe having more sensors = less power to share around?

And when I moved on to using 4 sensors I have to disconnect and reconnect a sensor, sometimes more than 1 to get the data to start flowing in. And the number of times each sensor reads over time is different and irregular, e.g. pin Y7 will read around once every 2 seconds and pin Y2 might take a minute to read once. I think this might have something to do with how long it takes to read and output data, and power shortage should not be a problem because I'm using an external source.

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #9 on: February 18, 2014, 03:35:32 PM »
Hi, the DHT sensor library ( nz_dhtsensor.c) is not currently designed for multiple sensors. I am busy adding that functionality, and should have it done the next day or 2. When done, it will read multiple sensors, one after the other. Currently, the way you did it, they are not synchronized.

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 564
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #10 on: February 25, 2014, 03:54:32 PM »
To get multiple sensors to work, please try following. Will include these new files with next beta release for V2.03. Replace the "nz_dhtSensor.c" and "nz_dhtSensor.h" files in your "../src/netcruzer/lib" folder with attached ones.

In your projdefs.h, include following configuration defines for nz_dhtSensor module. Note the DHT_SENSOR_NUMBER_OF_SENSORS define is new!
Code: [Select]
#define DHT_SENSOR_TIMEOUT                      ( 200 )     // Timeout 100ms
#define DHT_SENSOR_STARTUP_READ_DELAY           ( 2000 )    // Initial read delay 2 seconds (2000ms)
#define DHT_SENSOR_AUTO_READ_PERIOD             ( 2100 )    // Automatically read every 4 seconds
#define DHT_SENSOR_PIN_ARRAY_SIZE               ( 256 )     // Size of Pin Array read buffer, must be 4,8,16,32,64,128 or 256. Use 256 for optimum speed!
#define DHT_SENSOR_TYPE                         ( 2 )       // Sensor Type, 1=DHT11, 2=DHT22
#define DHT_SENSOR_NUMBER_OF_SENSORS            ( 2 )       // Number of Sensors

Your main code also now has to change to use this new library. This is an example how to configure and use it if you have 2 DHT22 Sensors connected to ports Y6 and Y7.
Code: [Select]
FIBER_TCB fbrTcbDHT22;    //DHT22 Fiber TCB

/**
 * Interrupt on change ISR, called if any of the configured ports change value
 */
void __attribute__((interrupt,no_auto_psv)) _CNInterrupt(void)
{
    WORD utick15bit_8us;
    intOnChangeClearIF();           //Clear "interrupt on change" IF (interrupt flag)

    //If "Interrupt on Change" is configured for inputs other than DHT Sensors, a check will be
    //required here to ensure a DHT sensor caused this interrupt! Not required for this example.
    //NZ_INT_DIS();        //Disable interrupts - not required if this ISR has a high priority!
    utick15bit_8us = getTick16bit_8us_noDisi();        //Get 8us time
    //The dhtSens_isr_MACRO() Macro must be called in interrupt. Will defer processing to Fiber
    dhtSens_isr_MACRO(utick15bit_8us);
    //NZ_INT_EN();        //Enable interrupt

    //Schedule the DHT22 fiber (calls the dhtSens_fbrTask() function) to run in lower interrupt priority.
    nzFbrSchedule(&fbrTcbDHT22);
}


/**
* Main application entry point.
*/
int main(void) {
    BYTE i;

    //Default initialization. All ports inputs. All analog features disabled. Tick 1ms
    nzSysInitDefault();

    //Create the DHT Sensor fiber! This will be executed in a lower interrupt priority than the main
    //DHT Sensor "interrupt on change" interrupt. The fiber will call the dhtSens_fbrTask() function.
    nzFbrCreate(2, TRUE, dhtSens_fbrTask, &fbrTcbDHT22);

    //DHT22 Initialization. First sensor is given with dhtSens_init(). Use dhtSens_addSensor() to add additional sensors.
    dhtSens_init(SBCPORT_ID_Y6, 3000);        //Initialize DHT22, using port Y6, startup delay 3 seconds
    dhtSens_addSensor(1, SBCPORT_ID_Y7);    //Add sensor on SBC Port Y7 (sensor index 1)

    //Configure the "interrupt on change" for pin that has a DHT Sensor - Y6 & Y7
    //IMPORTANT!!! Use equal or higher priority than is used for System Tick = 4 by default.
    #if (nzINT_PRIORITY_TICK >= 6)
    #error "Ensure Tick priority is smaller or equal to Interrupt On Change priority"
    #endif
    intOnChangeConfig(CHANGE_INT_ON|CHANGE_INT_PRI_6);    //Enable Interrupt on change at Int Priority level 6 (high)
    intOnChangeClearIF();                                //Clear "interrupt on change" IF (interrupt flag)
    intOnChangeEnablePort(SBCPORT_ID_Y6);                //Enable "interrupt on change" for each sensor
    intOnChangeEnablePort(SBCPORT_ID_Y7);                //Enable "interrupt on change" for each sensor

    delay_ms(120);   //Wait for DHT22 to settle

    //Main loop
    while(1)
    {
        nzSysTaskDefault();            //Main netcruzer task, call in main loop.

        dhtSens_task();    //DHT22 Task

        //Check if new data available for any of the sensors
        for(i=0; i<DHT_SENSOR_NUMBER_OF_SENSORS; i++) {
            if (dhtSens_hasNewTemperature(i)) {
                sprintf(debugTempBuf, "\nT%d: %d.%d",
                        i,
                        dhtSens_getTemperatureInteger(i),
                        dhtSens_getTemperatureDecimal(i));
                debugPutString(debugTempBuf);
            }
            if (dhtSens_hasNewHumidity(i)) {
                sprintf(debugTempBuf, "\nRH%d: %d.%d",
                        i,
                        dhtSens_getHumidityInteger(i),
                        dhtSens_getHumidityDecimal(i));
                debugPutString(debugTempBuf);
            }
        }
    }//end while
}
« Last Edit: February 25, 2014, 04:00:26 PM by modtro2 »

computski

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: DHT22 and DHT11 Sensor Library
« Reply #11 on: March 25, 2014, 05:55:17 PM »
Note on DHT22 sensors:
I've used these before, wrote some routines to poll them on a 65EBC.  I have found a couple of issues with these devices;

Firstly, you need to regularly poll them, because it seems that if you don't read the device for say 30 mins, then the next read will not read the current conditions, rather it reads the conditions 30 minutes ago! seems to buffer its last read before it sleeps.

To get around this, I read them every 60 secs and buffer the results in RAM, that way max lag to real world conditions is only 60 sec.

Secondly, the devices don't last long in high humidity conditions.  I used them to monitor sub-floor humidity in my house and the RH regularly hits 99 to 100% non condensing.  The device is spec'd to handle this, but in reality it fails after a few months and will permanently read 99 RH or 01 RH.  Apparently you can try and recalibrate them by baking them but I have not been successful.  Sensor has to be binned.   I sourced all of mine for about 5 AUD on Ebay, but its more the inconvenience of having to replace the device.