4 Replies Latest reply on Oct 23, 2017 5:28 PM by Amie_coolman93

    Problem with the example: Enabling DHT11 Humidity Sensor.

    estebaan

      Hi

       

      I want to make the example from the following page Intel® Quark™ MCU D2000 — Enable DHT11 Humidity Sensor: Application... ,but when I set the code, it does not compile and it shows errors.

       

      The code is:

       

      #include "qm_gpio.h" /*Calling QMSI GPIO API. See section 3.1 for more details. */

      #include "qm_scss.h" /*Calling QMSI SCSS API*/

      #define PIN_DHT11 0

       

       

      int main(void) {

        qm_gpio_port_config_t cfg;

        uint32_t cycles[80], i;

        uint8_t data[5];

       

       

        typedef struct {

        uint32_t direction; /* GPIO direction, 0b: input, 1b: output */

        uint32_t int_en; /* Interrupt enable */

        uint32_t int_type; /* Interrupt type, 0b: level; 1b: edge */

        uint32_t int_polarity; /* Interrupt polarity, 0b: low, 1b: high */

        uint32_t int_debounce; /* Debounce on/off */

        uint32_t int_bothedge; /* Interrupt on both rising and falling edges */

        void (*callback)(uint32_t int_status); /* Callback function */

        } qm_gpio_port_config_t;

       

       

        data[0] = data[1] = data[2] = data[3] = data[4] = 0;

        QM_PUTS( "DHT11 example");

       

       

        cfg.direction = BIT(PIN_DHT11);

        qm_gpio_set_config(QM_GPIO_0, &cfg);

        qm_gpio_set_pin(QM_GPIO_0, PIN_DHT11); /* set HIGH to DHT11 pin */

        clk_sys_udelay(250000); /* 250 ms */

        /* The three line above written in QMSI and are similar to Arduino’s

        *‘digitalWrite(PIN_DHT11, HIGH);’ which is to let pull-up raise data line level

        * And start reading the DHT11. After that proceed with 250 ms delay.

        * See section 3.4 for more details.

        */

        /* Send DHT11 start signal */

        qm_gpio_clear_pin(QM_GPIO_0, PIN_DHT11); /* set DHT11 pin LOW */

        clk_sys_udelay(20000); /* 20 ms */

        qm_gpio_set_pin(QM_GPIO_0, PIN_DHT11); /* set DHT11 pin HIGH */

        clk_sys_udelay(40); /* 40 us */

       

       

        cfg.direction = 0;

        qm_gpio_set_config(QM_GPIO_0, &cfg);

        clk_sys_udelay(10); /* 10 us */

       

       

        uint32_t expectPulse(bool level); /*function to read DHT11 pulse*/

        {

        uint32_t count = 0;

      while (qm_gpio_read_pin(QM_GPIO_0, PIN_DHT11) == level) {   /*Error*/

        if (count++ >= 100000) { /* 1 millisecond to read DHT11 pulse */

        return 0; /* Exceeded timeout, fail. */

        }

        }

        return count;

        }

       

       

        while (1) {

        if (!expectPulse(false)) {

        QM_PUTS("Timeout waiting for start signal low pulse." );

        continue;

        }

        if (!expectPulse(true)) {

        QM_PUTS("Timeout waiting for start signal high pulse.");

        continue;

        }

        for (i = 0; i < 80; i += 2) {

        cycles[i] = expectPulse(0); /* LOW */

        cycles[i + 1] = expectPulse(1); /* HIGH */

        }

        /*

        // Inspect pulses and determine which ones are 0 (high state cycle count < low

        // state cycle count), or 1 (high state cycle count > low state cycle count).

        */

        for (i = 0; i < 40; ++i) {

        uint32_t lowCycles = cycles[2 * i];

        uint32_t highCycles = cycles[2 * i + 1];

        if ((lowCycles == 0) || (highCycles == 0)) {

        QM_PUTS("Timeout waiting for pulse.");

        continue;

        }

        data[i / 8] <<= 1;

        /*Now compare the low and high cycle times to see if the bit is a 0 or 1. */

        if (highCycles > lowCycles) {

        /* // High cycles are greater than 50us low cycle count, must be a 1. */

        data[i / 8] |= 1;

        }

        /*

        // Else high cycles are less than (or equal to, a weird case) the 50us low

        // cycle count so this must be a zero. Nothing needs to be changed in the

        // stored data.

        */

        }QM_PRINTF( "Receive %d %d %d %d %d\n", data[0], data[1],data[2],data[3],

        data[4]);

        uint8_t TempF = data[2] * 1.8 + 32;

        QM_PRINTF ("h : %d, t : %d, f ; %d\r\n", data[0],data[2],TempF);

        /* Check we read 40 bits and that the checksum matches. */

        if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {

        } else {

        QM_PUTS("Checksum failure!");

        }

        }

        return 0;

      }

       

      The error is as follows:

       

      Multiple markers at this line

        - 'level' undeclared (first use in this function)

        - each undeclared identifier is reported only once for each function it appears in

       

      Thanks

        • 1. Re: Problem with the example: Enabling DHT11 Humidity Sensor.
          MichelleC_Intel

          Hi ,

           

          Can you double check  that you copied in the code as described in the doc -- below compiled and ran ok for me....

           

          --Michelle.

           

          #include "qm_gpio.h" /*Calling QMSI GPIO API. See section 3.1 for more details. */
          #include "qm_scss.h" /*Calling QMSI SCSS API*/

          #define PIN_DHT11 0

           

          uint32_t expectPulse(bool level) /*function to read DHT11 pulse*/
          {
          uint32_t count = 0;
          while (qm_gpio_read_pin(QM_GPIO_0, PIN_DHT11) == level) {
          if (count++ >= 100000) { /* 1 millisecond to read DHT11 pulse */
          return 0; /* Exceeded timeout, fail. */
          }
          }
          return count;
          }

           

           

           

          int main()
          {
          qm_gpio_port_config_t cfg;
          uint32_t cycles[80], i; /* same cycle number as in the DHT11 library */
          uint8_t data[5]; /* same data number as in the DHT11 library */
          /* Reset 40 bits of received data to zero. */
          data[0] = data[1] = data[2] = data[3] = data[4] = 0;
          QM_PUTS("DHT11 example");

           

           

          while(1){

            cfg.direction = BIT(PIN_DHT11);
            qm_gpio_set_config(QM_GPIO_0, &cfg); /* Act like Arduino’s pinMode. Setting
            * DHT11 pin on D2000 dev board as input.
            * See section 3.3 for more details.
            */
            qm_gpio_set_pin(QM_GPIO_0, PIN_DHT11); /* set HIGH to DHT11 pin */
            clk_sys_udelay(250000); /* 250 ms */
            /* The three line above written in QMSI and are similar to Arduino’s
            *‘digitalWrite(PIN_DHT11,HIGH);’ which is to let pull-up raise data line level
            * And start reading the DHT11. After that proceed with 250 ms delay.
            * See section 3.4 for more details.
            */
            /* Send DHT11 start signal */
            qm_gpio_clear_pin(QM_GPIO_0, PIN_DHT11); /* set DHT11 pin LOW */
            clk_sys_udelay(20000); /* 20 ms */
            qm_gpio_set_pin(QM_GPIO_0, PIN_DHT11); /* set DHT11 pin HIGH */
            clk_sys_udelay(40); /* 40 us */

            cfg.direction = 0;
            qm_gpio_set_config(QM_GPIO_0, &cfg); /* Setting the DHT11 pin as output to
            * start listening from the DHT11
            */
            clk_sys_udelay(10); /* 10 us */

            if(!expectPulse(false))
            {
            QM_PUTS("Timeout waiting for start signal low pulse.");
            continue;
            }
            if(!expectPulse(true))
            {
            QM_PUTS("Timeout waiting for start signal high pulse.");
            continue;
            }
            for (i=0; i<80; i+=2) {
            cycles[i] = expectPulse(0);
            cycles[i+1] = expectPulse(1);
            }
            /*
            // Inspect pulses and determine which ones are 0 (high state cycle count < low
            // state cycle count), or 1 (high state cycle count > low state cycle count).
            */
            for (i=0; i<40; ++i) {
            uint32_t lowCycles = cycles[2*i];
            uint32_t highCycles = cycles[2*i+1];
            if ((lowCycles == 0) || (highCycles == 0)) {
            QM_PUTS("Timeout waiting for pulse.");
            continue ;
            }
            data[i/8] <<= 1;
            /*Now compare the low and high cycle times to see if the bit is a 0 or 1. */
            if (highCycles > lowCycles) {
            /* // High cycles are greater than 50us low cycle count, must be a 1. */
            data[i/8] |= 1;
            }
            /*
            // Else high cycles are less than (or equal to, a weird case) the 50us low
            // cycle count so this must be a zero. Nothing needs to be changed in the
            // stored data.
            */
            }
            QM_PRINTF("Receive %d %d %d %d %d\n",data[0],data[1],data[2],data[3],data[4]);
            uint8_t TempF = data[2]*1.8 +32;
            QM_PRINTF("h : %d, t : %d, f ; %d r\n", data[0],data[2],TempF);
            /* Check we read 40 bits and that the checksum matches. */
            if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) {
            }
            else
            {
            QM_PUTS("Checksum failure!");
            }
            }
            return 0;
          }

          • 3. Re: Problem with the example: Enabling DHT11 Humidity Sensor.
            Amie_coolman93

            Hi,

            right now I'm having an internship related to IoT. They gave me the C1000 to play with DHT22.

            Right now I'm still cannot produce any value for Temperature and Humidity in Serial monitor.

            Do you know how to read the value?

            The program skip the calculation part and there is no bits that convert to Temperature and Humidity

            Below is the new coding and the image attached.WhatsApp Image 2017-10-23 at 3.34.25 PM.jpeg

             

            #include "clk.h"

            #include "qm_common.h"

            #include "qm_gpio.h"

            #include "qm_pinmux.h"

            #include "qm_pin_functions.h"

            #include "stdio.h"

            #include "stdlib.h"

            #include "stdint.h"

            #include <inttypes.h>

            #include <unistd.h>

            #include <sys/types.h>

             

            #define PIN_DHT22 22

            #define LED_PIN_ID (QM_PIN_ID_55)

            #define PIN_MUX_FN (QM_PIN_55_FN_GPIO_21)

             

            #define MAX_INTERVAL 85

            static uint8_t data[5] = { 0, 0, 0, 0, 0 };

            static qm_gpio_state_t state = 1 ;//QM_GPIO_HIGH=1;

             

            static void pin_mux_setup(void)

            {

            qm_pmux_select(LED_PIN_ID, PIN_MUX_FN);

            }

             

             

            static uint8_t sizecvt(const int read)

            {

              //digitalRead() and friends from wiringpi are defined as returning a value

              //< 256. However, they are returned as int() types. This is a safety function

             

              if (read > 255 || read < 0)

              {

                QM_PRINTF("Invalid data \n");

                exit(EXIT_FAILURE);

              }

              return (uint8_t)read;

            }

             

             

            static void read_dht_data(void)

            {

            static uint8_t laststate = true;

            static uint8_t counter = 0;

            static uint8_t j = 0, i;

             

            data[0] = data[1] = data[2] = data[3] = data[4] = 0;

             

            static qm_gpio_port_config_t cfg;

             

            //microseconds Minimum number of micro seconds to delay for.

            //void clk_sys_udelay(uint32_t microseconds);

            /* pull pin down for 20 milliseconds */

            //Act like Arduino’s pinMode as output.

            //pinMode( DHT_PIN, OUTPUT );

            cfg.direction = 0;

            qm_gpio_set_config(QM_GPIO_0, &cfg);

            qm_gpio_set_pin(QM_GPIO_0, PIN_DHT22);

            clk_sys_udelay(10); //10 ms

             

            //digitalWrite( DHT_PIN, LOW ) set DHT22 pin LOW

            qm_gpio_clear_pin(QM_GPIO_0, PIN_DHT22);

            clk_sys_udelay(20); //20 ms

             

            //Act like Arduino’s pinMode as input.

            //pinMode( DHT_PIN, INPUT );

            // then pull it up for 40 microseconds

            qm_gpio_set_pin(QM_GPIO_0, PIN_DHT22);

            clk_sys_udelay(40); //40 ms

             

            // prepare to read the pin

            cfg.direction = BIT(PIN_DHT22);

            qm_gpio_set_config(QM_GPIO_0, &cfg);

             

             

            //detect change and read data

            for ( i = 0; i<MAX_INTERVAL ; i++ )

            {

            counter = 0;

            while (sizecvt(qm_gpio_read_pin(QM_GPIO_0, PIN_DHT22, &state)) == laststate) //digitalRead( DHT_PIN )

            {

            counter++;

            clk_sys_udelay(1); //delayMicroseconds( 1 )

            if ( counter == 255)

            {

            break;

            }

            }

            laststate = sizecvt(qm_gpio_read_pin(QM_GPIO_0, PIN_DHT22, &state)); //digitalRead( DHT_PIN )

             

            if ( counter == 255 ) break;

             

            //ignore first 3 transitions

            if ( (i >= 4) && (i % 2 == 0) )

            {

            //shove each bit into the storage bytes

            data[j / 8] <<= 1;

            if ( counter > 16 )

            data[j/8] |= 1;   //OR Operator '|='

            j++;

            }

            }

             

            // 8b = 8 bits - RH = relative humidity

            // DATA = 8b of high RH data + 8b of low RH data + 8b of high temperature data + 8b of low temperature data + 8b CRC

            //pico_printf("Data (%d): 0x%x 0x%x 0x%x 0x%x 0x%x\n", j, data[0], data[1], data[2], data[3], data[4]);

             

            //check we read 40 bits (8bit x 5 ) + verify checksum in the last byte

            //print it out if data is good

             

            if ( (j >= 40) && (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )

            {

            float t, h;

                    h = (float)data[0] * 256 + (float)data[1];

                    h /= 10;

                    t = (float)(data[2] & 0x7F)* 256 + (float)data[3];

                    t /= 10.0;

                    if ((data[2] & 0x80) != 0)  t *= -1;

             

                QM_PRINTF("Humidity = %.2f %% Temperature = %.2f *C \n", h, t );

            //float f = c * 1.8f + 32;

            //pico_printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n", h, c, f );

            }

            else

            {

            QM_PUTS( "Error: read pin failed\n" );

            return read_dht_data();

            }

            }

             

            int main()

            {

            QM_PUTS("DHT22 temperature/humidity test\n");

            clk_sys_udelay( 5000 );

            pin_mux_setup();

             

             

            while (1)

            {

            read_dht_data();

            clk_sys_udelay( 1000 ); /* wait 5 seconds before next read */

            }

            clk_sys_udelay( 2000 );

            return 0;

            }

            • 4. Re: Problem with the example: Enabling DHT11 Humidity Sensor.
              Amie_coolman93

              Hi MicelleC_Intel,

              Now after several troubleshoot and I've got the result in serial terminal, but still the sensor not give any value to c1000.Please help me to settle this problem

              (coding from PDF ENABLING DHT IN D2000, I cahnge it to C1000 availability)3.JPG

              #include "qm_gpio.h" /*Calling QMSI GPIO API. See section 3.1 for more details. */

              #include "clk.h"

              #include "qm_pinmux.h"

              #include "qm_pin_functions.h"

              //#include "qm_scss.h" /*Calling QMSI SCSS API*/

               

              #define PIN_DHT22 22

              #define LED_PIN_ID (QM_PIN_ID_56)

              #define PIN_MUX_FN (QM_PIN_56_FN_GPIO_22)

               

              //static qm_ss_gpio_state_t state_ss;

              static qm_gpio_state_t state_ss;

              uint32_t expectPulse(bool level) /*function to read DHT11 pulse*/

              {

              uint32_t count = 0;

              while (qm_gpio_read_pin(QM_GPIO_0, PIN_DHT22, &state_ss) == level)

              {

              if (count++ >= 100000)

              { /* 1 millisecond to read DHT11 pulse */

              return 0; /* Exceeded timeout, fail. */

              }

              }

              return count;

              }

               

              static void pin_mux_setup(void)

              {

              qm_pmux_select(LED_PIN_ID, PIN_MUX_FN);

              }

               

              int main()

              {

              qm_gpio_port_config_t cfg;

              pin_mux_setup();

              uint32_t cycles[80], i; /* same cycle number as in the DHT11 library */

              uint8_t data[5]; /* same data number as in the DHT11 library */

               

              /* Reset 40 bits of received data to zero. */

              data[0] = data[1] = data[2] = data[3] = data[4] = 0;

               

              QM_PUTS("DHT22 example");

               

              while(1)

              {

              cfg.direction = BIT(PIN_DHT22);

              qm_gpio_set_config(QM_GPIO_0, &cfg);

              /* Act like Arduino’s pinMode. Setting

              * DHT11 pin on D2000 dev board as input.

              * See section 3.3 for more details.

              */

               

              qm_gpio_set_pin(QM_GPIO_0, PIN_DHT22); /* set HIGH to DHT11 pin */

              clk_sys_udelay(250000); /* 250 ms */

              /* The three line above written in QMSI and are similar to Arduino’s

              *‘digitalWrite(PIN_DHT11,HIGH);’ which is to let pull-up raise data line level

              * And start reading the DHT11. After that proceed with 250 ms delay.

              * See section 3.4 for more details.

              */

               

              /* Send DHT11 start signal */

              qm_gpio_clear_pin(QM_GPIO_0, PIN_DHT22); /* set DHT11 pin LOW */

              clk_sys_udelay(20000); /* 20 ms */

               

              qm_gpio_set_pin(QM_GPIO_0, PIN_DHT22); /* set DHT11 pin HIGH */

              clk_sys_udelay(40); /* 40 us */

               

               

              cfg.direction = 0;

              qm_gpio_set_config(QM_GPIO_0, &cfg); /* Setting the DHT11 pin as output to

              * start listening from the DHT11 */

              clk_sys_udelay(10); /* 10 us */

               

              if(!expectPulse(false))

                {

                QM_PUTS("Timeout waiting for start signal low pulse.");

                //continue;

                }

              if(!expectPulse(true))

                {

                QM_PUTS("Timeout waiting for start signal high pulse.");

                //continue;

                }

              for (i=0; i<80; i+=2)

                {

                cycles[i]   = expectPulse(0);

                cycles[i+1] = expectPulse(1);

                }

                /*

                // Inspect pulses and determine which ones are 0 (high state cycle count < low

                // state cycle count), or 1 (high state cycle count > low state cycle count).

                */

              for (i=0; i<40; ++i)

                {

                uint32_t lowCycles = cycles[2*i];

                uint32_t highCycles = cycles[2*i+1];

                if ((lowCycles == 0) || (highCycles == 0)) {

                QM_PUTS("Timeout waiting for pulse.");

                //continue ;

                }

              data[i/8] <<= 1;

               

                /*Now compare the low and high cycle times to see if the bit is a 0 or 1. */

              if (highCycles > lowCycles)

                {

                /* // High cycles are greater than 50us low cycle count, must be a 1. */

                data[i/8] |= 1;

                }

                /*

               

                // Else high cycles are less than (or equal to, a weird case) the 50us low

                // cycle count so this must be a zero. Nothing needs to be changed in the

                // stored data.

                */

                }

               

                QM_PRINTF("Receive %d %d %d %d %d\n",data[0],data[1],data[2],data[3],data[4]);

                uint8_t TempF = data[2]*1.8+32;

                QM_PRINTF("h : %d, t : %d, f : %d \n", data[0],data[2],TempF);

                    QM_PRINTF("h: %d , data:1 \n", data[0], data[1]);

                /* Check we read 40 bits and that the checksum matches. */

              if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))

              {

              }

              else

              {

              QM_PUTS("Checksum failure!");

              }

                }

                return 0;

              }DHT11 Temperature Sensor IssueProblem with the example: Enabling DHT11 Humidity Sensor.@