MQTT in ZM

Current Development version likely to have breaking changes
Post Reply
bogo
Posts: 3
Joined: Sun Oct 20, 2024 4:24 pm

MQTT in ZM

Post by bogo »

Hi, please help with MQTT; I installed new ZM from source - 'v1.37.64', but I still can't mqtt to work; in short - compile:
cmake -DZM_DB_PASS=...
make
make install

in Options -> MQTT I set the hostname, username, password and topic
in Monitor -> MQTT I enabled MQTT
Options -> Log -> Debug
restarted ZM more times, but no sign that ZM tries to connect MQTT - Mosquito writes no entries that client connected, netstat shows no connection was established, no MQTT related log in ZM log files

What I need to do to get MQTT publishing to work?
is 'Event Notification Server' nessesary?

The idea in behind is to turn on the lights on a sidewalk if someone comes/leaves house

br Bogo
bogo
Posts: 3
Joined: Sun Oct 20, 2024 4:24 pm

Re: MQTT in ZM

Post by bogo »

the problem is, that MOSQUITTOPP_FOUND is FALSE - but that's strange, because CMAKE reports that mosquittolib was found
-- Optional libraries found: zlib cURL OpenSSL PCRE Mosquitto Mosquittopp libVNC
-- Optional libraries not found: libVLC gsoap

if I try to hard-set #define MOSQUITTOPP_FOUND TRUE, I get compilation errors...
[ 24%] Building CXX object src/CMakeFiles/zm.dir/zm_mqtt.cpp.o
In file included from /usr/local/zm/zoneminder/src/zm_mqtt.cpp:8:
/usr/local/zm/zoneminder/src/zm_monitor.h:623:19: error: ‘MQTT’ was not declared in this scope
623 | std::unique_ptr<MQTT> mqtt;
| ^~~~
/usr/local/zm/zoneminder/src/zm_monitor.h:623:23: error: template argument 1 is invalid
623 | std::unique_ptr<MQTT> mqtt;
| ^
/usr/local/zm/zoneminder/src/zm_monitor.h:623:23: error: template argument 2 is invalid
/usr/local/zm/zoneminder/src/zm_mqtt.cpp:14:1: error: ‘MQTT’ does not name a type
14 | MQTT::MQTT(Monitor *monitor) :
| ^~~~
...
bogo
Posts: 3
Joined: Sun Oct 20, 2024 4:24 pm

Re: MQTT in ZM

Post by bogo »

so a digged a bit deeper and it seems mqtt basically works now...

what I did:
1) because of the compilation-error - changed include order in src/zm_mqtt.cpp from
#include "zm_mqtt.h"
#include "zm_monitor.h"
to
#include "zm_monitor.h"
#include "zm_mqtt.h"

2) hardcoded "#define MOSQUITTOPP_FOUND TRUE" to the beginning of these files:
src/zm_monitor.cpp
src/zm_monitor.h
src/zm_mqtt.cpp
src/zm_mqtt.h

3) every monitor starts it's own mqtt client-connection, the problem there is that the mqtt client_id is hardcoded in src/zm_mqtt.cpp as "ZoneMinder"; if mosquitto detects two connection with the same client_id, it closes the older one (with message in logs), so every monitor should connect with an uniqueue client_id
in src/zm_mqtt.cpp i changed
mosquittopp("ZoneMinder"),
to
mosquittopp(),
which generates automatically a client_id in form auto-<guid>, but it would be better to have something like this, fortunately I can not write it in cpp correctly:
mosquittopp("ZoneMinder" + monitor->Id()),
maybe someone more skillful than me could do a patch...?

br Bohuslav
User avatar
iconnor
Posts: 3263
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: MQTT in ZM

Post by iconnor »

SO I roughed this in a long time ago... and to my recollection.. it worked. I don't see how now though.

I have applied your fixes and some more so it should work now.
gyzod
Posts: 10
Joined: Wed Jun 15, 2022 8:52 pm

Re: MQTT in ZM

Post by gyzod »

That is good news. I've tried to make it work in the past, it must be the reason it didn't at the time.

Now I have 2 questions :

1) Why did i see :
MQTT enabled ? 1, subs
in the logs for each MQTT enabled camera ? it seems to come from line 719 in zm_monitor.cpp :
Error("MQTT enabled ? %d, subs %s", mqtt_enabled, mqtt_subscriptions_string.c_str());
which logs an error in any case.

2) What it the use for the subscriptions field in the MQTT of each camera ?

Thanks a lot !
Anna.N
Posts: 34
Joined: Sun Feb 25, 2024 10:55 am

Re: MQTT in ZM

Post by Anna.N »

I don't have MQTT enabled as I don't need it but I get the following error in the log for all my monitors
MQTT enabled ? 0, subs
User avatar
iconnor
Posts: 3263
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: MQTT in ZM

Post by iconnor »

Please update. THat error log should have been a debug level.
User avatar
iconnor
Posts: 3263
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: MQTT in ZM

Post by iconnor »

The use of the subscription field is to subscribe to MQTT events. Maybe you want to record when somethng else sends an MQTT or you want to include some data in the event recording.

This is all just the beginnings of what can be done. I roughed it in hoping someone else would do something useful with it.
Anna.N
Posts: 34
Joined: Sun Feb 25, 2024 10:55 am

Re: MQTT in ZM

Post by Anna.N »

iconnor wrote: Thu Oct 24, 2024 12:24 pm Please update.
Done. Error disappeared. Thanks
gyzod
Posts: 10
Joined: Wed Jun 15, 2022 8:52 pm

Re: MQTT in ZM

Post by gyzod »

Thank you iconnor
Post Reply