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

Hi!
Yes, I use this mqtt broker for a while now ( some Tasmota devices and my own weather station ).
I have disabled anonymous connexion and use only one user ‘mqtt’ + password (no SSL, it’s only on my LAN).

I rebooted the emoncms device after changing settings (I was not sure how to restart/load new settings).

From the emoncms machine, can you use the command line to connect to your remote broker?
e.g.

mosquitto_sub -d -v -h 192.168.1.180 -u 'mqtt' -P 'my_password' -t 'mybasetopic/#'

Where the topic (-t) is something you know that broker publishes?

A good item for using the command line is here Using The Mosquitto_pub and Mosquitto_sub MQTT Client Tools- Examples

Ok I found several issues :slight_smile:
mosquitto-client was not installed…now it’s installed.
I subscribed with the command line to ‘#’ so I can see all topics.
It works fine.
I was able to see the connexion from 192.168.1.152 in mosquitto (due to mosquitto_sub).
I reboot again, but no connexion from emoncms.

I though it was maybe missing some dependancies in emoncms and I found this page:

I followed instructions:

sudo apt-get install libmosquitto-dev
sudo pecl install Mosquitto-alpha
printf "extension=mosquitto.so" | sudo tee /etc/php/7.0/mods-available/mosquitto.ini 1>&2
sudo phpenmod mosquitto

This one I replaced 7.0 by 7.2 because there was no 7.0 directory on my server, only 7.2

I skipped this one:

cp /var/www/emoncms/default.settings.php /var/www/emoncms/settings.php

because settings shall be already ok

then I did this one:

sudo ln -s /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.service /lib/systemd/system
sudo systemctl enable emoncms_mqtt.service

but this very last command fail with:
Failed to enable unit: Unit file emoncms_mqtt.service does not exist.

but I checked and the file is here.

  • note I’m not on a raspberry, but a small computer with full ubuntu server 18.04

What does

ls -la /var/www/emoncms/scripts/services/emoncms_mqtt/

show?

What version are you running of emoncms? You may need to update.

An other hit!
My emoncms is installed in /var/www/html :disappointed_relieved:
I corrected the link but there seems to be some path hardcoded to /var/www/emoncms… Inside the mqtt files.

I will check a bit more, maybe re install will be necessary.

Btw, I run 9.9.9, I expect not too old (download recently)

It is the service file that expects it to be there.

You will need to edit the service file but this will get overwritten (as things stand) on any update.

This will be true for any service file and, strangely enough, is being discussed here emonSD issues created using new scripts + emonhub environment file (near the bottom).

You’ll be better off just adding a symlink.

You seem to be missing this part of the guide (not sure if it’s in the guide you followed)

Save and exit. Create a symlink to reference emoncms within the web root folder:

cd /var/www/html && sudo ln -s /var/www/emoncms

(from https://github.com/emoncms/emoncms/tree/master/docs/RaspberryPi#configure-emoncms-database-settings)

here’s what an emonpi looks like

pi@emonpi:~ $ ls -la /var/www
total 16
drwxr-xr-x  4 pi       root     4096 Aug 15  2018 .
drwxr-xr-x 14 root     root     4096 Apr 23 13:46 ..
drwxr-xr-x  9 pi       pi       4096 May  6 00:10 emoncms
drwxr-xr-x  2 www-data www-data 4096 Aug 15  2018 html
pi@emonpi:~ $ ls -la /var/www/html
total 12
drwxr-xr-x 2 www-data www-data 4096 Aug 15  2018 .
drwxr-xr-x 4 pi       root     4096 Aug 15  2018 ..
lrwxrwxrwx 1 root     root       17 Aug 15  2018 emoncms -> /var/www/emoncms/
-rw-r--r-- 1 root     root       41 Aug 15  2018 index.php

1 Like

That is a more obvious solution - thanks.

Hi !
The point is that I installed emoncms in /var/www/html
I followed the howto, but I come to this directory, I figured out that /var/www was not the right place for my setup, this directory is not visible using http from my laptop.
Would it work with a link the other way ? I mean:
ln -s /var/www/html/emoncms /var/www/emoncms ?

Maybe, I can’t remember; apache is a bit strict about following symlinks so depending on how yours is configured and exactly what paths are followed you may or may not trip up over something. I can’t be bothered to work it out - it’s simpler just to try it :slight_smile:
But even easier would probably be to just move your install

mv /var/www/html/emoncms /var/www 

and then install the same link that Paul showed you. That way you end up with a common situation that will be easier next time you have a problem (if you do!)

good idea ! and done! :slight_smile:

now I face an other issue… :crazy_face:

Got this in syslog:

 May 23 12:03:16 gunther systemd[1]: emoncms_mqtt.service: Service hold-off time over, scheduling restart.
May 23 12:03:16 gunther systemd[1]: emoncms_mqtt.service: Scheduled restart job, restart counter is at 856.
May 23 12:03:16 gunther systemd[1]: Stopped Emoncms emoncms_mqtt script.
May 23 12:03:16 gunther systemd[1]: Started Emoncms emoncms_mqtt script.
May 23 12:03:16 gunther emoncms_mqtt[31043]: PHP Fatal error:  Uncaught Error: Class 'Mosquitto\Client' not found in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php:113
May 23 12:03:16 gunther emoncms_mqtt[31043]: Stack trace:
May 23 12:03:16 gunther emoncms_mqtt[31043]: #0 {main}
May 23 12:03:16 gunther emoncms_mqtt[31043]:   thrown in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php on line 113
May 23 12:03:16 gunther emoncms_mqtt[31043]: Fatal error: Uncaught Error: Class 'Mosquitto\Client' not found in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php:113
May 23 12:03:16 gunther emoncms_mqtt[31043]: Stack trace:
May 23 12:03:16 gunther emoncms_mqtt[31043]: #0 {main}
May 23 12:03:16 gunther emoncms_mqtt[31043]:   thrown in /var/www/emoncms/scripts/services/emoncms_mqtt/emoncms_mqtt.php on line 113
May 23 12:03:16 gunther systemd[1]: emoncms_mqtt.service: Main process exited, code=exited, status=255/n/a
May 23 12:03:16 gunther systemd[1]: emoncms_mqtt.service: Failed with result 'exit-code'.

line 113 of the php file is:
$mqtt_client = new Mosquitto\Client($mqtt_server['client_id'],true);

What’s that backslash doing there? That feels wrong to me, though I know nothing.

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.