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. General Discussion
  3. Boolean switch, setting, requesting and storing in eeprom.

Boolean switch, setting, requesting and storing in eeprom.

Scheduled Pinned Locked Moved General Discussion
4 Posts 2 Posters 229 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.
  • Sergio RiusS Offline
    Sergio RiusS Offline
    Sergio Rius
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Sergio RiusS Offline
      Sergio RiusS Offline
      Sergio Rius
      wrote on last edited by
      #2

      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.

      mfalkviddM 1 Reply Last reply
      0
      • Sergio RiusS Sergio Rius

        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.

        mfalkviddM Offline
        mfalkviddM Offline
        mfalkvidd
        Mod
        wrote on last edited by mfalkvidd
        #3

        @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.

        1 Reply Last reply
        0
        • Sergio RiusS Offline
          Sergio RiusS Offline
          Sergio Rius
          wrote on last edited by
          #4

          @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. :sweat_smile:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          19

          Online

          11.7k

          Users

          11.2k

          Topics

          113.1k

          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