B4R Question Mqtt & B4RSerializator

Status
Not open for further replies.

marco.canta

Active Member
Licensed User
Longtime User
Hello, I have created a project that through Mqtt connects an app B4A, B4J and B4R. Ve versions B4A and B4J work perfectly using the B4xSerializator.
With B4R I have problems, I connect to the Broker Mqtt without problems, but I do not receive data, as I cannot transform the Payload into an Object of received data.

I tried with the B4RSerializator.bas module, but it gives me error in the imported module.

B4X:
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)

    Type TipoRX (VAlore1 As String, Valore2 As String, Valore3 As String)
    Dim ValTypeRx As TipoRX

    Dim data() As Object = Serzt.ConvertBytesToArray(Payload,data)

    ValTypeRx ????
    .....
        

End Sub

Thanks Marco
 

marco.canta

Active Member
Licensed User
Longtime User
I partially solved it by inserting the B4RSerializator.bas, but it gives me a series of errors that are not to solve.
I enclose code and imagine errors. Where am I wrong?

Thank you

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Public WiFi As ESP8266WiFi
    Public ESP8266 As ESP8266
    Private mqtt As MqttClient
    Public WiFiClient As WiFiSocket
    Private username As String = "*****"
    Private passWD As String = "*****"

    Type TipoTab (Val1 As String, Val2 As String, Val3)
    Dim ValTypeRx As TipoTab
    Dim ValTypeTx As TipoTab
End Sub


Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    
    If WiFi.Connect2(************", "**********") Then
        Log("Connected to : ", "WiFi")
        Dim serverIp() As Byte = Array As Byte(192, 168, 1, 31)
        Log("my IP : ", WiFi.LocalIp)
        mqtt.Initialize(WiFiClient.Stream, serverIp, 28029, "wemos_client", "Mqtt_MessageArrived", "Mqtt_Disconnected")
        Connect(0)
    Else
        Log("Failed to connect.")
    End If
End Sub



Sub Connect(unused As Byte)
    Dim mo As MqttConnectOptions
    mo.Initialize(username,passWD)
    Log("Mqtt Inizialize")
    Delay(1000)

    If mqtt.Connect2(mo) = False Then
        Log("Trying to connect again")
        CallSubPlus("Connect", 1000, 0)
        Return
    End If

    Log("Connected to broker")
    mqtt.Subscribe("MSE24_Dt", 0)
    Delay(600)
End Sub

Sub Mqtt_Disconnected
    Log("Disconnected")
    mqtt.Close
    Connect(0)
End Sub



Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)

    Dim obj As Object = Serializator.ConvertBytesToObject(Payload)

    If obj Is TipoTab Then
        Log("TipoTab")
    End If

End Sub


 
Upvote 0

marco.canta

Active Member
Licensed User
Longtime User
The errors are those in red in the photo.
In the Form B4RSerializator.bas

Maybe some library is missing? If so, which one ?

Thank you so much
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I don't see what version of B4R you are using but if you look at the link in my previous post, you will see that you do not need the "B4RSerializator.bas" file as B4RSerializator is included in rRandomAccessFile library. The B4RSerializator.bas file is used in the B4A code.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I am not sure but I think out is a reserved word in B4R. Why are you not using raf as in the many tutorials?
B4X:
Private raf As RandomAccessFile

I would really recommend that you read the tutorial I have posted twice here and maybe the MQTT tutorial (Link) again.

If after all that, you are still having problems, then post your project and erros and you will get more help.
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
Now I see your problem.
You should not use the B4RSerializator.bas file.

From B4R v1.50 is included a new type named B4RSerializator (in rRandomAccessFile library).
So just use this
B4X:
Sub Process_Globals
    Private ser As B4RSerializator
End Sub
and the functions
B4X:
    ser.ConvertBytesToArray
    ser.ConvertArrayToBytes2
    ser.ConvertBytesToArray
 
Upvote 0
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…