Boolean switch, setting, requesting and storing in eeprom.



  • I'm opening this topic because I'm getting strange results in a sketch I'm modifying, and perhaps is it something that I misunderstand.

    I created a "virtual" binary sensor in my device, so I can put it in passive mode, so it is still available, sends it's sensors info but doesn't do any action. Doesn't switch on and off.

    bool passiveMode = false;
    ...
    #define CHILD_ID_PASSIVEMODE  4
    MyMessage msgPassiveMode(CHILD_ID_PASSIVEMODE, V_STATUS);
    ...
    void sendStatus() {
    ...
      send(msgPassiveMode.set(passiveMode));
    }
    
    void setup() {
    ...
      passiveMode = loadState(0);
    }
    
    void presentation() {
    ...
      present(CHILD_ID_PASSIVEMODE, S_BINARY, "ModoPasivo");
    }
    ...
    void receive(const MyMessage &message) {
    
      const uint16_t commandType = mGetCommand(message);
      if (commandType == 2) {
        if (message.type == V_STATUS && message.sensor == CHILD_ID_PASSIVEMODE) {
          send(msgPassiveMode.set(passiveMode));
        }
      }
      else if (commandType == 1) {
        if (message.type == V_STATUS && message.sensor == CHILD_ID_PASSIVEMODE) {
          if (message.bValue != passiveMode) {
            passiveMode = message.bValue;
            saveState(0, passiveMode);
          }
        }
    }
    

    Well, that's all the code added. In the first run, without the memory slot been set, returned a value of "1" for passiveMode. Then, I set it to "0" and requested it again, and it replied again "1".

    I'm wondering what I I've misunderstood. Is not possible to store that value in the sketch as a boolean and rely on implicit conversions?
    The problem can be:
    The storage: Does saveState and readState understand true/false as 1/0?
    The Set/Req: I interpret that those functions should enforce the conversions as message has a bValue property available and send has overloads for it.

    I could change passiveMode to an uint8_t and never look back, but I need to understand what's the problem, here.



  • I've built up a new dummy sensor so I can debug it in my desk. The results still are so confusing:

    Initialising...
    loadState(0): 255  // The expected behavior for a non initialised slot.
    passiveMode: 1    // Makes the variable true. All ok.
    loadState(0): 255
    passiveMode: 1
    Received value: 48  // If I Set to 0 bValue is 48??
    Storing...
    passiveMode: 1
    Received value: 102  // If I Set false...
    Storing...
    passiveMode: 1
    loadState(0): 1
    passiveMode: 1
    Received value: 70   //If I Set False...
    Storing...
    passiveMode: 1
    

    That's so interesting. But not what I expected.


  • Mod

    @sergio-rius calling send from within receive will overwrite the incoming message. So when you're saving the value, you are probably saving the outgoing value, not the incoming value.



  • @mfalkvidd If you look close, I'm only sending as a response to a request from the "controller".

    But I'm still wrong for insist getting the received value from the property bValue. Seems logical to me that it would contain the boolean value instead of a byte size or whatever.

    If I had used the method ".getBool()" I would had the boolean value. Although, if the controller sets a boolean, the node still gets wrong values, so you still have to transmit an integer 0/1. I don't like it, because it forces processing all the values as integers in node-red, for example.

    As usual, I'll leave this, my stupidity, behind in the hopes it serves to anyone else. 😅


Log in to reply
 

Suggested Topics

  • 4
  • 9
  • 3
  • 274
  • 14
  • 5

0
Online

11.2k
Users

11.1k
Topics

112.5k
Posts