Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. distance + relay node

distance + relay node

Scheduled Pinned Locked Moved Development
6 Posts 3 Posters 2.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    carleki
    wrote on last edited by
    #1

    Hello, I'm trying to aggregate 2 sketches : distance and relay actuator node ...

    But I think I'm going the wrong way because it doesn't work ...

    Here is my code :

    // Sketch pour commander un relai en mode impultionnel. 
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <NewPing.h>
    // relais
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    int node_id = 9;
    
    // distance
    #define CHILD_ID 1
    #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 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)
    
    MySensor gw;
    NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
    MyMessage msg(CHILD_ID, V_DISTANCE);
    int lastDist;
    boolean metric = true; 
    
    void setup()  
    {   
      // Initialize library and add callback for incoming messages
      //gw.begin(incomingMessage, AUTO, true);
      gw.begin(NULL, node_id);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("RelaisPulDist", "1.1");
    
      // Fetch relay status
        // Register all sensors to gw (they will be created as child devices)
        gw.present(RELAY_1, S_LIGHT);
    
        gw.present(CHILD_ID, S_DISTANCE);
        boolean metric = gw.getConfig().isMetric;
        
        // Then set relay pins in output mode
        pinMode(RELAY_1, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(RELAY_1, RELAY_OFF);
    
    }
    
    void loop() 
    {
      // Alway process incoming messages whenever possible
      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 (dist != lastDist) {
          send(msg.set(dist));
          lastDist = dist;
      }
    }
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
        digitalWrite(RELAY_1, RELAY_ON);
        delay(500);  
        digitalWrite(RELAY_1, RELAY_OFF);
       } 
    
      }
    
       
    }
    
    
    

    Can you see what's going wrong ?
    thanls !

    1 Reply Last reply
    0
    • mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by
      #2

      Could you share any details on "it doesn't work"?

      C 1 Reply Last reply
      0
      • mfalkviddM mfalkvidd

        Could you share any details on "it doesn't work"?

        C Offline
        C Offline
        carleki
        wrote on last edited by
        #3

        @mfalkvidd said:

        Could you share any details on "it doesn't work"?

        The distance stays at 0 cm on my controller, and the relay actuator isn't triggered when I click it on my controller (I use my sensors with Jeedom)

        S 1 Reply Last reply
        0
        • C carleki

          @mfalkvidd said:

          Could you share any details on "it doesn't work"?

          The distance stays at 0 cm on my controller, and the relay actuator isn't triggered when I click it on my controller (I use my sensors with Jeedom)

          S Offline
          S Offline
          sundberg84
          Hardware Contributor
          wrote on last edited by
          #4

          @carmelo24 this is my motion and distance sketch:
          https://github.com/sundberg84/MySensors-2.0/blob/master/Padd13/MotionDistNoSleepAs.ino

          Note its for 2.0 and not using sleep()

          0 distance is (for me) an error value or fail to read the distance, i have used

          if (dist == 0){
              return;
            }
          

          To force the distance senor to do a new calculation and it works for me.
          It could be a issue with your sensor, power or hardware if you get it all the time.

          Look at your serial output and see for the node.

          Controller: Proxmox VM - Home Assistant
          MySensors GW: Arduino Uno - W5100 Ethernet, Gw Shield Nrf24l01+ 2,4Ghz
          MySensors GW: Arduino Uno - Gw Shield RFM69, 433mhz
          RFLink GW - Arduino Mega + RFLink Shield, 433mhz

          1 Reply Last reply
          1
          • C Offline
            C Offline
            carleki
            wrote on last edited by
            #5

            So this is my sketch right now. The relay works ok, but I have only 0cm or 1cm with for the distance (and I can hear a sound coming from the distance sensor ...) :

            // Sketch pour commander un relai en mode impultionnel. 
            
            #include <MySensor.h>
            #include <SPI.h>
            #include <NewPing.h>
            
            #define node_id 66
            #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
            #define RELAY_ON 1  // GPIO value to write to turn on attached relay
            #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
            #define CHILD_ID 1
            #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 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)
            MySensor gw;
            NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
            MyMessage msg(CHILD_ID, V_DISTANCE);
            //int lastDist;
            boolean metric = true; 
            
            
            void setup()  
            {   
              // Initialize library and add callback for incoming messages
              gw.begin(incomingMessage, node_id, true);
              // Send the sketch version information to the gateway and Controller
              gw.sendSketchInfo("RelayDistance", "1.1");
            
              // Fetch relay status
                // Register all sensors to gw (they will be created as child devices)
                gw.present(RELAY_1, S_LIGHT);
                gw.present(CHILD_ID, S_DISTANCE);
                boolean metric = gw.getConfig().isMetric;
                // Then set relay pins in output mode
                pinMode(RELAY_1, OUTPUT);   
                // Set relay to last known state (using eeprom storage) 
                digitalWrite(RELAY_1, RELAY_OFF);
            
            }
            
            void loop() 
            {
              // Alway process incoming messages whenever possible
              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 (dist != lastDist) {
               //   gw.send(msg.set(dist));
                //  lastDist = dist;
            //  }
              
            }
            
            void incomingMessage(const MyMessage &message) {
              // We only expect one type of message from controller. But we better check anyway.
              if (message.type==V_LIGHT) {
                 // Change relay state
                digitalWrite(RELAY_1, RELAY_ON);
                delay(100);  
                digitalWrite(RELAY_1, RELAY_OFF);
                 delay(100); 
                digitalWrite(RELAY_1, RELAY_ON);
                delay(100);  
                digitalWrite(RELAY_1, RELAY_OFF);
                 delay(100); 
                digitalWrite(RELAY_1, RELAY_ON);
                delay(100);  
                digitalWrite(RELAY_1, RELAY_OFF);
                 delay(100); 
                digitalWrite(RELAY_1, RELAY_ON);
                delay(100);  
                digitalWrite(RELAY_1, RELAY_OFF);
                
               } 
            }
            
            
            C 1 Reply Last reply
            0
            • C carleki

              So this is my sketch right now. The relay works ok, but I have only 0cm or 1cm with for the distance (and I can hear a sound coming from the distance sensor ...) :

              // Sketch pour commander un relai en mode impultionnel. 
              
              #include <MySensor.h>
              #include <SPI.h>
              #include <NewPing.h>
              
              #define node_id 66
              #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
              #define RELAY_ON 1  // GPIO value to write to turn on attached relay
              #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
              #define CHILD_ID 1
              #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 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)
              MySensor gw;
              NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
              MyMessage msg(CHILD_ID, V_DISTANCE);
              //int lastDist;
              boolean metric = true; 
              
              
              void setup()  
              {   
                // Initialize library and add callback for incoming messages
                gw.begin(incomingMessage, node_id, true);
                // Send the sketch version information to the gateway and Controller
                gw.sendSketchInfo("RelayDistance", "1.1");
              
                // Fetch relay status
                  // Register all sensors to gw (they will be created as child devices)
                  gw.present(RELAY_1, S_LIGHT);
                  gw.present(CHILD_ID, S_DISTANCE);
                  boolean metric = gw.getConfig().isMetric;
                  // Then set relay pins in output mode
                  pinMode(RELAY_1, OUTPUT);   
                  // Set relay to last known state (using eeprom storage) 
                  digitalWrite(RELAY_1, RELAY_OFF);
              
              }
              
              void loop() 
              {
                // Alway process incoming messages whenever possible
                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 (dist != lastDist) {
                 //   gw.send(msg.set(dist));
                  //  lastDist = dist;
              //  }
                
              }
              
              void incomingMessage(const MyMessage &message) {
                // We only expect one type of message from controller. But we better check anyway.
                if (message.type==V_LIGHT) {
                   // Change relay state
                  digitalWrite(RELAY_1, RELAY_ON);
                  delay(100);  
                  digitalWrite(RELAY_1, RELAY_OFF);
                   delay(100); 
                  digitalWrite(RELAY_1, RELAY_ON);
                  delay(100);  
                  digitalWrite(RELAY_1, RELAY_OFF);
                   delay(100); 
                  digitalWrite(RELAY_1, RELAY_ON);
                  delay(100);  
                  digitalWrite(RELAY_1, RELAY_OFF);
                   delay(100); 
                  digitalWrite(RELAY_1, RELAY_ON);
                  delay(100);  
                  digitalWrite(RELAY_1, RELAY_OFF);
                  
                 } 
              }
              
              
              C Offline
              C Offline
              carleki
              wrote on last edited by
              #6

              @carmelo24 it's ok now :)

              here is my sketch :

              // Sketch pour commander un relai en mode impultionnel. 
              
              #include <MySensor.h>
              #include <SPI.h>
              #include <NewPing.h>
              
              #define node_id 66
              #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
              #define RELAY_ON 1  // GPIO value to write to turn on attached relay
              #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
              #define CHILD_ID 1
              #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 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
              unsigned long SLEEP_TIME = 500; // Sleep time between reads (in milliseconds)
              MySensor gw;
              NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
              MyMessage msg(CHILD_ID, V_DISTANCE);
              boolean metric = true; 
              long temps;
              
              void setup()  
              {   
                // Initialize library and add callback for incoming messages
                gw.begin(incomingMessage, node_id, true);
                // Send the sketch version information to the gateway and Controller
                gw.sendSketchInfo("RelayDistance", "1.1");
              
                // Fetch relay status
                  // Register all sensors to gw (they will be created as child devices)
                  gw.present(RELAY_1, S_LIGHT);
                  gw.present(CHILD_ID, S_DISTANCE);
                  boolean metric = gw.getConfig().isMetric;
                  // Then set relay pins in output mode
                  pinMode(RELAY_1, OUTPUT);   
                  // Set relay to last known state (using eeprom storage) 
                  digitalWrite(RELAY_1, RELAY_OFF);
                  temps = millis();
              
              }
              
              void loop() 
              {
                // Alway process incoming messages whenever possible
                gw.process();
              
               
              // mesure de la distance si ça fait plus de 10 secondes qu'on ne l'a pas fait
               if((millis() - temps) > 10000) {
                     int dist = metric?sonar.ping_cm():sonar.ping_in();
                     gw.send(msg.set(dist));
                     Serial.print(dist);
                     Serial.print("toto");
                        
               
               temps = millis();
               
                }
              }
              
              
              void incomingMessage(const MyMessage &message) {
              // action du relais (avec impulsion, pour action comme un bouton poussoir)
                if (message.type==V_LIGHT) {
                   // Change relay state
                  digitalWrite(RELAY_1, RELAY_ON);
                  delay(100);  
                  digitalWrite(RELAY_1, RELAY_OFF);
                   delay(100); 
                  
                  
                 } 
              }
              
              
              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              12

              Online

              12.0k

              Users

              11.2k

              Topics

              113.4k

              Posts


              Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • MySensors
              • OpenHardware.io
              • Categories
              • Recent
              • Tags
              • Popular