add support for MQTT authentication ?

Hi,

emoncms does not currently support mqtt authentication using a username and password. It's not that hard to add it in it seems, since the library used for mqtt support, already supports this it's just that emoncms does not support it currently.

Here's what i did to add support for it:

in settings.php add 2 new variables: default values if using no authentication should be NULL for both

$mqtt_username = "joyrider3774";

$mqtt_password = "mypassword";

in the file Modules\process\process_processlist.php change function publish_to_mqtt to this:

public function publish_to_mqtt($topic, $time, $value)

    {

        global $mqtt_username, $mqtt_password;

        // Publish value to MQTT topic, see: http://openenergymonitor.org/emon/node/5943

        if ($this->mqtt && $this->mqtt->connect(true, NULL, $mqtt_username, $mqtt_password)) {

            $this->mqtt->publish($topic,$value,0);

            $this->mqtt->close();

        }

       

        return $value;

    }

 

if using mqtt input you should make the same change in \scripts \phpmqtt_input.php

where mqtt->connect is called add the " global $mqtt_username, $mqtt_password" line and then change the connect statement to "connect(true, NULL, $mqtt_username, $mqtt_password)"

if you do this everywhere where a mqtt connect call is made it's done (not sure where else this is used)

if this somehow ever gets in the official version it might also be a good idea to make the port which is hardcoded at 1883 at the moment be an option, i have not done this in my installation.

The problem i was having is that i was using mqtt also for other things in node-red other things that needed my mqtt (mosquito) server to be accessible from the internet (for owntracks) and i did not want to do this without having a password and login user setup for my mqtt server.

i test this only for posting to mqtt have not tried suscribing to mqtt yet