resend if st=fail



  • Hello everybody,

    has anyone built a function that resend the signal, if "st=fail" is detected?
    I've already looked at the API and ACK. But have become not really smart of it.

    This would be great for some devices like relay, alarm, ....

    Best Regards,
    n3ro


  • Hero Member

    @n3ro you can use and catch the Ack value yourself and make sure the message arrives.



  • @AWI do you have code example for me?


  • Admin

    gw.send() returns false when no ack was received.



  • Hey 🙂

    I have just build this test function to resend a signal if failed.
    But i don't know how to execute the ge.send in the string ("// execute code").

    #include <MySensor.h>
    #include <SPI.h>
    
    #define SENSOR_INFO "Test sensor"
    #define NODE_ID 200
    #define CHILD_ID 1
    #define OPEN 1
    #define CLOSE 0
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()
    {
      gw.begin(NULL, NODE_ID, false);
      gw.sendSketchInfo(SENSOR_INFO, "1.0");
      gw.present(CHILD_ID, S_DOOR);
    }
    
    void loop()
    {
      gwack("gw.send(msg.set(OPEN));");
      delay(500);
    }
    void gwack(char code[])
    {
      int repeat = 0;
      boolean sendOK = false;
      Serial.println(code); // string to execute
    
      while ((sendOK == false) or (repeat <= 10))
      {
        if (code) // execute code
        {
          Serial.println("OK");
          sendOK = true;
        }
        else
        {
          Serial.println("NOT OK!!");
          sendOK = false;
        }
        repeat++;
      }
    }
    

    Any ideas? 🙂



  • That's not going to work the way you wrote it. That would require the Arduino to be able to compile code, which is asking a bit much of the little micro. 🙂

    I would either do the retry-on-fail logic directly inline in your loop() function, or put the gw.send() call in gwack() and pass the msg value as a reference/pointer to gwack.



  • @rickmontana83 said:

    put the gw.send() call in gwack() and pass the msg value as a reference/pointer to gwack.

    What do you mean? 🙂
    Like this?

    void loop()
    {
    //gw.send(msg.set(OPEN));
      gwresend("msg.set","OPEN");
      delay(500);
    }
    void gwresend(char msgcode[],char option[])
    {
      int repeat = 0;
      boolean sendOK = false;
    
      while ((sendOK == false) or (repeat <= 10))
      {
        if (gw.send(msgcode(option)) // execute code
        {
          Serial.println("OK");
          sendOK = true;
        }
        else
        {
          Serial.println("NOT OK!!");
          sendOK = false;
        }
        repeat++;
      }
    }
    

    (code is not working 😉 )

    I'm very surprised that there is not already such a function.



  • I'd be surprised if that even compiles. (Does it?)

    I meant you should set up the message in loop, then pass the completed message to gwresend like:

    msg.set(OPEN);
    gwresend(msg);

    The signature for gwresend will be something like:

    void gwresend(MyMessage &msg)

    The execute code line would then just be:

    if (gw.send(msg)) { ...

    Don't take this the wrong way, but this is more of a basic C++ language question. You might want to poke around on StackOverflow for examples to better understand the language (like the pass by reference that I suggested above, which, being from a C background, I've always found a bit scary.)



  • Jeah now its working 🙂

    #include <MySensor.h>
    #include <SPI.h>
    
    #define SENSOR_INFO "Test sensor"
    #define NODE_ID 200
    #define CHILD_ID 1
    #define OPEN 1
    #define CLOSE 0
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    int repeat = 0;
    boolean sendOK = false;
    int repeatdelay = 0;
    
    void setup()
    {
      gw.begin(NULL, NODE_ID, false);
      gw.sendSketchInfo(SENSOR_INFO, "1.0");
      gw.present(CHILD_ID, S_DOOR);
    }
    
    void loop()
    {
      resend((msg.set(OPEN)), 5);
    delay(500);
    
    }
    void resend(MyMessage &msg, int repeats)
    {
      int repeat = 1;
      int repeatdelay = 0;
      boolean sendOK = false;
    
      while ((sendOK == false) and (repeat < repeats)) {
        if (gw.send(msg)) {
          sendOK = true;
        } else {
          sendOK = false;
          Serial.print("FEHLER ");
          Serial.println(repeat);
          repeatdelay += 250;
        } repeat++; delay(repeatdelay);
      }
    }```

 

297
Online

8.4k
Users

9.3k
Topics

97.9k
Posts