Community
OpenEnergyMonitor

Community

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

(why not) #1

Hi there !

I’m currently setting up emoncms on a small pc.
I already do have a mosquitto server and it seems emoncms can use MQTT.

I’m reading this documentation : https://guide.openenergymonitor.org/technical/mqtt/
But I think a couple of information are missing:

1/ Where can I set my own mosquitto broker informations (IP, user, password) in emoncms ?

2/ I understood that sensors shall publish on topics “emon/nodexxx/poweryyy” ( ex emon/kitchen/fridge ?) but what about the expected content format/unit ?

(Brian Orpin) #2

You can set the mqtt broker in the settings.php file in the root emoncms folder.

The content can be any numerical value.

You can also post using a valid JSON string, so

{
	"rain": 0,
	"temp_out_c": 10.7,
	"time": 1558212555
}

posted as the payload to topic emon/weather would load 2 values. As there is a time element (UNIX time in seconds), it will load that input with that timestamp.

1 Like
(why not) #3

Thank you !
I found it and update with my data as following:

//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'
                          );

my mqtt broker is on 192.168.1.180, user mqtt and password set.
I was expecting to see a connexion from ‘emoncms’ or 192.168.1.152 (my test server where emoncms is currently setup), but I seen none of those in mosquitto logs.

Did I missed something ?

(Brian Orpin) #4

Have you created that user/password pair on that broker? If not, and if any other connections you make to it are anonymous, just leave user & password as a blank string.

(why not) #5

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).

(Brian Orpin) #6

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 http://www.steves-internet-guide.com/mosquitto_pub-sub-clients/

(why not) #7

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
(Brian Orpin) #8

What does

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

show?

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

(why not) #9

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)

emonSD issues created using new scripts
(Brian Orpin) #10

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 (near the bottom).

(Paul) #11

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

[email protected]:~ $ 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
[email protected]:~ $ 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
(Brian Orpin) #12

That is a more obvious solution - thanks.

(why not) #13

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 ?

(Dave Howorth) #14

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!)

(why not) #15

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);

(Dave Howorth) #16

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

(why not) #17

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

see line 113 here:

or some other example here:

(Brian Orpin) #18

It is a instantiation.

(Dave Howorth) #19

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

So what’s causing the crash for @notme

(why not) #20

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: