homieiot / homie-esp8266

💡 ESP8266 framework for Homie, a lightweight MQTT convention for the IoT
http://homieiot.github.io/homie-esp8266
MIT License
1.36k stars 307 forks source link

Public variables in PropertyInputHandler are undefined. #728

Closed mFranz82 closed 2 years ago

mFranz82 commented 2 years ago

Hi!

This is more a question according to custom nodes.

According to this #456 I have another question as well.

I think it is a scope problem but I am not able to fix this.

I have a constructor in a class like this with public variables node and PIN. The constructor works as expected but I lose the reference to the PIN number in the hander function :-(


Valve::Valve(const int* PIN, const char* ID, const char* name, HomieNode* node)
: _node(node)
, _pin(PIN)
{
  // Pin has the desired value
  pinMode(*_pin, OUTPUT);

  _node->advertise(ID)
          .setName(name)
          .setDatatype("boolean")
          .settable(  [this](const HomieRange& range, const String& value) -> bool { 
                   // I need the PIN number in the handler function 
                   // But PIN is undefined in this scope. 0
                   printf("Value:  %d\n", _pin);
                   return valveInputHandler(range,value);
          });

bool Valve::valveInputHandler(const HomieRange& range, const String& value) {
  if (value != "true" && value != "false") return false;
  bool on = (value == "true");

  digitalWrite(_pin, on ? HIGH : LOW);

  return true;
}

If someone just posts a keyword for a google search that would be absolutely fine.

Thanks!

luebbe commented 2 years ago

Maybe take a look at this: https://github.com/luebbe/homie-node-collection? I always subclass my nodes from homie node instead of passing a node around.

Your valve is not different from a relay node. You should be able to use my relay node straight away. At least that's what I'm doing in my valve controlle (with benefits): https://github.com/luebbe/homie-relay-array

mFranz82 commented 2 years ago

Thanks a lot! This is what I need!

On 25. Feb 2022, at 14:02, Lübbe Onken @.***> wrote:

Maybe take a look at this: https://github.com/luebbe/homie-node-collection https://github.com/luebbe/homie-node-collection? I always subclass my nodes from homie node instead of passing a node around.

Your valve is not different from a relay node. You should be able to use my relay node straight away. At least that's what I'm doing in my valve controlle (with benefits): https://github.com/luebbe/homie-relay-array https://github.com/luebbe/homie-relay-array — Reply to this email directly, view it on GitHub https://github.com/homieiot/homie-esp8266/issues/728#issuecomment-1050834436, or unsubscribe https://github.com/notifications/unsubscribe-auth/ATCFF5PV2DIRDETUYXISFNTU454YDANCNFSM5PKBOF7A. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.