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. Cobine working DHT22 and LDR with a RelayWithButtonActuator

Cobine working DHT22 and LDR with a RelayWithButtonActuator

Scheduled Pinned Locked Moved Development
dht22 ldr relay
36 Posts 4 Posters 5.3k Views 5 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.
  • BartEB BartE

    @Dick
    laatste:163: error: 'msg' was not declared in this scope

    msg should become msgLight

    gw.send(msgLight.set(state ? false : true), true); // Send new state and request ack back
    

    and move one } just below this line

    gw.wait(HEARTBEAT); //sleep a bit
    }
    }
    

    to the very end of you sketch

    DickD Offline
    DickD Offline
    Dick
    wrote on last edited by
    #27

    no errors anymore. I test the script now and will also put the changes you posted befor.

    1 Reply Last reply
    0
    • BartEB BartE

      @Dick
      laatste:163: error: 'msg' was not declared in this scope

      msg should become msgLight

      gw.send(msgLight.set(state ? false : true), true); // Send new state and request ack back
      

      and move one } just below this line

      gw.wait(HEARTBEAT); //sleep a bit
      }
      }
      

      to the very end of you sketch

      DickD Offline
      DickD Offline
      Dick
      wrote on last edited by
      #28

      tested and also add the changes of your message (9hrs ago).
      What I see on the serial monitor is, temp (2x), hum (2x) and light level but nothing of the relay. it is still clicking but no change in the logging from 1 to 0 etc. I already adjusted the script this morning with the 'RelayWithButtonActuator Example'. so only the relay part is not working and not visible in the serial log. Any idea?

      BartEB 1 Reply Last reply
      0
      • DickD Dick

        tested and also add the changes of your message (9hrs ago).
        What I see on the serial monitor is, temp (2x), hum (2x) and light level but nothing of the relay. it is still clicking but no change in the logging from 1 to 0 etc. I already adjusted the script this morning with the 'RelayWithButtonActuator Example'. so only the relay part is not working and not visible in the serial log. Any idea?

        BartEB Offline
        BartEB Offline
        BartE
        Contest Winner
        wrote on last edited by
        #29

        @Dick are you sure it's not a hardware (wire) issue?

        What happens if you load the RelayWithButtonActuator example code (with the correct pinning of course) ?

        1 Reply Last reply
        0
        • BartEB BartE

          @Dick
          laatste:163: error: 'msg' was not declared in this scope

          msg should become msgLight

          gw.send(msgLight.set(state ? false : true), true); // Send new state and request ack back
          

          and move one } just below this line

          gw.wait(HEARTBEAT); //sleep a bit
          }
          }
          

          to the very end of you sketch

          DickD Offline
          DickD Offline
          Dick
          wrote on last edited by
          #30

          here the latist version but not working

          #include <SPI.h>
           #include <MySensor.h>
           #include <DHT.h>
           #include <Bounce2.h>
          
          #define CHILD_ID_HUM1 0
           #define CHILD_ID_HUM2 1
           #define CHILD_ID_TEMP1 3
           #define CHILD_ID_TEMP2 4
          
          #define CHILD_ID_LIGHT 0
           #define LIGHT_SENSOR_ANALOG_PIN A0
          
          // RelayWithActuator stuff
           #define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
           #define BUTTON_PIN 6 // Arduino Digital I/O pin number for button
           #define CHILD_ID 5 // Id of the sensor child
           #define RELAY_ON 1
           #define RELAY_OFF 0
           Bounce debouncer = Bounce();
           
          int oldValue=0;
          
           bool state;
           MySensor gw;
          MyMessage msgLight(CHILD_ID,V_LIGHT);
          MyMessage msgLightLevel(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
           int lastLightLevel;
          
          #define HEARTBEAT 10
           unsigned int timer = 0;
           // Sleep time between reads (in milliseconds)
           #define SLEEP_TIME 3000
          
          DHT* dht[2];
           byte sensorPin[2] = {3, 4};
           float lastTemp[2] = {0.0, 0.0};
           float lastHum[2] = {0.0, 0.0};
          
          boolean metric = true;
          //MyMessage msgHum(CHILD_ID_HUM1, V_HUM);
          //MyMessage msgTemp(CHILD_ID_TEMP1, V_TEMP);
          
          void setup()
           {
          
          gw.begin(incomingMessage, AUTO, true);
           
           gw.sendSketchInfo("HumidityAndRelay", "1.0");
           
           for (int i = 0; i < 2; i++)
          
          
           
           {
           dht[i] = new DHT;
           dht[i]->setup(sensorPin[i]);
           }
          
          
          
          gw.present(CHILD_ID_HUM1, S_HUM);
           gw.present(CHILD_ID_HUM2, S_HUM);
           gw.present(CHILD_ID_TEMP1, S_TEMP);
           gw.present(CHILD_ID_TEMP1, S_TEMP);
          
          // Setup the button and Activate internal pull-up
           pinMode(BUTTON_PIN,INPUT);
          // Activate internal pull-up
            digitalWrite(BUTTON_PIN,HIGH);
          
            pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
          
          
          // After setting up the button, setup debouncer
           debouncer.attach(BUTTON_PIN);
           debouncer.interval(5);
          
          // Register all sensors to gw (they will be created as child devices)
           gw.present(CHILD_ID, S_LIGHT);
          
          // Make sure relays are off when starting up
           digitalWrite(RELAY_PIN, RELAY_OFF);
           // Then set relay pins in output mode
           pinMode(RELAY_PIN, OUTPUT);
          
           pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
          
          // Set relay to last known state (using eeprom storage)
           state = gw.loadState(CHILD_ID);
           digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
          
          timer = 0;
           metric = gw.getConfig().isMetric;
           }
           
          void loop()
           {
           gw.process();
           timer++;
          
          if (timer > (SLEEP_TIME / HEARTBEAT))
           {
           // Reset timer again and for the next timed loop
           timer = 0;
           for (int i = 0; i < 2; i++)
           {
           delay(dht[i]->getMinimumSamplingPeriod());
           float temperature = dht[i]->getTemperature();
           if (isnan(temperature))
           {
           Serial.print(F("Failed reading temperature from DHT"));
           Serial.println(i);
           }
           else if (temperature != lastTemp[i])
           {
           lastTemp[i] = temperature;
           if (!metric)
           {
           temperature = dht[i]->toFahrenheit(temperature);
           }
          //gw.send(msgTemp.set(temperature, i));
           Serial.print(F("T"));
           Serial.print(i);
           Serial.print(F("= "));
           Serial.println(temperature);
           }
           float humidity = dht[i]->getHumidity();
           if (isnan(humidity))
           {
           Serial.print("Failed reading humidity from DHT");
           Serial.println(i);
           }
           else if (humidity != lastHum[i])
           {
           lastHum[i] = humidity;
          //gw.send(msgHum.set(humidity, 1));
           Serial.print(F("H"));
           Serial.print(i);
           Serial.print(F("= "));
           Serial.println(humidity);
           }
          
          int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
          //Serial.println(lightLevel);
           if (lightLevel != lastLightLevel) 
           {
          gw.send(msgLightLevel.set(lightLevel));
            //lastLightLevel = lightLevel;
            Serial.print("L=");
            
            Serial.println(lightLevel);
          
           }
          
            
          }
          
          
          
          
          debouncer.update();
            // Get the update value
            int value = debouncer.read();
            if (value != oldValue && value==0) 
            
            {
                gw.send(msgLight.set(state ? false : true), true); // Send new state and request ack back
            }
            
            oldValue = value;
           
          
          
          gw.wait(HEARTBEAT); //sleep a bit
           }
           }
          
          void incomingMessage(const MyMessage &message) 
          {
           // We only expect one type of message from controller. But we better check anyway.
           if (message.isAck()) 
           {
           Serial.println("This is an ack from gateway");
           }
          
          if (message.type == V_LIGHT) 
          {
           // Change relay state
           state = message.getBool();
           digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
           // Store state in eeprom
           gw.saveState(CHILD_ID, state);
           // Write some debug info
           Serial.print("Incoming change for sensor:");
           Serial.print(message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
          
          
          }
          
          } 
           
          
          
          BartEB 1 Reply Last reply
          0
          • DickD Dick

            here the latist version but not working

            #include <SPI.h>
             #include <MySensor.h>
             #include <DHT.h>
             #include <Bounce2.h>
            
            #define CHILD_ID_HUM1 0
             #define CHILD_ID_HUM2 1
             #define CHILD_ID_TEMP1 3
             #define CHILD_ID_TEMP2 4
            
            #define CHILD_ID_LIGHT 0
             #define LIGHT_SENSOR_ANALOG_PIN A0
            
            // RelayWithActuator stuff
             #define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
             #define BUTTON_PIN 6 // Arduino Digital I/O pin number for button
             #define CHILD_ID 5 // Id of the sensor child
             #define RELAY_ON 1
             #define RELAY_OFF 0
             Bounce debouncer = Bounce();
             
            int oldValue=0;
            
             bool state;
             MySensor gw;
            MyMessage msgLight(CHILD_ID,V_LIGHT);
            MyMessage msgLightLevel(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
             int lastLightLevel;
            
            #define HEARTBEAT 10
             unsigned int timer = 0;
             // Sleep time between reads (in milliseconds)
             #define SLEEP_TIME 3000
            
            DHT* dht[2];
             byte sensorPin[2] = {3, 4};
             float lastTemp[2] = {0.0, 0.0};
             float lastHum[2] = {0.0, 0.0};
            
            boolean metric = true;
            //MyMessage msgHum(CHILD_ID_HUM1, V_HUM);
            //MyMessage msgTemp(CHILD_ID_TEMP1, V_TEMP);
            
            void setup()
             {
            
            gw.begin(incomingMessage, AUTO, true);
             
             gw.sendSketchInfo("HumidityAndRelay", "1.0");
             
             for (int i = 0; i < 2; i++)
            
            
             
             {
             dht[i] = new DHT;
             dht[i]->setup(sensorPin[i]);
             }
            
            
            
            gw.present(CHILD_ID_HUM1, S_HUM);
             gw.present(CHILD_ID_HUM2, S_HUM);
             gw.present(CHILD_ID_TEMP1, S_TEMP);
             gw.present(CHILD_ID_TEMP1, S_TEMP);
            
            // Setup the button and Activate internal pull-up
             pinMode(BUTTON_PIN,INPUT);
            // Activate internal pull-up
              digitalWrite(BUTTON_PIN,HIGH);
            
              pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
            
            
            // After setting up the button, setup debouncer
             debouncer.attach(BUTTON_PIN);
             debouncer.interval(5);
            
            // Register all sensors to gw (they will be created as child devices)
             gw.present(CHILD_ID, S_LIGHT);
            
            // Make sure relays are off when starting up
             digitalWrite(RELAY_PIN, RELAY_OFF);
             // Then set relay pins in output mode
             pinMode(RELAY_PIN, OUTPUT);
            
             pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
            
            // Set relay to last known state (using eeprom storage)
             state = gw.loadState(CHILD_ID);
             digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
            
            timer = 0;
             metric = gw.getConfig().isMetric;
             }
             
            void loop()
             {
             gw.process();
             timer++;
            
            if (timer > (SLEEP_TIME / HEARTBEAT))
             {
             // Reset timer again and for the next timed loop
             timer = 0;
             for (int i = 0; i < 2; i++)
             {
             delay(dht[i]->getMinimumSamplingPeriod());
             float temperature = dht[i]->getTemperature();
             if (isnan(temperature))
             {
             Serial.print(F("Failed reading temperature from DHT"));
             Serial.println(i);
             }
             else if (temperature != lastTemp[i])
             {
             lastTemp[i] = temperature;
             if (!metric)
             {
             temperature = dht[i]->toFahrenheit(temperature);
             }
            //gw.send(msgTemp.set(temperature, i));
             Serial.print(F("T"));
             Serial.print(i);
             Serial.print(F("= "));
             Serial.println(temperature);
             }
             float humidity = dht[i]->getHumidity();
             if (isnan(humidity))
             {
             Serial.print("Failed reading humidity from DHT");
             Serial.println(i);
             }
             else if (humidity != lastHum[i])
             {
             lastHum[i] = humidity;
            //gw.send(msgHum.set(humidity, 1));
             Serial.print(F("H"));
             Serial.print(i);
             Serial.print(F("= "));
             Serial.println(humidity);
             }
            
            int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
            //Serial.println(lightLevel);
             if (lightLevel != lastLightLevel) 
             {
            gw.send(msgLightLevel.set(lightLevel));
              //lastLightLevel = lightLevel;
              Serial.print("L=");
              
              Serial.println(lightLevel);
            
             }
            
              
            }
            
            
            
            
            debouncer.update();
              // Get the update value
              int value = debouncer.read();
              if (value != oldValue && value==0) 
              
              {
                  gw.send(msgLight.set(state ? false : true), true); // Send new state and request ack back
              }
              
              oldValue = value;
             
            
            
            gw.wait(HEARTBEAT); //sleep a bit
             }
             }
            
            void incomingMessage(const MyMessage &message) 
            {
             // We only expect one type of message from controller. But we better check anyway.
             if (message.isAck()) 
             {
             Serial.println("This is an ack from gateway");
             }
            
            if (message.type == V_LIGHT) 
            {
             // Change relay state
             state = message.getBool();
             digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
             // Store state in eeprom
             gw.saveState(CHILD_ID, state);
             // Write some debug info
             Serial.print("Incoming change for sensor:");
             Serial.print(message.sensor);
             Serial.print(", New status: ");
             Serial.println(message.getBool());
            
            
            }
            
            } 
             
            
            
            BartEB Offline
            BartEB Offline
            BartE
            Contest Winner
            wrote on last edited by
            #31

            @Dick

            Change these lines

                oldValue = value;
                gw.wait(HEARTBEAT); //sleep a bit
              }
            }
            

            to

                oldValue = value;
              }
              gw.wait(HEARTBEAT); //sleep a bit
            } 
            
            DickD 2 Replies Last reply
            0
            • BartEB BartE

              @Dick

              Change these lines

                  oldValue = value;
                  gw.wait(HEARTBEAT); //sleep a bit
                }
              }
              

              to

                  oldValue = value;
                }
                gw.wait(HEARTBEAT); //sleep a bit
              } 
              
              DickD Offline
              DickD Offline
              Dick
              wrote on last edited by
              #32

              and about the test of the RelayWithButtonActuator Example,
              it works fine. I adjust the lines now and will test it

              1 Reply Last reply
              0
              • BartEB BartE

                @Dick

                Change these lines

                    oldValue = value;
                    gw.wait(HEARTBEAT); //sleep a bit
                  }
                }
                

                to

                    oldValue = value;
                  }
                  gw.wait(HEARTBEAT); //sleep a bit
                } 
                
                DickD Offline
                DickD Offline
                Dick
                wrote on last edited by
                #33

                tested but still clicking relay. As you can see that there is nothing to see in the serial log

                send: 4-4-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
                send: 4-4-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,st=ok:1.5.4
                send: 4-4-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
                read: 0-0-4 s=255,c=3,t=6,pt=0,l=1,sg=0:M
                repeater started, id=4, parent=0, distance=1
                send: 4-4-0-0 s=255,c=3,t=11,pt=0,l=16,sg=0,st=ok:HumidityAndRelay
                send: 4-4-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
                send: 4-4-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=ok:
                send: 4-4-0-0 s=1,c=0,t=7,pt=0,l=0,sg=0,st=ok:
                send: 4-4-0-0 s=3,c=0,t=6,pt=0,l=0,sg=0,st=ok:
                send: 4-4-0-0 s=3,c=0,t=6,pt=0,l=0,sg=0,st=ok:
                send: 4-4-0-0 s=5,c=0,t=3,pt=0,l=0,sg=0,st=ok:
                T0= 22.20
                H0= 61.80
                send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:74
                L=74
                T1= 25.00
                H1= 60.20
                send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:74
                L=74
                send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:74
                L=74
                send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:76
                L=76

                BartEB 1 Reply Last reply
                0
                • DickD Dick

                  tested but still clicking relay. As you can see that there is nothing to see in the serial log

                  send: 4-4-0-0 s=255,c=3,t=15,pt=2,l=2,sg=0,st=ok:0
                  send: 4-4-0-0 s=255,c=0,t=18,pt=0,l=5,sg=0,st=ok:1.5.4
                  send: 4-4-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
                  read: 0-0-4 s=255,c=3,t=6,pt=0,l=1,sg=0:M
                  repeater started, id=4, parent=0, distance=1
                  send: 4-4-0-0 s=255,c=3,t=11,pt=0,l=16,sg=0,st=ok:HumidityAndRelay
                  send: 4-4-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.0
                  send: 4-4-0-0 s=0,c=0,t=7,pt=0,l=0,sg=0,st=ok:
                  send: 4-4-0-0 s=1,c=0,t=7,pt=0,l=0,sg=0,st=ok:
                  send: 4-4-0-0 s=3,c=0,t=6,pt=0,l=0,sg=0,st=ok:
                  send: 4-4-0-0 s=3,c=0,t=6,pt=0,l=0,sg=0,st=ok:
                  send: 4-4-0-0 s=5,c=0,t=3,pt=0,l=0,sg=0,st=ok:
                  T0= 22.20
                  H0= 61.80
                  send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:74
                  L=74
                  T1= 25.00
                  H1= 60.20
                  send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:74
                  L=74
                  send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:74
                  L=74
                  send: 4-4-0-0 s=0,c=1,t=23,pt=2,l=2,sg=0,st=ok:76
                  L=76

                  BartEB Offline
                  BartEB Offline
                  BartE
                  Contest Winner
                  wrote on last edited by BartE
                  #34

                  @Dick If there is nothing visible in the log, then you can presume there is a hardware issue.

                  Some possible issues:

                  • loose relays wire (bad connection) --> try a different wire
                  • wrong pin is used (if set as input the pin will "float" resulting in unstable relay behaviour) --> double check your pin number in code and actual PCB
                  • pin double used ie the DHT drive poll this pin for some reason. --> use a different pin in both code and conection to relay
                  DickD 1 Reply Last reply
                  0
                  • BartEB BartE

                    @Dick If there is nothing visible in the log, then you can presume there is a hardware issue.

                    Some possible issues:

                    • loose relays wire (bad connection) --> try a different wire
                    • wrong pin is used (if set as input the pin will "float" resulting in unstable relay behaviour) --> double check your pin number in code and actual PCB
                    • pin double used ie the DHT drive poll this pin for some reason. --> use a different pin in both code and conection to relay
                    DickD Offline
                    DickD Offline
                    Dick
                    wrote on last edited by
                    #35

                    thank you BartE, I received today an new Uno and some other stuff so I can rebuild it al to see if it is an HW failure. I hope I can start tomorrow. I keep you informed but thanks for your support. to be continued.

                    BartEB 1 Reply Last reply
                    0
                    • DickD Dick

                      thank you BartE, I received today an new Uno and some other stuff so I can rebuild it al to see if it is an HW failure. I hope I can start tomorrow. I keep you informed but thanks for your support. to be continued.

                      BartEB Offline
                      BartEB Offline
                      BartE
                      Contest Winner
                      wrote on last edited by BartE
                      #36

                      @Dick Not sure what your approach is but normally i start with prototyping using these wires and when all works fine (software and hardware) i solder everything together on a PCB.

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


                      29

                      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