Magnetic light switch and 2 DHT 11



  • Hi..
    I been using this forum allot to create my MySensors nods and GW's and so on..

    Now to my problem. I have small food storage room like a "pantry" there no lights in it so now I want to make a MySensors thing to light a 12v LED Strip true a relay thats connected to the mains "230v". The relay is controlled by a magnetic switch, by the door magnet. This part I have figured.. Now for the DHT part i have tried my best but no luck.
    Is there some one that could take a luck att my sketch?

    // Enable debug prints to serial monitor
    #define MY_DEBUG
    
    // Enable and select radio type attached
     #define MY_RADIO_NRF24
    
    #define MY_TRANSPORT_WAIT_READY_MS 3000     //set how long to wait for transport ready. in milliseconds
    #include <SPI.h>
    #include <MySensors.h>
    #include <Bounce2.h> 
    #include <DHT.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 DHT_DATA_PIN 7
    
    // Set this offset if the sensor has a permanent small offset to the real temperatures
    #define SENSOR_TEMP_OFFSET 0
    
    // Sleep time between sensor updates (in milliseconds)
    // Must be >1000ms for DHT22 and >2000ms for DHT11
    static const uint64_t UPDATE_INTERVAL = 60000;
    
    // Force sending an update of the temperature after n sensor reads, so a controller showing the
    // timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
    // the value didn't change since;
    // i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
    static const uint8_t FORCE_UPDATE_N_READS = 10;
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    
    float lastTemp;
    float lastHum;
    uint8_t nNoUpdatesTemp;
    uint8_t nNoUpdatesHum;
    bool metric = true;
    
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    DHT dht;
    
    
    
    Bounce debouncer = Bounce();
    int oldValue = 0;
    bool state;
    
    MyMessage msg(CHILD_ID, V_STATUS);
    
    void setup()
    {
    pinMode(BUTTON_PIN, INPUT);              // Setup the button pin  
    digitalWrite(BUTTON_PIN, HIGH);           // Activate internal pull-up
    
    // After setting up the button, setup debouncer
    debouncer.attach(BUTTON_PIN);
    debouncer.interval(5);
    
    pinMode(RELAY_PIN, OUTPUT);               // set relay pin in output mode  
    digitalWrite(RELAY_PIN, RELAY_OFF);      // Make sure relay is off when starting up
    
    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!");
    }
    // Sleep for the time of the minimum sampling period to give the sensor time to power up
    // (otherwise, timeout errors might occure for the first reading)
    sleep(dht.getMinimumSamplingPeriod());
    }
    
    void presentation()  {
    // Send the sketch version information to the gateway and Controller
    sendSketchInfo("Relay & Button & Temp+Hum", "1.1");
    
    
    // Register all sensors to gw (they will be created as child devices)
    present(CHILD_ID_HUM, S_HUM);
    present(CHILD_ID_TEMP, S_TEMP);
    
    metric = getControllerConfig().isMetric;
    
    // Register all sensors to gw (they will be created as child devices)
    present(CHILD_ID, S_BINARY);
    }
    
    
    void loop()
    {
    debouncer.update();
    
      int value = debouncer.read();                               // Get the update value
      if (value != oldValue) {                      // check for new button push
      state =  !state;                                          // Toggle the state
      send(msg.set(state), false);                               // send new state to      controller, no ack requested
      digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);    // switch the relay to the new state 
      }
    
    oldValue = value;
    
    
    // Force reading sensor, so it works also after sleep()
    dht.readSensor(true);
    
    // Get temperature from DHT library
    float temperature = dht.getTemperature();
    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;
    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    // Reset no updates counter
    nNoUpdatesTemp = 0;
    temperature += SENSOR_TEMP_OFFSET;
    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 = dht.getHumidity();
    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
    sleep(UPDATE_INTERVAL); 
    }
    
    
    /*-------------------start of functions--------------------------*/
    
    void receive(const MyMessage &message) {
    // We only expect one type of message from controller. But we better check anyway.
    
    if (message.type == V_STATUS) {                               // check to see if incoming message is for a switch
    state = message.getBool();                                  // get the current state
    digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF);      // switch relay to current state
    
    // Write some debug info
    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
    }
    }


  • @fedyn First remark: as working at 230V mains is dangerous you may think about leaving the 230V always on and use the 12V also to power the arduino and switch the LED on using a MOSFET (relay may also be ok).

    Wrt to your sketch, imo you should not combine sleep() with a debouncer-design for your switch. The debounced PIN (your button) then will not be read while sleeping.
    You may either have to use a non-blocking-loop() (example how to do this: see the https://www.mysensors.org/build/pulse_water - sketch) or use PIN 2 or 3 for an interrupt-based solution (following the https://www.mysensors.org/build/motion - example).



  • @rejoe2 Thx.
    And Thx for the advise.
    Have tried now but no luck.



  • @fedyn Thx for reporting back. Just in case you expect anyone to help you further with this, it is necessary to post updated code together with more info what is "not working" (here: the switch/lightning part or the DHT).

    You may also find some more hints in how to combine relay and non-blocking loop() starting here: https://forum.mysensors.org/post/81401

    Btw.: you mentionned 2 DHT in the title of your post, but in the code, there's only one.


Log in to reply
 

Suggested Topics

23
Online

11.2k
Users

11.1k
Topics

112.5k
Posts