Binary / Temp sensor help - sleep time



  • I combined temp and binary (2x) sketches.
    All works ok if I remove "sleep time" but in this case I get flooded with temperature sensor data.
    If "sleep time" is used then binary switches don't change states.

    How to amend sketch to send temp to gw at certain interval ie 30 seconds?

         // 2x binary switch + dallas temp example 
         // Connect button or door/window reed switch between 
         // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND.
        // Connect dallas temp sensor to digital pin 3
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define CHILD_ID_4 4
    #define CHILD_ID_5 5
    #define BUTTON_PIN_4  4  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_5  5  // Arduino Digital I/O pin for button/reed switch
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    Bounce debouncer = Bounce(); 
    Bounce debouncer_2 = Bounce();
    int oldValue=-1;
    int oldValue_2 =-1;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg4(CHILD_ID_4,V_TRIPPED);
    MyMessage msg5(CHILD_ID_5,V_TRIPPED);
    
    void setup()  
    {  
      // Startup OneWire 
      sensors.begin();
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin();
      
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Door/Temp", "1.0");
      
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         gw.present(i, S_TEMP);
      }
    
     // Setup the button
      pinMode(BUTTON_PIN_4,INPUT);
      pinMode(BUTTON_PIN_5,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_4,HIGH);
      digitalWrite(BUTTON_PIN_5,HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON_PIN_4);
      debouncer.interval(5);
      debouncer_2.attach(BUTTON_PIN_5);
      debouncer_2.interval(5);
      
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      gw.present(CHILD_ID_4, S_DOOR);  
      gw.present(CHILD_ID_5, S_DOOR);  
    }
    
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg4.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
      debouncer_2.update();
      // Get the update value
      int value_2 = debouncer_2.read();
     
      if (value_2 != oldValue_2) {
         // Send in the new value
         gw.send(msg5.set(value_2==HIGH ? 1 : 0));
         oldValue_2 = value_2;
      }
     // Process incoming messages (like config from server)
      gw.process(); 
    
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures(); 
    
      // Read temperatures and send them to controller 
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
     
        // Only send data if temperature has changed and no error
        if (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msg.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
      gw.sleep(SLEEP_TIME);
    } 
    

  • Contest Winner

    @niccodemi

    you can try this way... untested but it compiles:

    // 2x binary switch + dallas temp example 
    // Connect button or door/window reed switch between 
    // digitial I/O pin 4,5 (BUTTON_PIN_4/BUTTON_PIN_5 below) and GND.
    // Connect dallas temp sensor to digital pin 3
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    //
    #define CHILD_ID_4 4
    #define CHILD_ID_5 5
    #define BUTTON_PIN_4  4  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_5  5  // Arduino Digital I/O pin for button/reed switch
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    //
    //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    Bounce debouncer = Bounce(); 
    Bounce debouncer_2 = Bounce();
    int oldValue=-1;
    int oldValue_2 =-1;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    //
    MyMessage msg(0,V_TEMP);
    MyMessage msg4(CHILD_ID_4,V_TRIPPED);
    MyMessage msg5(CHILD_ID_5,V_TRIPPED);
    //
    void setup()  
    {  
      sensors.begin();
      gw.begin();
      gw.sendSketchInfo("Door/Temp", "1.0");
      numSensors = sensors.getDeviceCount();
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) 
      {   
        gw.present(i, S_TEMP);
      }
      pinMode(BUTTON_PIN_4,INPUT);
      pinMode(BUTTON_PIN_5,INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON_PIN_4,HIGH);
      digitalWrite(BUTTON_PIN_5,HIGH);
      //
      debouncer.attach(BUTTON_PIN_4);
      debouncer.interval(5);
      debouncer_2.attach(BUTTON_PIN_5);
      debouncer_2.interval(5);
    
      gw.present(CHILD_ID_4, S_DOOR);  
      gw.present(CHILD_ID_5, S_DOOR);  
    }
    //
    void loop() 
    {
      debouncer.update();
      int value = debouncer.read();
      if (value != oldValue) 
      {
        gw.send(msg4.set(value==HIGH ? 1 : 0));
        oldValue = value;
      }
      debouncer_2.update();
      int value_2 = debouncer_2.read();
      if (value_2 != oldValue_2) 
      {
        gw.send(msg5.set(value_2==HIGH ? 1 : 0));
        oldValue_2 = value_2;
      }
      gw.process(); 
      updateTemperature(30);// update time in seconds
      //gw.sleep(SLEEP_TIME);
    }
    
    void updateTemperature(int frequency)
    {
      static unsigned long lastUpdateTime;
      if (millis() - lastUpdateTime >= frequency * 1000UL)
      {
        sensors.requestTemperatures(); 
        for (int i=0; i<numSensors && i< MAX_ATTACHED_DS18B20; i++) 
        {
          float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
          if (lastTemperature[i] != temperature && temperature != -127.00) 
          {
            gw.send(msg.setSensor(i).set(temperature,1));
            lastTemperature[i]=temperature;
          }
        }
        lastUpdateTime += frequency * 1000UL;
      }
    }


  • @BulldogLowell thank you, I uploaded it and so far it works fine.



Suggested Topics

  • 1
  • 2
  • 3
  • 3
  • 17
  • 3
  • 4
  • 6

0
Online

11.4k
Users

11.1k
Topics

112.6k
Posts