DHT12 + 2Relay + 2Button - Combining Problem



  • Hello
    I tried to combine a DHT12 + 2 Relay incl. Buttons.
    ![alt text](0_1536489069705_SENS-DHT-12.JPG image url)
    Well it works (somtimes longer.. sometimes not..), i combined the Scetches of both but often i have the problme, that the Relays don't act anymore and the buttons didn't either since the beginning...

    i found something on google that i have to change the sleep to wait command so it works little better but after some time it also even quits working.

    what is my mistake???

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

    Thanks for help!


  • Mod

    @michlb1982 what does the debug log say?



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

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

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



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


 

430
Online

7.7k
Users

8.6k
Topics

92.4k
Posts