MQTT: maintaining a subscription to a topic and acting upon message content as events?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

MQTT: maintaining a subscription to a topic and acting upon message content as events?

#1 Post by uxhamby » 25 Feb 2021, 04:07

Hi,

I seem to have a blank spot in my understanding of ESPEASY (ESP_Easy_mega_20210114_normal_ESP8266_4M1M) and MQTT. The controller setup page fields look tantalizing for what I want to do, but alas groping around in the dark and searches for relevant docs or tutorials have proven fruitless so far. FWIW, I am using PiDome MQTT protocol, for no particular reason than it has worked for things I've tried up to now.

I've got pretty good at publishing from rules / events to MQTT brokers / topics, but my issue now has to do with subscribing.

Is it intended that this sort of prefab version of ESPEASY, be able to take input via MQTT by maintaining a subscription to a topic and acting upon incoming message content as events? Am I expecting too much?

If not, are there any existing docs or tutorials on how its done, that I could be pointed to, or could someone initiate me into the realms of the necessary black magic?

Thanks,
Brian H.
Uxbridge Ont.

User avatar
Ath
Normal user
Posts: 3418
Joined: 10 Jun 2018, 12:06
Location: NL

Re: MQTT: maintaining a subscription to a topic and acting upon message content as events?

#2 Post by Ath » 25 Feb 2021, 08:18

The MQTT Controllers are used for defining the communication parameters with that server.
On the Devices pages you have a checkbox per configured controller that can be used to send out data from that device to the controller' receiving end.
For receiving data and commands from a MQTT source, there are several ways:
- you can use the cmd topic to send commands that will be executed (you can search the forum for examples)
- Some MQTT controllers support the /set endpoint to set a value to a task that supports it
- The MQTT Import plugin (working on an upgrade, including documentation) can fire events when receiving data via a subscription (this is probably what you are going to use)
- The Domoticz MQTT Helper can set a GPIO pin when receiving states from Domoticz, but that obviously requires a Domoticz server and also an add-on MQTT server intermediate like Mosquitto

more info can be found in the Controllers documentation https://espeasy.readthedocs.io/en/lates ... oller.html (it may be a bit scattered though) and links I added above

Generic MQTT information that helped me understand a lot more: https://www.hivemq.com/mqtt-essentials/
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MQTT: maintaining a subscription to a topic and acting upon message content as events?

#3 Post by TD-er » 25 Feb 2021, 16:23

What Ton (Ath) said.
And maybe to elaborate slightly on the subject of "incoming" data from MQTT.

There are several MQTT controllers.
They differ mainly in the way how a specific platform (e.g. Domoticz/PiDome/OpenHAB) expect the data to be formatted.
For Domoticz MQTT and OpenHAB there are some (very limited) options to receive data from the MQTT broker.
- OpenHAB can receive commands (like you also can use in rules), albeit in a non standard formatting.
- Domoticz and OpenHAB can receive updates for (a limited set of) specific plugins

On top of that there is the MQTT import plugin, which can (now, until Ton's PR is merged) only receive numerical data.
In that plugin you supply the topic(s) you wish to subscribe to.

chemmex
Normal user
Posts: 92
Joined: 15 Feb 2019, 16:18

Re: MQTT: maintaining a subscription to a topic and acting upon message content as events?

#4 Post by chemmex » 25 Feb 2021, 19:30

I found that the easiest (in my opinion) way to transfer commands and arguments to the Espeay is to subscribe to the command topic and send there event strings along with numerical arguments. Upon reception they will be treated as events and event values respectively. That said, if I subscribe to %sysname%/cmd and push there a string like "event,setctl,1,10" and use the following rule to process it:

Code: Select all

on setctl do
  if %eventvalue1%=0
     publish %sysname%/sysctl,"Maintenance mode"
     gpio,12,1
  endif
  if %eventvalue1%=1
     publish %sysname%/sysctl,"Continuous mode"
     timerset,1,%eventvalue2%
  endif
endon
this will end up in publishing "Continuous mode" to %sysname%/sysctl and starting a timer #1 for 10 seconds.

I use OpenHAB controller and I don't use MQTT import plugin because it opens a separate socket with different client_Id (I use client_Id for authorization)

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MQTT: maintaining a subscription to a topic and acting upon message content as events?

#5 Post by TD-er » 25 Feb 2021, 20:29

chemmex wrote: 25 Feb 2021, 19:30 [...]

I use OpenHAB controller and I don't use MQTT import plugin because it opens a separate socket with different client_Id (I use client_Id for authorization)
That should not happen anymore since a few builds as I now shared the MQTT client among the controller and all instances of the MQTT import plugin.
But still, I guess sending commands is indeed way more flexible via the MQTT broker.

User avatar
uxhamby
Normal user
Posts: 132
Joined: 29 Dec 2016, 18:13
Location: Toronto Canada

Re: MQTT: maintaining a subscription to a topic and acting upon message content as events?

#6 Post by uxhamby » 26 Feb 2021, 22:47

OK, so:

On the advice above, I changed over to using the 'Home Assistant (openHAB) MQTT' controller protocol.

I subscribed the controller to topic: <topic>/#. and got confirmation of the subscription in the info level log data stream.

From my laptop, I published, with mosquitto_pub, messages to topic: <topic>/cmd.

I send messages like 'gpio,2,0' and 'event,lite' and 'event,dark'.

These events are now executed by the ESPEASY. (f/w version = ESP_Easy_mega_20210223_normal_ESP8266_4M1M.bin)

Code: Select all

91656268: Subscribed to: BJH_ESP12/#
91656279: EVENT: MQTT#Connected
91661855: EVENT: Clock#Time=Fri,16:15
91664983: WD   : Uptime 1526 ConnectFailures 1 FreeMem 18680 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91683780: GPIO : port#2: set to 0
91694473: GPIO : port#2: set to 1
91694985: WD   : Uptime 1527 ConnectFailures 0 FreeMem 18616 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91721857: EVENT: Clock#Time=Fri,16:16
91724983: WD   : Uptime 1527 ConnectFailures 0 FreeMem 17016 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91754984: WD   : Uptime 1528 ConnectFailures 0 FreeMem 14784 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91769749: EVENT: lite
91769776: ACT  : gpio,2,0
91769779: GPIO : port#2: set to 0
91769785: ACT  : Publish,EnzoStuff/ESP12/,'2021-02-26 16:16:48, Test LED ON'
91781156: EVENT: Clock#Time=Fri,16:17
91784982: WD   : Uptime 1528 ConnectFailures 0 FreeMem 18208 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91799951: EVENT: dark
91799985: ACT  : gpio,2,1
91799988: GPIO : port#2: set to 1
91799995: ACT  : Publish,EnzoStuff/ESP12/,'2021-02-26 16:17:18, Test LED OFF'
91814982: WD   : Uptime 1529 ConnectFailures 0 FreeMem 14840 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91841153: EVENT: Clock#Time=Fri,16:18
91844980: WD   : Uptime 1529 ConnectFailures 0 FreeMem 18208 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91874983: WD   : Uptime 1530 ConnectFailures 0 FreeMem 18128 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91901156: EVENT: Clock#Time=Fri,16:19
91904984: WD   : Uptime 1530 ConnectFailures 0 FreeMem 18128 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91934351: EVENT: lite
91934380: ACT  : gpio,2,0
91934383: GPIO : port#2: set to 0
91934390: ACT  : Publish,EnzoStuff/ESP12/,'2021-02-26 16:19:33, Test LED ON'
91934995: WD   : Uptime 1531 ConnectFailures 0 FreeMem 16304 WiFiStatus WL_CONNECTED ESPeasy internal wifi status: Conn. IP Init
91941049: EVENT: dark
91941084: ACT  : gpio,2,1
91941086: GPIO : port#2: set to 1
91941093: ACT  : Publish,EnzoStuff/ESP12/,'2021-02-26 16:19:39, Test LED OFF'
91961162: EVENT: Clock#Time=Fri,16:20

Success, thanks!

Brian H.
Uxbridge Ont.

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests