B4J Question WiFi & Mqtt

marco.canta

Active Member
Licensed User
Longtime User
Hi, I have been trying for several hours to solve this problem: do not receive data from the MQTT.
I made a B4J Brocker and B4J Client program, the clients transmit and receive data in the "myTopic" without problems .... the B4R connects with MQTT but does not receive anything ... I use a WeMos D1 card.

I am attaching the code I use. Where could the problem be?
Thanks Marco


B4X:
Sub Process_Globals
    Public Serial1 As Serial
    
    Private WiFi As ESP8266WiFi
    Private WiFiStr As WiFiSocket
    Private MQTT As MqttClient
    Private MQTTOpt As MqttConnectOptions
    Private MQTTUser As String = "myUser"
    Private MQTTPassword As String = "myPassWord"
    Private MQTTHostName As String = "192.168.1.32"
    Private MQTTPort As Int = 28029
    
End Sub


Private Sub AppStart
    Serial1.Initialize(115200)
    Delay(1000)
    Log("AppStart")


    'Connect to local WiFi Access Point
    WiFi.Connect2("Vodafone-xxxx", "xxxxxxxxxxxxxx")
    If WiFi.IsConnected Then Log("Connected to WiFi, Local IP ", WiFi.LocalIp) Else Log("Not Connected to WiFi")

    'Connect to CloudMQTT broker
    MQTT.Initialize2(WiFiStr.stream, MQTTHostName, MQTTPort, "Wemos", "MQTT_MessageArrived", "MQTT_Disconnected")
    MQTTOpt.Initialize(MQTTUser, MQTTPassword)
    MQTT_Connect(0)
End Sub


Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
    Log("New Data")
    Log("Topic = ", Topic, " and Payload = ", Payload)

End Sub


Sub MQTT_Connect(Unused As Byte)
    If MQTT.Connect = False Then
        Log("Trying to connect to broker")
        MQTT.Connect2(MQTTOpt)
        CallSubPlus("MQTT_Connect", 1000, 0)
    Else
        Log("Connected to broker")
        MQTT.Subscribe("myTopic",0)
    End If
End Sub

Sub MQTT_Disconnected
    Log("Disconnected")
    MQTT.Close
    MQTT_Connect(0)
End Sub
 

marco.canta

Active Member
Licensed User
Longtime User
Could it be because in the Client I deal with b4J I use "B4XSerializator" ?
Can I also use it in B4R?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0
Top