Hi,
I'm facing WeMos D1R2 MQTT disconnected around every 1 minutes.
My Server Source Code
My B4R Client Source Code
Error Server Code
I don't know hardware or software problem.
Please help me investigate the problem.
Thank you.
I'm facing WeMos D1R2 MQTT disconnected around every 1 minutes.
My Server Source Code
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
Sub Process_Globals
Private broker As MqttBroker
Private mqtt_user As String="test"
Private mqtt_password As String="test123"
Private mqtt_port As Int = 25645
Private client As MqttClient
Private client_name As String="Server"
Private connected As Boolean
Private host As String="127.0.0.1"
Private timer1 As Timer
End Sub
Sub AppStart (Args() As String)
broker.Initialize("", mqtt_port)
broker.SetUserAndPassword(mqtt_user,mqtt_password)
broker.Start
broker.DebugLog = False
timer1.Initialize("timer1",5000)
timer1.Enabled=True
ConnectTo
StartMessageLoop
End Sub
Sub timer1_Tick
client.Publish2("server_client", "server_client".GetBytes("utf8"), 0, False)
End Sub
Public Sub ConnectTo
If connected Then client.Close
client.Initialize("client", $"tcp://${host}:${mqtt_port}"$, client_name)
Dim mo As MqttConnectOptions
mo.Initialize(mqtt_user, mqtt_password)
mo.SetLastWill("wemos/disconnect", client_name.GetBytes("utf8"), 0, False)
client.Connect2(mo)
End Sub
Private Sub client_Connected (Success As Boolean)
Log($"Connected: ${Success}"$)
If Success Then
connected=True
client.Subscribe("wemos_client", 0)
client.Subscribe("wemos/#", 0)
client.Publish2("wemos/connect", client_name.GetBytes("utf8"), 0, False)
Else
Log("Error connecting: " & LastException)
End If
End Sub
Private Sub client_Disconnected
connected=False
ConnectTo
End Sub
Private Sub client_MessageArrived (Topic As String, Payload() As Byte)
Dim PayLoadStr As String = BytesToString(Payload, 0, Payload.Length, "utf8")
Log(PayLoadStr)
If Topic="wemos/connect" Then
Log("client_MessageArrived-" & Topic & "-" & PayLoadStr)
Else If Topic="wemos/disconnect" Then
Log("client_MessageArrived-" & Topic & "-" & PayLoadStr)
Else If Topic="wemos_client" Then
Log("client_MessageArrived-" & Topic & "-" & PayLoadStr)
End If
End Sub
My B4R Client Source Code
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public WiFi As ESP8266WiFi
Public esp As ESP8266
Private mqtt As MqttClient
Public WiFiClient As WiFiSocket
Private username As String="test"
Private pwd As String="test123"
Private bc As ByteConverter
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
If WiFi.Connect2("EDIMAX","kh16@35d412t4878") Then
Log("Connected successfully to: ", "EDIMAX")
Dim serverIp() As Byte = Array As Byte(192, 168, 0, 82)
Log(WiFi.LocalIp)
mqtt.Initialize(WiFiClient.Stream, serverIp, 25645, "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,pwd)
mo.SetLastWill("wemos/disconnect","wemos_client",0,False)
If mqtt.Connect2(mo) = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to broker")
mqtt.Subscribe("wemos/disconnect", 0)
mqtt.Subscribe("server_client", 0)
mqtt.Publish("wemos/connect","wemos_client")
End Sub
Sub Mqtt_Disconnected
Log("Disconnected")
mqtt.Close
Connect(0)
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Dim PayLoadStr As String = bc.StringFromBytes(Payload)
If Topic="wemos/connect" Then
Log("client_MessageArrived-" , Topic ,"-" , PayLoadStr)
Else If Topic="wemos/disconnect" Then
Log("client_MessageArrived-" , Topic , "-" , PayLoadStr)
Else If Topic="server_client" Then
Log("client_MessageArrived-" , Topic , "-" , PayLoadStr)
mqtt.Publish("wemos_client","wemos_client")
End If
End Sub
Error Server Code
B4X:
client_MessageArrived-wemos_client-wemos_client
decode invoked with buffer UnpooledUnsafeDirectByteBuf(ridx: 1, widx: 30, cap: 480)
Received a message of type PUBLISH
onEvent processing messaging event from input ringbuffer ProtocolEvent wrapping PUBLISH
PUBLISH from clientID <Server> on topic <server_client> with QoS MOST_ONE
send publish message to <wemos_client> on topic <server_client>
An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
onEvent processing messaging event from input ringbuffer org.eclipse.moquette.spi.impl.events.LostConnectionEvent@22caaf40
Lost connection with client <wemos_client>
send publish message to <Server> on topic <wemos/disconnect>
wemos_client
I don't know hardware or software problem.
Please help me investigate the problem.
Thank you.