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. 3-in-1 Humidity Temp and Motion

3-in-1 Humidity Temp and Motion

Scheduled Pinned Locked Moved My Project
motion3 in 1temphumidity
47 Posts 28 Posters 43.2k Views 22 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #35

    I did some work today getting this temp / humidity / motion combo sensor running. The main issue I ran into was that though the temp and humidity did not report their values to the GW unless there was a change in values or the max no update trigger was reached, the motion sensor would report it's state value each cycle. @McPostal you also should be seeing this in your code. While this is not an issue if you are powering with a AC adaptor, I am planning on using batteries down the road for these sensors so I do not want it to broadcast unless required.

    I added some code to resolve this. I am still testing this out but in theory this code should allows the Motion sensor to sleep and only report on a state change or when the max no update trigger is reached.

    /**
     * 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
    //#define MY_DEBUG_VERBOSE_RF24
    
    // 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
    #define MOTION_DATA_PIN 3
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures
    #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
    #define CHILD_ID_MOTION 2
    
    float lastTemp;
    float lastHum;
    bool lastMotion = false;
    
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    uint8_t nNoUpdatesMotion;
    
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
    DHT dht;
    
    
    void presentation()  
    { 
      // Send the sketch version information to the gateway
      sendSketchInfo("Temp Humidity Motion", "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);
      present(CHILD_ID_MOTION, S_MOTION);
    
      metric = getConfig().isMetric;
    }
    
    
    void setup()
    {
      pinMode(MOTION_DATA_PIN, INPUT);
      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(5000);
      presentation();
    }
    
    
    void loop()      
    {  
      // Force reading sensor, so it works also after sleep()
      dht.readSensor(true);
    
      // Read digital motion value
      bool tripped = digitalRead(MOTION_DATA_PIN) == HIGH;
    
      if (tripped || nNoUpdatesMotion == FORCE_UPDATE_N_READS) {
        lastMotion = tripped;
        Serial.println(tripped);
        send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw
        nNoUpdatesMotion = 0;
      }
      else if (lastMotion) {
        Serial.println(tripped);
        send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw
        nNoUpdatesMotion = 0;
      }
      else {
        nNoUpdatesMotion++;
      }
    
      
      // 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;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        temperature += SENSOR_TEMP_OFFSET;
        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(digitalPinToInterrupt(MOTION_DATA_PIN), CHANGE, UPDATE_INTERVAL); 
    }
    

    Morkin.

    1 Reply Last reply
    1
    • McPostalM Offline
      McPostalM Offline
      McPostal
      wrote on last edited by
      #36

      Nice! I realized the motion was being updated every cycle after I posted my code. I also left out references to metric temperature readings. I fixed the motion part in my code but didn't post it yet because I had already added a second motion sensor. What I did different was not forcing the motion update. I want the time stamp to reflect the last time someone was in the room.

      1 Reply Last reply
      0
      • stockenS Offline
        stockenS Offline
        stocken
        wrote on last edited by
        #37

        Hi,
        I took your updated code for 2.0 and tried to add a relay.
        The code looks like this:

        #define MY_DEBUG
        
        // Enable and select radio type attached
        #define MY_RADIO_NRF24
        //#define MY_RADIO_RFM69
        
        #include <SPI.h>
        #include <MySensors.h>  
        #include <DHT.h>  
        
        #define CHILD_ID_HUM 10
        #define CHILD_ID_TEMP 11
        #define CHILD_ID_MOT 12 //motion sensor
        
        
        #define HUMIDITY_SENSOR_DIGITAL_PIN 4
        #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
        
        #define RELAY_1  7  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
        #define NUMBER_OF_RELAYS 1 // Total number of attached relays
        #define CHILD_ID 5
        #define RELAY_ON 1  // GPIO value to write to turn on attached relay
        #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
        
        
        DHT dht;
        float lastTemp;
        float lastHum;
        boolean metric = true; 
        
        unsigned long interval= 3000;//dht.getMinimumSamplingPeriod(); // the time we need to wait
        unsigned long previousMillis=0; // millis() returns an unsigned long.
        unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
        
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED); //me
        
        
        void before()
        {
        	for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        		// Then set relay pins in output mode
        		pinMode(pin, OUTPUT);
        		// Set relay to last known state (using eeprom storage)
        		digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        	}
        }
        
        void setup()  
        { 
          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        
         // metric = getConfig().isMetric;
        
         pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        }
        
        void presentation()  
        { 
          // Send the Sketch Version Information to the Gateway
          sendSketchInfo("4-1 Sensor", "1.0");
        
          // Register all sensors to gw (they will be created as child devices)
          present(CHILD_ID_HUM, S_HUM);
          present(CHILD_ID_TEMP, S_TEMP);
          present(CHILD_ID_MOT, V_TRIPPED); //me
          
          for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        	// Register all sensors to gw (they will be created as child devices)
        	present(sensor, S_BINARY);
        	}
        }
        
        void loop()      
        {  
          // Read digital motion value
          boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
           
        // only run loop if time has passed. 
        unsigned long currentMillis = millis(); // grab current time       
        
         // check if "interval" time has passed
         if ((unsigned long)(currentMillis - previousMillis) >= interval) {
        
         
               send(msgMot.set(tripped?"1":"0")); 
                      
          
              #ifdef MY_DEBUG
               Serial.print("Motion: ");
               Serial.println(tripped);
              #endif
        
        
        
         
          
          // Fetch temperatures from DHT sensor
          float temperature = dht.getTemperature();
          if (isnan(temperature)) {
              Serial.println("Failed reading temperature from DHT");
          } else if (temperature != lastTemp) {
            lastTemp = temperature;
            if (!metric) {
              temperature = dht.toFahrenheit(temperature);
            }
            send(msgTemp.set(temperature, 1));
            #ifdef MY_DEBUG
            Serial.print("T: ");
            Serial.println(temperature);
            #endif
          }
         
          // Fetch humidity from DHT sensor
          float humidity = dht.getHumidity();
          if (isnan(humidity)) {
              Serial.println("Failed reading humidity from DHT");
          } else if (humidity != lastHum) {
              lastHum = humidity;
              send(msgHum.set(humidity, 1));
              #ifdef MY_DEBUG
              Serial.print("H: ");
              Serial.println(humidity);
              #endif
          }   
        
        
        
        
           // save the "current" time
           previousMillis = millis();
        
         }  
          // Sleep until interrupt comes in on motion sensor. Send update every two minute.
          //sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
        
        }
        
        void receive(const MyMessage &message)
        {
        	// We only expect one type of message from controller. But we better check anyway.
        	if (message.type==V_STATUS) {
        		// Change relay state
        		digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
        		// Store state in eeprom
        		saveState(message.sensor, message.getBool());
        		// Write some debug info
        		Serial.print("Incoming change for sensor:");
        		Serial.print(message.sensor);
        		Serial.print(", New status: ");
        		Serial.println(message.getBool());
        	}
        }
        

        However, when i look in domoticz the relay isnt presented as a child.
        Plus, the motion sensor is called "S_LIGHT_LEVEL" and not "S_MOTION" as it does in my other device (that only have temp, hum motion and not a relay in the sketch).
        temp/hum sensor and motion sensor works as intended in domoticz.
        My first though is that the motion and relay somehow is a bit mixed up by the sketch, but im kinda new to this so im not sure.
        Would really appreciate if someone can look at the sketch and see if there is any obvious problem :)

        McPostalM 1 Reply Last reply
        0
        • stockenS stocken

          Hi,
          I took your updated code for 2.0 and tried to add a relay.
          The code looks like this:

          #define MY_DEBUG
          
          // Enable and select radio type attached
          #define MY_RADIO_NRF24
          //#define MY_RADIO_RFM69
          
          #include <SPI.h>
          #include <MySensors.h>  
          #include <DHT.h>  
          
          #define CHILD_ID_HUM 10
          #define CHILD_ID_TEMP 11
          #define CHILD_ID_MOT 12 //motion sensor
          
          
          #define HUMIDITY_SENSOR_DIGITAL_PIN 4
          #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
          
          #define RELAY_1  7  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
          #define NUMBER_OF_RELAYS 1 // Total number of attached relays
          #define CHILD_ID 5
          #define RELAY_ON 1  // GPIO value to write to turn on attached relay
          #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
          
          
          DHT dht;
          float lastTemp;
          float lastHum;
          boolean metric = true; 
          
          unsigned long interval= 3000;//dht.getMinimumSamplingPeriod(); // the time we need to wait
          unsigned long previousMillis=0; // millis() returns an unsigned long.
          unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
          
          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
          MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED); //me
          
          
          void before()
          {
          	for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
          		// Then set relay pins in output mode
          		pinMode(pin, OUTPUT);
          		// Set relay to last known state (using eeprom storage)
          		digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
          	}
          }
          
          void setup()  
          { 
            dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
          
           // metric = getConfig().isMetric;
          
           pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
          }
          
          void presentation()  
          { 
            // Send the Sketch Version Information to the Gateway
            sendSketchInfo("4-1 Sensor", "1.0");
          
            // Register all sensors to gw (they will be created as child devices)
            present(CHILD_ID_HUM, S_HUM);
            present(CHILD_ID_TEMP, S_TEMP);
            present(CHILD_ID_MOT, V_TRIPPED); //me
            
            for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
          	// Register all sensors to gw (they will be created as child devices)
          	present(sensor, S_BINARY);
          	}
          }
          
          void loop()      
          {  
            // Read digital motion value
            boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
             
          // only run loop if time has passed. 
          unsigned long currentMillis = millis(); // grab current time       
          
           // check if "interval" time has passed
           if ((unsigned long)(currentMillis - previousMillis) >= interval) {
          
           
                 send(msgMot.set(tripped?"1":"0")); 
                        
            
                #ifdef MY_DEBUG
                 Serial.print("Motion: ");
                 Serial.println(tripped);
                #endif
          
          
          
           
            
            // Fetch temperatures from DHT sensor
            float temperature = dht.getTemperature();
            if (isnan(temperature)) {
                Serial.println("Failed reading temperature from DHT");
            } else if (temperature != lastTemp) {
              lastTemp = temperature;
              if (!metric) {
                temperature = dht.toFahrenheit(temperature);
              }
              send(msgTemp.set(temperature, 1));
              #ifdef MY_DEBUG
              Serial.print("T: ");
              Serial.println(temperature);
              #endif
            }
           
            // Fetch humidity from DHT sensor
            float humidity = dht.getHumidity();
            if (isnan(humidity)) {
                Serial.println("Failed reading humidity from DHT");
            } else if (humidity != lastHum) {
                lastHum = humidity;
                send(msgHum.set(humidity, 1));
                #ifdef MY_DEBUG
                Serial.print("H: ");
                Serial.println(humidity);
                #endif
            }   
          
          
          
          
             // save the "current" time
             previousMillis = millis();
          
           }  
            // Sleep until interrupt comes in on motion sensor. Send update every two minute.
            //sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
          
          }
          
          void receive(const MyMessage &message)
          {
          	// We only expect one type of message from controller. But we better check anyway.
          	if (message.type==V_STATUS) {
          		// Change relay state
          		digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
          		// Store state in eeprom
          		saveState(message.sensor, message.getBool());
          		// Write some debug info
          		Serial.print("Incoming change for sensor:");
          		Serial.print(message.sensor);
          		Serial.print(", New status: ");
          		Serial.println(message.getBool());
          	}
          }
          

          However, when i look in domoticz the relay isnt presented as a child.
          Plus, the motion sensor is called "S_LIGHT_LEVEL" and not "S_MOTION" as it does in my other device (that only have temp, hum motion and not a relay in the sketch).
          temp/hum sensor and motion sensor works as intended in domoticz.
          My first though is that the motion and relay somehow is a bit mixed up by the sketch, but im kinda new to this so im not sure.
          Would really appreciate if someone can look at the sketch and see if there is any obvious problem :)

          McPostalM Offline
          McPostalM Offline
          McPostal
          wrote on last edited by McPostal
          #38

          @stocken

          Under "void presentation()" , present(CHILD_ID_MOT, V_TRIPPED); //me should be

          present(CHILD_ID_MOT, S_MOTION); //me

          that should fix the light level problem.

          I haven't done a relay yet. If no one else responds I look at it later tonight. I also have a relay but haven't had time to try and implement it yet.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Komaandy
            wrote on last edited by Komaandy
            #39

            Hello everybody,

            first off all a big thank you for creating this awsome sketch. It works perfect for me !!
            But, as you might already have guessed I have a little problem.

            I built 2 exact same nodes and flashed the exact same sketch ( only thing I changed is the child ID´s) but one node works just like a charm ( MSPIRAndy) but the other one (MYSENSOR_106) doesnt transmit its readings (?)

            Via the serial monitor the readings do exist and work perfect.

            Thanks a lot, all help is appreciated

            KOmaandy

            0_1488919647833_106.PNG

            0_1488919653276_mspiraNDY.PNG

            1 Reply Last reply
            0
            • gohanG Offline
              gohanG Offline
              gohan
              Mod
              wrote on last edited by
              #40

              Have you tried swapping radio between the 2 sensors? Just to exclude any radio issue

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Komaandy
                wrote on last edited by
                #41

                Yes i did swap the radios, but what seems to be the problem is the arduino itself.
                When swapping the arduinos ( both arduino pro mini 3,3 volt) both flashed with the same sketch one works and the other just doesnt.
                I just swapped the arduinos on the breadboard and left everything else in place...
                Thats really strange, given the fact that BOTH transmit good readings when connected to the arduino-serial-monitor ...

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  ryanbrown204
                  wrote on last edited by
                  #42

                  Do you have a schematic that you could post?

                  mfalkviddM 1 Reply Last reply
                  0
                  • R ryanbrown204

                    Do you have a schematic that you could post?

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

                    @ryanbrown204 welcome to the MySensors community!

                    This is a long thread with a lot of different people posting, so it is a bit hard to guess which schematic you are looking for. Could you clarify?

                    R 1 Reply Last reply
                    0
                    • mfalkviddM mfalkvidd

                      @ryanbrown204 welcome to the MySensors community!

                      This is a long thread with a lot of different people posting, so it is a bit hard to guess which schematic you are looking for. Could you clarify?

                      R Offline
                      R Offline
                      ryanbrown204
                      wrote on last edited by
                      #44

                      @mfalkvidd the schematic for the original 3-in-1 Humidity Temp and Motion. I would like to try me hand at building the first version and then adding the additions.

                      mfalkviddM 1 Reply Last reply
                      0
                      • R ryanbrown204

                        @mfalkvidd the schematic for the original 3-in-1 Humidity Temp and Motion. I would like to try me hand at building the first version and then adding the additions.

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

                        @ryanbrown204 temperature: https://www.mysensors.org/build/temp
                        Motion: https://www.mysensors.org/build/motion

                        See the first post in this thread for the modifications Konrad did.

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          fulljionslly
                          wrote on last edited by
                          #46
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • F Offline
                            F Offline
                            fulljionslly
                            wrote on last edited by
                            #47
                            This post is deleted!
                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            23

                            Online

                            11.7k

                            Users

                            11.2k

                            Topics

                            113.0k

                            Posts


                            Copyright 2019 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