Page 1 of 1
MQTT Component Authentication
Posted: Sun Aug 18, 2019 4:25 pm
by jhill
HI, I am trying to use the MQTT component with the Eclipse Mosquito open source MQTT broker. Mosquito requires the client to login prior to publishing or subscribing to topics. I cannot see an way to achieve this in the Flowcode 8 MQTT component.
Would it be possible to modify the component to provide the component with the login details along with the IP address, port etc?
Best Regards,
James.
Re: MQTT Component Authentication
Posted: Mon Aug 19, 2019 9:49 am
by LeighM
Mosquitto can be run without authentication, but if you need it I will have a look and see if this can be included into the component
Re: MQTT Component Authentication
Posted: Mon Aug 19, 2019 10:32 am
by jhill
Hi,
Thank you for getting back to me!
The instance of Mosquitto I would like to connect to is a part of a running configured system,
https://guide.openenergymonitor.org/technical/mqtt/
I would prefer not to remove the authentication,( this is the standard configuration I believe) would you mind seeing if this can be included?
Best Regards,
James
Re: MQTT Component Authentication
Posted: Mon Aug 19, 2019 12:02 pm
by LeighM
Hi James,
I've now added Name/Password Authentication
Could you try the attached updated component, to go into directory:
C:\ProgramData\MatrixTSL\FlowcodeV8\Components
Re: MQTT Component Authentication
Posted: Mon Aug 19, 2019 1:02 pm
by jhill
HI Leigh,
First test, this looks to work perfectly! I will have a more in depth test but publishing with login is working great.
Thanks so much for doing this so quickly!
Best Regards
James.
Re: MQTT Component Authentication
Posted: Mon Aug 19, 2019 1:43 pm
by Benj
Hi James,
Thanks for confirming, this latest change has now been pushed to the update system.
Just in case you see the "updates are available" dialogue pop up.
Re: MQTT Component Authentication
Posted: Mon Aug 19, 2019 3:08 pm
by jhill
Thanks Ben,
I wonder if you would mind having a quick look at the attached test prog. It workes fine in simulation (I have turned authentication off to post here) but gives a "segmentation fault" when trying to execute on a raspberry pi zero w.
Best Regards,
James.
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 7:43 am
by LeighM
Hi James,
The Raspberry Pi Zero is not on our supported devices list.
The RPi3 target will not produce code compatible with Zero,
you could try the RPi1 as the target, but this has not been tested.
Leigh
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 10:31 am
by jhill
Hi Leigh,
Thanks for getting back to me, I did try RPi1 but with the same result.
I have just tried with a Pi3B+ Rev 1.3 running Raspian 9 (no GUI), and get the same "segmentation fault"
Is it possible i am missing a library or something?
Best Regards,
James
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 10:55 am
by LeighM
It would be worth adding a DebugPrint component at various points in the program to see how far it gets.
See "Console Debugger (Raspberry Pi)" in the Runtime Tools menu.
This will output text to the terminal window.
Also you need to check the return values from the MQTT_Client functions that you are calling.
In particular you need to check the Connect and Read, if Read returns 0 then skip the Get calls.
Hope that helps,
Leigh
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 11:01 am
by LeighM
I've dug out a demo/test which might help ...
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 1:28 pm
by jhill
Hi Leigh,
Thank you very much for your help. I now have your example, edited and running, first on the PI3, but also on the Pi Zero without issue.
I have tracked down the cause of the Segmentation fault. In your example, the GetTopic Macro uses the string variable .Topic, which is set to nothing (“”)
If instead of using a variable. I define a topic using “” directly in the GetTopic macro, e.g. “P1” this causes the Segmentation Fault.
If I assign a Topic in the calculation block, i.e .Topic=”P1” this does not cause a segmentation fault.
I am somewhat confused as to what I should put as the topic in the GetTopic macro, as the program always returns the value as defined in the subscribe macro, in my case emon/emontx8/P1. If I subscribe to just emon/emontx8 and then try to GetTopic P1, this does not work. (its subscribes ok, but returns no values).
It appears that whatever I set in the GetTopic makro is ignored, and the value defined in the subscription value is returned, is this correct? If I subscribe to 2 values, ether value is returned randomly, despite what is set in the Get Topic Makro.
I am not sure why putting a value directly into the GetTopic makro results in a Segmentation fault, is this intentional?
Best Regards,
James.
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 1:51 pm
by LeighM
Hi James,
Ah, right, I see the problem now.
The GetTopic expects a named buffer to receive the string value that is returned by the function.
When you subscribe to a topic this just registers with the broker that you want to receive updates on this topic.
Once you are connected to the broker it will push messages to you, which can be detected and read with the Read function.
If you register for more than one topic, say two, then when you do the Read it can receive a data message for either of the topics.
So the call to GetTopic returns the topic associated with this message, the GetPayload gets the value for the topic.
You will see in my demo that the received topic is determined by the call to GetTopic and displayed via the debug channel.
So you can use GetTopic to determine what data you have just received, by doing a string compare with any topics you are expecting, and then take the appropriate action.
Hope that explains it.
Leigh
Re: MQTT Component Authentication
Posted: Tue Aug 20, 2019 3:18 pm
by jhill
Hi Leigh,
Ah sorry, I was completely not getting it, I understand how I was breaking it now
Thanks again for your help, great support!
Best Regards,
James.
Re: MQTT Component Authentication
Posted: Mon Sep 02, 2019 5:19 am
by secs
Hi all. I just came looking in the forums for the same authentication issue and found this thread. Just wanted to add my MQTT server also uses authentication so will download the compnent and give it a whirl.
On a side not, if I run this in the simulator with it try and connect to the server?
Peter
Re: MQTT Component Authentication
Posted: Mon Sep 02, 2019 7:34 am
by LeighM
if I run this in the simulator with it try and connect to the server?
Yes, it should do
Re: MQTT Component Authentication
Posted: Mon Sep 02, 2019 8:04 am
by secs
Thanks Leighm
Ok I tried the example for the MQTT scada thing and using the authentication, it connects to my server.
However I am having issues making a new program.
One thing I noticed was in the properties of the MQTT client in the example, the network component points to a component that shows on the component list.
However if I create a new program and add a mqtt client, it points to 0and there is NO network component in the tree.
Should there be one?
Re: MQTT Component Authentication
Posted: Mon Sep 02, 2019 9:11 am
by LeighM
Hi
You need to add a Network Comms component (see Comms menu) into the project,
then select this in the "Network Component" property of the MQTT Client component.
Re: MQTT Component Authentication
Posted: Mon Sep 02, 2019 9:56 am
by secs
Thanks Leigh
Yes I did that before you replied and it connected using simulation. What threw me was in the initial stage I simply added the MQTT client and it must have did the networking. I then upgraded after finding this thread as I needed authentication and in playing around, I even created a new program. It didnt add the networking component.
Anyways its a work in progress. I am intending to run it on a RPI3+ so will I have to change the networking compoent to that one?
Great product and have nejoyed returning to Flowcode after a couple of years away
Re: MQTT Component Authentication
Posted: Mon Sep 02, 2019 11:30 am
by LeighM
Yes, to work on the Raspberry Pi target you need to add the TCP/IP (Raspberry Pi) component and link it to the Network Comms component.

- netcomms.jpg (20.09 KiB) Viewed 10995 times

- net_comms_properties.jpg (34.81 KiB) Viewed 10995 times