B4R Question MQTT issue

Bokka

Member
Hello!

I'm trying to write a very, very simple and basic program to connect an ESP8266 wifi module to an a MQTT server.

I have wrote that code:

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Public client As MqttClient
    Public connected As Boolean
    Public client As MqttClient
End Sub


Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'example of connecting to a local network
    If wifi.Connect2("wifi-net", "pwd") Then
        Log("Connected to network")
    Else
        Log("Failed to connect to network")
    End If
    
    
End Sub

Sub ConnectMQTT
    client.Initialize("client", "tcp://your_ip:port_here", "prova")
    Dim options As MqttConnectOptions
    options.Initialize("user", "password")
    client.Connect2(options)
    
End Sub

Sub client_Connected (Success As Boolean)
    connected = Success
    If Success Then
        Log("CONNESSO")
    Else
        Log("NON CONNESSO")
    End If
    
End Sub

Sub client_Disconnected
    connected = False
    Log("Disconnesso mqtt")
    'CallSub(Main, "SetState")
End Sub

Sub client_MessageArrived (Topic As String, Payload() As Byte)
    
End Sub



Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.

Sub Service_Destroy

End Sub

The board connect without problems to the wifi network, but in the line 32 of the code, I receive this error message:

"
Main - 32: Impossibile convertire il tipo: {Type=StringLiteral,Rank=0, RemoteObject=True} in: {Type=Stream,Rank=0, RemoteObject=True}
Main - 32: I tipi non corrispondono (warning #22)
Main - 31: Routine ConnectMQTT non utilizzata. (warning #12)
"

The line is:

" client.Initialize("client", "tcp://your_ip:port_here", "prova")"

Where I'm wrong?

Thanks a lot in advance!
 

Mark Read

Well-Known Member
Licensed User
Longtime User
client.Initialize("client", "tcp://your_ip:port_here", "prova")
I cannot see where you are defing the Ip and the Port! It should look something like this:

Code:
' In Process Globals
Dim host As String="192.168.4.1"
Dim myName As String="prova"
Dim port As Int = 1883

'Line 31'
client.Initialize("client", $"tcp://${MQTThost}:${port}"$, myName)
 
Upvote 0

Bokka

Member
Thanks for the reply!

The problem that the compiler reports on my code is inherent to the string "client".

I had omitted a couple of things in the code so as not to publish the address.

Now I report the code exactly as it is written and the error remains the same.

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 600
#End Region
'Ctrl+Click to open the C code folder: ide://run?File=%WINDIR%\System32\explorer.exe&Args=%PROJECT%\Objects\Src

Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Public client As MqttClient
    Public connected As Boolean
    Public client As MqttClient
    Dim host As String="0ed834272c314afea7aae83a2d027e0d.s1.eu.hivemq.cloud"
    Dim myName As String="prova"
    Dim port As Int = 8883
End Sub


Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'example of connecting to a local network
    If wifi.Connect2("wifi-net", "pwd") Then
        Log("Connected to network")
    Else
        Log("Failed to connect to network")
    End If
    
    
End Sub

Sub ConnectMQTT
    client.Initialize("client", host, port, myName)
    Dim options As MqttConnectOptions
    options.Initialize("user", "password")
    client.Connect2(options)
    
End Sub

Sub client_Connected (Success As Boolean)
    connected = Success
    If Success Then
        Log("CONNESSO")
    Else
        Log("NON CONNESSO")
    End If
    
End Sub

Sub client_Disconnected
    connected = False
    Log("Disconnesso mqtt")
    'CallSub(Main, "SetState")
End Sub

Sub client_MessageArrived (Topic As String, Payload() As Byte)
    
End Sub



Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.

Sub Service_Destroy

End Sub
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Looking at the HiveMQ homepage, I see that the port is firstly 8884 not 8883. I do not have the time to read the whole page but have you really made an account with the user and password: "user" / "password" ??

According to the homepage, you also need SSL activated.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Note line 4 below!

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Public client As MqttClient                                        <= is defined twice!!!!
    Public connected As Boolean
    Public client As MqttClient
    Dim host As String="0ed834272c314afea7aae83a2d027e0d.s1.eu.hivemq.cloud"
    Dim myName As String="prova"
    Dim port As Int = 8883
End Sub
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I have not used Hivemq. I have only used the B4R MQTT library.
 
Upvote 0
Top