Problems with MQTT Read

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

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

Problems with MQTT Read

Post by secs »

Hi guys.

I was chatting on another topic but thought I should start a new one as the other was in a feature request.

I have upgraded my MQTT component so it includes the authentication property.

I run a MQTT server/broker on a local server. Its Mosquito.

Through help from LeighM I have the MQTT component Initialising in the main section at the start of the program. I then run the rest of the commands in a macro where I have the client

Connect - Returns a non zero
Subscribes - Returns a non zero
Read - returns a zero.

I also have run a MQTT client on my local Linux box and it sees the subscription update succesfully.

I have changed the subscription topic to one I know is consistantly being updated but no matter what I do I cant get a non zero from the read.

Now I realise there could be a million reasons but can anyone suggest a method I can use to see why I cant get it to read and thus then maybe see a fix?

I will sleep on it and see if I can nut out a solution

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: Problems with MQTT Read

Post by LeighM »

Best to post/attach your program, if possible

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

Re: Problems with MQTT Read

Post by secs »

Not a problem. I think I have attached the correct file. Just off to work so if not will attach correct one later.

Its a test in progress so a bit (lot) rough :-)

Peter
Attachments
Flowcode1.fcfx
(16.64 KiB) Downloaded 151 times

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

Re: Problems with MQTT Read

Post by secs »

The client I am using on the desktop returns the fllowing

88
qos : 0, retain : false, cmd : publish, dup : false, topic : homeassistant/climate/probofrain, messageId : , length : 36, Raw payload : 5656

The value 88 is what I have something else publishing. Does this shed any light?

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: Problems with MQTT Read

Post by LeighM »

Hi Peter,
The problem is that you are only doing only one Read directly after the Subscribe.
You need to do the Read in a loop, this will listen for any incoming messages from the broker, which will not be continuous.
So most of the time the Read will return 0.
You would be better to Connect and Subscribe once at the start of your program, then just do the Read in a loop which will detect the messages from the broker when they are sent. (Which is usually at the point at which the broker receives messages from Publishing clients)
One addition to that is that whilst connected there is a timeout on the broker which will disconnect you if you don’t periodically send a Ping. (See Keep Alive property of the MQTT component).
Hope that helps,
Leigh

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

Re: Problems with MQTT Read

Post by secs »

Thanks Leigh.

Yes that makes sense.

I have never really looked into the protocol but use it extensively in a house automation system via Node-Red and HomeAssistant.

Because it simply works in the background I have never had to study it but sort of wondered if that was the case in a way. Will modify it and have a play. Flowcode will work well in hooking external stuff to the home automation and I will pass that on to the Automation forums.

Regards, 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: Problems with MQTT Read

Post by LeighM »

Brilliant, thanks! :D

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

Re: Problems with MQTT Read

Post by secs »

Just an update.

For those interested, I run a Mosquitto Broker and looked up the stats for it
The mqtt documentation explains that the maximum value of the keepalive is 18 hours 12 minutes and 15 seconds. But the mosquitto server disconnects the clients if it does not receive messages before 60 seconds plus a tolerance of 30 seconds that is to say maximum to 90 seconds.

So I fugure a ping every 30 seconds should be ok.

I was looking at it the other way around. I forgot that the broker basically tells the client there is a new published packet and was thinking that it would increase network traffic by leaving it logged in. I use MQTT to have my CCTV cameras talk to my House automation and it simply works.

Just in the process of rewriting the macro and will give it a go

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

Re: Problems with MQTT Read

Post by secs »

Ok its running in simulation mode using the network comms for the MQTT client network options.

So just to clarify, if I want to upload this to the RPI I need to add a RPI network component and point the MQTT client to it and when I use it in simulate poiunt it to the network comms. In other words, it wont select the comms component it requires auto-magically?

So far so good.

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: Problems with MQTT Read

Post by LeighM »

Add the components as this post ...
viewtopic.php?f=77&t=21448#p96922
then it will work for both target and simulation

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

Re: Problems with MQTT Read

Post by secs »

Sorry. Yep I get it now. Many thanks...

Post Reply