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

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. DHT12 + 2Relay + 2Button - Combining Problem

DHT12 + 2Relay + 2Button - Combining Problem

Scheduled Pinned Locked Moved Development
14 Posts 3 Posters 1.8k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • rejoe2R Offline
    rejoe2R Offline
    rejoe2
    wrote on last edited by
    #3

    Question: First all worked well, until you added the code for the DHT?
    IMO, the

    // Sleep for a while to save energy
      wait(UPDATE_INTERVAL);
    

    part blocks the entire code for most of the time. You should instead use a non-blocking loop structure using millis(). See the https://www.mysensors.org/build/pulse_power example (follow the path, when "#define SLEEP_MODE true" is used).

    Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

    michlb1982M 1 Reply Last reply
    1
    • rejoe2R rejoe2

      Question: First all worked well, until you added the code for the DHT?
      IMO, the

      // Sleep for a while to save energy
        wait(UPDATE_INTERVAL);
      

      part blocks the entire code for most of the time. You should instead use a non-blocking loop structure using millis(). See the https://www.mysensors.org/build/pulse_power example (follow the path, when "#define SLEEP_MODE true" is used).

      michlb1982M Offline
      michlb1982M Offline
      michlb1982
      wrote on last edited by
      #4

      @rejoe2
      thanks for the link.. i will try... and report if it worked..

      1 Reply Last reply
      1
      • michlb1982M Offline
        michlb1982M Offline
        michlb1982
        wrote on last edited by
        #5

        Hello again.. long time ago.. but still here. ;-)

        well thankss for the tip... but now i have a other problem...
        the relay will work when the order comes from the gateway ... but when i press the button, nothing happens...
        any ideas?

        here the code:

        #define MY_DEBUG
        #define MY_RADIO_NRF24
        #define MY_REPEATER_FEATURE
        
        // Define Node ID
        #define MY_NODE_ID 101
        
        #define RELAY_ON 1
        #define RELAY_OFF 0
        
        #define SSR_A_ID 1   // Id of the sensor child
        #define SSR_B_ID 2   // Id of the sensor child
        
        #define CHILD_ID_HUM 0
        #define CHILD_ID_TEMP 0//1
        #define SENSOR_TEMP_OFFSET 0
        
        #include <SPI.h>
        #include <MySensors.h>
        #include <Bounce2.h>
        #include <DHT12.h>
        #include <Wire.h>
         
        const int buttonPinA = 7;
        const int buttonPinB = 8;
        const int relayPinA = 6;
        const int relayPinB = 5;
        int oldValueA = 0;
        int oldValueB = 0;
        bool stateA = false;
        bool stateB = false;
        
        float lastTemp;
        float lastHum;
        uint8_t nNoUpdatesTemp;
        uint8_t nNoUpdatesHum;
        bool metric = true;
        static const uint8_t FORCE_UPDATE_N_READS = 10;//10
        static const uint64_t UPDATE_INTERVAL = 30000;//30000
        
        Bounce debouncerA = Bounce();
        Bounce debouncerB = Bounce();
        
        MyMessage msgA(SSR_A_ID, V_STATUS);
        MyMessage msgB(SSR_B_ID, V_STATUS);
        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
        
        DHT12 dht12; 
        
        void setup()
        {
          //Serial.begin(115200);
          Wire.begin();
          pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
          pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
        
        
          // After setting up the buttons, setup debouncer
          debouncerA.attach(buttonPinA);
          debouncerA.interval(5); //5
          debouncerB.attach(buttonPinB);
          debouncerB.interval(5);//5
        
          // Make sure relays are off when starting up
          digitalWrite(relayPinA, RELAY_OFF);
          digitalWrite(relayPinB, RELAY_OFF);
          // Then set relay pins in output mode
          pinMode(relayPinA, OUTPUT);
          pinMode(relayPinB, OUTPUT);
          
        }
        
        void presentation()  {
          // Send the sketch version information to the gateway and Controller
          sendSketchInfo("2xRelais,2xButton,DHT12", "1.2");
        
          // Register all sensors to gw (they will be created as child devices)
          present(SSR_A_ID, S_LIGHT);
          present(SSR_B_ID, S_LIGHT);
        
          present(CHILD_ID_HUM, S_HUM);
          present(CHILD_ID_TEMP, S_TEMP);
        
          metric = getControllerConfig().isMetric;
        
        }
        
        /*
           Example on how to asynchronously check for new messages from gw
        */
        void loop()
        {
          debouncerA.update();
          // Get the update value
          int valueA = debouncerA.read();
          if (valueA != oldValueA) {
            send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
          oldValueA = valueA;
          }
         
        
          debouncerB.update();
          // Get the update value
          int valueB = debouncerB.read();
          if (valueB != oldValueB) {
            send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
          oldValueB = valueB;
          }
        
            
          // Force reading sensor, so it works also after sleep()
        //  dht12.readSensor(true);
        
          // Get temperature from DHT library
          float temperature = dht12.readTemperature();
          if (isnan(temperature)) {
            Serial.println("Failed reading temperature from DHT!");
          } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
            // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
            lastTemp = temperature;
        
            // apply the offset before converting to something different than Celsius degrees
            temperature += SENSOR_TEMP_OFFSET;
        
            if (!metric) {
              temperature = dht12.readTemperature();
            }
            // Reset no updates counter
            nNoUpdatesTemp = 0;
            send(msgTemp.set(temperature, 1));
        
            #ifdef MY_DEBUG
            Serial.print("T: ");
            Serial.println(temperature);
            #endif
          } else {
            // Increase no update counter if the temperature stayed the same
            nNoUpdatesTemp++;
          }
        
          // Get humidity from DHT library
          float humidity = dht12.readHumidity();
          if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
          } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
            // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
            lastHum = humidity;
            // Reset no updates counter
            nNoUpdatesHum = 0;
            send(msgHum.set(humidity, 1));
        
            #ifdef MY_DEBUG
            Serial.print("H: ");
            Serial.println(humidity);
            #endif
          } else {
            // Increase no update counter if the humidity stayed the same
            nNoUpdatesHum++;
          }
        
          // Sleep for a while to save energy
          wait(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_STATUS) {
              
            switch (message.sensor) {
              case 1:
                stateA = message.getBool();
                digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
                break;
              case 2:
                stateB = message.getBool();
                digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF);
                break;
            }
           
              // Write some debug info
            Serial.print("Incoming change for sensor:");
            Serial.println(message.sensor);
            //Serial.print("from node:");
            //Serial.println(message.sender);
            //Serial.print(", New status: ");
            Serial.println(message.getBool());
          }
        }
        
        mfalkviddM 1 Reply Last reply
        0
        • michlb1982M michlb1982

          Hello again.. long time ago.. but still here. ;-)

          well thankss for the tip... but now i have a other problem...
          the relay will work when the order comes from the gateway ... but when i press the button, nothing happens...
          any ideas?

          here the code:

          #define MY_DEBUG
          #define MY_RADIO_NRF24
          #define MY_REPEATER_FEATURE
          
          // Define Node ID
          #define MY_NODE_ID 101
          
          #define RELAY_ON 1
          #define RELAY_OFF 0
          
          #define SSR_A_ID 1   // Id of the sensor child
          #define SSR_B_ID 2   // Id of the sensor child
          
          #define CHILD_ID_HUM 0
          #define CHILD_ID_TEMP 0//1
          #define SENSOR_TEMP_OFFSET 0
          
          #include <SPI.h>
          #include <MySensors.h>
          #include <Bounce2.h>
          #include <DHT12.h>
          #include <Wire.h>
           
          const int buttonPinA = 7;
          const int buttonPinB = 8;
          const int relayPinA = 6;
          const int relayPinB = 5;
          int oldValueA = 0;
          int oldValueB = 0;
          bool stateA = false;
          bool stateB = false;
          
          float lastTemp;
          float lastHum;
          uint8_t nNoUpdatesTemp;
          uint8_t nNoUpdatesHum;
          bool metric = true;
          static const uint8_t FORCE_UPDATE_N_READS = 10;//10
          static const uint64_t UPDATE_INTERVAL = 30000;//30000
          
          Bounce debouncerA = Bounce();
          Bounce debouncerB = Bounce();
          
          MyMessage msgA(SSR_A_ID, V_STATUS);
          MyMessage msgB(SSR_B_ID, V_STATUS);
          MyMessage msgHum(CHILD_ID_HUM, V_HUM);
          MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
          
          DHT12 dht12; 
          
          void setup()
          {
            //Serial.begin(115200);
            Wire.begin();
            pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
            pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
          
          
            // After setting up the buttons, setup debouncer
            debouncerA.attach(buttonPinA);
            debouncerA.interval(5); //5
            debouncerB.attach(buttonPinB);
            debouncerB.interval(5);//5
          
            // Make sure relays are off when starting up
            digitalWrite(relayPinA, RELAY_OFF);
            digitalWrite(relayPinB, RELAY_OFF);
            // Then set relay pins in output mode
            pinMode(relayPinA, OUTPUT);
            pinMode(relayPinB, OUTPUT);
            
          }
          
          void presentation()  {
            // Send the sketch version information to the gateway and Controller
            sendSketchInfo("2xRelais,2xButton,DHT12", "1.2");
          
            // Register all sensors to gw (they will be created as child devices)
            present(SSR_A_ID, S_LIGHT);
            present(SSR_B_ID, S_LIGHT);
          
            present(CHILD_ID_HUM, S_HUM);
            present(CHILD_ID_TEMP, S_TEMP);
          
            metric = getControllerConfig().isMetric;
          
          }
          
          /*
             Example on how to asynchronously check for new messages from gw
          */
          void loop()
          {
            debouncerA.update();
            // Get the update value
            int valueA = debouncerA.read();
            if (valueA != oldValueA) {
              send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
            oldValueA = valueA;
            }
           
          
            debouncerB.update();
            // Get the update value
            int valueB = debouncerB.read();
            if (valueB != oldValueB) {
              send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
            oldValueB = valueB;
            }
          
              
            // Force reading sensor, so it works also after sleep()
          //  dht12.readSensor(true);
          
            // Get temperature from DHT library
            float temperature = dht12.readTemperature();
            if (isnan(temperature)) {
              Serial.println("Failed reading temperature from DHT!");
            } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
              // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
              lastTemp = temperature;
          
              // apply the offset before converting to something different than Celsius degrees
              temperature += SENSOR_TEMP_OFFSET;
          
              if (!metric) {
                temperature = dht12.readTemperature();
              }
              // Reset no updates counter
              nNoUpdatesTemp = 0;
              send(msgTemp.set(temperature, 1));
          
              #ifdef MY_DEBUG
              Serial.print("T: ");
              Serial.println(temperature);
              #endif
            } else {
              // Increase no update counter if the temperature stayed the same
              nNoUpdatesTemp++;
            }
          
            // Get humidity from DHT library
            float humidity = dht12.readHumidity();
            if (isnan(humidity)) {
              Serial.println("Failed reading humidity from DHT");
            } else if (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS) {
              // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
              lastHum = humidity;
              // Reset no updates counter
              nNoUpdatesHum = 0;
              send(msgHum.set(humidity, 1));
          
              #ifdef MY_DEBUG
              Serial.print("H: ");
              Serial.println(humidity);
              #endif
            } else {
              // Increase no update counter if the humidity stayed the same
              nNoUpdatesHum++;
            }
          
            // Sleep for a while to save energy
            wait(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_STATUS) {
                
              switch (message.sensor) {
                case 1:
                  stateA = message.getBool();
                  digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
                  break;
                case 2:
                  stateB = message.getBool();
                  digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF);
                  break;
              }
             
                // Write some debug info
              Serial.print("Incoming change for sensor:");
              Serial.println(message.sensor);
              //Serial.print("from node:");
              //Serial.println(message.sender);
              //Serial.print(", New status: ");
              Serial.println(message.getBool());
            }
          }
          
          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #6

          @michlb1982 there are no digitalWrite commands in the loop. Withiut digitalWrite, there will be no changes to the relay.

          1 Reply Last reply
          0
          • michlb1982M Offline
            michlb1982M Offline
            michlb1982
            wrote on last edited by
            #7

            @mfalkvidd you are right, but in the original Relay-Scetch there is also no digitalWrite command in the loop-section... https://www.mysensors.org/build/relay
            only in the viod receive message section is a digitalwrite command....

            there is also another problmem...sometimes (i can't say when) there is the RElay not working or works lately.. .maybe it is when the DHT is measuring? i can't explain...

            mfalkviddM 1 Reply Last reply
            0
            • michlb1982M michlb1982

              @mfalkvidd you are right, but in the original Relay-Scetch there is also no digitalWrite command in the loop-section... https://www.mysensors.org/build/relay
              only in the viod receive message section is a digitalwrite command....

              there is also another problmem...sometimes (i can't say when) there is the RElay not working or works lately.. .maybe it is when the DHT is measuring? i can't explain...

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

              @michlb1982 yes you are right, sorry for the confusion. The relay will be switched when the node receives ack for the outgoing message.

              Looking at the debug logs (from node and gateway) will probably be the quickest way to figure out what is wrong.

              1 Reply Last reply
              0
              • michlb1982M Offline
                michlb1982M Offline
                michlb1982
                wrote on last edited by
                #9

                well i can't find anything strange in the node debug messages..

                90243 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:35.541 -> Incoming change for sensor:2
                08:25:35.541 -> 1
                92163 TSF:MSG:READ,0-0-101,s=1,c=1,t=2,pt=0,l=1,sg=0:0
                08:25:37.478 -> Incoming change for sensor:1
                08:25:37.478 -> 0
                92843 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                08:25:38.139 -> Incoming change for sensor:2
                08:25:38.139 -> 0
                93059 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:38.375 -> Incoming change for sensor:2
                08:25:38.375 -> 1
                93458 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                08:25:38.769 -> Incoming change for sensor:2
                08:25:38.769 -> 0
                93674 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:38.963 -> Incoming change for sensor:2
                08:25:39.003 -> 1
                94012 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                08:25:39.315 -> Incoming change for sensor:2
                08:25:39.315 -> 0
                94229 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:39.524 -> Incoming change for sensor:2
                08:25:39.557 -> 1
                94721 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                08:25:40.049 -> Incoming change for sensor:2
                08:25:40.049 -> 0
                94893 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:23.5
                08:25:40.213 -> T: 23.50
                94973 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:40.6
                08:25:40.284 -> H: 40.60
                08:25:40.284 -> 94981 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:40.284 -> Incoming change for sensor:2
                08:25:40.284 -> 1
                95182 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                08:25:40.495 -> Incoming change for sensor:2
                08:25:40.495 -> 0
                95396 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:40.702 -> Incoming change for sensor:2
                08:25:40.702 -> 1
                97184 TSF:MSG:READ,0-0-101,s=1,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:42.505 -> Incoming change for sensor:1
                08:25:42.505 -> 1
                97225 TSF:MSG:READ,0-0-101,s=1,c=1,t=2,pt=0,l=1,sg=0:1
                08:25:42.547 -> Incoming change for sensor:1
                08:25:42.547 -> 1
                

                and the acting time delay is only sometimes... i don't know jet how to reproduce it....
                ok with the time delay i can live... but the buttons would be great if the could work...
                any ideas left?

                mfalkviddM 1 Reply Last reply
                0
                • michlb1982M michlb1982

                  well i can't find anything strange in the node debug messages..

                  90243 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:35.541 -> Incoming change for sensor:2
                  08:25:35.541 -> 1
                  92163 TSF:MSG:READ,0-0-101,s=1,c=1,t=2,pt=0,l=1,sg=0:0
                  08:25:37.478 -> Incoming change for sensor:1
                  08:25:37.478 -> 0
                  92843 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                  08:25:38.139 -> Incoming change for sensor:2
                  08:25:38.139 -> 0
                  93059 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:38.375 -> Incoming change for sensor:2
                  08:25:38.375 -> 1
                  93458 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                  08:25:38.769 -> Incoming change for sensor:2
                  08:25:38.769 -> 0
                  93674 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:38.963 -> Incoming change for sensor:2
                  08:25:39.003 -> 1
                  94012 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                  08:25:39.315 -> Incoming change for sensor:2
                  08:25:39.315 -> 0
                  94229 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:39.524 -> Incoming change for sensor:2
                  08:25:39.557 -> 1
                  94721 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                  08:25:40.049 -> Incoming change for sensor:2
                  08:25:40.049 -> 0
                  94893 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=0,pt=7,l=5,sg=0,ft=0,st=OK:23.5
                  08:25:40.213 -> T: 23.50
                  94973 TSF:MSG:SEND,101-101-0-0,s=0,c=1,t=1,pt=7,l=5,sg=0,ft=0,st=OK:40.6
                  08:25:40.284 -> H: 40.60
                  08:25:40.284 -> 94981 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:40.284 -> Incoming change for sensor:2
                  08:25:40.284 -> 1
                  95182 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:0
                  08:25:40.495 -> Incoming change for sensor:2
                  08:25:40.495 -> 0
                  95396 TSF:MSG:READ,0-0-101,s=2,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:40.702 -> Incoming change for sensor:2
                  08:25:40.702 -> 1
                  97184 TSF:MSG:READ,0-0-101,s=1,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:42.505 -> Incoming change for sensor:1
                  08:25:42.505 -> 1
                  97225 TSF:MSG:READ,0-0-101,s=1,c=1,t=2,pt=0,l=1,sg=0:1
                  08:25:42.547 -> Incoming change for sensor:1
                  08:25:42.547 -> 1
                  

                  and the acting time delay is only sometimes... i don't know jet how to reproduce it....
                  ok with the time delay i can live... but the buttons would be great if the could work...
                  any ideas left?

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

                  @michlb1982 when/how many times did you press the button?

                  There are no send commands in the log, so the button press was never registered.

                  1 Reply Last reply
                  0
                  • mfalkviddM Offline
                    mfalkviddM Offline
                    mfalkvidd
                    Mod
                    wrote on last edited by mfalkvidd
                    #11

                    Now I see. During the 30 second wait, no button presses will be registered. You’ll need to use interrupts, or modify the code to not use wait/sleep in the loop.

                    This is what rejoe2 mentioned in https://forum.mysensors.org/post/93454

                    1 Reply Last reply
                    0
                    • michlb1982M Offline
                      michlb1982M Offline
                      michlb1982
                      wrote on last edited by
                      #12

                      @mfalkvidd you are so right!!! the "wait" is the problem... i changed the script.. i use a if to check if the update-interval is over and then i go through the dht loop....
                      now the bounce are also working..!

                      here my code...

                      #define MY_DEBUG
                      #define MY_RADIO_NRF24
                      //#define MY_REPEATER_FEATURE
                      
                      // Define Node ID
                      #define MY_NODE_ID 101
                      
                      #define RELAY_ON 1
                      #define RELAY_OFF 0
                      
                      #define SSR_A_ID 1   // Id of the sensor child
                      #define SSR_B_ID 2   // Id of the sensor child
                      
                      #define CHILD_ID_HUM 0
                      #define CHILD_ID_TEMP 0//1
                      #define SENSOR_TEMP_OFFSET 0
                      
                      #include <SPI.h>
                      #include <MySensors.h>
                      #include <Bounce2.h>
                      #include <DHT12.h>
                      #include <Wire.h>
                       
                      const int buttonPinA = 7;
                      const int buttonPinB = 8;
                      const int relayPinA = 6;
                      const int relayPinB = 5;
                      int oldValueA = 0;
                      int oldValueB = 0;
                      unsigned long startMillis;
                      unsigned long currentMillis;
                      
                      bool stateA = false;
                      bool stateB = false;
                      
                      float lastTemp;
                      float lastHum;
                      uint8_t nNoUpdatesTemp;
                      uint8_t nNoUpdatesHum;
                      bool metric = true;
                      static const uint8_t FORCE_UPDATE_N_READS = 10;//10
                      static const uint64_t UPDATE_INTERVAL = 30000;//30000
                      
                      
                      Bounce debouncerA = Bounce();
                      Bounce debouncerB = Bounce();
                      
                      MyMessage msgA(SSR_A_ID, V_STATUS);
                      MyMessage msgB(SSR_B_ID, V_STATUS);
                      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                      
                      DHT12 dht12; 
                      
                      void setup()
                      {
                        //Serial.begin(115200);
                        Wire.begin();
                        pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
                        pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
                      
                      
                      
                        // After setting up the buttons, setup debouncer
                        debouncerA.attach(buttonPinA);
                        debouncerA.interval(5); //5
                        debouncerB.attach(buttonPinB);
                        debouncerB.interval(5);//5
                      
                        // Make sure relays are off when starting up
                        digitalWrite(relayPinA, RELAY_ON);
                        digitalWrite(relayPinB, RELAY_ON);
                        // Then set relay pins in output mode
                        pinMode(relayPinA, OUTPUT);
                        pinMode(relayPinB, OUTPUT);
                      }
                      
                      void presentation()  {
                        // Send the sketch version information to the gateway and Controller
                        sendSketchInfo("2xRelais,2xButton,DHT12", "1.2a");
                      
                        // Register all sensors to gw (they will be created as child devices)
                        present(SSR_A_ID, S_LIGHT);
                        present(SSR_B_ID, S_LIGHT);
                      
                        present(CHILD_ID_HUM, S_HUM);
                        present(CHILD_ID_TEMP, S_TEMP);
                      
                        metric = getControllerConfig().isMetric;
                      
                      }
                      
                      /*
                         Example on how to asynchronously check for new messages from gw
                      */
                      void loop()
                      {
                        currentMillis = millis();
                        debouncerA.update();
                        // Get the update value
                        int valueA = debouncerA.read();
                        if (valueA != oldValueA) {
                          send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
                        oldValueA = valueA;
                        }
                       
                      
                        debouncerB.update();
                        // Get the update value
                        int valueB = debouncerB.read();
                        if (valueB != oldValueB) {
                          send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                        oldValueB = valueB;
                        }
                      
                          
                        // Force reading sensor, so it works also after sleep()
                      //  dht12.readSensor(true);
                        if (currentMillis - startMillis >= UPDATE_INTERVAL) 
                        {
                        // Get temperature from DHT library
                        float temperature = dht12.readTemperature();
                        if (isnan(temperature)) {
                          Serial.println("Failed reading temperature from DHT!");
                        } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
                          // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
                          lastTemp = temperature;
                      
                          // apply the offset before converting to something different than Celsius degrees
                          temperature += SENSOR_TEMP_OFFSET;
                      
                          if (!metric) {
                            temperature = dht12.readTemperature();
                          }
                          // Reset no updates counter
                          nNoUpdatesTemp = 0;
                          send(msgTemp.set(temperature, 1));
                      
                          #ifdef MY_DEBUG
                          Serial.print("T: ");
                          Serial.println(temperature);
                          #endif
                        } else {
                          // Increase no update counter if the temperature stayed the same
                          nNoUpdatesTemp++;
                        }
                      
                        // Get humidity from DHT library
                        float humidity = dht12.readHumidity();
                        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++;
                        }
                        startMillis = currentMillis;
                        Serial.print("Startzeit: ");
                        Serial.println(startMillis);
                        }
                        // Sleep for a while to save energy
                        //wait(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_STATUS) {
                            
                          switch (message.sensor) {
                            case 1:
                              stateA = message.getBool();
                              digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
                              break;
                            case 2:
                              stateB = message.getBool();
                              digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF);
                              break;
                          }
                         
                            // Write some debug info
                          Serial.print("Incoming change for sensor:");
                          Serial.println(message.sensor);
                          //Serial.print("from node:");
                          //Serial.println(message.sender);
                          //Serial.print(", New status: ");
                          Serial.println(message.getBool());
                        }
                      }
                      
                      mfalkviddM 1 Reply Last reply
                      1
                      • michlb1982M michlb1982

                        @mfalkvidd you are so right!!! the "wait" is the problem... i changed the script.. i use a if to check if the update-interval is over and then i go through the dht loop....
                        now the bounce are also working..!

                        here my code...

                        #define MY_DEBUG
                        #define MY_RADIO_NRF24
                        //#define MY_REPEATER_FEATURE
                        
                        // Define Node ID
                        #define MY_NODE_ID 101
                        
                        #define RELAY_ON 1
                        #define RELAY_OFF 0
                        
                        #define SSR_A_ID 1   // Id of the sensor child
                        #define SSR_B_ID 2   // Id of the sensor child
                        
                        #define CHILD_ID_HUM 0
                        #define CHILD_ID_TEMP 0//1
                        #define SENSOR_TEMP_OFFSET 0
                        
                        #include <SPI.h>
                        #include <MySensors.h>
                        #include <Bounce2.h>
                        #include <DHT12.h>
                        #include <Wire.h>
                         
                        const int buttonPinA = 7;
                        const int buttonPinB = 8;
                        const int relayPinA = 6;
                        const int relayPinB = 5;
                        int oldValueA = 0;
                        int oldValueB = 0;
                        unsigned long startMillis;
                        unsigned long currentMillis;
                        
                        bool stateA = false;
                        bool stateB = false;
                        
                        float lastTemp;
                        float lastHum;
                        uint8_t nNoUpdatesTemp;
                        uint8_t nNoUpdatesHum;
                        bool metric = true;
                        static const uint8_t FORCE_UPDATE_N_READS = 10;//10
                        static const uint64_t UPDATE_INTERVAL = 30000;//30000
                        
                        
                        Bounce debouncerA = Bounce();
                        Bounce debouncerB = Bounce();
                        
                        MyMessage msgA(SSR_A_ID, V_STATUS);
                        MyMessage msgB(SSR_B_ID, V_STATUS);
                        MyMessage msgHum(CHILD_ID_HUM, V_HUM);
                        MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
                        
                        DHT12 dht12; 
                        
                        void setup()
                        {
                          //Serial.begin(115200);
                          Wire.begin();
                          pinMode(buttonPinA, INPUT_PULLUP); // Setup the button Activate internal pull-up
                          pinMode(buttonPinB, INPUT_PULLUP); // Setup the button Activate internal pull-up
                        
                        
                        
                          // After setting up the buttons, setup debouncer
                          debouncerA.attach(buttonPinA);
                          debouncerA.interval(5); //5
                          debouncerB.attach(buttonPinB);
                          debouncerB.interval(5);//5
                        
                          // Make sure relays are off when starting up
                          digitalWrite(relayPinA, RELAY_ON);
                          digitalWrite(relayPinB, RELAY_ON);
                          // Then set relay pins in output mode
                          pinMode(relayPinA, OUTPUT);
                          pinMode(relayPinB, OUTPUT);
                        }
                        
                        void presentation()  {
                          // Send the sketch version information to the gateway and Controller
                          sendSketchInfo("2xRelais,2xButton,DHT12", "1.2a");
                        
                          // Register all sensors to gw (they will be created as child devices)
                          present(SSR_A_ID, S_LIGHT);
                          present(SSR_B_ID, S_LIGHT);
                        
                          present(CHILD_ID_HUM, S_HUM);
                          present(CHILD_ID_TEMP, S_TEMP);
                        
                          metric = getControllerConfig().isMetric;
                        
                        }
                        
                        /*
                           Example on how to asynchronously check for new messages from gw
                        */
                        void loop()
                        {
                          currentMillis = millis();
                          debouncerA.update();
                          // Get the update value
                          int valueA = debouncerA.read();
                          if (valueA != oldValueA) {
                            send(msgA.set(stateA ? false : true), true); // Send new state and request ack back
                          oldValueA = valueA;
                          }
                         
                        
                          debouncerB.update();
                          // Get the update value
                          int valueB = debouncerB.read();
                          if (valueB != oldValueB) {
                            send(msgB.set(stateB ? false : true), true); // Send new state and request ack back
                          oldValueB = valueB;
                          }
                        
                            
                          // Force reading sensor, so it works also after sleep()
                        //  dht12.readSensor(true);
                          if (currentMillis - startMillis >= UPDATE_INTERVAL) 
                          {
                          // Get temperature from DHT library
                          float temperature = dht12.readTemperature();
                          if (isnan(temperature)) {
                            Serial.println("Failed reading temperature from DHT!");
                          } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
                            // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
                            lastTemp = temperature;
                        
                            // apply the offset before converting to something different than Celsius degrees
                            temperature += SENSOR_TEMP_OFFSET;
                        
                            if (!metric) {
                              temperature = dht12.readTemperature();
                            }
                            // Reset no updates counter
                            nNoUpdatesTemp = 0;
                            send(msgTemp.set(temperature, 1));
                        
                            #ifdef MY_DEBUG
                            Serial.print("T: ");
                            Serial.println(temperature);
                            #endif
                          } else {
                            // Increase no update counter if the temperature stayed the same
                            nNoUpdatesTemp++;
                          }
                        
                          // Get humidity from DHT library
                          float humidity = dht12.readHumidity();
                          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++;
                          }
                          startMillis = currentMillis;
                          Serial.print("Startzeit: ");
                          Serial.println(startMillis);
                          }
                          // Sleep for a while to save energy
                          //wait(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_STATUS) {
                              
                            switch (message.sensor) {
                              case 1:
                                stateA = message.getBool();
                                digitalWrite(message.sensor + 4, stateA ? RELAY_ON : RELAY_OFF);
                                break;
                              case 2:
                                stateB = message.getBool();
                                digitalWrite(message.sensor + 4, stateB ? RELAY_ON : RELAY_OFF);
                                break;
                            }
                           
                              // Write some debug info
                            Serial.print("Incoming change for sensor:");
                            Serial.println(message.sensor);
                            //Serial.print("from node:");
                            //Serial.println(message.sender);
                            //Serial.print(", New status: ");
                            Serial.println(message.getBool());
                          }
                        }
                        
                        mfalkviddM Offline
                        mfalkviddM Offline
                        mfalkvidd
                        Mod
                        wrote on last edited by
                        #13

                        @michlb1982 great work, thanks for reporting back

                        1 Reply Last reply
                        0
                        • rejoe2R Offline
                          rejoe2R Offline
                          rejoe2
                          wrote on last edited by
                          #14

                          @michlb1982 Congratulations also from my side :smile: !

                          One additional remark: In case you want to use more relays and buttons, you may hav a closer look to the use of array functions. Good, but rather complex example: https://forum.mysensors.org/post/51488

                          Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

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


                          16

                          Online

                          11.7k

                          Users

                          11.2k

                          Topics

                          113.1k

                          Posts


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

                          • Don't have an account? Register

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