B4R Question [SOLVED] Can't connect B4R MQTT client to B4J MQTT broker

Yafuhenk

Active Member
Licensed User
Longtime User
Hi,

I read several posts and studied several examples but I can't connect my ESP8266 D1 mini to the broker.

Here is the code for the B4J MQTT broker::
Sub Process_Globals
    Private Broker As MqttBroker
    Private Client As MqttClient
    Private ESPTopic As String = "ESP8266_1"
    Private Name As String = "SKIKK_1"
End Sub

Sub AppStart (Args() As String)
    Broker.Initialize("", 1883)
    Broker.DebugLog = False
    Broker.Start
    Client.Initialize("Client", "tcp://127.0.0.1:1883", Name)
    Client.Connect
    StartMessageLoop
End Sub

Sub Client_Connected (Success As Boolean)
    If Success Then
        Log(Name & " connected")
        Client.Subscribe(ESPTopic, 0)
    Else
        Log(Name & " not connected")
    End If
End Sub

Sub Client_MessageArrived (Topic As String, Payload() As Byte)
    If Topic = ESPTopic Then
        Dim bc As ByteConverter
        Dim s As String = bc.StringFromBytes(Payload , "UTF8")
        Log(s)
    End If
End Sub

Log:
Waiting for debugger to connect...
Program started.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by io.netty.util.internal.PlatformDependent0 (file:/C:/Users/Skikk/Documents/Easy4Sales/Coding/B4X/Additional%20Libraries/moqueutte.jar) to field java.nio.Buffer.address
WARNING: Please consider reporting this to the maintainers of io.netty.util.internal.PlatformDependent0
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
SKIKK_1 connected

Here is the code for the B4R MQTT client::
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
    Private Serial1 As Serial
    Private Wifi As ESP8266WiFi
    Private Client As MqttClient
    Private EthClient As EthernetSocket
    Private ESPTopic As String = "ESP8266_1"
End Sub

Private S
Sub AppStart
    'Initialize serial port (check baud rate CH340 driver)
    Serial1.Initialize(115200)
    Log("AppStart")
    'Connect to the internet
    If Wifi.Connect2("YOUR_SSID", "YOUR_KEY") = True Then
        Log("Connected to wireless network.")
        Log("My ip: ", Wifi.LocalIp)
        'Connect to the MQTT broker
        'Initialize the client (See https://www.b4x.com/android/forum/threads/mqtt.65669/#content)
        'The ip address of the MQTT broker is 192.168.0.168, so I assume the array of byte should be as written in the next line
        Client.Initialize(EthClient.Stream, Array As Byte(192, 168, 0 ,168), 1883, ESPTopic, "Client_MessageArrived", "Client_Disconnected")
        'Make the connection
        Connect(0)
    Else
        Log("Failed to connect.")
        Return
    End If
End Sub

Sub Connect(unused As Byte)
    'Try to connect until connection is true
    If Client.Connect = False Then
        Log("trying to connect again")
        CallSubPlus("Connect", 1000, 0)
        Return
    End If
    Log("Connected to broker")
    'Subsribe to a topic
    Client.Subscribe(ESPTopic, 0)
End Sub

Sub Client_MessageArrived (Topic As String, Payload() As Byte)
    'Show the incoming message
    'Convert the array of bytes to string
    Dim bc As ByteConverter
    Log("Message arrived. Topic=", Topic, " payload: ", bc.StringFromBytes(Payload))
End Sub

Sub Client_Disconnected
    Log("Disconnected")
    Client.Close
    'When disconnected try to connect again
    Connect(0)
End Sub

Log:
********************* PROGRAM STARTING ****************
rld��|�d�| �d�b|����r�c�c��nn�lgg���cp��dslrlx�g� �d�� co�|���b��gg�l��l`�gol`os���g cl�dx�o�{������cg�|�c��oo�l`�gol`o{���n c��`{��oc��`� ��|�d`��n�d����o�s��g|�l�l`b��|s�l�o��o�l`��{�d�l� �AppStart
rld��|�d�| �d�b|����r�c�c��nn�lgg���cp��dslrlx�g� �d�� co�|���b��gg�l��l`�gol`os���g cl�dx�o�{������cg�|�c��oo�l`�gol`o{���n c��`{��oc��`� ��|�d`��n�d����o�s��g|�l�l`b��|s�l�o��o�l`��{�d�l� �AppStart
Connected to wireless network.
My ip: 192.168.0.142
trying to connect again
trying to connect again
trying to connect again
trying to connect again
trying to connect again

I don't understand the warnings from the B4J log nor the strange characters from the B4R log (I checked the baud rate from the CH340 USB driver)
Is there any reason why the connection from the Client to the Broker is not succesful?

Thanks for your time,

Henk
 
Last edited:

candide

Active Member
Licensed User
Client.Initialize(EthClient.Stream, Array As Byte(192, 168, 0 ,168), 1883, ESPTopic, "Client_MessageArrived", "Client_Disconnected")
code seems OK
you can try first:
B4X:
        Client.Initialize(EthClient.Stream, "192.168.0.168", 1883, "ESP8266_1", "Client_MessageArrived", "Client_Disconnected")
 
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
code seems OK
you can try first:
B4X:
        Client.Initialize(EthClient.Stream, "192.168.0.168", 1883, "ESP8266_1", "Client_MessageArrived", "Client_Disconnected")
No difference, same result.

Log IP as String instead of Array Of Bytes):
AppStart
Connected to wireless network.
My ip: 192.168.0.142
trying to connect again
trying to connect again
trying to connect again
trying to connect again

By the way I am almost sure that the way the IP address has been initialized in my first post is correct. At least that is what I conclude from this post
https://www.b4x.com/android/forum/t...tripes-with-esp32-and-b4a.143711/#post-911988
 

Attachments

  • B4JBroker.zip
    1 KB · Views: 202
  • B4RClient.zip
    1.5 KB · Views: 196
Last edited:
Upvote 0

candide

Active Member
Licensed User
problem is with EthClient, change to mqttSocket and it is working
B4X:
'    Private EthClient As EthernetSocket
    Private mqttSocket As WiFiSocket
 
Last edited:
Upvote 0

Yafuhenk

Active Member
Licensed User
Longtime User
Unfortunately is does not. I already tried that before.
Here is the proof:

1. Server runs

1691307576906.png


2. Connect unsuccessful (with WifiSocket and Initialize2 as suggest)

1691307779726.png
 
Upvote 0

candide

Active Member
Licensed User
Sorry if i was not clear...
but i confirm, after a few modifications, this version of your code is working for me (connected to an esp-Broker)
B4X:
Sub Process_Globals
    Private Serial1 As Serial
    Private Wifi As ESP8266WiFi
    Private Client As MqttClient
    Private MQTTOpt As MqttConnectOptions
'    Private EthClient As EthernetSocket
    Private mqttSocket As WiFiSocket
    Private ESPTopic As String = "ESP8266_1"
End Sub

Sub AppStart
    'Initialize serial port (check baud rate CH340 driver)
    Serial1.Initialize(115200)
    Log("AppStart")
    'Connect to the internet
    If Wifi.Connect2("XXXXXX", "YYYYYY") = True Then
        Log("Connected to wireless network.")
        Log("My ip: ", Wifi.LocalIp)
        'Connect to the MQTT broker
        'Initialize the client (See https://www.b4x.com/android/forum/threads/mqtt.65669/#content)
        'The ip address of the MQTT broker is 192.168.1.44, so I assume the array of byte should be as written in the next line
        Client.Initialize(mqttSocket.Stream, Array As Byte(192, 168, 1 ,44), 1883, ESPTopic, "Client_MessageArrived", "Client_Disconnected")
'        Client.Initialize2(mqttSocket.Stream, "192.168.1.44", 1883, "ESP8266_1", "Client_MessageArrived", "Client_Disconnected")
        'Make the connection
        Connect(0)
    Else
        Log("Failed to connect.")
        Return
    End If
End Sub


Sub Connect(unused As Byte)
    Log("Try to connect until connection is true")
    If Client.Connect = False Then

        Log(" connection error")
        CallSubPlus("Connect", 2500, 0)
    '    Return
   Else
        Log("Connected to broker")
        'Subsribe to a topic
        Client.Subscribe(ESPTopic, 0)
    End If   
End Sub
first issue was too use with "EthClient" and a second was in "sub Connect" with "return" i suppose
 
Upvote 0
Top