Android Question [IoT] MQTT Protocol string issue from cloud

Danbicky

Member
Licensed User
Longtime User
Hi Guys,

I am using the JMQTT library Version 1 and the basic draw canvas example.

This connects to my VPS running mosquitto and authenticates perfectly, the problem is when it receives a string message "clear" it throws a java io error.
Any ideas on how this can be solved?

Dim obj As Object = serializator.ConvertBytesToObject(Payload) '<---fails here

My global aim for this application is to create a MQTT android app running as a service and to be able to respond to notifications by bringing up a pop up with notification information and to also send messages back to the broker to control embedded devices connected.

Any input on this would be great, or if anyone has a simple MQTT service example that would be brilliant.

Massive thanks

Dans
 

Danbicky

Member
Licensed User
Longtime User
Hi Erel,

I am using your canvas example for the client connected to my MQTT broker. The client publishing the string "clear" is node red on my vps server, all of my embedded nodes are able to receive this information from the subscribed topic, your default canvas app throws an error when any string message is received from my vps mosquito broker:

your default code that throws the error is:


B4X:
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    If Topic = "danbicks" Then
        Log(BytesToString(Payload, 0, password.Length, "utf8"))
        Return
    End If
    Dim obj As Object = serializator.ConvertBytesToObject(Payload)
    If obj Is CircleData Then
        If Topic = mytopic Then Return 'the circle was already drawn
        Dim cd As CircleData = obj
        DrawCircleData(cd)
    Else 'obj is string
        Dim s As String = obj
        Select s
            Case "clear"
                Canvas1.ClearRect(0, 0, Canvas1.Width, Canvas1.Height)
            Case "close"
                MainForm.Close
        End Select
    End If
End Sub

I will do more tests and see where it is falling over, your B4J gives me a different error:java.util.zip.ZipException: incorrect header check

Dans
 
Last edited:
Upvote 0

Danbicky

Member
Licensed User
Longtime User
Do you know of any examples that run as a service for MQTT that I could try?

Brilliant I will modify the code and see if I can get it working.

Thanks

Dans
 
Upvote 0

Danbicky

Member
Licensed User
Longtime User
Erel,

Thanks buddy for the example, I need to re focus with B4A, have not used it for some time been dabbling in C so simple mistakes. I have however modified your example which does compile, I have opted to use B4J for test purpose and now get an index out of bounds error, getting there!

java.lang.StringIndexOutOfBoundsException: String index out of range: 9

New modified routine!

B4X:
Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    If Topic = "danbicks" Then
        Log(BytesToString(Payload, 0, password.Length, "utf8")) >> BOMBS OUT HERE INDEX ERROR
       
        Dim VarString As String = BytesToString(Payload, 0, password.Length, "utf8")
       
        Select VarString
            Case "clear"
                Log("Woop Woop")
            Case "close"
                Log("Close received")
        End Select
       
        Return
        End If
End Sub
 
Upvote 0

udg

Expert
Licensed User
Longtime User
In bytestostring you should use payload.length not pasword.length
sorry to be so short with my reply but using a tablet virtual keyboard is boring and prone to many errors
 
Upvote 0

Danbicky

Member
Licensed User
Longtime User
Thanks udg, Awesome works Woooo.

I have a lot to learn about B4 platform, time to start tinkering and progressing.

Regards

Dans
 
Upvote 0
Top