Hi,
I read several posts and studied several examples but I can't connect my ESP8266 D1 mini to the broker.
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
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: