Navigation

    • Register
    • Login
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. rhuehn
    3. Posts
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Posts made by rhuehn

    • RE: Prevent Relay from triggering on power loss or broker reboot

      Hey guys! Thanks so very much for all the great comments. I haven't replied in a few days as I wanted to go back to the drawing board..... I didn't like that sketch, the sleep functions, etc etc.... It didn't also make sense clearly based on everyone's feedback ( Sorry I am just getting started with IDE... ) So, based on that, I've come up with the following, and if you could comment, modify where I messed up, it would be greatly appreciated. This sketch seems to work, and keep state on power LOSS. There's a relay for the garage door ( trigger, PIN 4 ), a reed switch to detect if the door is open or closed ( PIN 2 ), a DHT for Temp / Humidity ( PIN 7 ), and a PIR ( PIN 3 ) for motion activated events. Below is the sketch, and would really appreciate any great feedback.

      Thanks again!

      
      #define MY_DEBUG
      #define MY_RADIO_NRF24
      #define MY_REPEATER_FEATURE
      #define MY_NODE_ID 50
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <Bounce2.h>
      #include <DHT.h>
      
      #define RELAY_1  4  // 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 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
      #define TOGGLE_INTERVAL 350
      
      #define CHILD_ID_SW 2
      #define BUTTON_PIN  2  // Arduino Digital I/O pin for button/reed switch
      
      // 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;
      
      float lastTemp;
      float lastHum;
      uint8_t nNoUpdatesTemp;
      uint8_t nNoUpdatesHum;
      bool metric = true;
      
      #define CHILD_ID_HUM 10
      #define CHILD_ID_TEMP 11
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7
      
      // Set this offset if the sensor has a permanent small offset to the real temperatures
      #define SENSOR_TEMP_OFFSET -1
      
      #define CHILD_ID_PIR 3
      #define PIR_PIN 3
      
      byte StatePIR=0;
      byte oldStatePIR=0;
      
      Bounce debouncer = Bounce(); 
      int oldValue=-1;
      
      MyMessage msgSw(CHILD_ID_SW,V_TRIPPED);
      MyMessage msgHum(CHILD_ID_HUM, V_HUM); // 1
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP); // 0
      MyMessage msgPir(CHILD_ID_PIR, V_TRIPPED);
      
      DHT dht;
      
      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); // 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());
      
        pinMode(PIR_PIN, INPUT_PULLUP);
        // 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 presentation()
      {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo("Garage Multi Sensor", "1.0");
      
          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);
              present(CHILD_ID_SW, S_DOOR);
              present(CHILD_ID_HUM, S_HUM);
              present(CHILD_ID_TEMP, S_TEMP);
              present(CHILD_ID_PIR, S_MOTION);
      
              metric = getConfig().isMetric;
          }
      }
      
      void loop()
      {
      
      StatePIR=digitalRead(PIR_PIN);
        if (StatePIR != oldStatePIR) {
          oldStatePIR=StatePIR;
          send(msgPir.set(StatePIR ? "ON" : "OFF"));
        
        // 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;
          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++;
        }
      
      }
      
        debouncer.update();
        // Get the update value
        int value = debouncer.read();
      
        if (value != oldValue) {
           // Send in the new value
           send(msgSw.set(value==HIGH ? 1 : 0));
           oldValue = value;
        }
      }
      
      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);
              //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
              delay( TOGGLE_INTERVAL );
              digitalWrite( RELAY_1, 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());
          }
      }
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Prevent Relay from triggering on power loss or broker reboot

      Hello @Boots33

      The Reed switch should NOT have any effect on the relay. ( It shouldn't anyways, it only senses open / closed

      From my understanding here:

      https://home-assistant.io/components/mysensors/

      It needs to be done in the loop function:

      Present the sensorโ€™s S_TYPE.
      Send at least one initial value per V_TYPE. In version 2.0 of MySensors this has to be done in the loop function. See below for an example in 2.0 of how to make sure the initial value has been received by the controller.

      Below is an example from that page:

      /*
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * http://www.mysensors.org/build/relay
       */
      
      #define MY_DEBUG
      #define MY_RADIO_NRF24
      #define MY_REPEATER_FEATURE
      #define MY_NODE_ID 1
      #include <SPI.h>
      #include <MySensors.h>
      #include <Bounce2.h>
      
      #define RELAY_PIN  5
      #define BUTTON_PIN  3
      #define CHILD_ID 1
      #define RELAY_ON 1
      #define RELAY_OFF 0
      
      Bounce debouncer = Bounce();
      bool state = false;
      bool initialValueSent = false;
      
      MyMessage msg(CHILD_ID, V_STATUS);
      
      void setup()
      {
        pinMode(BUTTON_PIN, INPUT_PULLUP);
        debouncer.attach(BUTTON_PIN);
        debouncer.interval(10);
      
        // Make sure relays are off when starting up
        digitalWrite(RELAY_PIN, RELAY_OFF);
        pinMode(RELAY_PIN, OUTPUT);
      }
      
      void presentation()  {
        sendSketchInfo("Relay+button", "1.0");
        present(CHILD_ID, S_BINARY);
      }
      
      void loop()
      {
        if (!initialValueSent) {
          Serial.println("Sending initial value");
          send(msg.set(state?RELAY_ON:RELAY_OFF));
          Serial.println("Requesting initial value from controller");
          request(CHILD_ID, V_STATUS);
          wait(2000, C_SET, V_STATUS);
        }
        if (debouncer.update()) {
          if (debouncer.read()==LOW) {
            state = !state;
            // Send new state and request ack back
            send(msg.set(state?RELAY_ON:RELAY_OFF), true);
          }
        }
      }
      
      void receive(const MyMessage &message) {
        if (message.isAck()) {
           Serial.println("This is an ack from gateway");
        }
      
        if (message.type == V_STATUS) {
          if (!initialValueSent) {
            Serial.println("Receiving initial value from controller");
            initialValueSent = true;
          }
          // Change relay state
          state = (bool)message.getInt();
          digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
          send(msg.set(state?RELAY_ON:RELAY_OFF));
        }
      }
      

      I was just following some guidelines to use HASS+ My Sensors together.

      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Prevent Relay from triggering on power loss or broker reboot

      Hey @Boots33 Thanks very much for the reply. I apologize I should have elaborated further.

      My home automation platform is Home Assistant. I am using a few mysensors around the house for temp, motion etc, and the purpose of this very sensor / sketch is to be used as a garage door opener. I am using an Uno, with the following PINS:

      PIR - PIN 3
      DHT - PIN 7
      Relay - PIN 4
      Reed switch - PIN 2

      The PIR and the DHT are just fun addon sensors that might trigger an action based on motion or temp.

      The Reed switch can tell when the garage door is open or closed based on contact, and the relay's purpose is to provide a momentary "press" of the garage door wall control, ( I have it wired into the opener and relay ).

      It currently works great, but on loss of power at the house ( The arduino is plugged in ), the relay trips on reload, causing the garage door to open / close, and well, if we are away from the house, this is obviously not good. ๐Ÿ˜‰

      I just tried implementing your change you recommended, and unfortunately on reboot, the relay still triggers. Ideally, if I could have the relay not trigger on reboot, then I would consider it safe. I'm sure it's possible, I just can't figure out how to not have it reset / trigger on AC power fail. The interface appears as follows:

      0_1488935309554_upload-d5417f76-6875-4689-bc03-2fa7f45d760c

      I did make the modification, but the results are the same, so I won't re paste the code block.

      Thanks very much for the help @Boots33 if there are any other code mods I could make, I really would appreciate it.

      Thanks!

      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Prevent Relay from triggering on power loss or broker reboot

      @Boots33 Thanks very much for the reply, and I know this is late in my response so I apologize about that....

      So this sketch does the following. A Replay that momentarily interrupts the garage door button to simulate a press of the control button, has a PIR and a DHT for motion and temp / hum, and also has a reed switch that can determine if the door is open or closed. I've been playing none stop with the sketch, and have tweaked it to the best of my ability, but STILL on power loss, the relay always clicks... therefore opening or closing the door...... Based on these changes, is there any way I can clean this up ( I'm at a loss unfortunately ) so that on power loss the relay remains as it was "state"? I've tried a few combinations without any luck to this point. My revised sketch is below, and I really appreciate any help.

      Thanks

      #define MY_DEBUG
      #define MY_RADIO_NRF24
      #define MY_REPEATER_FEATURE
      #define MY_NODE_ID 50
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <Bounce2.h>  
      #include <DHT.h>  
      
      #define CHILD_ID_TEMP 11
      #define CHILD_ID_HUM 10
      #define CHILD_ID_MOT 3   // Id of the sensor child
      #define CHILD_ID_REL 1
      #define RELAY_PIN  4
      #define RELAY_ON 1
      #define RELAY_OFF 0
      #define TOGGLE_INTERVAL 350  //Tells how many milliseconds the relay will be held closed
      #define CLOSED 0
      #define OPEN 1
      #define BUTTON_PIN_1  2
      #define CHILD_ID_SW 2
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7
      #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)
      
      // Set this offset if the sensor has a permanent small offset to the real temperatures
      #define SENSOR_TEMP_OFFSET 1
      
      Bounce debouncer = Bounce();
      int oldValue=-1;
      
      bool state = false;
      bool initialValueSent = false;
      byte StatePIR=0;
      byte oldStatePIR=0;
      unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
      
      DHT dht;
      float lastTemp;
      float lastHum;
      boolean metric = true; 
      bool StateREL=0, StateREL1=0;
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      MyMessage msg(CHILD_ID_REL, V_STATUS);
      MyMessage msgSW(CHILD_ID_SW,V_TRIPPED);
      
      void presentation()  
      { 
        // Send the Sketch Version Information to the Gateway
        sendSketchInfo("Garage Multi Sensor", "1.0");
      
        // Register all sensors to gw (they will be created as child devices)
        delay(100);
        present(CHILD_ID_TEMP, S_TEMP);
        delay(100);
        present(CHILD_ID_HUM, S_HUM);
        delay(100);
        present(CHILD_ID_MOT, S_MOTION);
        delay(100);
        present(CHILD_ID_REL, S_BINARY);
        delay(100);
        present(CHILD_ID_SW, S_DOOR);
      
        metric = getConfig().isMetric;
      }
      
      void setup()
      {
        // Setup the button
        pinMode(BUTTON_PIN_1,INPUT);
        // Activate internal pull-up
        digitalWrite(BUTTON_PIN_1,HIGH);
        // After setting up the button, setup debouncer
        debouncer.attach(BUTTON_PIN_1);
        debouncer.interval(5);
      
      
        // Make sure relays are off when starting up
        pinMode(RELAY_PIN, OUTPUT);
        digitalWrite(RELAY_PIN, StateREL1);
      
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        if (SLEEP_TIME <= 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());
      
      //  metric = getConfig().isMetric;
      
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      }
      
      void loop()
      {
          debouncer.update();
        // Get the update value
        int value = debouncer.read();
      
        if (value != oldValue) {
           // Send in the new value
           send(msgSW.set(value==HIGH ? 1 : 0));
           oldValue = value;
        }
        StatePIR=digitalRead(DIGITAL_INPUT_SENSOR);
        if (StatePIR != oldStatePIR) {
          oldStatePIR=StatePIR;
          send(msgMot.set(StatePIR ? 1 : 0));
        }
       
        if (!initialValueSent) {
          Serial.println("Sending initial value");
          send(msg.set(state?RELAY_ON:RELAY_OFF));
          Serial.println("Requesting initial value from controller");
          request(CHILD_ID_REL, V_STATUS);
          wait(2000, C_SET, V_STATUS);
        }
        if (debouncer.update()) {
          if (debouncer.read()==LOW) {
            state = !state;
            // Send new state and request ack back
            send(msg.set(state?RELAY_ON:RELAY_OFF), true);
          }
        }
       
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
              
        Serial.println(tripped);
        send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        
        delay(dht.getMinimumSamplingPeriod());
       
        // 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);
          }
          temperature += SENSOR_TEMP_OFFSET;
          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
        }
        
        sleep(INTERRUPT,CHANGE, SLEEP_TIME); //sleep a bit
      }
      
      void receive(const MyMessage &message) {
        if (message.isAck()) {
           Serial.println("This is an ack from gateway");
        }
        if (message.type == V_STATUS) {
          if (!initialValueSent) {
            Serial.println("Receiving initial value from controller");
            initialValueSent = true;
          }
          // Change relay state
          state = (bool)message.getInt();
          digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
          //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
          delay( TOGGLE_INTERVAL );
          digitalWrite( RELAY_PIN, RELAY_OFF );
          // Store state in eeprom
          saveState(message.sensor, message.getBool());
          send(msg.set(state?RELAY_ON:RELAY_OFF));
        }
      }
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Prevent Relay from triggering on power loss or broker reboot

      @hek Thanks for the reply, but I'm not 100% sure as I am still somewhat new to mysensors and arduino. Is there any chance you could possibly show an example using the above sketch I pasted previously?

      Thanks!

      posted in Troubleshooting
      rhuehn
      rhuehn
    • Prevent Relay from triggering on power loss or broker reboot

      Hey guys:

      I'm currently using a relay to control a garage door opener, but when the MQTT broker reboots, or the Arduino reboots, the relay's click on and off, of course opening / closing the garage door. Is there a way to have the relay's remember state and NOT click on and off in case of power or broker restart? I am not much of a coder, and combled this together, so if anyone can help it would be greatly appreciated!

      
      #define MY_DEBUG
      #define MY_RADIO_NRF24
      #define MY_REPEATER_FEATURE
      #define MY_NODE_ID 50
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <Bounce2.h>  
      #include <DHT.h>  
      
      #define CHILD_ID_TEMP 11
      #define CHILD_ID_HUM 10
      #define CHILD_ID_MOT 3   // Id of the sensor child
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7
      #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 RELAY_PIN  4
      #define CHILD_ID_REL 1
      #define RELAY_ON 1
      #define RELAY_OFF 0
      #define TOGGLE_INTERVAL 350  //Tells how many milliseconds the relay will be held closed
      #define CHILD_ID_SW 2
      #define BUTTON_PIN_2  2  // Arduino Digital I/O pin for button/reed switch
      
      Bounce debouncer = Bounce();
      bool state = false;
      bool initialValueSent = false;
      
      Bounce debouncer2 = Bounce(); 
      int oldValue2=-1;
      
      unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
      
      DHT dht;
      float lastTemp;
      float lastHum;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      MyMessage msg(CHILD_ID_REL, V_STATUS);
      MyMessage msgSW(CHILD_ID_SW,V_TRIPPED);
      
      void presentation()  
      { 
        // Send the Sketch Version Information to the Gateway
        sendSketchInfo("Temp/Hum/Mot", "1.3");
        // Register all sensors to gw (they will be created as child devices)
        delay(100);
        present(CHILD_ID_TEMP, S_TEMP);
        delay(100);
        present(CHILD_ID_HUM, S_HUM);
        delay(100);
        present(CHILD_ID_MOT, S_MOTION);
        delay(100);
        present(CHILD_ID_REL, S_BINARY);
        delay(100);
        present(CHILD_ID_SW, S_DOOR); 
      
        metric = getConfig().isMetric;
      }
      
      void setup()
      {
          // Setup the button
        pinMode(BUTTON_PIN_2,INPUT);
        // Activate internal pull-up
        digitalWrite(BUTTON_PIN_2,HIGH);
        // After setting up the button, setup debouncer
        debouncer2.attach(BUTTON_PIN_2);
        debouncer2.interval(5);
        // Make sure relays are off when starting up
        digitalWrite(RELAY_PIN, RELAY_OFF);
        pinMode(RELAY_PIN, OUTPUT);
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
        if (SLEEP_TIME <= 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());
      
      //  metric = getConfig().isMetric;
      
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        delay(100);
        send(msgTemp.set("0"));
        delay(100);
        send(msgHum.set("0"));
        delay(100);
        send(msgMot.set("0"));
      }
      
      void loop()
      {
        debouncer2.update();
        // Get the update value
        int value2 = debouncer2.read();
      
        if (value2 != oldValue2) {
           // Send in the new value
           send(msgSW.set(value2==HIGH ? 1 : 0));
           oldValue2 = value2;
        }
      {
        if (!initialValueSent) {
          Serial.println("Sending initial value");
          send(msg.set(state?RELAY_ON:RELAY_OFF));
          Serial.println("Requesting initial value from controller");
          request(CHILD_ID_REL, V_STATUS);
          wait(2000, C_SET, V_STATUS);
        }
        if (debouncer.update()) {
          if (debouncer.read()==LOW) {
            state = !state;
            // Send new state and request ack back
            send(msg.set(state?RELAY_ON:RELAY_OFF), true);
          }
        }
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
              
        Serial.println(tripped);
        send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        
        delay(dht.getMinimumSamplingPeriod());
       
        // 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
        }
        
        sleep(INTERRUPT,CHANGE, SLEEP_TIME); //sleep a bit
        }
      }
      
      void receive(const MyMessage &message) {
        if (message.isAck()) {
           Serial.println("This is an ack from gateway");
        }
      
        if (message.type == V_STATUS) {
          if (!initialValueSent) {
            Serial.println("Receiving initial value from controller");
            initialValueSent = true;
          }
          // Change relay state
          state = (bool)message.getInt();
          digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
          digitalWrite( RELAY_PIN, RELAY_ON );
          //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
          delay( TOGGLE_INTERVAL );
          digitalWrite( RELAY_PIN, RELAY_OFF );
          //Added this to tell the controller that we shut off the relay
           // Store state in eeprom
          saveState(message.sensor, message.getBool());
          send(msg.set(state?RELAY_ON:RELAY_OFF));
        }
      }
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Mysensors 2.0 + Relay, DHT, and PIR - No Relay

      Hello @Nuubi Yes I believe that is exactly why the Relay goes unresponsive. If I remove that sleep block, the PIR floods the gateway, and I am not sure how to get around it? Is there any possible recommendations you could make to the code so that the DHT, PIR AND Relay all work without the PIR flooding the gateway?

      Thanks very much!

      posted in Hardware
      rhuehn
      rhuehn
    • Mysensors 2.0 + Relay, DHT, and PIR - No Relay

      Hello folks:

      I'm trying to put together a powered multi sensor using mysensors 2, that will have a DHT, PIR, and a relay to act as a quick trigger for the garage. ( momentary push ), but I can't seem to get the relay to trigger with the following sketch:

      /**
      DHT-Motion, MySensors 2.0.0, motion sensor pin 3, DHT pin 7
       */
      
      // Enable debug prints
      #define MY_DEBUG
      #define MY_NODE_ID 50         // My Node ID variable
      // 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 7
      #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
      
      // Set this offset if the sensor has a permanent small offset to the real temperatures
      #define SENSOR_TEMP_OFFSET -1
      
      // 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_MO 3
      #define CHILD_ID_HUM 10
      #define CHILD_ID_TEMP 11
      
      
      #define RELAY_1  4  // 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 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
      #define CHILD_ID_OPENER 0
      #define CHILD_ID_STATUS 1
      
      #define TOGGLE_INTERVAL 350  //Tells how many milliseconds the relay will be held closed
      
      #define CLOSED 0
      #define OPEN 1
      
      
      
      DHT dht;
      float lastTemp;
      float lastHum;
      uint8_t nNoUpdatesTemp;
      uint8_t nNoUpdatesHum;
      bool metric = true; 
      
      MyMessage msgMot(CHILD_ID_MO, V_TRIPPED);
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgOpener(CHILD_ID_OPENER, V_STATUS);
      MyMessage msgStatus(CHILD_ID_STATUS, V_TEXT);
      
      
      void presentation()  
      { 
        // Send the sketch version information to the gateway
        sendSketchInfo("DHT-Motion_Relay", "1.1");
        
        // Register all sensors to gw (they will be created as child devices)
        present(CHILD_ID_MO, S_MOTION);
        wait(30);
        present(CHILD_ID_HUM, S_HUM);
        wait(30);
        present(CHILD_ID_TEMP, S_TEMP);
        wait(30);
        sendHeartbeat();
        wait(30);
        present(CHILD_ID_OPENER, S_BINARY);
        wait(30);
        present(CHILD_ID_STATUS, S_INFO);
        wait(30);
      }
      
      
      void setup()
      {
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as 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(dht.getMinimumSamplingPeriod());
        pinMode(RELAY_1, OUTPUT);
        digitalWrite(RELAY_1, loadState(RELAY_1)?RELAY_OFF:RELAY_ON);
        
      }
      
      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;
          if (!metric) {
            temperature = dht.toFahrenheit(temperature);
          }
          // Reset no updates counter
          nNoUpdatesTemp = 0;
          temperature += SENSOR_TEMP_OFFSET;
          send(msgTemp.set(temperature, 1));
          wait(50);
      
          #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));
          wait(50);
          
          #ifdef MY_DEBUG
          Serial.print("H: ");
          Serial.println(humidity);
          #endif
        } else {
          // Increase no update counter if the humidity stayed the same
          nNoUpdatesHum++;
        }
         // Read digital motion value
        bool tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
              
        Serial.println(tripped);
        send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        wait(300);
        
        // Sleep for a while to save energy
        sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, UPDATE_INTERVAL); 
      }
      void receive(const MyMessage &message) {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LIGHT) {
           // Change relay state
           digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
           digitalWrite( RELAY_1, RELAY_ON );
          //Keep the relay on for the amount of time defined by TOGGLE_INTERVAL
          delay( TOGGLE_INTERVAL );
          digitalWrite( RELAY_1, RELAY_OFF );
          //Added this to tell the controller that we shut off the relay
           // 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());
         } 
      }
      
      

      Can anyone help with where I have gone wrong? Board, NRF, etc all work with the default relay sketch, but when I am combining, I am not having any luck with the relay, everything else appears fine based on serial output:

      Starting sensor (RNNNA-, 2.0.0)
      TSM:INIT
      TSM:RADIO:OK
      TSP:ASSIGNID:OK (ID=50)
      TSM:FPAR
      TSP:MSG:SEND 50-50-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
      TSP:MSG:READ 0-0-50 s=255,c=3,t=8,pt=1,l=1,sg=0:0
      TSP:MSG:FPAR RES (ID=0, dist=0)
      TSP:MSG:PAR OK (ID=0, dist=1)
      TSM:FPAR:OK
      TSM:ID
      TSM:CHKID:OK (ID=50)
      TSM:UPL
      TSP:PING:SEND (dest=0)
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
      TSP:MSG:READ 0-0-50 s=255,c=3,t=25,pt=1,l=1,sg=0:1
      TSP:MSG:PONG RECV (hops=1)
      TSP:CHKUPL:OK
      TSM:UPL:OK
      TSM:READY
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=ok:0100
      !TSP:MSG:SEND 50-50-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=fail:2.0.0
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,ft=1,st=ok:0
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=11,pt=0,l=16,sg=0,ft=0,st=ok:DHT-Motion_Relay
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,ft=0,st=ok:1.1
      TSP:MSG:SEND 50-50-0-0 s=3,c=0,t=1,pt=0,l=0,sg=0,ft=0,st=ok:
      TSP:MSG:SEND 50-50-0-0 s=10,c=0,t=7,pt=0,l=0,sg=0,ft=0,st=ok:
      TSP:MSG:SEND 50-50-0-0 s=11,c=0,t=6,pt=0,l=0,sg=0,ft=0,st=ok:
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=22,pt=5,l=4,sg=0,ft=0,st=ok:2197
      TSP:MSG:SEND 50-50-0-0 s=0,c=0,t=3,pt=0,l=0,sg=0,ft=0,st=ok:
      TSP:MSG:READ 0-0-50 s=255,c=3,t=6,pt=0,l=1,sg=0:M
      TSP:MSG:SEND 50-50-0-0 s=1,c=0,t=36,pt=0,l=0,sg=0,ft=0,st=ok:
      Request registration...
      TSP:MSG:SEND 50-50-0-0 s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=ok:2
      TSP:MSG:READ 0-0-50 s=255,c=3,t=27,pt=1,l=1,sg=0:1
      Node registration=1
      Init complete, id=50, parent=0, distance=1, registration=1
      TSP:MSG:SEND 50-50-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=ok:20.0
      T: 20.00
      TSP:MSG:SEND 50-50-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=ok:23.0
      H: 23.00
      0
      TSP:MSG:SEND 50-50-0-0 s=3,c=1,t=16,pt=0,l=1,sg=0,ft=0,st=ok:0
      
      
      posted in Hardware
      rhuehn
      rhuehn
    • RE: 3 in 1 incl battery monitor

      Hey @carlierd Just curious if you are still using a DHT22 with your sketch? Have you modified the code much from the above? I am looking at implementing something similiar ( identical actually ) and was wondering if much has changed for you over the last few months?

      Thanks!

      posted in My Project
      rhuehn
      rhuehn
    • RE: Help with PIR and Water Sensor or Nano - Water Sensor won't trigger

      Got it working with this sleep function properly:

        // Sleep until interrupt comes in on motion or water sensor. Send update every two minute. 
        gw.sleep(DIGITAL_INPUT_SENSOR - 2, CHANGE, DIGITAL_INPUT_WATER_SENSOR - 2, CHANGE, SLEEP_TIME);
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Help with PIR and Water Sensor or Nano - Water Sensor won't trigger

      Hi @Boots33 Yes it will work if I comment out the sleep for the PIR, but the PIR will then fire every second as it is not detecting motion. sketch below, ( commented out ) but is there not a way to have the water sensor trigger in theory the same as the PIR triggers? ie: sleep for a bit UNLESS motion / water is detected?

      
      #include <MySensor.h>
      #include <SPI.h>
      #include <DHT.h> 
      
      #define MY_NODE_ID 16         // My Node ID variable
      #define MY_DEBUG              // Turn on Debug
      #define MY_RADIO_NRF24        // Enable and select radio type attached
      
      #define SKETCH_NAME           "4 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define DIGITAL_INPUT_SENSOR 3 // Arduino Digital I/O pin for PIR Motion Sensor
      #define CHILD_ID_HUM 10       // Child id for Humidity
      #define CHILD_ID_TEMP 11      // Child id for Temperature
      #define CHILD_ID_MOT 2      // Child id for Motion
      
      #define DIGITAL_INPUT_WATER_SENSOR 2 // Digital input did you attach your soil sensor.
      #define CHILD_ID_WAT 4      // Child id for Motion
      
      
      #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7 // Where is my DHT22 data pin connected to
      // unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
      
      
      MySensor gw;
      DHT dht;
      float lastTemp;
      float lastHum;
      boolean metric = true;
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      MyMessage msgWat(CHILD_ID_WAT, V_TRIPPED);
      int lastWaterValue = -1; 
      
      void setup()  
      {   
        // Initialize library and add callback for incoming messages
        gw.begin();
      // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Laundry Room Sensor", "1.0");
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        pinMode(DIGITAL_INPUT_WATER_SENSOR, INPUT);
          
          // 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_MOT, S_MOTION);
          gw.present(CHILD_ID_WAT, S_MOTION);
      
          metric = gw.getConfig().isMetric;
          
      }
        
      
      
      void loop() 
      {
      // Read digital Water value
      int WaterValue = digitalRead(DIGITAL_INPUT_WATER_SENSOR); // 1 = Not triggered, 0 = In water
      if (WaterValue != lastWaterValue) {
      Serial.println(WaterValue);
      if (WaterValue==0)
      {
      Serial.println("Water!!!");
      gw.send(msgWat.set(0));
      }
      else
      {
      Serial.println("No Water");
      gw.send(msgWat.set(1)); // Send tripped when water detected
      lastWaterValue = WaterValue;
      }
      }
      
      
      
      
      
      {     
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
              
        Serial.println(tripped);
        gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
       
      {
      
      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));
          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));
            Serial.print("H: ");
            Serial.println(humidity);
        }
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
       // gw.sleep(INTERRUPT,CHANGE,SLEEP_TIME);
      }
      }
      }
      

      Below is the latest debug:

      end: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
      H: 39.00
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
      H: 40.00
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
      H: 39.00
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
      H: 40.00
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
      H: 39.00
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
      H: 40.00
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Help with PIR and Water Sensor or Nano - Water Sensor won't trigger

      Thats @Boots33 but unfortunately I tried that as well, without success.

      I added the following to the end:

        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        gw.sleep(INTERRUPT,CHANGE,SLEEP_TIME);
      

      Debug below, showing that the sensor works but only when PIR is triggered:

      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:23.0
      T: 23.00
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:24.0
      T: 24.00
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
      H: 40.00
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
      H: 39.00
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
      H: 40.00
      
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • Help with PIR and Water Sensor or Nano - Water Sensor won't trigger

      I'm hoping someone can possibly help me, I'm sure I've overlooked something but after a few days I can't seem to get this working right. I have a PIR, DHT, and a Water sensor attached to a Nano. I need the Water sensor to trigger as soon as it detects water. If I use a sketch on it's own, it works great, BUT with the combined sketch including the PIR, the sensor only sends an alert for water if the PIR becomes active, and I can't figure out why. It works fine without the PIR and sends an alert, but with the PIR and water sensor, it will only trigger water if the PIR detects motion.... Please help! ๐Ÿ™‚

      My Sketch is below:

      
      #include <MySensor.h>
      #include <SPI.h>
      #include <DHT.h> 
      
      #define MY_NODE_ID 16         // My Node ID variable
      #define MY_DEBUG              // Turn on Debug
      #define MY_RADIO_NRF24        // Enable and select radio type attached
      
      #define SKETCH_NAME           "4 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define DIGITAL_INPUT_SENSOR 3 // Arduino Digital I/O pin for PIR Motion Sensor
      #define CHILD_ID_HUM 10       // Child id for Humidity
      #define CHILD_ID_TEMP 11      // Child id for Temperature
      #define CHILD_ID_MOT 2      // Child id for Motion
      
      #define DIGITAL_INPUT_WATER_SENSOR 2 // Digital input did you attach your soil sensor.
      #define CHILD_ID_WAT 4      // Child id for Motion
      
      
      #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7 // Where is my DHT22 data pin connected to
      unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
      
      
      MySensor gw;
      DHT dht;
      float lastTemp;
      float lastHum;
      boolean metric = true;
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      MyMessage msgWat(CHILD_ID_WAT, V_TRIPPED);
      int lastWaterValue = -1; 
      
      void setup()  
      {   
        // Initialize library and add callback for incoming messages
        gw.begin();
      // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Laundry Room Sensor", "1.0");
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        pinMode(DIGITAL_INPUT_WATER_SENSOR, INPUT);
          
          // 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_MOT, S_MOTION);
          gw.present(CHILD_ID_WAT, S_MOTION);
      
          metric = gw.getConfig().isMetric;
          
      }
        
      
      
      void loop() 
      {
      // Read digital Water value
      int WaterValue = digitalRead(DIGITAL_INPUT_WATER_SENSOR); // 1 = Not triggered, 0 = In water
      if (WaterValue != lastWaterValue) {
      Serial.println(WaterValue);
      if (WaterValue==0)
      {
      Serial.println("Water!!!");
      gw.send(msgWat.set(0));
      }
      else
      {
      Serial.println("No Water");
      gw.send(msgWat.set(1)); // Send tripped when water detected
      lastWaterValue = WaterValue;
      }
      }
      
      
      
      
      
      {     
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
              
        Serial.println(tripped);
        gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
       
      {
      
      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));
          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));
            Serial.print("H: ");
            Serial.println(humidity);
        }
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        gw.sleep(INTERRUPT,CHANGE);
      }
      }
      }
      

      Debug:

      sensor started, id=16, parent=0, distance=1
      send: 16-16-0-0 s=255,c=3,t=11,pt=0,l=19,sg=0,st=ok:Laundry Room Sensor
      send: 16-16-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
      send: 16-16-0-0 s=10,c=0,t=7,pt=0,l=0,sg=0,st=ok:
      send: 16-16-0-0 s=11,c=0,t=6,pt=0,l=0,sg=0,st=ok:
      send: 16-16-0-0 s=2,c=0,t=1,pt=0,l=0,sg=0,st=ok:
      send: 16-16-0-0 s=4,c=0,t=1,pt=0,l=0,sg=0,st=ok:
      1
      No Water
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:1
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:25.0
      T: 25.00
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
      H: 39.00
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      send: 16-16-0-0 s=11,c=1,t=0,pt=7,l=5,sg=0,st=ok:26.0
      T: 26.00
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:40.0
      H: 40.00
      0
      Water!!!
      send: 16-16-0-0 s=4,c=1,t=16,pt=2,l=2,sg=0,st=ok:0
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      send: 16-16-0-0 s=10,c=1,t=1,pt=7,l=5,sg=0,st=ok:39.0
      H: 39.00
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      0
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      1
      send: 16-16-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:1
      

      Thank you very much

      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Combining Multiple sensors - Reed switch not working

      thanks @fets have I specified the wait time at the wrong location? In the sketch below, everything works, BUT the PIR triggers 0 over and over and over again.....

       #define MY_GW_ID 50
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      #include <Bounce2.h>
      
      
      //Constants
      #define SKETCH_NAME           "3 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define CHILD_ID_TEMP 11                  // Child id for Temperature
      #define CHILD_ID_HUM 10                   // Child id for Humidity
      #define CHILD_ID_MOT 2                   // Child id for Motion
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
      #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_SW 3                    // Child id for Reed Switch
      #define BUTTON_PIN  2                    // Arduino Digital I/O pin for button/reed switch
      
      #define RELAY_1  4  // 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 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
      
      
      int node_id=50;                          // What is my node id
      
      
      MySensor gw;
      Bounce debouncer = Bounce(); 
      int oldValue=-1;
      DHT dht;
      //Store last values
      float lastTemp = 0 ;                  
      float lastHum = 0 ;
      ;             
      
      boolean lastTripped = false ;
      boolean metric = true; 
      
      boolean gwsend = true;              // to determine if data has to be sent
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      MyMessage msgsw(CHILD_ID_SW,V_TRIPPED);
      
      void setup()  
      { 
        gw.begin(NULL,node_id,false);
        gw.wait(5);
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("3 in 1 Sensor", "1.0",true);
      
      
       //  - NEW 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);
      
      
       //  - NEW Setup the button END
      
      
      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.present(CHILD_ID_HUM, S_HUM,"Garage Humidity");
        gw.present(CHILD_ID_TEMP, S_TEMP,"Garage Temperature");
        gw.present(CHILD_ID_SW, S_DOOR,"Garage Door");
      
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      
        // Read Reed switch value
      {
        debouncer.update();
        // Get the update value
        int value = debouncer.read();
       
        if (value != oldValue) {
           // Send in the new value
           gw.send(msgsw.set(value==HIGH ? 1 : 0));
           oldValue = value;
        }
      
        // Read Temp - Humidity 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.send(msgTemp.set(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.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
      
      {     
        // Read digital motion PIR value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
         
              
        Serial.println(tripped);
        gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        }
      }
      }
      

      Thanks

      posted in Development
      rhuehn
      rhuehn
    • RE: Combining Multiple sensors - Reed switch not working

      Thanks very much @LastSamurai removing the sleep function seemed to work, but the PIR reports like crazy!

      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      0
      send: 50-50-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      
      

      I don't really need sleep, but will be adding a relay to this sketch. Any other suggestions that might not trigger the PIR all the time? Thanks very much again!

      posted in Development
      rhuehn
      rhuehn
    • Combining Multiple sensors - Reed switch not working

      Hello:

      I have a multi sensor with a DHT, PIR, NRF, and Reed switch. The PIR is connected to PIN 3, and the Reed switch is connected to PIN 2. The reed switch is setup exactly as the mysensors example here, ( https://www.mysensors.org/build/binary ) and works perfectly fine when only using that sketch. When trying to combine a few sketches, I'm getting everything else to work except for the Reed switch, and can't figure out where I've gone wrong. Is anyone able to possibly shed some light? My code is below, and really appreciate the help thank you.

        #define MY_GW_ID 50
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      #include <Bounce2.h>
      
      unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
      //Constants
      #define SKETCH_NAME           "3 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define CHILD_ID_TEMP 1                  // Child id for Temperature
      #define CHILD_ID_HUM 0                   // Child id for Humidity
      #define CHILD_ID_MOT 2                   // Child id for Motion
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
      #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_SW 3                    // Child id for Reed Switch
      #define BUTTON_PIN  2                    // Arduino Digital I/O pin for button/reed switch
      
      int node_id=50;                          // What is my node id
      
      
      MySensor gw;
      Bounce debouncer = Bounce(); 
      int oldValue=-1;
      DHT dht;
      //Store last values
      float lastTemp = 0 ;                  
      float lastHum = 0 ;
      ;             
      
      boolean lastTripped = false ;
      boolean metric = true; 
      
      boolean gwsend = true;              // to determine if data has to be sent
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      MyMessage msgsw(CHILD_ID_SW,V_TRIPPED);
      
      void setup()  
      { 
        gw.begin(NULL,node_id,false);
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("3 in 1 Sensor", "1.0",true);
      
       //  - NEW 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);
      
      
       //  - NEW Setup the button END
      
      
      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.present(CHILD_ID_HUM, S_HUM,"Garage Humidity");
        gw.present(CHILD_ID_TEMP, S_TEMP,"Garage Temperature");
        gw.present(CHILD_ID_SW, S_DOOR,"Garage Door");
         
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      
        // Read Reed switch value
      {
        debouncer.update();
        // Get the update value
        int value = debouncer.read();
       
        if (value != oldValue) {
           // Send in the new value
           gw.send(msgsw.set(value==HIGH ? 1 : 0));
           oldValue = value;
        }
      
        // Read Temp - Humidity 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.send(msgTemp.set(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.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
      
      {     
        // Read digital motion PIR value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
              
        Serial.println(tripped);
        gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
       
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
        }
      }
      }
      
      posted in Development
      rhuehn
      rhuehn
    • RE: Low Bat Powered 3 in 1 sensor Please Help

      Thanks @AWI for the reply. I thought I was sending values based on the above code with the following:

      //Misc. variables
      unsigned long SLEEP_TIME =60000UL;    // Sleep time between reads (in milliseconds)
      int sleepcycle=1;                     // Counter to count the amout of time not sending data
      int humoffset=2;                      // only data is send if humidity is changed for this amout
      int tempoffset=1.0;                   // only data is if temperature is changed for this amout 
      int gwsendtimout=20;                  // each 20*sleep_time (20 minutes) data will be send
      

      The Temp and Humidity in the sketch is quite honestly of lower importance to me as the PIR. I believe ( please correct me if I am wrong ), but the sensor should only report every 20 minutes, and only if a temp changes by a degree or humidity changes by 2 correct?

      The PIR, and sensor should sleep as often as possible, but based on motion in the vicinity of the sensor, should trigger an action immediately, and based on that, could send the temp and hum readings at the same time, OR every 20 minutes if there is a change...

      Thanks @AWI comments welcome.

      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: My 2AA battery sensor

      I have updated the original post if anyone can offer any suggestions. it is located Here

      Any feedback is greatly appreciated

      posted in My Project
      rhuehn
      rhuehn
    • RE: Low Bat Powered 3 in 1 sensor Please Help

      Thanks @AWI I have modified the sketch, and everything below seems to be working ok. If you can look at it, and post a comment if you think it is the most optimal for power use, and the vcc readings are correct it would be greatly appreciated. The complete sketch is below. I will likely look at changing the DHT's to HTU21 in the future. Is there a low powered PIR that you know of?:

       #define MY_GW_ID 31
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      #include <Vcc.h>
      
      //Constants
      #define SKETCH_NAME           "3 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define CHILD_ID_TEMP 1                  // Child id for Temperature
      #define CHILD_ID_HUM 0                   // Child id for Humidity
      #define CHILD_ID_VOLT 3                  // Child id for battery reading
      #define CHILD_ID_MOT 2                   // Id of the sensor child
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
      #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)
      
      int node_id=31;                        // What is my node id
      
      //Misc. variables
      unsigned long SLEEP_TIME =60000UL;    // Sleep time between reads (in milliseconds)
      int sleepcycle=1;                     // Counter to count the amout of time not sending data
      int humoffset=2;                      // only data is send if humidity is changed for this amout
      int tempoffset=1.0;                   // only data is if temperature is changed for this amout 
      int gwsendtimout=20;                  // each 20*sleep_time (20 minutes) data will be send
      
      const float VccMin   = 1.7;           // Minimum expected Vcc level, in Volts.
      const float VccMax   = 3.1;           // Maximum expected Vcc level, in Volts.
      const float VccCorrection = 1.0/1.0;  // Measured Vcc by multimeter divided by reported Vcc
      
      Vcc vcc(VccCorrection);
      
      MySensor gw;
      DHT dht;
      //Store last values
      float lastTemp = 0 ;                  
      float lastHum = 0 ;
      float batteryV=0;
      int oldBatteryPcnt = 0;             
      
      boolean lastTripped = false ;
      boolean metric = true; 
      
      boolean gwsend = true;              // to determin if data has to be send
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgVolt(CHILD_ID_VOLT, V_VOLTAGE);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      
      void setup()  
      { 
        gw.begin(NULL,node_id,false);
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("3 in 1 Sensor", "1.0",true);
      
      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.present(CHILD_ID_HUM, S_HUM,"Kitchen Humidity");
        gw.present(CHILD_ID_TEMP, S_TEMP,"Kitchen Temperature");
        gw.present(CHILD_ID_VOLT, S_MULTIMETER,"Kitchen Battery Voltage");
         
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      
      {  
        // get the battery Voltage
         batteryV  = vcc.Read_Volts();
         int batteryPcnt = vcc.Read_Perc(VccMin, VccMax);
      
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gwsend=true;
           oldBatteryPcnt = batteryPcnt;
         }
          
        delay(dht.getMinimumSamplingPeriod());
      
        float temp1 = dht.getTemperature();
        float humidity = dht.getHumidity();
      
        if (isnan(temp1)) {
      //      Serial.println("Failed reading temperature from DHT");
        } else if ((temp1 <= lastTemp-tempoffset)||(temp1 >= lastTemp+tempoffset)) {
          lastTemp = temp1;
          if (!metric) {
            temp1 = dht.toFahrenheit(temp1);
          }
          gwsend=true;
        }
      
        if (isnan(humidity)) {
          //      Serial.println("Failed reading humidity from DHT");
        } else if ((humidity <= lastHum-humoffset)||(humidity >= lastHum+humoffset)) {
            lastHum = humidity;
            gwsend=true;
          }
      
        if (sleepcycle>gwsendtimout){
          gwsend=true;
         }
      
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
        if (tripped != lastTripped ) {      
          Serial.println(tripped);
           
      
      if (gwsend){     
          gw.sendBatteryLevel(oldBatteryPcnt);
          gw.send(msgVolt.set(batteryV, 1));
          gw.send(msgTemp.set(lastTemp, 1));  
          gw.send(msgHum.set(lastHum, 1));
          gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw
          gwsend=false;
          sleepcycle=1;
        }
        sleepcycle++;  
        gw.sleep(INTERRUPT, CHANGE, 0 );
      }
      }
      

      Thanks again

      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: My 2AA battery sensor

      Thanks @mfalkvidd Starting to realize that..... I might swap to the HTU21D, but keep in mind I'm also using a PIR..... Is there a reliable, low voltage PIR sensor that works well? I haven't seen one yet ( 2 x AA BATT's, under 2.8 V )?

      Thanks

      posted in My Project
      rhuehn
      rhuehn
    • RE: My 2AA battery sensor

      Hey @m26872 Congrats on the testing and results! Looks great! I was wondering if I could get your feedback on something. I'm trying to build a very low powered ( 2 x AA ) Battery powered 3 in 1 sensor. ( Platform is a Pro Mini 8Mhz, 3.3v, with an PIR HC-SR501, NRF24L01, and a DHT 11 Temp and Hum sensor. The sensor should also report battery power periodically ideally using the vcc library ( not implemented yet ). Because these components use power hungry devices ( PIR, DHT ), I have already moded the PIR to work at 3.3, and have removed the regulator / LED from the pro mini. Would it make sense to use a step up / down regulator, or power directly from the VCC pin? I can't decide what would be more effective in prolonging battery life? Also, in terms of reporting battery state. I'm not sure what would be best also, hence the question around the VCC library?

      Below is my sketch. It is really taken from various posts on this forum and stitched together. If you could have a peak at it, and see if it is "optimal", as I don't believe it is today. ie: Perhaps I could only report temp / hum if it changes +/- a degree? Or send everything in a single radio announcement, then "Deep Sleep"? Lots of questions, and of course I am sure there are lots of answers. I'm just trying to build the most optimal battery efficient ( 2 x AA ) powered sensor no problem right! ๐Ÿ™‚ Below is my current code:

       #define MY_GW_ID 31
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      
      int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
      
      //Constants
      #define SKETCH_NAME           "3 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define CHILD_ID_HUM 0                    // Child id for Humidity
      #define CHILD_ID_TEMP 1                   // Child id for Temperature
      #define CHILD_ID_MOT 2                   // Id of the sensor child
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
      #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)
      
      //Misc. variables
      uint8_t switchState = 2;
      unsigned long SLEEP_TIME = 820000;      // Sleep time between reads (in milliseconds) (close to 15')
      unsigned long SLEEP_TIME2 = 275000;     // Sleep time after int. (in milliseconds) (close to 5')
      uint8_t cycleInProgress = 0;
      uint8_t firstReportDone = 0;
      uint8_t firstReportWithZeroDone = 0;
      int oldBatteryPcnt = 0;
      
      MySensor gw;
      DHT dht;
      float lastTemp = 0 ;
      float lastHum = 0 ;
      boolean lastTripped = false ;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      
      void setup()  
      { 
         // use the 1.1 V internal reference
      #if defined(__AVR_ATmega2560__)
         analogReference(INTERNAL1V1);
      #else
         analogReference(INTERNAL);
      #endif
        gw.begin();
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("3 in 1 Sensor", "1.0");
      
      
       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.present(CHILD_ID_HUM, S_HUM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
        gw.present(CHILD_ID_MOT, S_MOTION);
         
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      
      {  
      
         delay(dht.getMinimumSamplingPeriod());
      
        float temperature = dht.getTemperature();
        float humidity = dht.getHumidity();
        
        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));
          Serial.print("T: ");
          Serial.println(temperature);
        }
        
        if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum) {
            lastHum = humidity;
            gw.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
        
        // get the battery Voltage
         int sensorValue = analogRead(BATTERY_SENSE_PIN);
         #ifdef DEBUG
         Serial.println(sensorValue);
         #endif
         
         // 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;
      
         #ifdef DEBUG
         Serial.print("Battery Voltage: ");
         Serial.print(batteryV);
         Serial.println(" V");
      
         Serial.print("Battery percent: ");
         Serial.print(batteryPcnt);
         Serial.println(" %");
         #endif
      
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
        if (tripped != lastTripped ) {      
          Serial.println(tripped);
          gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        }
        
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gw.sendBatteryLevel(batteryPcnt);
           oldBatteryPcnt = batteryPcnt;
         }
        
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        gw.sleep(INTERRUPT, CHANGE, 0 );
      }
      

      thanks very much!

      posted in My Project
      rhuehn
      rhuehn
    • RE: Low Bat Powered 3 in 1 sensor Please Help

      Hello @AWI and @dynamite

      My apologies for the delay in this reply as I was travelling and did not have access to the net... Thank you both for the replies, I just got back and couldn't wait to reply:

      @AWI I will comment on the items below from your first post, below:

      PIR HC-SR501 is standard a 5V sensor. You need to modify / connect it different to have it work at 3.3V
      This has been modified to work @ 3.3V

      The PIR will not allow for lower consumption that around 60uA (that is what it takes).
      Using the gw.sleep() with a timer value takes around 6uA , you can only go lower by using only interrupt.
      I believe I need to look at this at this seems to be a better option, and more suitable for the application

      A pro-mini should be modified. At least remove/ disconnect the led and preferably the regulator.
      These have also been modified as mentioned by yourself.

      Burning a bootloader to run on 1 mHz will reduce the consumption by a few percent only when active (not in sleep). But keeps the arduino awake longer when a lot of processing is to be done. So just keep it at 8 mHz.
      Sounds like a great idea, I will keep it as is

      I believe @dynamite is referencing this post above, and BTW @dynamite I want to do the EXACT same thing:

      link Here

      So a few things, based on that Ikea module, We are trying to acheive the lowest power consumption from either 2 or 3 X AA Batteries. modified to work with perhaps a PIR HC-SR501 ( discarding the original ), along with a DHT11 ( or 22 ), and report battery levels, and send the information back to a gateway, perhaps every hour unless the temp changes +/- 1 degree, or the PIR wakes, causing an action to be triggered, then go back to deep sleep.

      I tried a few modification to your suggestions, but lost functionality with the PIR, and battery reporting. My sketch is below, and not working very well:

       #define MY_NODE_ID 31
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>
      #include <Vcc.h>  
      
      
      
      //Constants
      #define SKETCH_NAME           "3 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define CHILD_ID_HUM 0                    // Child id for Humidity
      #define CHILD_ID_TEMP 1                   // Child id for Temperature
      #define CHILD_ID_MOT 2                   // Id of the sensor child
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
      #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)
      
      //Misc. variables
      uint8_t switchState = 2;
      unsigned long SLEEP_TIME = 820000;      // Sleep time between reads (in milliseconds) (close to 15')
      unsigned long SLEEP_TIME2 = 275000;     // Sleep time after int. (in milliseconds) (close to 5')
      uint8_t cycleInProgress = 0;
      uint8_t firstReportDone = 0;
      uint8_t firstReportWithZeroDone = 0;
      const float VccExpected   = 3.0;
      const float VccCorrection = 2.860/2.92;  // Measured Vcc by multimeter divided by reported Vcc
      Vcc vcc(VccCorrection);
      static int oldBatteryPcnt = 0;
      
      MySensor gw;
      DHT dht;
      float lastTemp = 0 ;
      float lastHum = 0 ;
      boolean lastTripped = false ;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      
      void setup()  
      { 
        gw.begin();
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("3 in 1 Sensor", "1.0");
      
      
       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.present(CHILD_ID_HUM, S_HUM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
        gw.present(CHILD_ID_MOT, S_MOTION);
         
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      {
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
        if (tripped != lastTripped ) {      
          Serial.println(tripped);
          gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        }
        delay(dht.getMinimumSamplingPeriod());
      
        float temperature = dht.getTemperature();
        float humidity = dht.getHumidity();
        
        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));
          Serial.print("T: ");
          Serial.println(temperature);
        }
        
        if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum) {
            lastHum = humidity;
            gw.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
        // Read Battery Value
          int batteryPcnt = (int)vcc.Read_Perc(VccExpected);
          if (oldBatteryPcnt != batteryPcnt)
          {
              gw.sendBatteryLevel(batteryPcnt);
              oldBatteryPcnt = batteryPcnt;
          }
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        gw.sleep(DIGITAL_INPUT_SENSOR, CHANGE, 0 );
      }
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Low Bat Powered 3 in 1 sensor Please Help

      Hey @dynamite Thanks for the link. The sketch looks great, but I don't believe it deviates much from the sketch I have posted above. If we are trying to conserve utmost battery and only wake when the PIR triggers, or if there is a change in temp ( +/- a deg ), and then fire everything over in 1 burst, or alternatively every hour. Then look at possibly slowing down the pro mini to 1Mhz vs 8. I don't know what do you think?

      posted in Troubleshooting
      rhuehn
      rhuehn
    • Low Bat Powered 3 in 1 sensor Please Help

      Hello:

      I'm trying to build a very low powered ( 2 x AA ) Battery powered 3 in 1 sensor. ( Platform is a Pro Mini 8Mhz, 3.3v, with an PIR HC-SR501, NRF24L01, and a DHT 11 Temp and Hum sensor. The sensor should also report battery power periodically ideally using the vcc library.

      The code below, primarily taken from here works well, thanks very much to @AWI sam-ple code

      @AWI also mentions a great sketch here Good Example

      And I really want to implement it, using the vcc library to report battery power, but am having a really hard time getting these sketched together. There are several good post about low power consumption, and combining gw.send functions in one burst at the end of the sketch to save power. Also, clocking the pro mini down to 1Mhz to save power, and only update the gateway when temp or humidity levels change by .5 - 1 deg.

      Below is my code that is lacking a lot of this functionality. I've been trying for two weeks to try and implement this, but keep banging my head against the desk.... Is anyone able to help a newbie out? Thanks very much for any assistance.

       #define MY_GW_ID 31
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      
      int BATTERY_SENSE_PIN = A0;  // select the input pin for the battery sense point
      
      //Constants
      #define SKETCH_NAME           "3 in 1 Sensor"
      #define SKETCH_VERSION        "1.0"
      #define CHILD_ID_HUM 0                    // Child id for Humidity
      #define CHILD_ID_TEMP 1                   // Child id for Temperature
      #define CHILD_ID_MOT 2                   // Id of the sensor child
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7    // Where is my DHT22 data pin connected to
      #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)
      
      //Misc. variables
      uint8_t switchState = 2;
      unsigned long SLEEP_TIME = 820000;      // Sleep time between reads (in milliseconds) (close to 15')
      unsigned long SLEEP_TIME2 = 275000;     // Sleep time after int. (in milliseconds) (close to 5')
      uint8_t cycleInProgress = 0;
      uint8_t firstReportDone = 0;
      uint8_t firstReportWithZeroDone = 0;
      int oldBatteryPcnt = 0;
      
      MySensor gw;
      DHT dht;
      float lastTemp = 0 ;
      float lastHum = 0 ;
      boolean lastTripped = false ;
      boolean metric = true; 
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgMot(CHILD_ID_MOT, V_TRIPPED);
      
      void setup()  
      { 
         // use the 1.1 V internal reference
      #if defined(__AVR_ATmega2560__)
         analogReference(INTERNAL1V1);
      #else
         analogReference(INTERNAL);
      #endif
        gw.begin();
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("Humidity/Motion", "1.0");
      
      
       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.present(CHILD_ID_HUM, S_HUM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
        gw.present(CHILD_ID_MOT, S_MOTION);
         
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      
      {  
        // get the battery Voltage
         int sensorValue = analogRead(BATTERY_SENSE_PIN);
         #ifdef DEBUG
         Serial.println(sensorValue);
         #endif
         
         // 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;
      
         #ifdef DEBUG
         Serial.print("Battery Voltage: ");
         Serial.print(batteryV);
         Serial.println(" V");
      
         Serial.print("Battery percent: ");
         Serial.print(batteryPcnt);
         Serial.println(" %");
         #endif
      
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gw.sendBatteryLevel(batteryPcnt);
           oldBatteryPcnt = batteryPcnt;
         }
           
           
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
        if (tripped != lastTripped ) {      
          Serial.println(tripped);
          gw.send(msgMot.set(tripped?"1":"0"));  // Send tripped value to gw 
        }
        delay(dht.getMinimumSamplingPeriod());
      
        float temperature = dht.getTemperature();
        float humidity = dht.getHumidity();
        
        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));
          Serial.print("T: ");
          Serial.println(temperature);
        }
        
        if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum) {
            lastHum = humidity;
            gw.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        }
      
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gw.sendBatteryLevel(batteryPcnt);
           oldBatteryPcnt = batteryPcnt;
         }
        
       
        // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
        gw.sleep(INTERRUPT,CHANGE, SLEEP_TIME);
      }
      
      posted in Troubleshooting
      rhuehn
      rhuehn
    • RE: Multisensor PIR based on IKEA Molgan

      PLEASE! A How to would be amazing! Looking forward to it!

      posted in My Project
      rhuehn
      rhuehn
    • Motion Sensor stopped working on combined sketch ( Hum, Temp, Motion + Bat Level )

      Hello:

      **Please see edit BELOW:

      It appears that after a while the motion sensor actually works.... It's something to do with the sleep time....

      What I am trying to do is have the sensor sleep as much as possible to save battery, BUT should always trigger on motion movement. I think I have an error with sleep values and that is affecting the motion sensor from triggering.

      If anyone could recommend a way to sleep the unit as much as possible ( 3 in 1 Temp, Hum, Motion, + Bat reporting ), but always trigger when motion is around that would be greatly appreciated.**

      Sorry if this is a bit of a newbie question, but I can't figure out why my motion sensor stopped working in my sketch? It works fine if I upload the basic mysensors motion sensor sketch, but as it is combined in this code, it no longer reports any motion movement back ( physically waving my hand in front of it has no reaction ). I have tried to combine the following code, and also trying to make it as ultra lower power as possible, as it will be a small battery operated ( 3 x AA Batts ) pro mini, 8Mhz, 3.3v . Can anyone please suggest what I have done wrong? Below is the code:

      
      #define MY_NODE_ID 31
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable and select radio type attached
      #define MY_RADIO_NRF24
      //#define MY_RADIO_RFM69
      
      #include <SPI.h>
      #include <MySensor.h>  
      #include <DHT.h>  
      #include <Vcc.h>
      
      
      #define CHILD_ID_TEMP 1               // Child id for temperatur
      #define CHILD_ID_HUM 0                // Child id for humidity
      #define CHILD_ID_VOLT 3               // Child id for battery reading
      #define HUMIDITY_SENSOR_DIGITAL_PIN 7 // Where is my DHT22 data pin connected to
      
      #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
      
      int node_id=31;                        // What is my node id
      
      unsigned long SLEEP_TIME =30000UL;    // Sleep time between reads (in milliseconds)
      int sleepcycle=1;                     // Counter to count the amout of time not sending data
      int humoffset=2;                      // only data is send if humidity is changed for this amout
      int tempoffset=0.5;                   // only data is if temperature is changed for this amout 
      int gwsendtimout=20;                  // each 20*sleep_time (10 minutes) data will be send
      
      const float VccMin   = 1.5;           // Minimum expected Vcc level, in Volts.
      const float VccMax   = 3.2;           // Maximum expected Vcc level, in Volts.
      const float VccCorrection = 1.0/1.0;  // Measured Vcc by multimeter divided by reported Vcc
      
      Vcc vcc(VccCorrection);
      
      MySensor gw;
      DHT dht;
      //Store last values
      float lastTemp = 0 ;                  
      float lastHum = 0 ;
      float batteryV=0;
      int oldBatteryPcnt = 0;             
      boolean lastTripped = false ;
      boolean metric = true; 
      
      boolean gwsend = true;              // to determin if data has to be send
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgVolt(CHILD_ID_VOLT, V_VOLTAGE);
      MyMessage msg(CHILD_ID, V_TRIPPED);
      
      
      void setup()  
      { 
        gw.begin(NULL,31,false);
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
      
        // Send the Sketch Version Information to the Gateway
        gw.sendSketchInfo("Kitchen", "Sensor",true);
      
        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.present(CHILD_ID_HUM, S_HUM,"Kitchen Humidity");
        gw.present(CHILD_ID_TEMP, S_TEMP,"Kitchen Temperatuur");
        gw.present(CHILD_ID_VOLT, S_MULTIMETER,"Kitchen Battery voltage");
        gw.present(CHILD_ID, S_MOTION,"Kitchen Motion Sensor");
         
        metric = gw.getConfig().isMetric;
      }
      
      void loop()      
      
      {  
        // get the battery Voltage
         batteryV  = vcc.Read_Volts();
         int batteryPcnt = vcc.Read_Perc(VccMin, VccMax);
      
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gwsend=true;
           oldBatteryPcnt = batteryPcnt;
         }
          
        delay(dht.getMinimumSamplingPeriod());
      
        float temp1 = dht.getTemperature();
        float humidity = dht.getHumidity();
      
        if (isnan(temp1)) {
      //      Serial.println("Failed reading temperature from DHT");
        } else if ((temp1 <= lastTemp-tempoffset)||(temp1 >= lastTemp+tempoffset)) {
          lastTemp = temp1;
          if (!metric) {
            temp1 = dht.toFahrenheit(temp1);
          }
          gwsend=true;
        }
      
        if (isnan(humidity)) {
          //      Serial.println("Failed reading humidity from DHT");
        } else if ((humidity <= lastHum-humoffset)||(humidity >= lastHum+humoffset)) {
            lastHum = humidity;
            gwsend=true;
          }
      
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        Serial.println(tripped);
        gwsend=true;
        
        if (sleepcycle>gwsendtimout){
          gwsend=true;
         }
      
      if (gwsend){     
          gw.sendBatteryLevel(oldBatteryPcnt);
          gw.send(msgVolt.set(batteryV, 1));
          gw.send(msgTemp.set(lastTemp, 1));  
          gw.send(msgHum.set(lastHum, 1));
          gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
          
          gwsend=false;
          sleepcycle=1;
        }
        sleepcycle++;  
        gw.sleep(SLEEP_TIME);
      }
      

      Debug Below:

      send: 31-31-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
      send: 31-31-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,st=ok:1.5.4
      send: 31-31-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
      sensor started, id=31, parent=0, distance=1
      send: 31-31-0-0 s=255,c=3,t=11,pt=0,l=7,sg=0,st=ok:Kitchen
      send: 31-31-0-0 s=255,c=3,t=12,pt=0,l=6,sg=0,st=ok:Sensor
      send: 31-31-0-0 s=0,c=0,t=7,pt=0,l=16,sg=0,st=ok:Kitchen Humidity
      send: 31-31-0-0 s=1,c=0,t=6,pt=0,l=19,sg=0,st=ok:Kitchen Temperatuur
      send: 31-31-0-0 s=3,c=0,t=30,pt=0,l=23,sg=0,st=ok:Kitchen Battery voltage
      send: 31-31-0-0 s=2,c=0,t=1,pt=0,l=21,sg=0,st=ok:Kitchen Motion Sensor
      0
      send: 31-31-0-0 s=255,c=3,t=0,pt=1,l=1,sg=0,st=ok:100
      send: 31-31-0-0 s=3,c=1,t=38,pt=7,l=5,sg=0,st=ok:3.4
      send: 31-31-0-0 s=1,c=1,t=0,pt=7,l=5,sg=0,st=ok:19.0
      send: 31-31-0-0 s=0,c=1,t=1,pt=7,l=5,sg=0,st=ok:24.0
      send: 31-31-0-0 s=2,c=1,t=16,pt=0,l=1,sg=0,st=ok:0
      
      

      If I upload an older sketch, or even the basic motion sensor sketch, no issues, so it's not a hardware issue, just the combination of sketches broke the motion sensor readings ( 0 or 1 )

      Thanks very much

      posted in Troubleshooting
      rhuehn
      rhuehn