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. Development
  3. implementing multiple sensors

implementing multiple sensors

Scheduled Pinned Locked Moved Development
83 Posts 28 Posters 86.9k Views 15 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.
  • BulldogLowellB BulldogLowell

    @Hoffan said:

    #include <Relay.h>
    #include <Sensor.h>
    #include <SPI.h>
    #include <EEPROM.h>
    #include <RF24.h>
    #include <Bounce2.h>
    #include <DHT.h>

        #define RELAY_1  7  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
        #define NUMBER_OF_RELAYS 2 
        #define RELAY_ON 0
        #define RELAY_OFF 1
    
        #define BUTTON_PIN1  3  // Arduino Digital I/O pin for button/reed switch
        #define BUTTON_PIN2  4  // Arduino Digital I/O pin for button/reed switch
        #define BUTTON_PIN3  5  // Arduino Digital I/O pin for button/reed switch
        #define BUTTON_PIN4  6  // Arduino Digital I/O pin for button/reed switch
    
        #define CHILD_ID_HUM 0
        #define CHILD_ID_TEMP 1
        #define HUMIDITY_SENSOR_DIGITAL_PIN 8
    
        Sensor gw;
        Bounce debouncer1 = Bounce();
        Bounce debouncer2 = Bounce();
        Bounce debouncer3 = Bounce();
        Bounce debouncer4 = Bounce();
    
        int oldValue1=-1;
        int oldValue2=-1;
        int oldValue3=-1;
        int oldValue4=-1;
    
        DHT dht;
        float lastTemp;
        float lastHum;
        boolean metric = true; 
    
    
        void setup()  
        {  
          gw.begin();
    
          pinMode(BUTTON_PIN1,INPUT);
          digitalWrite(BUTTON_PIN1,HIGH);
          pinMode(BUTTON_PIN2,INPUT);
          digitalWrite(BUTTON_PIN2,HIGH);
          pinMode(BUTTON_PIN3,INPUT);
          digitalWrite(BUTTON_PIN3,HIGH);
          pinMode(BUTTON_PIN4,INPUT);
          digitalWrite(BUTTON_PIN4,HIGH);
    
          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
    
          // After setting up the button, setup debouncer
          debouncer1.attach(BUTTON_PIN1);
          debouncer1.interval(5);
          debouncer2.attach(BUTTON_PIN2);
          debouncer2.interval(5);
          debouncer3.attach(BUTTON_PIN3);
          debouncer3.interval(5);
          debouncer4.attach(BUTTON_PIN4);
          debouncer4.interval(5);
    
    
    
    
          // 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 below.
          gw.sendSensorPresentation(BUTTON_PIN1, S_DOOR);  
          gw.sendSensorPresentation(BUTTON_PIN2, S_DOOR);  
          gw.sendSensorPresentation(BUTTON_PIN3, S_DOOR);  
          gw.sendSensorPresentation(BUTTON_PIN4, S_DOOR);  
    
    
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("4Door2Relay", "1.0");
    
           // Register all sensors to gw (they will be created as child devices)
          gw.sendSensorPresentation(CHILD_ID_HUM, S_HUM);
          gw.sendSensorPresentation(CHILD_ID_TEMP, S_TEMP);
    
          metric = gw.isMetricSystem();
    
          // Register all sensors to gw (they will be created as child devices)
          for (int i=0; i<NUMBER_OF_RELAYS;i++) {
            gw.sendSensorPresentation(RELAY_1+i, S_LIGHT);
          }
          // Fetch relay status
          for (int i=0; i<NUMBER_OF_RELAYS;i++) {
            // Make sure relays are off when starting up
            digitalWrite(RELAY_1+i, RELAY_OFF);
            // Then set relay pins in output mode
            pinMode(RELAY_1+i, OUTPUT);   
    
            // Request/wait for relay status
            gw.getStatus(RELAY_1+i, V_LIGHT);
            setRelayStatus(gw.getMessage()); // Wait here until status message arrive from gw
          }
    
        }
    
    
        /*
        *  Example on how to asynchronously check for new messages from gw
        */
        void loop() 
        {
           debouncer1.update();
           debouncer2.update();
           debouncer3.update();
           debouncer4.update();
    
    
    
          // Get the update value
          int value = debouncer1.read();
          if (value != oldValue1) {
             // Send in the new value
             gw.sendVariable(BUTTON_PIN1, V_TRIPPED, value==HIGH ? "1" : "0");  // Change to V_LIGHT if you use S_LIGHT in presentation above
             oldValue1 = value;
            }
    
        value = debouncer2.read();
    
        if (value != oldValue2) {
        // Send in the new value
        gw.sendVariable(BUTTON_PIN2, V_TRIPPED, value==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
        oldValue2 = value;
        }
    
        value = debouncer3.read();
    
        if (value != oldValue3) {
        // Send in the new value
        gw.sendVariable(BUTTON_PIN3, V_TRIPPED, value==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
        oldValue3 = value;
        } 
         value = debouncer4.read();
    
        if (value != oldValue4) {
        // Send in the new value
        gw.sendVariable(BUTTON_PIN4, V_TRIPPED, value==HIGH ? "1" : "0"); // Change to V_LIGHT if you use S_LIGHT in presentation above
        oldValue4 = value;
        } 
    
         delay(dht.getMinimumSamplingPeriod());
    
          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);
            }
            gw.sendVariable(CHILD_ID_TEMP, V_TEMP, temperature, 1);
              Serial.print("T: ");
              Serial.println(temperature);
          }
    
          float humidity = dht.getHumidity();
          if (isnan(humidity)) {
              Serial.println("Failed reading humidity from DHT");
          } else if (humidity != lastHum) {
              lastHum = humidity;
              gw.sendVariable(CHILD_ID_HUM, V_HUM, humidity, 1);
              Serial.print("H: ");
              Serial.println(humidity);
          }
    
    
          if (gw.messageAvailable()) {
            message_s message = gw.getMessage(); 
            setRelayStatus(message);
          }
        }
    
        void setRelayStatus(message_s message) {
          if (message.header.messageType==M_SET_VARIABLE &&
              message.header.type==V_LIGHT) {
             int incomingRelayStatus = atoi(message.data);
             // Change relay state
             digitalWrite(message.header.childId, incomingRelayStatus==1?RELAY_ON:RELAY_OFF);
             // Write some debug info
             Serial.print("Incoming change for relay on pin:");
             Serial.print(message.header.childId);
             Serial.print(", New status: ");
             Serial.println(incomingRelayStatus);
           }
        }
    

    can you elaborate on "doesn't work" ?

    do you have sensors on the Vera UI?

    Try to debug putting Serial.print(Something) in places so you can see how the sketch progresses...

    HoffanH Offline
    HoffanH Offline
    Hoffan
    wrote on last edited by
    #36

    @BulldogLowell

    Yes all the sensors came up in the Vera UI, And looks like they work for some minutes
    But when i push å button its looks like all things stop working, and after a reboot on the Arduino its working again for a short time and so on

    BulldogLowellB 2 Replies Last reply
    0
    • HoffanH Hoffan

      @BulldogLowell

      Yes all the sensors came up in the Vera UI, And looks like they work for some minutes
      But when i push å button its looks like all things stop working, and after a reboot on the Arduino its working again for a short time and so on

      BulldogLowellB Offline
      BulldogLowellB Offline
      BulldogLowell
      Contest Winner
      wrote on last edited by BulldogLowell
      #37
      This post is deleted!
      1 Reply Last reply
      0
      • HoffanH Hoffan

        @BulldogLowell

        Yes all the sensors came up in the Vera UI, And looks like they work for some minutes
        But when i push å button its looks like all things stop working, and after a reboot on the Arduino its working again for a short time and so on

        BulldogLowellB Offline
        BulldogLowellB Offline
        BulldogLowell
        Contest Winner
        wrote on last edited by
        #38

        @Hoffan said:

        @BulldogLowell

        Yes all the sensors came up in the Vera UI, And looks like they work for some minutes
        But when i push å button its looks like all things stop working, and after a reboot on the Arduino its working again for a short time and so on

        Try threading in the sensor reads into each block to read and change its state in one block for each sensor/pin.

        I edited you code so it can be read,,, attached.

        Remember AutoFormat under Tools in your arduino menu

        void loop() 
        {
          debouncer1.update();
          // Get the update value
          int value = debouncer1.read();
          if (value != oldValue1) 
          {
            gw.sendVariable(BUTTON_PIN1, V_TRIPPED, value==HIGH ? "1" : "0");
            oldValue1 = value;
          }
          //
          debouncer2.update();
          value = debouncer2.read();
          if (value != oldValue2) 
          {
            gw.sendVariable(BUTTON_PIN2, V_TRIPPED, value==HIGH ? "1" : "0");
            oldValue2 = value;
          }
          //
          debouncer3.update();
          value = debouncer3.read();
          if (value != oldValue3) 
          {
            gw.sendVariable(BUTTON_PIN3, V_TRIPPED, value==HIGH ? "1" : "0"); 
            oldValue3 = value;
          } 
          //
          debouncer4.update();
          value = debouncer4.read();
          if (value != oldValue4) 
          {
            gw.sendVariable(BUTTON_PIN4, V_TRIPPED, value==HIGH ? "1" : "0");
            oldValue4 = value;
          } 
        

        FourSensorTwoRelay.ino

        1 Reply Last reply
        0
        • W Offline
          W Offline
          waynehead99
          wrote on last edited by
          #39

          So I have created a multi sensor that uses the DHT and motion to get Temp/Hum/Motion. Problem I have though is the update of the hum/temp. I have not been able to figure out a way to get a reliable update on those without blasting the GW/Vera with unneeded announcements (disable sleep function and it keeps updating pretty much every second). So right now the script updates the temp/hum only when motion is seen. I have tried a few things with my limited knowledge of arduino and can't get this to work properly. Can someone please take a look at this sketch and tell me what I am missing? If i change the variables for sleep at the end and try to do an interrupt and a sleep delay, i get an error.
          7-21-2014 1-05-03 PM.png

          Motion_Temp_Sensor13b3.ino

          1 Reply Last reply
          0
          • W Offline
            W Offline
            waynehead99
            wrote on last edited by
            #40

            Nobody wants to take a crack at this? I figured this to be a common combo and would really like to get it to work properly.

            M 1 Reply Last reply
            0
            • B Offline
              B Offline
              Bandra
              wrote on last edited by
              #41

              I'm coming close to finalising my multisensor sketch. I'll post code once I clean it up a little. I have a newborn at home, so if I forget to put code here then somebody please remind me in a little while :)

              1 Reply Last reply
              0
              • W waynehead99

                Nobody wants to take a crack at this? I figured this to be a common combo and would really like to get it to work properly.

                M Offline
                M Offline
                mikeones
                wrote on last edited by
                #42

                @waynehead99 Here is a sketch I run with DHT and motion support.

                #include <Sleep_n0m1.h>
                #include <SPI.h>
                #include <EEPROM.h>  
                #include <RF24.h>
                #include <Sensor.h>  
                #include <DHT.h>  
                
                #define CHILD_ID_HUM 0
                #define CHILD_ID_TEMP 1
                #define HUMIDITY_SENSOR_DIGITAL_PIN 4
                
                Sensor gw;
                DHT dht;
                Sleep sleep;
                float lastTemp;
                float lastHum;
                boolean lastValue = false;
                boolean metric = false;
                
                #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
                #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                #define CHILD_ID 2   // Id of the sensor child
                
                long previousMillis_T = 0;     // will store last time temp data sent
                unsigned long startTime_T;
                unsigned long sensorInterval = 30000; // change this to desired sensor read interval in milliseconds
                
                void setup()  
                {  
                  gw.begin();
                
                  // Send the sketch version information to the gateway and Controller
                  gw.sendSketchInfo("Motion Sensor and DHT", "1.0");
                  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
                  pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
                  // Register all sensors to gw (they will be created as child devices)
                  gw.sendSensorPresentation(CHILD_ID, S_MOTION);
                  gw.sendSensorPresentation(CHILD_ID_HUM, S_HUM);
                  gw.sendSensorPresentation(CHILD_ID_TEMP, S_TEMP);
                  metric = gw.isMetricSystem();
                  startTime_T = millis();
                  Serial.println("Setup complete");
                }
                
                void loop()     
                {     
                  // Read digital motion value
                  boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
                  
                  if (lastValue != tripped) {
                	gw.sendVariable(CHILD_ID, V_TRIPPED, tripped?"1":"0");  // Send tripped value to gw
                	lastValue=tripped;
                	//Serial.println(tripped);
                  }
                 
                  if (millis() - startTime_T >= sensorInterval) {
                	delay(dht.getMinimumSamplingPeriod());
                	float temperature = dht.getTemperature();
                	if (isnan(temperature)) {
                		Serial.println("Failed reading temperature from DHT");
                	} else if (temperature) {
                	  lastTemp = temperature;
                	  if (!metric) {
                		temperature = dht.toFahrenheit(temperature);
                	  }
                	  gw.sendVariable(CHILD_ID_TEMP, V_TEMP, temperature, 1);
                		Serial.print("T: ");
                		Serial.println(temperature);
                	}
                	
                	//delay(dht.getMinimumSamplingPeriod());
                	float humidity = dht.getHumidity();
                	if (isnan(humidity)) {
                		Serial.println("Failed reading humidity from DHT");
                	} else if (humidity) {
                		lastHum = humidity;
                		gw.sendVariable(CHILD_ID_HUM, V_HUM, humidity, 1);
                		Serial.print("H: ");
                		Serial.println(humidity);
                	}
                	startTime_T = millis();
                  }
                
                  // Power down the radio.  Note that the radio will get powered back up
                  // on the next write() call.
                  delay(1000); //delay to allow serial to fully print before sleep
                  
                }
                
                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  waynehead99
                  wrote on last edited by
                  #43

                  Thanks that worked... and for my understanding... looks like your not really using interrupts at all? Looking at the code makes complete sense to me, and proves I was over thinking things on mine.

                  Thanks again.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mikeones
                    wrote on last edited by
                    #44

                    The sensor is not running on battery power so I did not spend much time with the interrupt/sleep options.

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      Tibus
                      wrote on last edited by
                      #45

                      is it the same process with the new 1.4 library version?

                      Could you please give us a exemple here or on github to have multiple sensor type on the same arduino with the new 1.4 library?

                      if it's the same implementation, we could write multiple "gw.present(ID, XXXX);" and send a message for each child ID type?

                      Thank's very much.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Tibus
                        wrote on last edited by
                        #46

                        Sorry, I found the new implementation in the "DallasTemperatureSensor" Example

                        1 Reply Last reply
                        0
                        • mountainmanM Offline
                          mountainmanM Offline
                          mountainman
                          wrote on last edited by mountainman
                          #47

                          Here is a version I'm currently using that makes use of interrupts so might be more suitable for a battery driven DHT/Motion multi sensor. It also includes the battery monitoring. Any feedback or improvements would be appreciated.

                          #include <SPI.h>
                          #include <MySensor.h>
                          #include <DHT.h>
                          
                          #define CHILD_ID_HUM 0
                          #define CHILD_ID_TEMP 1
                          #define CHILD_ID_MOTION 2   // Id of the sensor child
                          
                          #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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                          
                          unsigned long SLEEP_TIME = F; // Sleep time between reports (in milliseconds)
                          
                          
                          int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                          int oldValue=-1;
                          
                          MySensor gw;
                          DHT dht;
                          float lastTemp;
                          float lastHum;
                          boolean metric = false;
                          
                          int oldBatteryPcnt = 0;
                          int battLoop =0;
                          
                          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                          // Initialize motion message
                          MyMessage msg(CHILD_ID_MOTION, V_TRIPPED);
                          
                          boolean pinTriggered=0;//waitTime is number of seconds to hol in while loop
                          long lastDebounceTime = 0;  // the last time the output pin was toggled
                          long debounceDelay = 50;    // the debounce time; increase if the output flickers
                          
                          
                          void setup()
                          {
                          
                            // use the 1.1 V internal reference
                            analogReference(INTERNAL);
                          
                            gw.begin();
                            dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                          
                            // Send the Sketch Version Information to the Gateway
                            gw.sendSketchInfo("Motion Humidity w/ Batt", "1.0");
                          
                            // Register all sensors to gw (they will be created as child devices)
                            gw.present(CHILD_ID_HUM, S_HUM);
                            gw.present(CHILD_ID_TEMP, S_TEMP);
                            gw.present(CHILD_ID_MOTION, S_MOTION);
                          
                            pinMode(DIGITAL_INPUT_SENSOR,INPUT);      // sets the motion sensor digital pin as input
                            digitalWrite(DIGITAL_INPUT_SENSOR,HIGH);   // Activate internal pull-up
                          
                            metric = gw.getConfig().isMetric;
                          
                            check_batt();
                          
                          }
                          
                          void loop()
                          {
                            if (pinTriggered)
                            {
                          	Serial.println(millis()-lastDebounceTime);
                          	// Read digital motion value
                          	boolean value = digitalRead(DIGITAL_INPUT_SENSOR);
                          	// If the switch changed, due to noise or pressing
                          	if (value != oldValue) {
                          	  {
                          		lastDebounceTime = millis();
                          		// Send in the new value
                          		gw.send(msg.set(value==HIGH ? 1 : 0));
                          		battLoop++;
                          		oldValue = value;
                          		Serial.print("Mot: ");
                          		Serial.println(value);
                          	  }
                          	}
                          
                          	pinTriggered=0;
                          	// }
                            }
                            else
                            {
                          
                          	delay(dht.getMinimumSamplingPeriod());
                          
                          	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);
                          	  }
                          	  gw.send(msgTemp.set(temperature, 1));
                          	  battLoop++;
                          	  Serial.print("T: ");
                          	  Serial.println(temperature);
                          	}
                          
                          	float humidity = dht.getHumidity();
                          	if (isnan(humidity))
                          	{
                          	  Serial.println("Failed reading humidity from DHT");
                          	}
                          	else if (humidity != lastHum)
                          	{
                          	  lastHum = humidity;
                          	  gw.send(msgHum.set(humidity, 1));
                          	  battLoop++;
                          	  Serial.print("H: ");
                          	  Serial.println(humidity);
                          	}
                          
                          
                          	if (battLoop > 10)
                          	{
                          	  check_batt();
                          	  battLoop=0;
                          	}
                            }
                          
                            // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                            pinTriggered = gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                          }
                          
                          
                          void check_batt()
                          {
                            // get the battery Voltage
                            int sensorValue = analogRead(BATTERY_SENSE_PIN);
                            Serial.println(sensorValue);
                          
                            // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                            // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                            // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                            // 3.44/1023 = Volts per bit = 0.003363075
                            float batteryV  = sensorValue * 0.003363075;
                            int batteryPcnt = sensorValue / 10;
                          
                            Serial.print("Battery Voltage: ");
                            Serial.print(batteryV);
                            Serial.println(" V");
                          
                            Serial.print("Battery percent: ");
                            Serial.print(batteryPcnt);
                            Serial.println(" %");
                          
                            if (oldBatteryPcnt != batteryPcnt) {
                          	// Power up radio after sleep
                          	gw.sendBatteryLevel(batteryPcnt);
                          	oldBatteryPcnt = batteryPcnt;
                            }
                          }
                          
                          T C 2 Replies Last reply
                          0
                          • W Offline
                            W Offline
                            waynehead99
                            wrote on last edited by
                            #48

                            How did you get 3 options for the sleep interrupt? Whenever I would try and add the sleep_time option it would give me compile errors.

                            1 Reply Last reply
                            0
                            • mountainmanM mountainman

                              Here is a version I'm currently using that makes use of interrupts so might be more suitable for a battery driven DHT/Motion multi sensor. It also includes the battery monitoring. Any feedback or improvements would be appreciated.

                              #include <SPI.h>
                              #include <MySensor.h>
                              #include <DHT.h>
                              
                              #define CHILD_ID_HUM 0
                              #define CHILD_ID_TEMP 1
                              #define CHILD_ID_MOTION 2   // Id of the sensor child
                              
                              #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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                              
                              unsigned long SLEEP_TIME = F; // Sleep time between reports (in milliseconds)
                              
                              
                              int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                              int oldValue=-1;
                              
                              MySensor gw;
                              DHT dht;
                              float lastTemp;
                              float lastHum;
                              boolean metric = false;
                              
                              int oldBatteryPcnt = 0;
                              int battLoop =0;
                              
                              MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                              MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                              // Initialize motion message
                              MyMessage msg(CHILD_ID_MOTION, V_TRIPPED);
                              
                              boolean pinTriggered=0;//waitTime is number of seconds to hol in while loop
                              long lastDebounceTime = 0;  // the last time the output pin was toggled
                              long debounceDelay = 50;    // the debounce time; increase if the output flickers
                              
                              
                              void setup()
                              {
                              
                                // use the 1.1 V internal reference
                                analogReference(INTERNAL);
                              
                                gw.begin();
                                dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                              
                                // Send the Sketch Version Information to the Gateway
                                gw.sendSketchInfo("Motion Humidity w/ Batt", "1.0");
                              
                                // Register all sensors to gw (they will be created as child devices)
                                gw.present(CHILD_ID_HUM, S_HUM);
                                gw.present(CHILD_ID_TEMP, S_TEMP);
                                gw.present(CHILD_ID_MOTION, S_MOTION);
                              
                                pinMode(DIGITAL_INPUT_SENSOR,INPUT);      // sets the motion sensor digital pin as input
                                digitalWrite(DIGITAL_INPUT_SENSOR,HIGH);   // Activate internal pull-up
                              
                                metric = gw.getConfig().isMetric;
                              
                                check_batt();
                              
                              }
                              
                              void loop()
                              {
                                if (pinTriggered)
                                {
                              	Serial.println(millis()-lastDebounceTime);
                              	// Read digital motion value
                              	boolean value = digitalRead(DIGITAL_INPUT_SENSOR);
                              	// If the switch changed, due to noise or pressing
                              	if (value != oldValue) {
                              	  {
                              		lastDebounceTime = millis();
                              		// Send in the new value
                              		gw.send(msg.set(value==HIGH ? 1 : 0));
                              		battLoop++;
                              		oldValue = value;
                              		Serial.print("Mot: ");
                              		Serial.println(value);
                              	  }
                              	}
                              
                              	pinTriggered=0;
                              	// }
                                }
                                else
                                {
                              
                              	delay(dht.getMinimumSamplingPeriod());
                              
                              	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);
                              	  }
                              	  gw.send(msgTemp.set(temperature, 1));
                              	  battLoop++;
                              	  Serial.print("T: ");
                              	  Serial.println(temperature);
                              	}
                              
                              	float humidity = dht.getHumidity();
                              	if (isnan(humidity))
                              	{
                              	  Serial.println("Failed reading humidity from DHT");
                              	}
                              	else if (humidity != lastHum)
                              	{
                              	  lastHum = humidity;
                              	  gw.send(msgHum.set(humidity, 1));
                              	  battLoop++;
                              	  Serial.print("H: ");
                              	  Serial.println(humidity);
                              	}
                              
                              
                              	if (battLoop > 10)
                              	{
                              	  check_batt();
                              	  battLoop=0;
                              	}
                                }
                              
                                // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                pinTriggered = gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                              }
                              
                              
                              void check_batt()
                              {
                                // get the battery Voltage
                                int sensorValue = analogRead(BATTERY_SENSE_PIN);
                                Serial.println(sensorValue);
                              
                                // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                                // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                                // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                                // 3.44/1023 = Volts per bit = 0.003363075
                                float batteryV  = sensorValue * 0.003363075;
                                int batteryPcnt = sensorValue / 10;
                              
                                Serial.print("Battery Voltage: ");
                                Serial.print(batteryV);
                                Serial.println(" V");
                              
                                Serial.print("Battery percent: ");
                                Serial.print(batteryPcnt);
                                Serial.println(" %");
                              
                                if (oldBatteryPcnt != batteryPcnt) {
                              	// Power up radio after sleep
                              	gw.sendBatteryLevel(batteryPcnt);
                              	oldBatteryPcnt = batteryPcnt;
                                }
                              }
                              
                              T Offline
                              T Offline
                              Tibus
                              wrote on last edited by Tibus
                              #49

                              @mountainman
                              Thank you verry much, exactly what i need!

                              For child's id, if you have two sensor you have to change the ID in the second?

                              Sensor 1 :
                              #define CHILD_ID_HUM 0
                              #define CHILD_ID_TEMP 1
                              #define CHILD_ID_MOTION 2

                              Sensor 2:
                              #define CHILD_ID_HUM 3
                              #define CHILD_ID_TEMP 4
                              #define CHILD_ID_MOTION 5

                              1 Reply Last reply
                              0
                              • mountainmanM Offline
                                mountainmanM Offline
                                mountainman
                                wrote on last edited by
                                #50

                                @waynehead99 Are you using the new 1.4 Lib?

                                1 Reply Last reply
                                0
                                • E egbertje

                                  Somebody has good result in a working multisensor and wants to share his code?

                                  NuubiN Offline
                                  NuubiN Offline
                                  Nuubi
                                  wrote on last edited by Nuubi
                                  #51

                                  @egbertje Good Question :-) It would be nice to learn from existing working sketches..

                                  1 Reply Last reply
                                  0
                                  • AnticimexA Offline
                                    AnticimexA Offline
                                    Anticimex
                                    Contest Winner
                                    wrote on last edited by
                                    #52

                                    @waynehead99 you most likely need 1.4 Lib for the new sleep constructor. I have not used sleep on 1.3 so I do not remember what arguments you had available there.
                                    I have also extended sleep with an additional constructor which @hek has pulled into the release branch of 1.4 that permits you to use two external interrupts with independent triggers for sleep in addition to a timeout. Great for nodes with multiple sensors that still run on battery.

                                    Do you feel secure today? No? Start requiring some signatures and feel better tomorrow ;)

                                    1 Reply Last reply
                                    0
                                    • E Offline
                                      E Offline
                                      egbertje
                                      wrote on last edited by egbertje
                                      #53

                                      @Mountainman ;

                                      Im trying your code and its almost what im looking for.
                                      I have two questions about it:
                                      1 : Is it possible that the battery info has is own child id?
                                      2 : Why dont you implement a light sensor like an LDR?

                                      I think that including LDR/light then you have the ideal multisensor ; light + temp/hum + motion + batt monitoring !
                                      But for now im playing with your code and try to implement light.

                                      1 Reply Last reply
                                      0
                                      • mountainmanM mountainman

                                        Here is a version I'm currently using that makes use of interrupts so might be more suitable for a battery driven DHT/Motion multi sensor. It also includes the battery monitoring. Any feedback or improvements would be appreciated.

                                        #include <SPI.h>
                                        #include <MySensor.h>
                                        #include <DHT.h>
                                        
                                        #define CHILD_ID_HUM 0
                                        #define CHILD_ID_TEMP 1
                                        #define CHILD_ID_MOTION 2   // Id of the sensor child
                                        
                                        #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 INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
                                        
                                        unsigned long SLEEP_TIME = F; // Sleep time between reports (in milliseconds)
                                        
                                        
                                        int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
                                        int oldValue=-1;
                                        
                                        MySensor gw;
                                        DHT dht;
                                        float lastTemp;
                                        float lastHum;
                                        boolean metric = false;
                                        
                                        int oldBatteryPcnt = 0;
                                        int battLoop =0;
                                        
                                        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                                        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                                        // Initialize motion message
                                        MyMessage msg(CHILD_ID_MOTION, V_TRIPPED);
                                        
                                        boolean pinTriggered=0;//waitTime is number of seconds to hol in while loop
                                        long lastDebounceTime = 0;  // the last time the output pin was toggled
                                        long debounceDelay = 50;    // the debounce time; increase if the output flickers
                                        
                                        
                                        void setup()
                                        {
                                        
                                          // use the 1.1 V internal reference
                                          analogReference(INTERNAL);
                                        
                                          gw.begin();
                                          dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
                                        
                                          // Send the Sketch Version Information to the Gateway
                                          gw.sendSketchInfo("Motion Humidity w/ Batt", "1.0");
                                        
                                          // Register all sensors to gw (they will be created as child devices)
                                          gw.present(CHILD_ID_HUM, S_HUM);
                                          gw.present(CHILD_ID_TEMP, S_TEMP);
                                          gw.present(CHILD_ID_MOTION, S_MOTION);
                                        
                                          pinMode(DIGITAL_INPUT_SENSOR,INPUT);      // sets the motion sensor digital pin as input
                                          digitalWrite(DIGITAL_INPUT_SENSOR,HIGH);   // Activate internal pull-up
                                        
                                          metric = gw.getConfig().isMetric;
                                        
                                          check_batt();
                                        
                                        }
                                        
                                        void loop()
                                        {
                                          if (pinTriggered)
                                          {
                                        	Serial.println(millis()-lastDebounceTime);
                                        	// Read digital motion value
                                        	boolean value = digitalRead(DIGITAL_INPUT_SENSOR);
                                        	// If the switch changed, due to noise or pressing
                                        	if (value != oldValue) {
                                        	  {
                                        		lastDebounceTime = millis();
                                        		// Send in the new value
                                        		gw.send(msg.set(value==HIGH ? 1 : 0));
                                        		battLoop++;
                                        		oldValue = value;
                                        		Serial.print("Mot: ");
                                        		Serial.println(value);
                                        	  }
                                        	}
                                        
                                        	pinTriggered=0;
                                        	// }
                                          }
                                          else
                                          {
                                        
                                        	delay(dht.getMinimumSamplingPeriod());
                                        
                                        	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);
                                        	  }
                                        	  gw.send(msgTemp.set(temperature, 1));
                                        	  battLoop++;
                                        	  Serial.print("T: ");
                                        	  Serial.println(temperature);
                                        	}
                                        
                                        	float humidity = dht.getHumidity();
                                        	if (isnan(humidity))
                                        	{
                                        	  Serial.println("Failed reading humidity from DHT");
                                        	}
                                        	else if (humidity != lastHum)
                                        	{
                                        	  lastHum = humidity;
                                        	  gw.send(msgHum.set(humidity, 1));
                                        	  battLoop++;
                                        	  Serial.print("H: ");
                                        	  Serial.println(humidity);
                                        	}
                                        
                                        
                                        	if (battLoop > 10)
                                        	{
                                        	  check_batt();
                                        	  battLoop=0;
                                        	}
                                          }
                                        
                                          // Sleep until interrupt comes in on motion sensor. Send update every two minute.
                                          pinTriggered = gw.sleep(INTERRUPT, CHANGE, SLEEP_TIME);
                                        }
                                        
                                        
                                        void check_batt()
                                        {
                                          // get the battery Voltage
                                          int sensorValue = analogRead(BATTERY_SENSE_PIN);
                                          Serial.println(sensorValue);
                                        
                                          // 1M, 470K divider across battery and using internal ADC ref of 1.1V
                                          // Sense point is bypassed with 0.1 uF cap to reduce noise at that point
                                          // ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts
                                          // 3.44/1023 = Volts per bit = 0.003363075
                                          float batteryV  = sensorValue * 0.003363075;
                                          int batteryPcnt = sensorValue / 10;
                                        
                                          Serial.print("Battery Voltage: ");
                                          Serial.print(batteryV);
                                          Serial.println(" V");
                                        
                                          Serial.print("Battery percent: ");
                                          Serial.print(batteryPcnt);
                                          Serial.println(" %");
                                        
                                          if (oldBatteryPcnt != batteryPcnt) {
                                        	// Power up radio after sleep
                                        	gw.sendBatteryLevel(batteryPcnt);
                                        	oldBatteryPcnt = batteryPcnt;
                                          }
                                        }
                                        
                                        C Offline
                                        C Offline
                                        codenea
                                        wrote on last edited by
                                        #54

                                        @mountainman Is there any instructions on how to wire/setup the hardware to work with your code?

                                        1 Reply Last reply
                                        0
                                        • mountainmanM Offline
                                          mountainmanM Offline
                                          mountainman
                                          wrote on last edited by
                                          #55

                                          @egbertje;

                                          The battery percentage is associated with the parent Arduino Node in the vera UI.
                                          I have some LDRs ordered from Ebay so that is the next step. I had previously used the BH1750 I2C sensor in a multi sensor with the 1.3 lib but I want to debug on its own as it seemed to be giving unexpected readings.

                                          @codenea The instructions are pretty much from the main page although if I get a chance I try to draw it up and post some pics. My setup not very pretty yet with perf board and jumper wires. A dedicated PCB would be nice.

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


                                          11

                                          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