dht22 2x and LDR and RelayWithButtonActuator



  • dht22 2x and LDR and RelayWithButtonActuator. I cannot get it working the button with the relay. The combi script is like this and working without the button and relais

    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    #include <Bounce2.h>
    
    #define RELAY_ON 1
    #define RELAY_OFF 0
    
    MySensor gw;
    
    #define RADIO_ID 2                    // radio Id, whatever channel you assigned to
    #define noRelays 1
    const int relayPin[] = {A2}; //  switch around pins to your desire
    const int buttonPin[] = {5}; //  switch around pins to your desire
    
    
    //#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 LIGHT_SENSOR_ANALOG_PIN A0 // Arduino Analog I/O pin number for light sensor 
    
    
    
    #define CHILD_ID_HUM1 0
    #define CHILD_ID_HUM2 1
    #define CHILD_ID_TEMP1 3
    #define CHILD_ID_TEMP2 4
    //#define CHILD_ID_RELAYWITHACTUATOR 5   // Id of the sensor child
    #define CHILD_ID_LIGHT 6
    
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    
    
    
    //Bounce debouncer = Bounce(); 
    //int oldValue=0;
    //bool state;
    
    unsigned long SLEEP_TIME = 60000; // Sleep time between reads (in milliseconds)
    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);
    
    MyMessage msgHum1(CHILD_ID_HUM2, V_HUM);
    MyMessage msgTemp1(CHILD_ID_TEMP2, V_TEMP);
    
    MyMessage msgLDR(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    ///MyMessage msgRelay(CHILD_ID_RELAYWITHACTUATOR, V_LIGHT);
    
    int lastLightLevel;
    
    
    void setup()
    {
      //gw.begin();
    gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      // Send the sketch version information to the gateway and Controller
      //gw.sendSketchInfo("Light Sensor", "1.0");
      gw.sendSketchInfo("Combi LDR, Relay, Button, Temp, Hum, PIR sensors", "1.0");
     delay(250);
    
    for (int i = 0; i < noRelays; i++)
      {
     Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
      }
    
      //Setup Temp and Hum
      for (int i = 0; i < 2; i++)
      {
        dht[i] = new DHT;
        dht[i]->setup(sensorPin[i]);
      }
    
      //gw.sendSketchInfo("Humidity", "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);
      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);
      //gw.present(CHILD_ID_RELAYWITHACTUATOR, S_LIGHT);
    
      metric = gw.getConfig().isMetric;
      
      // 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_RELAYWITHACTUATOR);
      //digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
    
      
    }
    
    void loop()
    {
      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("Temp "));
          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("Humidity "));
          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(msgLDR.set(lightLevel));
    
          lastLightLevel = lightLevel;
          Serial.print("Lightlevel =");
          
          Serial.println(lightLevel);
    
          
      }
    
      }
    
      
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
    
      }
    
     
      
      
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
    }
    

    this is the working relay with button script

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    #define USE_MOTION_SENSOR
    #define MOTION_SENSOR_PIN    3
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    Bounce motionsDebouncer = Bounce();
    //
    MySensor gw;
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
    
      pinMode( MOTION_SENSOR_PIN, INPUT_PULLUP );
      motionsDebouncer.attach(MOTION_SENSOR_PIN);
      motionsDebouncer.interval(50);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {
      if ( motionsDebouncer.update()) {
        int value = motionsDebouncer.read();
        Serial.println( "Motion sensor is " + (String)value );
      }
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
      }
      }
    }
    
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
      delay( 50 ); // give the input pins some rest. Incomming messages are still being processed.
    }
    

    Can anybody find what I did wrong during the combining of the scripts?


  • Mod

    In what way does it not work?


  • Mod

    Looks like the same problem as in the other thread, the code is using the same child id for two different things. The relay and hum1 both use 0.



  • Both DHTs are working, the LDR also but the button and the relay are not working. pushing the button the relays should change its status.



  • @mfalkvidd ok I understand but now I only have 1 relay, sorry for the perhaps silly questions.


  • Mod

    @Dick easiest way to fix is probably to uncomment

    #define CHILD_ID_RELAYWITHACTUATOR 5
    

    and change

      msg[i].sensor = i;  
      gw.present(i, S_LIGHT); 
    

    to

      msg[i].sensor = CHILD_ID_RELAYWITHACTUATOR;  
      gw.present(CHILD_ID_RELAYWITHACTUATOR, S_LIGHT); // quick workaround, only works for 1 relay
    

    That will break if you start using more than one relay though, so keep that in mind if you expand the sketch in the future


 

503
Online

7.9k
Users

8.8k
Topics

94.2k
Posts