Multi-Sensor Button+dallas problem



  • Hi,
    I made a multisensor node with latest library :
    2x Relay + 2x Dallas+ Sonar Distance + 2x Button

    The node is working but :

    • If i include sleep() or wait() in dallas sketch the node become slow and button is very slow to transfer data (more than 15sec)
    • If i remove sleep() or wait() its better but dallas send to many informations

    Is it possible to send dallas information every minute without a wait ?
    For the moment my solution is to convert FLOAT temperature to INT.

    // Enable debug prints to serial monitor
    #define MY_DEBUG 
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    
    // Enable repeater functionality for this node
    #define MY_REPEATER_FEATURE
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <Bounce2.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <NewPing.h>
    
    //-------------------RELAY-----------------------------//
    #define RELAY_1  A1  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2  A2  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 2 // Total number of attached relays
    #define RELAY_ON 0  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
    
    //-------------------DALLAS----------------------------//
    #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 2
    //unsigned long WAIT_TIME = 5000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    int lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    bool receivedConfig = false;
    bool metric = true;
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    //-------------------SONAR----------------------------//
    #define CHILD_ID 5
    #define TRIGGER_PIN  6  // Arduino pin tied to trigger pin on the ultrasonic sensor.
    #define ECHO_PIN     5  // Arduino pin tied to echo pin on the ultrasonic sensor.
    #define MAX_DISTANCE 1000 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage msg1(CHILD_ID, V_DISTANCE);
    int lastDist;
    
    //-------------------BUTTON----------------------------//
    #define CHILD_ID_6 6
    #define CHILD_ID_7 7
    #define BUTTON_PIN_6  4  // Arduino Digital I/O pin for button/reed switch
    #define BUTTON_PIN_7  8  // Arduino Digital I/O pin for button/reed switch
    Bounce debouncer1 = Bounce(); 
    Bounce debouncer2 = Bounce();  
    int oldValue1=-1;
    int oldValue2=-1;
    MyMessage msg6(CHILD_ID_6,V_TRIPPED);
    MyMessage msg7(CHILD_ID_7,V_TRIPPED);
    
    //=======================BEFORE==========================================//
    void before() { 
        //-------------------DALLAS----------------------------//
        //Startup up the OneWire library
        sensors.begin();
    
        //-------------------RELAY-----------------------------//
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to OFF
        digitalWrite(pin, RELAY_OFF);
      }
     }
    
    //=======================SETUP============================================//
    void setup() {
        //-------------------DALLAS----------------------------//
        //requestTemperatures() will not block current thread
        sensors.setWaitForConversion(false);
    
        //-------------------SONAR----------------------------//
        metric = getConfig().isMetric;
    
        //-------------------BUTTON----------------------------//
        // Setup the button
        pinMode(BUTTON_PIN_6,INPUT);
        pinMode(BUTTON_PIN_7,INPUT);
        // Activate internal pull-up
        digitalWrite(BUTTON_PIN_6,HIGH);
        digitalWrite(BUTTON_PIN_7,HIGH);
        // After setting up the button, setup debouncer
        debouncer1.attach(BUTTON_PIN_6);
        debouncer1.interval(5);
        debouncer2.attach(BUTTON_PIN_7);
        debouncer2.interval(5); 
    }
    
    //=======================PRESENTATION======================================//
    void presentation()  
    {   
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Poele", "1.0");
        
      //-------------------RELAY-----------------------------//
      // Register all sensors to gw (they will be created as child devices)
      present(1, S_BINARY);
      present(2, S_BINARY);
    
      //-------------------DALLAS----------------------------//
      // 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++) {   
      present(i+3, S_TEMP);
      }
      
      //-------------------SONAR----------------------------//
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID, S_DISTANCE);
    
      //-------------------BUTTON----------------------------//
      present(CHILD_ID_6, S_DOOR);  
      present(CHILD_ID_7, S_DOOR); 
    }
    
    
    //=======================LOOP==============================================//
    void loop() 
    {
          //-------------------BUTTON----------------------------//
          debouncer1.update();
          // Get the update value
          int value1 = debouncer1.read();
          if (value1 != oldValue1) {
          // Send in the new value
          send(msg6.set(value1==HIGH ? 1 : 0));
          oldValue1 = value1;
          }
          debouncer2.update();
          // Get the update value
          int value2 = debouncer2.read();
          if (value2 != oldValue2) {
          // Send in the new value
          send(msg7.set(value2==HIGH ? 1 : 0));
          oldValue2 = value2;
          }
        
          //-------------------SONAR----------------------------// 
          int dist = metric?sonar.ping_cm():sonar.ping_in();
          Serial.print("Ping: ");
          Serial.print(dist); // Convert ping time to distance in cm and print result (0 = outside set distance range)
          Serial.println(metric?" cm":" in");
    
          if (dist != lastDist && dist != lastDist+1 && dist != lastDist-1) {
            send(msg1.set(dist));
            lastDist = dist;
           }
         
     
         //-------------------DALLAS----------------------------// 
         // Fetch temperatures from Dallas sensors
         sensors.requestTemperatures();
         
         // query conversion time and sleep until conversion completed
         int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
         
         // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
         wait(conversionTime);
         
         // 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
         int temperature = static_cast<float>(static_cast<int>((getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
         
        // Only send data if temperature has changed and no error
        #if COMPARE_TEMP == 1
        if (lastTemperature[i+3] != temperature && temperature != -127.00 && temperature != 85.00) {
        #else
        if (temperature != -127.00 && temperature != 85.00) {
        #endif
     
          send(msg.setSensor(i+3).set(temperature,1));
         // Save new temperatures for next compare
          lastTemperature[i+3]=temperature;
          } 
          }
        
          
    
    }
    
    //=======================RECEIVE==============================================//
    void receive(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }``` 
    
    Thanks

  • Mod

    @Thib. you should be able to use something like the lastSend variable used in the UV meter example


  • Hero Member

    @Thib. When you are using devices like the ultrasonic distance sensor it is best to use non blocking delays . do a google search for "Arduino non blocking delay" to get more info.

    The basic code will look something like below

    unsigned long previousMillis = 0;        // will store last time temperature was updated
    const long interval = 60000;           // interval at which to update the temperature (milliseconds)
    
    void setup() {
     
    }
    
    void loop() {
      
      unsigned long currentMillis = millis();   // get the current time
      if (currentMillis - previousMillis >= interval) {
       
      /*---------------------Put your code to send temp here----------------------------  */
        
        previousMillis = currentMillis;         // save the last time you sent the temperature
      }
    }
    


  • Thanks for your answers, i will try it



  • @Boots33 , Thanks i have compiled your example and its working good !


Log in to reply
 

Suggested Topics

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts