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.
  • DickD Offline
    DickD Offline
    Dick
    wrote on last edited by
    #4

    is somebody interested in challanging my problem?

    BartEB 1 Reply Last reply
    0
    • DickD Dick

      is somebody interested in challanging my problem?

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

      @Dick please note: i did not test it on an actual node but this combination should work

      #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
      
      // RelayWithActuator stuff
      #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
      #define BUTTON_PIN  3  // 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(); 
      bool state;
      MySensor gw;
      MyMessage msg(CHILD_ID,V_LIGHT);
      
      #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);
        for (int i = 0; i < 2; i++)
        {
          dht[i] = new DHT;
          dht[i]->setup(sensorPin[i]);
        }
      
        gw.sendSketchInfo("HumidityAndRelay", "1.0");
      
        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_PULLUP);
        
        // 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);   
            
        // 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);
            }
          }
        } 
        
        // Get the button update state (true if a change was detected) 
        if (debouncer.update() && debouncer.read() == 0) {
            gw.send(msg.set(state?false:true), true); // Send new state and request ack back
        }
        
        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());
         } 
      }
      
      DickD 1 Reply Last reply
      0
      • BartEB BartE

        @Dick please note: i did not test it on an actual node but this combination should work

        #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
        
        // RelayWithActuator stuff
        #define RELAY_PIN  4  // Arduino Digital I/O pin number for relay 
        #define BUTTON_PIN  3  // 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(); 
        bool state;
        MySensor gw;
        MyMessage msg(CHILD_ID,V_LIGHT);
        
        #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);
          for (int i = 0; i < 2; i++)
          {
            dht[i] = new DHT;
            dht[i]->setup(sensorPin[i]);
          }
        
          gw.sendSketchInfo("HumidityAndRelay", "1.0");
        
          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_PULLUP);
          
          // 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);   
              
          // 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);
              }
            }
          } 
          
          // Get the button update state (true if a change was detected) 
          if (debouncer.update() && debouncer.read() == 0) {
              gw.send(msg.set(state?false:true), true); // Send new state and request ack back
          }
          
          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());
           } 
        }
        
        DickD Offline
        DickD Offline
        Dick
        wrote on last edited by
        #6

        @BartE
        It works fine but the relay does not stay ON after changing the status from "0" to "1".
        Another thing is that the light messuring part is missing, it was the the script I posted. I will try to get it implemented in yours. So If you know how to change the behaviour of the relay, I should appreciate it.

        BartEB 1 Reply Last reply
        0
        • DickD Dick

          @BartE
          It works fine but the relay does not stay ON after changing the status from "0" to "1".
          Another thing is that the light messuring part is missing, it was the the script I posted. I will try to get it implemented in yours. So If you know how to change the behaviour of the relay, I should appreciate it.

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

          @Dick how does relay exactly response to the button?
          If you press the button (and keep it down) is the relay turned on?
          or is it switched on when you release the button again?
          The button can be "reversed" connected (on = off and off - on)
          You can play a bit with this line

          if (debouncer.update() && debouncer.read() == HIGH) {
          

          or

          if (debouncer.update() && debouncer.read() == LOW) {
          

          and how is the button wired? Does is connect to ground or to Vcc (5 volt) ?
          This sketch requires connection to ground (with internal PULL-UP)

          About the light measuring ?? i just did copy your example from the first post and copied the RelayWithSwitch example in it. (Please notice the message part of sending humidity and temperature are still commented out as in the posted sketch)

          The part i did add my self is the timer stuff (so keeping the switched being probed while the DHT sensor is only probed twice a minute)

          Cheers BartE

          DickD 1 Reply Last reply
          0
          • BartEB BartE

            @Dick how does relay exactly response to the button?
            If you press the button (and keep it down) is the relay turned on?
            or is it switched on when you release the button again?
            The button can be "reversed" connected (on = off and off - on)
            You can play a bit with this line

            if (debouncer.update() && debouncer.read() == HIGH) {
            

            or

            if (debouncer.update() && debouncer.read() == LOW) {
            

            and how is the button wired? Does is connect to ground or to Vcc (5 volt) ?
            This sketch requires connection to ground (with internal PULL-UP)

            About the light measuring ?? i just did copy your example from the first post and copied the RelayWithSwitch example in it. (Please notice the message part of sending humidity and temperature are still commented out as in the posted sketch)

            The part i did add my self is the timer stuff (so keeping the switched being probed while the DHT sensor is only probed twice a minute)

            Cheers BartE

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

            @BartE The button connect to gnd to switch. if connected to gnd the relay switch for a second and than again in rest, also if I keept the switch to gnd so it is momentory.

            Also about the light sensor integration as stated in my last meassage, I was experimenting I only get an error BOLTED in my script "no matching function for call to 'MyMessage::MyMessage(int, mysensor_data, mysensor_data)'"

            #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 0

            // RelayWithActuator stuff
            #define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
            #define BUTTON_PIN A1 // 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();
            bool state;
            MySensor gw;
            MyMessage msg(CHILD_ID,V_LIGHT, V_LIGHT_LEVEL);bolded text
            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);
            for (int i = 0; i < 2; i++)
            {
            dht[i] = new DHT;
            dht[i]->setup(sensorPin[i]);
            }

            gw.sendSketchInfo("HumidityAndRelay", "1.0");

            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);
            gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);

            // Setup the button and Activate internal pull-up
            pinMode(BUTTON_PIN,INPUT_PULLUP);

            // 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);

            // 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(msg.set(lightLevel));

              //lastLightLevel = lightLevel;
              Serial.print("L=");
              
              Serial.println(lightLevel);
            
              
            }
            

            }

            // Get the button update state (true if a change was detected)
            if (debouncer.update() && debouncer.read() == 0) {
            gw.send(msg.set(state?false:true), true); // Send new state and request ack back
            }

            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());
            

            }
            }

            DickD 1 Reply Last reply
            0
            • DickD Dick

              @BartE The button connect to gnd to switch. if connected to gnd the relay switch for a second and than again in rest, also if I keept the switch to gnd so it is momentory.

              Also about the light sensor integration as stated in my last meassage, I was experimenting I only get an error BOLTED in my script "no matching function for call to 'MyMessage::MyMessage(int, mysensor_data, mysensor_data)'"

              #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 0

              // RelayWithActuator stuff
              #define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
              #define BUTTON_PIN A1 // 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();
              bool state;
              MySensor gw;
              MyMessage msg(CHILD_ID,V_LIGHT, V_LIGHT_LEVEL);bolded text
              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);
              for (int i = 0; i < 2; i++)
              {
              dht[i] = new DHT;
              dht[i]->setup(sensorPin[i]);
              }

              gw.sendSketchInfo("HumidityAndRelay", "1.0");

              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);
              gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);

              // Setup the button and Activate internal pull-up
              pinMode(BUTTON_PIN,INPUT_PULLUP);

              // 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);

              // 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(msg.set(lightLevel));

                //lastLightLevel = lightLevel;
                Serial.print("L=");
                
                Serial.println(lightLevel);
              
                
              }
              

              }

              // Get the button update state (true if a change was detected)
              if (debouncer.update() && debouncer.read() == 0) {
              gw.send(msg.set(state?false:true), true); // Send new state and request ack back
              }

              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());
              

              }
              }

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

              @Dick
              I get some closer but not complete working. Did also some playing met de debounce but no result. here the script till now with the error log

              italicised texttempHumand_relay:24: error: redefinition of 'MyMessage msg'

              MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);

                        ^
              

              tempHumand_relay:23: error: 'MyMessage msg' previously declared here

              MyMessage msg(CHILD_ID,V_LIGHT);

                     ^
              

              E:\Arduino Projects\tempHumand_relay\tempHumand_relay.ino: In function 'void setup()':

              tempHumand_relay:43: error: 'incomingMessage' was not declared in this scope

              gw.begin(incomingMessage, AUTO, true);

                      ^
              

              E:\Arduino Projects\tempHumand_relay\tempHumand_relay.ino: In function 'void loop()':

              tempHumand_relay:151: error: a function-definition is not allowed here before '{' token

              void incomingMessage(const MyMessage &message) {

                                                          ^
              

              tempHumand_relay:171: error: expected '}' at end of input

              }

              ^

              redefinition of 'MyMessage msg'

              and now the code till now

              #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 0

              // RelayWithActuator stuff
              #define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
              #define BUTTON_PIN A1 // 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();
              bool state;
              MySensor gw;
              MyMessage msg(CHILD_ID,V_LIGHT);
              MyMessage msg(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);
              for (int i = 0; i < 2; i++)
              {
              dht[i] = new DHT;
              dht[i]->setup(sensorPin[i]);
              }

              gw.sendSketchInfo("HumidityAndRelay", "1.0");

              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_PULLUP);

              // 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);

              // 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(msg.set(lightLevel));

                //lastLightLevel = lightLevel;
                Serial.print("L=");
                
                Serial.println(lightLevel);
              
              
              
                
              }
              

              }

              // Get the button update state (true if a change was detected)
              if (debouncer.update() && debouncer.read() == 0) {
              gw.send(msg.set(state?false:true), true); // Send new state and request ack back
              }

              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());
              

              }
              }
              }

              bolded text

              1 Reply Last reply
              0
              • BartEB Offline
                BartEB Offline
                BartE
                Contest Winner
                wrote on last edited by BartE
                #10

                @Dick
                Couple of remarks

                This peice of code tries to declare the same variable twice (msg)

                MyMessage msg(CHILD_ID,V_LIGHT);
                MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                

                change to name to msgLight and msgLightLeven

                MyMessage msgLight(CHILD_ID,V_LIGHT);
                MyMessage msgLightLevel(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                

                You also have to changethis line:

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

                to this

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

                Also this line

                gw.send(msg.set(lightLevel));
                

                needs to be adapted to

                gw.send(msgLightLevel.set(lightLevel));
                

                And i think the last } needs to be removed
                Oh and please use the code mark up function for posting source code
                (by using the </> icon)

                DickD 1 Reply Last reply
                0
                • BartEB BartE

                  @Dick
                  Couple of remarks

                  This peice of code tries to declare the same variable twice (msg)

                  MyMessage msg(CHILD_ID,V_LIGHT);
                  MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                  

                  change to name to msgLight and msgLightLeven

                  MyMessage msgLight(CHILD_ID,V_LIGHT);
                  MyMessage msgLightLevel(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                  

                  You also have to changethis line:

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

                  to this

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

                  Also this line

                  gw.send(msg.set(lightLevel));
                  

                  needs to be adapted to

                  gw.send(msgLightLevel.set(lightLevel));
                  

                  And i think the last } needs to be removed
                  Oh and please use the code mark up function for posting source code
                  (by using the </> icon)

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

                  thank you Bart, I am learning with these adjustment, how things work together aso. I change the code as you mentioned and still getting verifying errors as included. I hope you can help me so I can finish this, for me not so easy project.

                  E:\Arduino Projects\tempHumand_relay\tempHumand_relay.ino: In function 'void setup()':

                  tempHumand_relay:43: error: 'incomingMessage' was not declared in this scope

                  gw.begin(incomingMessage, AUTO, true);

                          ^
                  

                  E:\Arduino Projects\tempHumand_relay\tempHumand_relay.ino: In function 'void loop()':

                  tempHumand_relay:130: error: 'msgLightLevel' was not declared in this scope

                     gw.send(msgLightLevel.set(lightLevel));
                  
                             ^
                  

                  tempHumand_relay:145: error: 'msgLight' was not declared in this scope

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

                  tempHumand_relay:151: error: a function-definition is not allowed here before '{' token

                  void incomingMessage(const MyMessage &message) {

                                                              ^
                  

                  tempHumand_relay:170: error: expected '}' at end of input

                  }

                  'incomingMessage' was not declared in this scope

                  DickD 1 Reply Last reply
                  0
                  • DickD Dick

                    thank you Bart, I am learning with these adjustment, how things work together aso. I change the code as you mentioned and still getting verifying errors as included. I hope you can help me so I can finish this, for me not so easy project.

                    E:\Arduino Projects\tempHumand_relay\tempHumand_relay.ino: In function 'void setup()':

                    tempHumand_relay:43: error: 'incomingMessage' was not declared in this scope

                    gw.begin(incomingMessage, AUTO, true);

                            ^
                    

                    E:\Arduino Projects\tempHumand_relay\tempHumand_relay.ino: In function 'void loop()':

                    tempHumand_relay:130: error: 'msgLightLevel' was not declared in this scope

                       gw.send(msgLightLevel.set(lightLevel));
                    
                               ^
                    

                    tempHumand_relay:145: error: 'msgLight' was not declared in this scope

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

                    tempHumand_relay:151: error: a function-definition is not allowed here before '{' token

                    void incomingMessage(const MyMessage &message) {

                                                                ^
                    

                    tempHumand_relay:170: error: expected '}' at end of input

                    }

                    'incomingMessage' was not declared in this scope

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

                    does anyone know how to go on with the final part of this project?

                    DickD 1 Reply Last reply
                    0
                    • DickD Dick

                      does anyone know how to go on with the final part of this project?

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

                      Compilation finished without errors.
                      but I sea nothing on my serial monitor. What could be the issue?The script is now

                      #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 0
                      // RelayWithActuator stuff
                      #define RELAY_PIN 5 // Arduino Digital I/O pin number for relay
                      #define BUTTON_PIN A1 // 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();
                      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_PULLUP);
                      // 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);
                      // 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, );
                      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);
                      
                      
                      
                        
                      }
                      }
                      // Get the button update state (true if a change was detected)
                      if (debouncer.update() && debouncer.read() == 0) {
                      gw.send(msgLight.set(state?false:true), true); // Send new state and request ack back
                      }
                      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());
                      }
                      }
                      
                      
                      1 Reply Last reply
                      0
                      • mfalkviddM Offline
                        mfalkviddM Offline
                        mfalkvidd
                        Mod
                        wrote on last edited by mfalkvidd
                        #14

                        Is DEBUG enabled in MyConfig.h? If it isn't, you'll see nothing from MySensors on the serial monitor.
                        If you want your own serial prints to be visible, you need to run

                        Serial.begin(9600);
                        

                        (or whatever speed you want to use in the Serial Monitor) inside setup().

                        You have commented out gw.begin. That means MySensors will not be started. If you don't want to handle incoming messages yet, you can use

                        gw.begin();
                        
                        DickD 1 Reply Last reply
                        0
                        • mfalkviddM mfalkvidd

                          Is DEBUG enabled in MyConfig.h? If it isn't, you'll see nothing from MySensors on the serial monitor.
                          If you want your own serial prints to be visible, you need to run

                          Serial.begin(9600);
                          

                          (or whatever speed you want to use in the Serial Monitor) inside setup().

                          You have commented out gw.begin. That means MySensors will not be started. If you don't want to handle incoming messages yet, you can use

                          gw.begin();
                          
                          DickD Offline
                          DickD Offline
                          Dick
                          wrote on last edited by
                          #15

                          debug is enabled and I change gw.begin () and it worked, I see temp and two hum mesurements. But also my relay is uncontroling switching every secunds and the changing is not visible in the monitoring window. has that something to do with the
                          gw.begin(incomming
                          change?

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

                            Try commenting out

                            state = gw.loadState(CHILD_ID);
                            

                            until you are ready to handle incoming messages.

                            DickD 1 Reply Last reply
                            0
                            • mfalkviddM mfalkvidd

                              Try commenting out

                              state = gw.loadState(CHILD_ID);
                              

                              until you are ready to handle incoming messages.

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

                              I did what you suggested but no result. Perhaps I have to split-up the project into my initial plan (ldr, and 2x temp/Hum) and make another node for the relay because I do not know to solve the current issue.

                              1 Reply Last reply
                              0
                              • alexsh1A Offline
                                alexsh1A Offline
                                alexsh1
                                wrote on last edited by alexsh1
                                #18

                                Try to define BUTTON_PIN to a digital pin and see if it makes a difference.
                                Change the following in the setup

                                pinMode(BUTTON_PIN, INPUT);
                                digitalWrite(BUTTON_PIN, HIGH);
                                
                                

                                This is my sketch working on arduino+relay on my corner stand lamp and it is working fine on MySensors 1.5.4

                                // This code is basically the same as the default RelayWithButtonActuator sketch. The only difference is the pin numbering.
                                
                                #include <MySensor.h>
                                #include <SPI.h>
                                #include <Bounce2.h>
                                
                                #define RELAY_PIN  3  // Arduino Digital I/O pin number for relay 
                                #define BUTTON_PIN  4  // Arduino Digital I/O pin number for button 
                                #define CHILD_ID 1   // Id of the sensor child
                                #define RELAY_ON 1
                                #define RELAY_OFF 0
                                #define NODE_ADDRESS 2
                                
                                Bounce debouncer = Bounce(); 
                                int oldValue=0;
                                bool state;
                                MySensor gw;
                                MyMessage msg(CHILD_ID,V_LIGHT);
                                
                                void setup()  
                                {  
                                  gw.begin(incomingMessage, NODE_ADDRESS, true);
                                
                                  // Send the sketch version information to the gateway and Controller
                                  gw.sendSketchInfo("Relay & Button", "1.0");
                                
                                 // 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);
                                
                                  // 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);   
                                      
                                  // Set relay to last known state (using eeprom storage) 
                                  state = gw.loadState(CHILD_ID);
                                  digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                }
                                
                                
                                /*
                                *  Example on how to asynchronously check for new messages from gw
                                */
                                void loop() 
                                {
                                  gw.process();
                                  debouncer.update();
                                  // Get the update value
                                  int value = debouncer.read();
                                  if (value != oldValue && value==0) {
                                      gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                                  }
                                  oldValue = value;
                                } 
                                 
                                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());
                                   } 
                                }
                                
                                DickD 1 Reply Last reply
                                0
                                • alexsh1A alexsh1

                                  Try to define BUTTON_PIN to a digital pin and see if it makes a difference.
                                  Change the following in the setup

                                  pinMode(BUTTON_PIN, INPUT);
                                  digitalWrite(BUTTON_PIN, HIGH);
                                  
                                  

                                  This is my sketch working on arduino+relay on my corner stand lamp and it is working fine on MySensors 1.5.4

                                  // This code is basically the same as the default RelayWithButtonActuator sketch. The only difference is the pin numbering.
                                  
                                  #include <MySensor.h>
                                  #include <SPI.h>
                                  #include <Bounce2.h>
                                  
                                  #define RELAY_PIN  3  // Arduino Digital I/O pin number for relay 
                                  #define BUTTON_PIN  4  // Arduino Digital I/O pin number for button 
                                  #define CHILD_ID 1   // Id of the sensor child
                                  #define RELAY_ON 1
                                  #define RELAY_OFF 0
                                  #define NODE_ADDRESS 2
                                  
                                  Bounce debouncer = Bounce(); 
                                  int oldValue=0;
                                  bool state;
                                  MySensor gw;
                                  MyMessage msg(CHILD_ID,V_LIGHT);
                                  
                                  void setup()  
                                  {  
                                    gw.begin(incomingMessage, NODE_ADDRESS, true);
                                  
                                    // Send the sketch version information to the gateway and Controller
                                    gw.sendSketchInfo("Relay & Button", "1.0");
                                  
                                   // 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);
                                  
                                    // 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);   
                                        
                                    // Set relay to last known state (using eeprom storage) 
                                    state = gw.loadState(CHILD_ID);
                                    digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                                  }
                                  
                                  
                                  /*
                                  *  Example on how to asynchronously check for new messages from gw
                                  */
                                  void loop() 
                                  {
                                    gw.process();
                                    debouncer.update();
                                    // Get the update value
                                    int value = debouncer.read();
                                    if (value != oldValue && value==0) {
                                        gw.send(msg.set(state?false:true), true); // Send new state and request ack back
                                    }
                                    oldValue = value;
                                  } 
                                   
                                  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());
                                     } 
                                  }
                                  
                                  DickD Offline
                                  DickD Offline
                                  Dick
                                  wrote on last edited by
                                  #19

                                  thank you Alex, it did not work. It is strange, in my topic BartE posted a srcipt what workerd almost perfect. Relay change was good and saw it in the Serial minitor changing from 0 to 1. after adding the LDR stuff it went wrong. So it must be something in the LDR add-on. combining is still not easy for me but I am learning.

                                  alexsh1A 1 Reply Last reply
                                  0
                                  • DickD Dick

                                    thank you Alex, it did not work. It is strange, in my topic BartE posted a srcipt what workerd almost perfect. Relay change was good and saw it in the Serial minitor changing from 0 to 1. after adding the LDR stuff it went wrong. So it must be something in the LDR add-on. combining is still not easy for me but I am learning.

                                    alexsh1A Offline
                                    alexsh1A Offline
                                    alexsh1
                                    wrote on last edited by alexsh1
                                    #20

                                    @Dick With sketches it is like this sometimes - troubleshooting is time consuming.
                                    I would suggest the following. Try to comment out line by line on the LDR code, upload and see if it works. Try to insert as many Serial.print as you could to print the status of variables to the serial port.

                                    I do not have the LDR so cannot help you much on this one. I have multiple other sketches combined together working just fine.

                                    DickD 1 Reply Last reply
                                    1
                                    • alexsh1A alexsh1

                                      @Dick With sketches it is like this sometimes - troubleshooting is time consuming.
                                      I would suggest the following. Try to comment out line by line on the LDR code, upload and see if it works. Try to insert as many Serial.print as you could to print the status of variables to the serial port.

                                      I do not have the LDR so cannot help you much on this one. I have multiple other sketches combined together working just fine.

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

                                      @alexsh1
                                      That could I do, thanks for the tip and will do that today. Any idea is welcome :smiley:

                                      BartEB 1 Reply Last reply
                                      1
                                      • DickD Dick

                                        @alexsh1
                                        That could I do, thanks for the tip and will do that today. Any idea is welcome :smiley:

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

                                        @Dick I think this issue sits here you use Digital port 0 (which is the serial debug port) i.s.o. Analog port 0 (A0).

                                        Change this line

                                         #define LIGHT_SENSOR_ANALOG_PIN 0
                                        

                                        to

                                         #define LIGHT_SENSOR_ANALOG_PIN A0
                                        

                                        And you better also add this line to the setup function

                                        pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
                                        
                                        DickD 1 Reply Last reply
                                        0
                                        • BartEB BartE

                                          @Dick I think this issue sits here you use Digital port 0 (which is the serial debug port) i.s.o. Analog port 0 (A0).

                                          Change this line

                                           #define LIGHT_SENSOR_ANALOG_PIN 0
                                          

                                          to

                                           #define LIGHT_SENSOR_ANALOG_PIN A0
                                          

                                          And you better also add this line to the setup function

                                          pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
                                          
                                          DickD Offline
                                          DickD Offline
                                          Dick
                                          wrote on last edited by
                                          #23

                                          have not tried all the given options but started again with all the previous advises of Bart. Noe I still struggle with an error
                                          "gw.begin(incomingMessage, AUTO, true);" in mij script. I checkt the "{ }" over and over again but it must be something else.
                                          can anybody take a look?

                                          #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);
                                          
                                          
                                          // 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(msg.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());
                                          
                                          
                                          }
                                          
                                           
                                           
                                          
                                          
                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          27

                                          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