B4J Question B4J Bridge Problem?

irwan s

Member
I am trying to establish connection between my b4j-bridge to mqtt broker (broker.hivemq.com) using nanopi . I found something strange here. If I connect b4j-bridge to nanopi, the mqtt connection will failed ( I see this using the log "failed"), but when I disconnected the b4j bridge and compile the program, it says connected and I can receive the message that is being published. However, I need to connect b4j-bridge to my nanopi because I want to access the GPIO for button and LEDS.

If you guys notice something strange in my program, please comment :). I am looking forward to your comments and feedbacks.

here is my code :
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
   
#End Region

Sub Process_Globals
    Private Mqtt As MqttClient
    Private MQTTServerURI As String = "tcp://52.58.80.28:1883"
    Private MQTTConnected As Boolean
    Private mytopic As String = "cynthia/status"
    'Private serializator As B4XSerializator
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
   
End Sub

Sub AppStart (Args() As String)
    Log("hai")   
    mqtt_Connect
    StartMessageLoop
End Sub

Sub mqtt_Connect
    Dim clientId As String = "bed1"
    Mqtt.Initialize("mqtt",MQTTServerURI,clientId)
    Mqtt.Connect   
End Sub

Private Sub mqtt_Connected (Success As Boolean)
    If Success = True Then
        MQTTConnected = Success
        Log("Connected")
        Mqtt.Publish(mytopic,"Berhasil000".GetBytes("UTF8"))
        'Mqtt.Subscribe(mytopic,0)
        Log("done")
    Else
        MQTTConnected = False
        Log("Failed")
       
    End If
End Sub

Private Sub mqtt_Disconnected
    Log("Disconnected")
   
End Sub

Private Sub mqtt_MessageArrived (Topic As String, Payload() As Byte)
    Log("Message Arrive: ")
    Log(BytesToString(Payload,0,Payload.Length,"UTF-8"))   
End Sub



'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   
    Return True
End Sub
 

irwan s

Member
yes Erel. But this is what happens to me right now. When I say connect b4j-bridge it means I am using the connect from Tools >B4j-Bridge>Connect>MyIp
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'am trying to establish connection between my b4j-bridge to mqtt broker (broker.hivemq.com) using nanopi
There is no such thing. B4J-Bridge will never connect to a MQTT broker.
Only the IDE connects to B4J-Bridge.

I guess that you meant to say that you are using B4J-Bridge to run your app which connects to a broker.
Maybe the nanopi only supports a single TCP connection.

Tip - use this code to maintain a MQTT connection: https://www.b4x.com/android/forum/threads/b4x-mqtt-connect-reconnect.80815/
 
Upvote 0

irwan s

Member
There is no such thing. B4J-Bridge will never connect to a MQTT broker.
Only the IDE connects to B4J-Bridge.

I guess that you meant to say that you are using B4J-Bridge to run your app which connects to a broker.
Maybe the nanopi only supports a single TCP connection.

Tip - use this code to maintain a MQTT connection: https://www.b4x.com/android/forum/threads/b4x-mqtt-connect-reconnect.80815/
yes truly, Erel. I am really sorry. That is what I meant. I am so glad you understand it :) I will try your tip and inform if succeded
 
Upvote 0
Top