combining button with dht11 issue



  • Hi.
    I have put a dht11 sensor on my arduino nano light switch. The hardware switch is incase no UI or ALEXA down. I don't need the humidity component so I removed it.

    
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_REPEATER_FEATURE
    #define MY_NODE_ID 5
    #include <SPI.h>
    #include <MySensors.h> 
    #include <elapsedMillis.h> 
    #include <Bounce2.h>
    #include <DHT.h>
    #define DHT_DATA_PIN 7
    #define RELAY_PIN  5
    #define BUTTON_PIN  3
    #define CHILD_ID 1
    #define RELAY_ON 1
    #define RELAY_OFF 0
    #define SENSOR_TEMP_OFFSET 0
    static const uint64_t UPDATE_INTERVAL = 5000;
    static const uint8_t FORCE_UPDATE_N_READS = 1;
    Bounce debouncer = Bounce();
    bool state = false;
    bool initialValueSent = false;
    #define CHILD_ID_TEMP 3
    unsigned long interval=10000; // the time we need to wait
    unsigned long previousMillis=0; // millis() returns an unsigned long.
    float lastTemp;
    
    uint8_t nNoUpdatesTemp;
    bool metric = true;
    MyMessage msg(CHILD_ID, V_STATUS);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    
    void presentation()  
    { 
      sendSketchInfo("kitchen switch", "1.1");
      
      present(CHILD_ID, S_BINARY);
     present(CHILD_ID_TEMP, S_TEMP);
      metric = getConfig().isMetric;
    }
    
    void setup()
    {
      pinMode(BUTTON_PIN, INPUT_PULLUP);
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(10);
      digitalWrite(RELAY_PIN, RELAY_OFF);
      pinMode(RELAY_PIN, OUTPUT);
      
      dht.setup(DHT_DATA_PIN); // set data pin of DHT sensor
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      }
    }
    
    void loop()      
    {  
     
    if (!initialValueSent) {
        Serial.println("Sending initial value");
        send(msg.set(state?RELAY_ON:RELAY_OFF));
        Serial.println("Requesting initial value from controller");
        request(CHILD_ID, V_STATUS);
        wait(5000, C_SET, V_STATUS);
        presentation();
      }
      if (debouncer.update()) {
        if (debouncer.read()==LOW) {
          state = !state;
        send(msg.set(state?RELAY_ON:RELAY_OFF), true);   
        }
      }
    
     unsigned long currentMillis = millis();
     if ((unsigned long)(currentMillis - previousMillis) >= interval) {
      
      dht.readSensor(true);
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
    
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        nNoUpdatesTemp = 0;
        temperature += SENSOR_TEMP_OFFSET;
        send(msgTemp.set(temperature, 1));
    
        #ifdef MY_DEBUG
        Serial.print("T: ");
        Serial.println(temperature);
        #endif
      } else {
        nNoUpdatesTemp++;
      }
     previousMillis = millis();
     }}
    void receive(const MyMessage &message) {
      if (message.isAck()) {
         Serial.println("This is an ack from gateway");
      }
      if (message.type == V_STATUS) {
        if (!initialValueSent) {
          Serial.println("Receiving initial value from controller");
          initialValueSent = true;
        }
        state = (bool)message.getInt();
        digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
        send(msg.set(state?RELAY_ON:RELAY_OFF));
      }
    }
    

    my problem is that the temp shows ok, the switch sends commands when tripped but is not on the Home Assistant UI or in entities. I need this switch to show up to set triggers.

    Any help appreciated

    Pi3 Home assistant + mosquitto
    esp8266 wireless gateway
    nano nodes Nrf2401 radios



  • sorted works a treat now. My bad, used a sketch I had tinkered with in the past. sketches melded now. Finished sketch below for them that might need. The sketch is for domestic light swith.

    #define MY_NODE_ID 5
    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h>
    #include <DHT.h>
    #define DHT_DATA_PIN 7
    #define CHILD_ID 3
    #define BUTTON_PIN  3
    #define SENSOR_TEMP_OFFSET 0
    #define CHILD_ID_TEMP 1
    Bounce debouncer = Bounce();
    static const uint64_t UPDATE_INTERVAL = 5000;
    static const uint8_t FORCE_UPDATE_N_READS = 2;
    float lastTemp;
    unsigned long interval = 5000;
    unsigned long previousMillis = 0;
    uint8_t nNoUpdatesTemp;
    int oldValue = -1;
    bool metric = true;
    MyMessage msg(CHILD_ID, V_LIGHT);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    
    void presentation()
    {
      sendSketchInfo("kitchen Lights", "1.1");
      present(CHILD_ID, S_LIGHT);
      present(CHILD_ID_TEMP, S_TEMP);
      metric = getConfig().isMetric;
    }
    
    void setup()
    {
      pinMode(BUTTON_PIN, INPUT);
      digitalWrite(BUTTON_PIN, HIGH);
      debouncer.attach(BUTTON_PIN);
      debouncer.interval(5);
      dht.setup(DHT_DATA_PIN);
      if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
        Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
      }
    }
    
    void loop()
    {
      debouncer.update();
      int value = debouncer.read();
      if (value != oldValue) {
        send(msg.set(value == HIGH ? 1 : 0));
        oldValue = value;
      }
      unsigned long currentMillis = millis();
      if ((unsigned long)(currentMillis - previousMillis) >= interval) {
        dht.readSensor(true);
        float temperature = dht.getTemperature();
        if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT!");
        } else if (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS) {
          lastTemp = temperature;
          if (!metric) {
            temperature = dht.toFahrenheit(temperature);
          }
          nNoUpdatesTemp = 0;
          temperature += SENSOR_TEMP_OFFSET;
          send(msgTemp.set(temperature, 1));
    #ifdef MY_DEBUG
          Serial.print("T: ");
          Serial.println(temperature);
    #endif
        } else {
          nNoUpdatesTemp++;
        }
        previousMillis = millis();
      }
    }
    

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.