How to set MQTT server / what are expected mqtt data?

Looks weird to me too, but it’s seems expected:

see line 113 here:

or some other example here:

It is a instantiation.

Ah something to do with namespaces in PHP I see. Far above my paygrade.

So what’s causing the crash for @notme

Yes I assume it’s to create the mqtt client.
but does not work. Maybe the php mosquitto addon is not correctly installed, but I followed the howto.
Only changed 7.0 to 7.2 here:

/etc/php/7.0/mods-available/mosquitto.ini

(I dont have 7.0 directory)

I try to find a short php test of mosquitto module

edit:
hmm I guess it’s an issue with php mosquitto module

I tested the short code here:
https://mosquitto-php.readthedocs.io/en/latest/overview.html#installation

and got this error in apache log:
[Thu May 23 12:34:14.899863 2019] [php7:error] [pid 19351] [client 192.168.1.14:44826] PHP Fatal error: Uncaught Error: Class 'Mosquitto\\Client' not found in /var/www/html/test2.php:3\nStack trace:\n#0 {main}\n thrown in /var/www/html/test2.php on line 3

edit2: short code to test the module:

<?php
if (!extension_loaded('Mosquitto-php')) {
  echo "Mosquitto not loaded";
} else {
  echo "Mosquitto loaded";
}
?>

shows Mosquitto not loaded on my side.
I will dig on this :slight_smile:

I strongly suspect the issue is due to not installing the latest version.

sudo pecl install Mosquitto-alpha

I thought this had come up before and been changed, we should be using

sudo pecl install Mosquitto-beta

support for php7 was only introduced in version 0.4.0 beta of the extension

https://pecl.php.net/package/Mosquitto

https://pecl.php.net/package-changelog.php?package=Mosquitto&release=0.4.0

I think just running the revised install line will overerite the alpha version, but watch for error messages and check the version afterwards, worse case, you will need to remove and reinstall the extension (a couple more lines that’s all).

There was a long discussion on a similar (the same) issue here Install mosquitto extension for PHP and Apache?

[edit]
The package does not actually support 7.2 I think.

One step forward :slight_smile:
I uninstalled alpha and installed beta.
I passed the line 113 !! but now stop on line 155
$publish_to_mqtt = $redis->hgetall("publish_to_mqtt");

syslog:
May 23 15:26:39 gunther emoncms_mqtt[1550]: PHP Fatal error: Uncaught Error: Call to a member function hgetall() on boolean in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php:155
May 23 15:26:39 gunther emoncms_mqtt[1550]: Stack trace:
May 23 15:26:39 gunther emoncms_mqtt[1550]: #0 {main}
May 23 15:26:39 gunther emoncms_mqtt[1550]: thrown in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php on line 155
May 23 15:26:39 gunther emoncms_mqtt[1550]: Fatal error: Uncaught Error: Call to a member function hgetall() on boolean in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php:155
May 23 15:26:39 gunther emoncms_mqtt[1550]: Stack trace:
May 23 15:26:39 gunther emoncms_mqtt[1550]: #0 {main}
May 23 15:26:39 gunther emoncms_mqtt[1550]: thrown in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php on line 155
May 23 15:26:39 gunther systemd[1]: emoncms_mqtt.service: Main process exited, code=exited, status=255/n/a
May 23 15:26:39 gunther systemd[1]: emoncms_mqtt.service: Failed with result ‘exit-code’.

good point: logs from my mosquitto broker shows connexions from emoncms :smile:
(with socket error a few seconds later)

@borpin Yes, I have seen this discussion too but not sure what to do? shall I downgrade to 7.0 (not sure how to do but I can search) ?

Alpha or any beta less than 0.4.0 certainly won’t support any php 7.?, but 0.4.0 is only listed as php 7.0 and there are reports of it working fine with 7.1 so whilst I’ve not seen any thing about using 7.2, certainly 0.4.0 is the better option to try.

@notme - What versions of redis server and redis php extension are you using? I assume they are both installed and also enabled in settings.conf.

redis is installed but not enable in settings.conf

I followed emoncms/LinuxInstall.md at master · emoncms/emoncms · GitHub
it does not say to enable it :slight_smile:
What does redis do ?

It is a in-memory database table that’s used for several things in emoncms, in this instance when an input process is a “publish to mqtt” it just puts the value to a redis queue and the mqtt service pops them off and does the publishing bit.

ok so I set “true” for redis_enabled in this section and restart

//2 #### Redis
    $redis_enabled = false;
    $redis_server = array( 'host'   => 'localhost',
                           'port'   => 6379,
                           'auth'   => '',
                           'dbnum'   => '',
                           'prefix' => 'emoncms');

Yes, but we don’t know what versions you have, I’m gonna take a guess at redis-server 3.2.6 and php redis 4.3.0 ? In which case you should be ok, but we don’t run php 7.2 so cannot be sure

phpinfo give redis 4.3.0, not sure how to get redis server version.

I have enabled redis in settings.conf.

mosquitto.log (on broker side).
1558619228: New client connected from 192.168.1.152 as emoncms (c1, k5, u'mqtt').
(nothing following, so it’s still connected)

syslog (emoncms device):
$ tail -99f /var/log/syslog | grep emoncms
May 23 15:47:06 gunther systemd[1]: Started Emoncms emoncms_mqtt script.

No error message :slight_smile:

All seems to be fine !

I will try to publish something and see if emoncms receive it

Guys !
I plan to use mqtt for inputs, but for quick check, I set an output (I forwared one of my inputs to publish on mqtt)

I just succeeded in publishing this:

23.05.2019 16:12:08 emon/emonTX/ct1_volt 236.38 
23.05.2019 16:12:16 emon/emonTX/ct1_volt 236.68 

Many many thanks too all of you for your fantastic support ! :smiley:

1 Like

The mqtt input for emoncms can be a great help if you specifically have mqtt data to log, but don’t be tempted to transition to mqtt from http just to make it mqtt, you can always make your input data available to mqtt services as well! you do not need to exclusively use one or the other. Comparisons pop up here on the forum quite frequently and whilst mqtt has it’s advantages, http is on the whole more reliable, accurate and robust where emoncms is concerned, depending on how it’s implemented of course, you can also have bad http as well as good mqtt.

See this discussion from just yesterday My Solar app calculation errors

1 Like

Well, I see a little advantage for mqtt vs http.
I am used to it from other projects (my own made weather station & some tasmota devices)…well except php mosquitto as you can guess ! :smiley:
I can easily spy, I mean subscribe from my laptop or phone to same topics I intend to use to feed emoncms. it sounds to me like a good mean to check, monitor, debug my setup.
I’m less comfortable with http as it seems less easy to me to debug the traffic between emonTX/Arduino & emoncms.

I also like the idea that mqtt is supposed to be a lightweight protocol vs http get…even if clearly, I assume the difference is not noticeable on my LAN with a few bytes every 10 or 20sec.

But nothing is frozen, I have to test & play with all those stuff :slight_smile:
I’m just starting: my emoncms receive data only since yesterday, and currently only voltage :wink:

what file contains these please anyone?

these what? @Ashipa

This settings

> //3 #### MQTT
>     // The 'subscriber' topic format is rx/* - where * is the emoncms input node number.
>     // The 'publisher' topic format is user selectable from the 'Publish to MQTT' input process, for example power/solar
>     $mqtt_enabled = true;          // Activate MQTT by changing to true
>     $mqtt_server = array( 'host'     => '192.168.1.180',
>                           'port'     => 1883,
>                           'user'     => 'mqtt',
>                           'password' => 'my_password',
>                           'basetopic'=> 'emon',
>                           'client_id' => 'emoncms'
>                           );

The MQTT Broker connection settings used, are held in the settings files.

There are 2 of them that work in a hierarchical manner

/var/www/emoncms/default-settings.ini

DO NOT EDIT the default file.

/var/www/emoncms/settings.ini

The settings.ini file holds the details for the default settings that are overridden. Add the new broker details here (the fields can be copied from the default file).