MQTT Component Authentication

Moderator: Benj

Post Reply
jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

MQTT Component Authentication

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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post 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

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post 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
Attachments
MQTT_Client.fcpx
(51.04 KiB) Downloaded 233 times

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: MQTT Component Authentication

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

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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.
Attachments
pi_mqtt_test_Clean.fcfx
(10.03 KiB) Downloaded 215 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post 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

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post by LeighM »

I've dug out a demo/test which might help ...
Attachments
MQTT MOSQUITTO BL0036 SUBSCRIBE DEMO.fcfx
(18.73 KiB) Downloaded 227 times

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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. :D

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.

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post 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

jhill
Posts: 65
Joined: Thu Jan 31, 2019 3:46 pm
Has thanked: 11 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post by jhill »

Hi Leigh,

Ah sorry, I was completely not getting it, I understand how I was breaking it now :oops:

Thanks again for your help, great support!

Best Regards,

James.

secs
Posts: 71
Joined: Fri Jul 31, 2015 9:21 pm
Has thanked: 2 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post by LeighM »

if I run this in the simulator with it try and connect to the server?
Yes, it should do

secs
Posts: 71
Joined: Fri Jul 31, 2015 9:21 pm
Has thanked: 2 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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?

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

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

secs
Posts: 71
Joined: Fri Jul 31, 2015 9:21 pm
Has thanked: 2 times
Been thanked: 12 times
Contact:

Re: MQTT Component Authentication

Post 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

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: MQTT Component Authentication

Post 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
netcomms.jpg (20.09 KiB) Viewed 8080 times
net_comms_properties.jpg
net_comms_properties.jpg (34.81 KiB) Viewed 8080 times

Post Reply