Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. DHT and DOOR sensor

DHT and DOOR sensor

Scheduled Pinned Locked Moved My Project
20 Posts 6 Posters 2.2k Views 6 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Terence Faul

    Will give that a go. But if I load the DHT only sketch on the same node it reads fine.

    Could you check my sketch I’m a bit of a noob with code

    mfalkviddM Offline
    mfalkviddM Offline
    mfalkvidd
    Mod
    wrote on last edited by
    #9

    @terence-faul the sketch looks good to me. Could you post your "dht only" sketch?

    1 Reply Last reply
    0
    • HomerH Offline
      HomerH Offline
      Homer
      wrote on last edited by
      #10

      I can't believe I spotted am error in the code! I guess I'm starting to get a bit better at this! 😁😁

      1 Reply Last reply
      1
      • T Offline
        T Offline
        Terence Faul
        wrote on last edited by
        #11
        /**
         * The MySensors Arduino library handles the wireless radio link and protocol
         * between your home built sensors/actuators and HA controller of choice.
         * The sensors forms a self healing radio network with optional repeaters. Each
         * repeater and gateway builds a routing tables in EEPROM which keeps track of the
         * network topology allowing messages to be routed to nodes.
         *
         * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
         * Copyright (C) 2013-2015 Sensnology AB
         * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
         *
         * Documentation: http://www.mysensors.org
         * Support Forum: http://forum.mysensors.org
         *
         * This program is free software; you can redistribute it and/or
         * modify it under the terms of the GNU General Public License
         * version 2 as published by the Free Software Foundation.
         *
         *******************************
         *
         * REVISION HISTORY
         * Version 1.0: Henrik EKblad
         * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
         *
         * DESCRIPTION
         * This sketch provides an example of how to implement a humidity/temperature
         * sensor using a DHT11/DHT-22.
         *
         * For more information, please visit:
         * http://www.mysensors.org/build/humidity
         *
         */
        
        // Enable debug prints
        #define MY_DEBUG
        
        // Enable and select radio type attached
        #define MY_RADIO_NRF24
        //#define MY_RADIO_RFM69
        //#define MY_RS485
        
        #include <SPI.h>
        #include <MySensors.h>
        #include <DHT.h>
        
        // Set this to the pin you connected the DHT's data pin to
        #define DHT_DATA_PIN 4
        
        // Set this offset if the sensor has a permanent small offset to the real temperatures.
        // In Celsius degrees (as measured by the device)
        #define SENSOR_TEMP_OFFSET 0
        
        // Sleep time between sensor updates (in milliseconds)
        // Must be >1000ms for DHT22 and >2000ms for DHT11
        static const uint64_t UPDATE_INTERVAL = 60000;
        
        // Force sending an update of the temperature after n sensor reads, so a controller showing the
        // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
        // the value didn't change since;
        // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
        static const uint8_t FORCE_UPDATE_N_READS = 10;
        
        #define CHILD_ID_HUM 0
        #define CHILD_ID_TEMP 1
        
        float lastTemp;
        float lastHum;
        uint8_t nNoUpdatesTemp;
        uint8_t nNoUpdatesHum;
        bool metric = true;
        
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        DHT dht;
        
        
        void presentation()
        {
          // Send the sketch version information to the gateway
          sendSketchInfo("TemperatureAndHumidity", "1.1");
        
          // Register all sensors to gw (they will be created as child devices)
          present(CHILD_ID_HUM, S_HUM);
          present(CHILD_ID_TEMP, S_TEMP);
        
          metric = getControllerConfig().isMetric;
        }
        
        
        void setup()
        {
          dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
          if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
            Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
          }
          // Sleep for the time of the minimum sampling period to give the sensor time to power up
          // (otherwise, timeout errors might occure for the first reading)
            sleep(dht.getMinimumSamplingPeriod());
        }
        
        
        void loop()
        {
          // Force reading sensor, so it works also after sleep()
          dht.readSensor(true);
        
          // Get temperature from DHT library
          float temperature = dht.getTemperature();
          if (isnan(temperature)) {
            Serial.println("Failed reading temperature from DHT!");
          } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
            // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
            lastTemp = temperature;
        
            // apply the offset before converting to something different than Celsius degrees
            temperature += SENSOR_TEMP_OFFSET;
        
            if (!metric) {
              temperature = dht.toFahrenheit(temperature);
            }
            // Reset no updates counter
            nNoUpdatesTemp = 0;
            send(msgTemp.set(temperature, 1));
        
            #ifdef MY_DEBUG
            Serial.print("T: ");
            Serial.println(temperature);
            #endif
          } else {
            // Increase no update counter if the temperature stayed the same
            nNoUpdatesTemp++;
          }
        
          // Get humidity from DHT library
          float humidity = dht.getHumidity();
          if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
          } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
            // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
            lastHum = humidity;
            // Reset no updates counter
            nNoUpdatesHum = 0;
            send(msgHum.set(humidity, 1));
        
            #ifdef MY_DEBUG
            Serial.print("H: ");
            Serial.println(humidity);
            #endif
          } else {
            // Increase no update counter if the humidity stayed the same
            nNoUpdatesHum++;
          }
        
          // Sleep for a while to save energy
          sleep(UPDATE_INTERVAL);
        }
        
        mfalkviddM 1 Reply Last reply
        0
        • T Terence Faul
          /**
           * The MySensors Arduino library handles the wireless radio link and protocol
           * between your home built sensors/actuators and HA controller of choice.
           * The sensors forms a self healing radio network with optional repeaters. Each
           * repeater and gateway builds a routing tables in EEPROM which keeps track of the
           * network topology allowing messages to be routed to nodes.
           *
           * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
           * Copyright (C) 2013-2015 Sensnology AB
           * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
           *
           * Documentation: http://www.mysensors.org
           * Support Forum: http://forum.mysensors.org
           *
           * This program is free software; you can redistribute it and/or
           * modify it under the terms of the GNU General Public License
           * version 2 as published by the Free Software Foundation.
           *
           *******************************
           *
           * REVISION HISTORY
           * Version 1.0: Henrik EKblad
           * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
           *
           * DESCRIPTION
           * This sketch provides an example of how to implement a humidity/temperature
           * sensor using a DHT11/DHT-22.
           *
           * For more information, please visit:
           * http://www.mysensors.org/build/humidity
           *
           */
          
          // Enable debug prints
          #define MY_DEBUG
          
          // Enable and select radio type attached
          #define MY_RADIO_NRF24
          //#define MY_RADIO_RFM69
          //#define MY_RS485
          
          #include <SPI.h>
          #include <MySensors.h>
          #include <DHT.h>
          
          // Set this to the pin you connected the DHT's data pin to
          #define DHT_DATA_PIN 4
          
          // Set this offset if the sensor has a permanent small offset to the real temperatures.
          // In Celsius degrees (as measured by the device)
          #define SENSOR_TEMP_OFFSET 0
          
          // Sleep time between sensor updates (in milliseconds)
          // Must be >1000ms for DHT22 and >2000ms for DHT11
          static const uint64_t UPDATE_INTERVAL = 60000;
          
          // Force sending an update of the temperature after n sensor reads, so a controller showing the
          // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
          // the value didn't change since;
          // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
          static const uint8_t FORCE_UPDATE_N_READS = 10;
          
          #define CHILD_ID_HUM 0
          #define CHILD_ID_TEMP 1
          
          float lastTemp;
          float lastHum;
          uint8_t nNoUpdatesTemp;
          uint8_t nNoUpdatesHum;
          bool metric = true;
          
          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
          DHT dht;
          
          
          void presentation()
          {
            // Send the sketch version information to the gateway
            sendSketchInfo("TemperatureAndHumidity", "1.1");
          
            // Register all sensors to gw (they will be created as child devices)
            present(CHILD_ID_HUM, S_HUM);
            present(CHILD_ID_TEMP, S_TEMP);
          
            metric = getControllerConfig().isMetric;
          }
          
          
          void setup()
          {
            dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
            if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
              Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
            }
            // Sleep for the time of the minimum sampling period to give the sensor time to power up
            // (otherwise, timeout errors might occure for the first reading)
              sleep(dht.getMinimumSamplingPeriod());
          }
          
          
          void loop()
          {
            // Force reading sensor, so it works also after sleep()
            dht.readSensor(true);
          
            // Get temperature from DHT library
            float temperature = dht.getTemperature();
            if (isnan(temperature)) {
              Serial.println("Failed reading temperature from DHT!");
            } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
              // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
              lastTemp = temperature;
          
              // apply the offset before converting to something different than Celsius degrees
              temperature += SENSOR_TEMP_OFFSET;
          
              if (!metric) {
                temperature = dht.toFahrenheit(temperature);
              }
              // Reset no updates counter
              nNoUpdatesTemp = 0;
              send(msgTemp.set(temperature, 1));
          
              #ifdef MY_DEBUG
              Serial.print("T: ");
              Serial.println(temperature);
              #endif
            } else {
              // Increase no update counter if the temperature stayed the same
              nNoUpdatesTemp++;
            }
          
            // Get humidity from DHT library
            float humidity = dht.getHumidity();
            if (isnan(humidity)) {
              Serial.println("Failed reading humidity from DHT");
            } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
              // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
              lastHum = humidity;
              // Reset no updates counter
              nNoUpdatesHum = 0;
              send(msgHum.set(humidity, 1));
          
              #ifdef MY_DEBUG
              Serial.print("H: ");
              Serial.println(humidity);
              #endif
            } else {
              // Increase no update counter if the humidity stayed the same
              nNoUpdatesHum++;
            }
          
            // Sleep for a while to save energy
            sleep(UPDATE_INTERVAL);
          }
          
          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #12

          Thanks @terence-faul
          I can't see any problem with the sketches.

          1 Reply Last reply
          0
          • skywatchS Offline
            skywatchS Offline
            skywatch
            wrote on last edited by
            #13

            @Terence-Faul The only thing I can say is that it is generally good practice to put all #include statements at the start of the code, then all the #define statements and finally all other items. At the moment you #define things before you #include the library and maybe that is an issue for the compiler?

            mfalkviddM 1 Reply Last reply
            0
            • skywatchS skywatch

              @Terence-Faul The only thing I can say is that it is generally good practice to put all #include statements at the start of the code, then all the #define statements and finally all other items. At the moment you #define things before you #include the library and maybe that is an issue for the compiler?

              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by mfalkvidd
              #14

              @skywatch while your recommendation is good in general, all MySensors defines (MY_*) must be defined before including MySensors.h. Otherwise the defines won't be available to the library, so the library will need to fall back on the default settings.

              skywatchS 1 Reply Last reply
              0
              • T Offline
                T Offline
                Terence Faul
                wrote on last edited by
                #15

                Thanks so much for the help, what is the next step then? create two sensors?

                K 1 Reply Last reply
                0
                • T Terence Faul

                  Thanks so much for the help, what is the next step then? create two sensors?

                  K Offline
                  K Offline
                  kimot
                  wrote on last edited by kimot
                  #16

                  @terence-faul
                  comment step by step blocks of code for DOOR switch and try when it start to work.

                  1 Reply Last reply
                  0
                  • mfalkviddM mfalkvidd

                    @skywatch while your recommendation is good in general, all MySensors defines (MY_*) must be defined before including MySensors.h. Otherwise the defines won't be available to the library, so the library will need to fall back on the default settings.

                    skywatchS Offline
                    skywatchS Offline
                    skywatch
                    wrote on last edited by
                    #17

                    @mfalkvidd Ah yes, I forgot that bit!

                    1 Reply Last reply
                    0
                    • skywatchS Offline
                      skywatchS Offline
                      skywatch
                      wrote on last edited by
                      #18
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • T Terence Faul

                        Hi

                        Please assist, I am trying to combine the default straight from the examples, but the DHT give an error on compile

                        /**
                         * The MySensors Arduino library handles the wireless radio link and protocol
                         * between your home built sensors/actuators and HA controller of choice.
                         * The sensors forms a self healing radio network with optional repeaters. Each
                         * repeater and gateway builds a routing tables in EEPROM which keeps track of the
                         * network topology allowing messages to be routed to nodes.
                         *
                         * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
                         * Copyright (C) 2013-2015 Sensnology AB
                         * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
                         *
                         * Documentation: http://www.mysensors.org
                         * Support Forum: http://forum.mysensors.org
                         *
                         * This program is free software; you can redistribute it and/or
                         * modify it under the terms of the GNU General Public License
                         * version 2 as published by the Free Software Foundation.
                         *
                         *******************************
                         *
                         * REVISION HISTORY
                         * Version 1.0: Henrik EKblad
                         * Version 1.1 - 2016-07-20: Converted to MySensors v2.0 and added various improvements - Torben Woltjen (mozzbozz)
                         *
                         * DESCRIPTION
                         * This sketch provides an example of how to implement a humidity/temperature
                         * sensor using a DHT11/DHT-22.
                         *
                         * For more information, please visit:
                         * http://www.mysensors.org/build/humidity
                         *
                         */
                        
                        // Enable debug prints
                        #define MY_DEBUG
                        
                        // Enable and select radio type attached
                        #define MY_RADIO_RF24
                        //#define MY_RADIO_RFM69
                        //#define MY_RS485
                        
                        #include <SPI.h>
                        #include <MySensors.h>
                        #include <DHT.h>
                        #include <Bounce2.h>
                        
                        // Set this to the pin you connected the DHT's data pin to
                        #define DHT_DATA_PIN 3
                        
                        #define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
                        
                        // Set this offset if the sensor has a permanent small offset to the real temperatures.
                        // In Celsius degrees (as measured by the device)
                        #define SENSOR_TEMP_OFFSET 0
                        
                        // Sleep time between sensor updates (in milliseconds)
                        // Must be >1000ms for DHT22 and >2000ms for DHT11
                        static const uint64_t UPDATE_INTERVAL = 60000;
                        
                        // Force sending an update of the temperature after n sensor reads, so a controller showing the
                        // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
                        // the value didn't change since;
                        // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
                        static const uint8_t FORCE_UPDATE_N_READS = 10;
                        
                        #define CHILD_ID 3
                        #define CHILD_ID_HUM 0
                        #define CHILD_ID_TEMP 1
                        
                        
                        float lastTemp;
                        float lastHum;
                        uint8_t nNoUpdatesTemp;
                        uint8_t nNoUpdatesHum;
                        bool metric = true;
                        
                        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                        DHT dht;
                        
                        Bounce debouncer = Bounce();
                        int oldValue=-1;
                        
                        // Change to V_LIGHT if you use S_LIGHT in presentation below
                        MyMessage msg(CHILD_ID,V_TRIPPED);
                        
                        
                        void presentation()
                        {
                          // Send the sketch version information to the gateway
                          sendSketchInfo("TemperatureAndHumidity", "1.1");
                        
                          // Register all sensors to gw (they will be created as child devices)
                          present(CHILD_ID_HUM, S_HUM);
                          present(CHILD_ID_TEMP, S_TEMP);
                        
                        	// Register binary input sensor to gw (they will be created as child devices)
                        // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage.
                        // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
                          present(CHILD_ID, S_DOOR);
                        
                          metric = getControllerConfig().isMetric;
                        }
                        
                        
                        void setup()
                        {
                          dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
                          if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
                            Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
                          }
                          // Sleep for the time of the minimum sampling period to give the sensor time to power up
                          // (otherwise, timeout errors might occure for the first reading)
                          sleep(dht.getMinimumSamplingPeriod());
                        	// Setup the button
                        pinMode(BUTTON_PIN,INPUT);
                        // Activate internal pull-up
                        digitalWrite(BUTTON_PIN,HIGH);
                        
                        // After setting up the button, setup debouncer
                        debouncer.attach(BUTTON_PIN);
                        debouncer.interval(5);
                        }
                        
                        
                        void loop()
                        {
                          // Force reading sensor, so it works also after sleep()
                          dht.readSensor(true);
                        
                          // Get temperature from DHT library
                          float temperature = dht.getTemperature();
                          if (isnan(temperature)) {
                            Serial.println("Failed reading temperature from DHT!");
                          } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
                            // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
                            lastTemp = temperature;
                        
                            // apply the offset before converting to something different than Celsius degrees
                            temperature += SENSOR_TEMP_OFFSET;
                        
                            if (!metric) {
                              temperature = dht.toFahrenheit(temperature);
                            }
                            // Reset no updates counter
                            nNoUpdatesTemp = 0;
                            send(msgTemp.set(temperature, 1));
                        
                            #ifdef MY_DEBUG
                            Serial.print("T: ");
                            Serial.println(temperature);
                            #endif
                          } else {
                            // Increase no update counter if the temperature stayed the same
                            nNoUpdatesTemp++;
                          }
                        
                          // Get humidity from DHT library
                          float humidity = dht.getHumidity();
                          if (isnan(humidity)) {
                            Serial.println("Failed reading humidity from DHT");
                          } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
                            // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
                            lastHum = humidity;
                            // Reset no updates counter
                            nNoUpdatesHum = 0;
                            send(msgHum.set(humidity, 1));
                        
                            #ifdef MY_DEBUG
                            Serial.print("H: ");
                            Serial.println(humidity);
                            #endif
                          } else {
                            // Increase no update counter if the humidity stayed the same
                            nNoUpdatesHum++;
                        
                        		debouncer.update();
                          // Get the update value
                          int value = debouncer.read();
                        
                          if (value != oldValue) {
                             // Send in the new value
                             send(msg.set(value==HIGH ? 1 : 0));
                             oldValue = value;
                          }
                        
                        
                          // Sleep for a while to save energy
                          sleep(UPDATE_INTERVAL);
                        }
                        
                        

                        The error is below

                        /Users/terencefaul/Documents/PlatformIO/Projects/Mysensors DHT and Door/src/BinarySwitchSleepSensor.ino.ino: In function 'void loop()':
                        /Users/terencefaul/Documents/PlatformIO/Projects/Mysensors DHT and Door/src/BinarySwitchSleepSensor.ino.ino:129:22: error: no matching function for call to 
                        'DHT::readSensor(bool)'
                        dht.readSensor(true);```
                        S Offline
                        S Offline
                        sharpy
                        wrote on last edited by
                        #19

                        Hi @terence-faul did you fix this?

                        i'm also trying too merge a door & dht sketch but have not got a clue where too start

                        if you have solved this can you post the working sketch

                        1 Reply Last reply
                        0
                        • HomerH Offline
                          HomerH Offline
                          Homer
                          wrote on last edited by
                          #20

                          How much power are you feeding the sensor? I've had issues when I've fed too little power and it's resolved once I use more power.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          39

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.1k

                          Posts


                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • MySensors
                          • OpenHardware.io
                          • Categories
                          • Recent
                          • Tags
                          • Popular