Acknowledge GPIO change using MQTT

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
mkuttnig
New user
Posts: 3
Joined: 11 Sep 2018, 21:36

Acknowledge GPIO change using MQTT

#1 Post by mkuttnig » 11 Sep 2018, 21:46

Hi All,

A common pattern with multiple control apps seems to be, that if you are turning on /of a relay, the node responds with a state message (MQTT)

I am using openhab and I send an MQTT message to turn on/off the pool light:

/pool/GPIO/2 : 1
/pool/GPIO/2 : 0

The switch in openHab turns to the new status. That works fine.
However, if i use Alexa or some other source to publish that MQTT message, the switch does not update. (of course)

What i'd like to do is sending something like:
/pool/light : 1
/pool/light : 0

And the device responding:
/pool/light/state : 1
/pool/light/state : 0

That way, all others will know that the light was in fact turned on (by whoever)

I was able to get this done creating my own firmware like this:

Code: Select all

  if (!strcmp(topic,"/pool/light")) {
    if (!memcmp(payload,"ON",length)) {
      state=LOW; action=true; // LOW is LED ON (inverted driver)
    }
    if (!memcmp(payload,"OFF",length)) {
      digitalWrite(LED_BUILTIN, LOW);
      state=HIGH; action=true; // HIGH is LED ON (inverted driver)
    }
    if (action) {
      digitalWrite(LED_BUILTIN, state);
      client.publish("/pool/light/state",state==LOW ? "ON" : "OFF");
    }
  }
but I'd like to use ESPEasy instead...

Can this be done?

Thank you!

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Acknowledge GPIO change using MQTT

#2 Post by grovkillen » 11 Sep 2018, 21:58

I just skimmed through but yes, you can do that with rules. I always use events to trigger commands and then I publish an acknowledgement message on a specific topic. Pretty much what you are aiming for I reckon.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

mkuttnig
New user
Posts: 3
Joined: 11 Sep 2018, 21:36

Re: Acknowledge GPIO change using MQTT

#3 Post by mkuttnig » 12 Sep 2018, 16:24

Thank you @grovkillen!

Are you saying you define custom events in rules like switchon, switchoff and then invoking them through a particular MQTT message (e.g. /pool/light/cmd : event,switchon)
I was thinking that already, that will work. Not the most elegant solution, but it will do the job ;-)

I will investigate deeper into event parameters. I think I read something that there is a way to pass params with just any event...

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Acknowledge GPIO change using MQTT

#4 Post by grovkillen » 12 Sep 2018, 17:01

mkuttnig wrote: 12 Sep 2018, 16:24 Thank you @grovkillen!

Are you saying you define custom events in rules like switchon, switchoff and then invoking them through a particular MQTT message (e.g. /pool/light/cmd : event,switchon)
I was thinking that already, that will work. Not the most elegant solution, but it will do the job ;-)

I will investigate deeper into event parameters. I think I read something that there is a way to pass params with just any event...
Yep it's a common use of rules. We think it's a rather elegant easy ourself but it's in the eye of the beholder I guess ;)

We'll try to make the rule editor better in the future though, I can agree on that.

Currently only one parameter (a float value) but up to three parameters are in the scoop for upcoming releases.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

mkuttnig
New user
Posts: 3
Joined: 11 Sep 2018, 21:36

Re: Acknowledge GPIO change using MQTT

#5 Post by mkuttnig » 12 Sep 2018, 18:22

It might be more elegant than I thought using the eventvalue:

Code: Select all

on light do
    if [%eventvalue%]=1
        GPIO,2,1
        Publish %sysname%/light/state,0
    else
        GPIO,2,0
        Publish %sysname%/light/state,1
    endif
endon
unfortunately the comparison always goes to the ELSE branch.
I tried to publish the eventvalue back to MQTT and it comes in as a string "%eventvalue% - looks like the placeholder is never set to the real event value.

I post to this topic: pool/cmd with this payload: event,light=1 - yet always the ELSE is executed...

That's what I get in the log:
EVENT: light=1
ACT : GPIO,2,0
SW : GPIO 2 Set to 0
ACT : Publish pool/light/state,1
EVENT: light=0
ACT : GPIO,2,0
SW : GPIO 2 Set to 0
ACT : Publish pool/light/state,1


If that worked, I agree it is a nice way to do this :-)

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Acknowledge GPIO change using MQTT

#6 Post by grovkillen » 12 Sep 2018, 18:58

Doing that is currently not working but on the wish list. It's a good suggestion. In the meantime you need to assign the eventvalue to a dummy variable.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests