Customizable Sonar Sensor



  • Hi,

    I would like to build a customizable node (customizable by the controller) wich can detect presence for a certain distance.
    The node comportement is good...

    • Is it good practice to do a sketch like this ?
    • "gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);", response is provided by the controller ?
    • Can i store my minimum range distance in EEPROM ? or is it better to do a "gw.request" ?
    #include <SPI.h>
    #include <MySensor.h>  
    #include <NewPing.h>
    
    
    #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.
    /** Default value **/
    #define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
    
                              
    unsigned long SLEEP_TIME = 5000; // Sleep time between reads (in milliseconds)
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    // Id of the sensor child
    #define CHILD_ID_SONAR 0
    #define CHILD_ID_SONAR_MIN_RANGE 1
    
    
    //Setting default min / max range default value
    int minRange = 50;
    boolean inRange = false;
    boolean outRange = false;
    
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    
    MyMessage SonarMessage(CHILD_ID_SONAR, V_DISTANCE);
    
    unsigned long lastSend; // the timeStamp you use to constantly check vs the current time (millis() returns an unsigned long timestamp)
    unsigned long IntervalBetweenSend = 2000; // the frequency that this timer will execute here... 10 seconds.
    
    boolean metric = true; 
    
    
    void setup()  
    { 
      gw.begin(incomingMessage, AUTO, true);
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Distance Sensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(0, S_DISTANCE);
      
      // Presenting custom 
      gw.present(CHILD_ID_SONAR_MIN_RANGE, S_DISTANCE); //minRange
      
      // Request/wait for dimmer status
      //gw.request(CHILD_ID_SONAR_MIN_RANGE, V_VAR1);
      gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);
      //gw.process();
      //gw.request(CHILD_ID_SONAR_MAX_RANGE, V_DISTANCE);
      //gw.process();
      //gw.saveState(1,2);
      
      boolean metric = gw.getConfig().isMetric;
    }
    
    void loop(){
      
      // Process incomming message
      gw.process();
      
      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 (millis() - lastSend > IntervalBetweenSend) {
        
        int dist = metric?sonar.ping_cm():sonar.ping_in();
            
          if ( dist <= minRange && inRange == false) {
              
                gw.send(SonarMessage.set(dist));
                inRange = true;
                lastSend = millis();
              
          }
          
          if ( dist > minRange && inRange == true) {
             gw.send(SonarMessage.set(dist));
             inRange = false;
             lastSend = millis();
          } 
        
        
      }
    
    }
    
    void incomingMessage(const MyMessage &message) {
      // echo "1;1;2;1;13;40" >> /dev/ttyUSB0 (Defining minrange to 40)
      // echo "1;2;2;1;13;50" >> /dev/ttyUSB0 (Defining maxrange to 50)
       Serial.print("Incoming Message");
       
      // We wait for V_VAR1 value
      if (message.type == V_DISTANCE) {
         // Store state in eeprom
         //gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(" (type : ");
         Serial.print(message.type);
         Serial.print(")");
         Serial.print(", New status: ");
         Serial.println(message.getInt());
         
         if ( message.sensor == CHILD_ID_SONAR_MIN_RANGE ){
           Serial.println("Updating Minrange");
           minRange = message.getInt();
         }  
         
         
       } 
    }```


  • can someone can just answer this question :
    "gw.request(CHILD_ID_SONAR_MIN_RANGE, V_DISTANCE);", response is provided by the controller ?

    thank you 🙂


  • Hero Member

    @beanl said:

    gw.request(

    Yes, the controller is supposed to provide a response. However, not all controllers currently support that.

    Cheers
    Al



  • ok thank you 🙂


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 2
  • 198
  • 2
  • 2

8
Online

11.2k
Users

11.1k
Topics

112.5k
Posts