mc-Things

mc-Platform => MQTT => Topic started by: plains203 on June 23, 2016, 01:16:00 am

Title: MQTT QoS and Retain
Post by: plains203 on June 23, 2016, 01:16:00 am
I am having trouble working out how to set the QoS on my Mqtt messages and also the retain option. Can someone point me in the right direction please?
So far I have this line which I know doesn't work but not sure what should either.
Lplan.Publish("TempC/Lounge", payload, QoS, True)

Thanks
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 23, 2016, 02:05:56 am
Hello.

When you say it does not work, what do you mean by that?
Does mcStudio gives you a error message?
Is the MQTT message delivered but with wrong QOS?
Is the MQTT message delivered with false retain?
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 23, 2016, 03:02:20 am
With the string above McStudio doesn't report an error but I don't believe the QoS part is correct as I want to specify a level. My broker recieves the message without qos or retain though.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 23, 2016, 09:56:53 am
I have now tried this myself and it seems that QoS is working as it should but it seems that it ignores the retain parameter.

In your code you have to change to a QoS Constant like this

QoS.AtMostOnce
QoS.AtLeastOnce
QoS.ExactlyOnce

Try one of the above and see if you could get the QOS to work.
Title: Re: MQTT QoS and Retain
Post by: mc-John on June 23, 2016, 12:36:14 pm
The code was changed to ignore the retain parameter because we had problems with devices that travel between gateways. We have to think how we, if possible, can implement this with multiple gateways without problems.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 23, 2016, 12:59:00 pm
Ahh that explains why it does not "work" thanks for information.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 23, 2016, 09:59:02 pm
I have now tried this myself and it seems that QoS is working as it should but it seems that it ignores the retain parameter.

In your code you have to change to a QoS Constant like this

QoS.AtMostOnce
QoS.AtLeastOnce
QoS.ExactlyOnce

Try one of the above and see if you could get the QOS to work.


Thanks for this. Curious as to how you knew what code to use? Is there something I am missing in the script guide or something?
I know with some IDE's it will show you the options once you start typing the code.
Title: Re: MQTT QoS and Retain
Post by: mc-Abe on June 23, 2016, 10:50:17 pm
If you use Ctrl+Space, in most cases you will get a suggestions for what to type in. The feature is not bulletproof but we are working on making it better.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 23, 2016, 11:06:16 pm
Once I set the QoS type it no longer passed the MQTT messages through. I tried with the retain flag removed although without the QoS flag being correct and retain set as true in the ide it would pass message with no QoS. So it looks like a bug to me. I would like to use the QoS flag. Retain is not important at this point for what I am doing.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 01:22:33 am
Please post the that you currently use then I could verify the code on one of my modules.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 04:07:25 am
This doesn't post anything,

Class Temperature
    Shared Event GetTemp() RaiseEvent Every 30 Seconds
       
        Dim TempC As Float = TempSensor.GetTemp
        Dim TempF As Float = TempSensor.ToFarenheit(TempC)
        Dim payload As ListOfByte = New ListOfByte
        Dim payString As String = ""
       
        payString = TempC.ToString()
       
        payload.Add(payString)
        Lplan.Publish("MC/TempC/Lounge", payload, QoS.ExactlyOnce)
       
    End Event
   
End Class

_________

This does post but no QoS settings

Class Temperature
    Shared Event GetTemp() RaiseEvent Every 30 Seconds
       
        Dim TempC As Float = TempSensor.GetTemp
        Dim TempF As Float = TempSensor.ToFarenheit(TempC)
        Dim payload As ListOfByte = New ListOfByte
        Dim payString As String = ""
       
        payString = TempC.ToString()
       
        payload.Add(payString)
        Lplan.Publish("MC/TempC/Lounge", payload, QoS)
       
    End Event
   
End Class
________________

This posts fine no QoS

Class Temperature
    Shared Event GetTemp() RaiseEvent Every 30 Seconds
       
        Dim TempC As Float = TempSensor.GetTemp
        Dim TempF As Float = TempSensor.ToFarenheit(TempC)
        Dim payload As ListOfByte = New ListOfByte
        Dim payString As String = ""
       
        payString = TempC.ToString()
       
        payload.Add(payString)
        Lplan.Publish("MC/TempC/Lounge", payload)
       
    End Event
   
End Class

_______

I have double checked this with reboots, different modules etc.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 04:40:04 am
Here is a simple code that publish to my broker without any issues

Code: [Select]
Class Temperature
    Shared Event GetTemp() RaiseEvent Every 30 Seconds
       
        Dim payload As ListOfByte = New ListOfByte
       
        payload.Add("test")
        Lplan.Publish("MC/TempC/Lounge", payload, QoS.ExactlyOnce)
       
    End Event
   
End Class

As you could see on the image from my broker you could see that Q was 2 so it is working.
Please try the above code and if it is not working then verify that your broker handles QoS as it should.


[attachment deleted by admin]
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 06:13:10 am
I tried that. I am using Mosquitto 3.1 as a broker and confirmed it will support QoS levels because I have sent some messages and seen in the diagnostic output that the QoS flag is set correctly. I used mqtt-spy to Publish the message. I am not seeing any messages from the device I used the test code on or my own code. If I remove the QoS reference then it works fine, just no QoS setting.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 06:19:08 am
That sounds strange. I send you a private message with information how you could connect to my MQTT broker just to verify this strange behavior.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 06:28:54 am
Thanks for helping. I have reconfigured for your broker and have setup the test. It will be interesting to see the results.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 06:30:14 am
I see no connection yet from your mcGateway, maybe you need to reboot it for the new connection to take place.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 06:35:55 am
Now i see that you are connected and i also see that you are publishing with QoS2

Example this:
Received PUBLISH from plains203 (d0, q2, r0, m1, 'MC/TempC/Bedroom

So everything seems to be working ok.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 06:46:34 am
Ok now it's strange because I have received 27.625 on MC/TempC/Lounge with QoS 1 so your setup works and mine is not showing me anything when I sue that QoS tag. I don't get it. I will need to update my gateway firmware to the newest version and test again I guess.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 06:57:51 am
I recieved a good number of messages and most were tagged but occasionally the tag seemed to be missing on a couple of them.

[attachment deleted by admin]
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 07:07:28 am
With Tag you mean the QoS?

That I did on purpose.

But what i have seen is that QoS 0 and 1 is working on my client on my pc and 0, 1 and 2 is working on the mqtt broker, but if i publish a message with q2 from mcModule then the broker gets it but not my client.

I dont know what happening here but i will make some more tests.
For now, upgrade your mcGateway to latest firmware and make some test with QoS 0 and QoS 1 and verify if that is working.
Title: Re: MQTT QoS and Retain
Post by: mc-Abe on June 24, 2016, 08:42:40 am
I believe there might be some issues here with the way the QoS is handled by the gateway I will do some testing today and keep you informed.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 08:50:28 am
Thanks, yes I agree there is something strange going on with the QoS.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 08:53:18 am
A few of your messages came through with retain flags Kristofferis. Did you send though through the modules or just using a MQTT Client?
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 08:55:49 am
0 and 1 worked from my modules but not 2.
The 2 that you did see was a test from my client to verify that the broker worked with 2.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 09:08:23 am
I received a retain flag on 3 messages as per this screenshot.

[attachment deleted by admin]
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 09:13:33 am
Ahh, my answer will be better if I actually reads the question  :)
Yes all the messages with retain was from my client not mcGateway.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 09:17:47 am
Ok thanks that clears that up for me then. I will try on the weekend with the qos 1 option but now it is late. Thanks for your help & I'll be interested to hear from you mc-Abe. Also I think I noticed a debug log option in mcStudio, I should try that.
Title: Re: MQTT QoS and Retain
Post by: mc-Abe on June 24, 2016, 12:56:18 pm
There are definitely some issues around the QoS. I am investigating and will have a solution soon.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 24, 2016, 01:00:30 pm
Good work Abe, and as always just let me know if you need a hand.
Title: Re: MQTT QoS and Retain
Post by: mc-Abe on June 24, 2016, 06:58:49 pm
Just wanted to let you know that I have resolved this issue but need to do a lot more testing as it was a big change. We will have an update at some point next week that will fix this.

The issue I discovered explains why no QoS2 (Exactly One) MQTT messages get to the subscriber. It was also not honoring proper operation for QoS1 (At Least Once) and messages could have ended up not being delivered.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 24, 2016, 07:02:50 pm
Thanks heaps for letting us know. This should be a nice improvement for my device that has flakey connectivity with the gateway.
Title: Re: MQTT QoS and Retain
Post by: mc-Abe on June 29, 2016, 04:01:35 pm
We just uploaded a new version of the mc-Gateway. This should have resolved the QoS issue. Note that we are still not supporting the Retain feature.
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 29, 2016, 04:07:19 pm
Perfect, thanks.
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 29, 2016, 05:52:38 pm
I am having another issue now that I have added a second event to my I am finding that some of my modules aren't sending messages but are still sending beacons. I changed one string value between modules and some are working some are not. They all sent out one message after programming. I also attempted to connect to one module in mcSudio and had it connect to a different one about 5 different times. This only started after I had 6 instead of 3 talking. I had to restart mcStudio a few times to get the connection to work. I will paste my messy code below so you can see it.

Class Temperature
    Const area As String = "Outside"
   
   
    Shared Event GetTemp() RaiseEvent Every 2 Minutes
       
        Dim TempC As Float = TempSensor.GetTemp
        //Dim TempF As Float = TempSensor.ToFarenheit(TempC)
        Dim payload As ListOfByte = New ListOfByte
        Dim payString As String = ""
        Dim decpos As Integer
        Dim topic As String
       
        topic = "MC/TempC/" + area
       
        //TempC = (TempC * 10).ToInteger() / 10
       
        payString = TempC.ToString
        decpos = payString.IndexOf(46, 0)
        payString = payString.SubString(0, (decpos + 2))
        payload.Add(payString)
       
        Lplan.Publish(topic, payload)
       
    End Event
   
    Shared Event CheckVoltage() RaiseEvent Every 6 Hours
        Dim BattVolt As Short = Device.BatteryVoltage
        Dim voltpayload As ListOfByte = New ListOfByte
        Dim voltstring As String       
        Dim topic As String
       
        topic = "MC/Voltage/" + area
       
        voltstring = BattVolt.ToString
        voltpayload.Add(voltstring)
        Lplan.Publish(topic, voltpayload)
       
    End Event
   
End Class
Title: Re: MQTT QoS and Retain
Post by: kristofferis on June 30, 2016, 03:05:06 am
And the problem started when you added the CheckVoltage() event?

If you delete that part of the code and upload to all your modules then they all are sending as they should?
Title: Re: MQTT QoS and Retain
Post by: plains203 on June 30, 2016, 08:27:00 am
Been out all day and night but to answer a few of them worked perfectly with this code and others only published 3 or 4 updates. I changed the battery voltage to report every 2 minutes and then the rest of the modules report correctly although they occasionally only publish the temperature and not the voltage. It seems like there is something going wrong with the build function in mcStudio because I rebuilt and re flashed the code on one of the modules that had the 6 hour voltage publish and it worked properly after that, with the same code it was failing with. I have since updated the gateway firmware to the next revision.