Publish to MQTT from a MQTT feed input

Hi!

I have found that if i try to "publish to MQTT" from a regular input (using the http API) it works great, however if the input is received from MQTT itself it doesn't get published at all.

i need to use input logic engine to derive a completely new feed and export it to the MQTT broker so other bits of software can use it.

Functioning as designed, bug?

I'm with emoncms 9.2 on debian and a slightly modified mqtt input script to have the topics organized:

 

root@debian:/# mosquitto_sub -v -t emoncms/#

emoncms/to/2 293,1,0,5,7,23574
emoncms/from/andar1/MainPower 217.77
emoncms/from/andar1/LightsPower 52.47
emoncms/from/andar1/SocketsPower 105.76
emoncms/from/andar1/ACPower 0
emoncms/from/andar1/TVsPower 56.17
emoncms/from/andar1/KitchenPower 0
 

 

Thanks!

 

 

 

 

Bra1n's picture

Re: Publish to MQTT from a MQTT feed input

Perhaps not very helpful but you are not alone, I raised this issue back in September but got no response and ended up changing my strategy. See http://openenergymonitor.org/emon/node/11306

TrystanLea's picture

Re: Publish to MQTT from a MQTT feed input

I wonder if the issue could be caused by an attempt to create a second connection here:
https://github.com/emoncms/emoncms/blob/master/Modules/process/process_p...

I dont have a test system handy at the moment would you mind trying to see if changing the line:

if ($this->mqtt && $this->mqtt->connect(true,NULL,$mqtt_server['user'],$mqtt_server['password']))

to

if ($this->mqtt)

solves it in this instance? We would need to have a way to establish if a connection has already been created by the parent phpmqtt_input.php script for a longer term solution.

It may also need a couple of lines added just below line 49 here:

https://github.com/emoncms/emoncms/blob/master/Modules/process/process_p...

} elseif ($mqtt==true) {
    $this->mqtt = $mqtt;
}

The parent script phpmqtt_input which includes and creates an instance of the process_processlist class has already created a mqtt connection which is available in the global namespace and needs to be transferred to the process_processlist class properties.

cab123's picture

Re: Publish to MQTT from a MQTT feed input

Hi Bra1n, your issue is exactly the same can you share your workaround?

Hi TrystanLea, i'll try to change those later today when i get home.

 

I could build the input logic to create the new feed on a exisitng feed that is received via traditional http API, rather than a MQTT feed, but i would like to migrate everything to MQTT so this would be useless in the future.

 

Bra1n's picture

Re: Publish to MQTT from a MQTT feed input

Not really a work round I changed strategy and moved to MySQL for all my feeds doing some of my processing there and node-red for the rest of the processing

I'm starting to find I only use MQTT to notify node-red an event has occured and processing is required

joyrider3774's picture

Re: Publish to MQTT from a MQTT feed input

am i correct in understanding that you use the phpmqttinput script to receive data from mqtt as an input on a certain node and then you do publish to mqtt again ? and this last mqtt publish does not work then ? only reason i can see for doing this is if you alter the incoming mqtt data somehow. Unless i don't understand the problem correctly.

i can test this out this evening to see if i have the same problem. what i do already is have phpmqttinput script get data for a certain node (rx/nodeid) and i also publish to mqtt although not with the inputs getting their data from mqtt only with other inputs getting their data from http api. and that does seem to work together.

I'll try to add a new publish to mqtt from one of these mqtt inputs again and see if iget the same problem. If that's the problem i wonder if starting the mqttinput script before or after launching wamp has an impact somehow)

 

on side note, the only problem i seem to be having is that the phpmqttinput script errors out from time to time saying that the (remote ?) host closed the connection and then no input is received anymore but i'm guessing this is a windows / wamp related issue although not certain. my way around this is to run the input mqtt script from within a batch file in an etneral loop using goto statement so that when it does crash / errors out it's automatically restarted

cab123's picture

Re: Publish to MQTT from a MQTT feed input

Hi joyrider3774, your undestanding is absolutely correct. I want to change the incoming MQTT feed by adding and subtracting two different input feeds. You confirmed that the workaround i suggested on my previous post would work if  a existing HTTP API feed is used to to create the new feed.

I think you will easily replicate this issue.

Bra1n, i am still in a MQTT-centric-begginer approach :-) i want everything  in there so i can share data easily between my progs.

 

joyrider3774's picture

Re: Publish to MQTT from a MQTT feed input

@TrystanLea

I found a (possible) solution for the problem. I could replicate the problem with me. so i thought i'd change all variablenames of $mqtt inside phpmqtt_input.php to $mqttnew to make sure a new connection is made. but when running the script i got this weird stack trace:

C:\wamp\www\emoncmsnew\scripts>php C:\wamp\www\emoncmsnew\scripts\phpmqtt_input.php

Fatal error: Cannot redeclare class phpMQTT in C:\wamp\www\emoncmsnew\Lib\phpMQT
T.php on line 36

Call Stack:
    0.0010     266808   1. {main}() C:\wamp\www\emoncmsnew\scripts\phpmqtt_input
.php:0
    0.0120    1036168   2. Process->__construct() C:\wamp\www\emoncmsnew\scripts
\phpmqtt_input.php:75
    0.0120    1036360   3. Process->get_process_list() C:\wamp\www\emoncmsnew\Mo
dules\process\process_model.php:34
    0.0120    1036440   4. Process->load_modules() C:\wamp\www\emoncmsnew\Module
s\process\process_model.php:59
    0.0130    1039496   5. Process->get_module_class() C:\wamp\www\emoncmsnew\Mo
dules\process\process_model.php:103
    0.0150    1359464   6. Process_ProcessList->__construct() C:\wamp\www\emoncm
snew\Modules\process\process_model.php:132
    0.0150    1457720   7. require('C:\wamp\www\emoncmsnew\Lib\phpMQTT.php') C:\
wamp\www\emoncmsnew\Modules\process\process_processlist.php:46

don't really understand why though but if i change the the require('Lib\phpMQTT.php') to require_once('Lib\phpMQTT.php') inside process_processlist.php and also inside phpmqtt_input.php that error goes away and then in combination with renaming $mqtt to $mqttnew in phpmqtt_input.php  the problem is solved, then the publish to  mqtt from a node comming from mqtt works. at least it does with me.

i do think it is actually using 2 connections now one for publishing and one for listening although not 100% certain, but it would probably be safer to have 2 connections, does it not ?

while before it seemed it was somehow sharing the connection or at least i think so but am not certain. Also since i don't fully understand the impacts from the require to require_once statements i'll wait some comments from u. As to my understanding require includes the lib\phpmqtt.php eacht time while require_once only includes it once

if you want me to try something else please tell me what or if the info given above has enough info to find the culprit or is the actual solution then all is fine :)

edit:

not certain but with my changes looking at the mosquito output it seems i have 2 emoncms input suscriber connections or at least i think so since it's sending a ping and receives ack from input suscriber 2 times after each other or are 2 pings send each time or does it just get displayed like that ?

the publisher one always connects and then disconnects which is normal

1452023351: New connection from 127.0.0.1 on port 1883.
1452023351: New client connected from 127.0.0.1 as Emoncms Publisher (c1, k10, u
'joyrider3774').
1452023351: Sending CONNACK to Emoncms Publisher (0, 0)
1452023351: Received PUBLISH from Emoncms Publisher (d0, q0, r0, m0, 'Plug5 Watt
', ... (1 bytes))
1452023351: Received DISCONNECT from Emoncms Publisher
1452023351: Client Emoncms Publisher disconnected.
1452023352: Received PINGREQ from Emoncms input subscriber
1452023352: Sending PINGRESP to Emoncms input subscriber
1452023352: Received PINGREQ from Emoncms input subscriber
1452023352: Sending PINGRESP to Emoncms input subscriber
cab123's picture

Re: Publish to MQTT from a MQTT feed input

Hi here are my findings:

process_processlist.php_new -> with the changes suggested by TrystanLea

process_processlist.php_bck -> original file from emoncms 9.2

emoncms/from/floor1/MainPower -> regular input feed from a emontx (http api) that is published to MQTT

emoncms/from/floor2/MainPower -> MQTT input feed that is transformed and is published back to MQTT

 

%reboot the system%

root@debian:/# mosquitto_sub -v -t emoncms/from/+/MainPower/#
emoncms/from/floor1/MainPower 625.19
emoncms/from/floor1/MainPower 623
emoncms/from/floor1/MainPower 624.52
emoncms/from/floor1/MainPower 643.54
emoncms/from/floor1/MainPower 636.44
emoncms/from/floor1/MainPower 644.81
emoncms/from/floor1/MainPower 675.98
 -------------------------------------> /etc/init.d/mqtt_input restart

emoncms/from/floor1/MainPower 680.58
emoncms/from/floor1/MainPower 678.09
emoncms/from/floor1/MainPower 666.66
emoncms/from/floor1/MainPower 647.47

 -------------------------------------> cp process_processlist.php_new process_processlist.php

(nothing!)

 -------------------------------------> /etc/init.d/mqtt_input restart
 
emoncms/from/floor2/MainPower 806.022
emoncms/from/floor2/MainPower 925.282
emoncms/from/floor2/MainPower 741.592
emoncms/from/floor2/MainPower 778.282
emoncms/from/floor2/MainPower 827.392

 -------------------------------------> cp process_processlist.php_bck process_processlist.php
 
emoncms/from/floor1/MainPower 1108.22
emoncms/from/floor1/MainPower 1025.05
emoncms/from/floor2/MainPower 835.452
emoncms/from/floor1/MainPower 1011.89
emoncms/from/floor1/MainPower 1000.62
emoncms/from/floor2/MainPower 857.882
emoncms/from/floor1/MainPower 1014.04
emoncms/from/floor1/MainPower 1021.8
emoncms/from/floor2/MainPower 818.702

 

If i reboot the system i get back to the original issue.

Does this help?

joyrider3774's picture

Re: Publish to MQTT from a MQTT feed input

could you try what i changed ? i only tested it rather quickly but it did seem to post the data to mqtt comming form an mqtt input fine. I just don't know if the changes are any good even if it worked :)

cab123's picture

Re: Publish to MQTT from a MQTT feed input

Hi joyrider3774, indeed your changes worked. Rebooted and the system and seems stable for now, i'll keep those settings and report back if i find any issue.

 

Thanks!!

joyrider3774's picture

Re: Publish to MQTT from a MQTT feed input

Lets hope some variant of it makes it to the official version otherwise you' might have to keep changing the files again with each update but i think that depends on how your updating

allmac's picture

Re: Publish to MQTT from a MQTT feed input

Cab123,

How did you added MQTT publishing INPUT data from HTTP?
I am able to publish the reverse... no HTTP input data, but only MQTT input data.

To publish MQTT received data to MQTT broker I have the following in my Emonhub config file:

[[MQTT]]
    Type = EmonHubMqttInterfacer
    [[[init_settings]]]
        mqtt_host = localhost
        mqtt_port = 1883
    [[[runtimesettings]]]
        pubchannels = ToRFM12,
        subchannels = ToEmonCMS,
        basetopic = emonhub/

And I receive this MQTT data via Serial (USB):

[[SerialDirect]]
     Type = EmonHubSerialInterfacer
      [[[init_settings]]]
           com_port = /dev/ttyACM0 #USB
           com_baud = 9600
      [[[runtimesettings]]]
           pubchannels = ToEmonCMS,
           subchannels = ToRFM12
           baseid = 5 #(emonPi default:5)
           quiet = false #(default:true)

EMONHUB LOG:
SerialDirect Publishing: emonhub/rx/5/values 220,52,40,110,124,33

EMONCMS LOG:
Received mqtt message: emonhub/rx/5/values 346,180,40,111,124,33

How do you receive your MQTT data? RF?

cab123's picture

Re: Publish to MQTT from a MQTT feed input

Hi, yes, i have a standard emonTx v2 and a RF link that posts data to a standard RFM2Pi interfacer. From there the data is passed to the MQTT broker just like you do.

My HTTP API is from another emonTx, that has built-in ethernet and deliver the data directly to emoncms. I want to change that to MQTT in the future.

 

To send data to HTTP you need a different interfacer type, like this one:

[[Local_CMS]]
    Type = EmonHubEmoncmsHTTPInterfacer
    [[[init_settings]]]
    [[[runtimesettings]]]
       pubchannels = ToRFM12,
        subchannels = ToEmonCMS,
        url = http://localhost/emoncms
        apikey = XXXXXXXXXXXXXXXXXXXX
        senddata = 1
        sendstatus = 1

 

This was working and is just commented on my emonhub.conf since i don't need it anymore. Hope it helps

allmac's picture

Re: Publish to MQTT from a MQTT feed input

Thanx! I am already able to send data to HTTP. I was looking for send data received via HTTP to MQTT.
But I found how to.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.