Hi there, i've tried to connect my ethernet shield to mqtt broker... i'm using 52.58.157.180 ( broker.hivemq.com) but it couldnt connect.. this is my code. I'm following errel tutorial btw.. but still cannot connect
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#StackBufferSize: 300
#End Region
Sub Process_Globals
Public Serial1 As Serial
' Private astream As AsyncStreams
Private eth As Ethernet
Private ethClient As EthernetSocket
Private btn As Pin
Private serverIp() As Byte = Array As Byte(52,58,157,180)
Private MacAddress() As Byte = Array As Byte(0xCE, 0xAC, 0xEA, 0xCE, 0xFE, 0xED)
Private const serverPort As UInt = 1883
Public mqtt As MqttClient
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
If eth.InitializeDHCP(MacAddress) = False Then
Log("Error connecting to network.")
Return
Else
Log("Connected to network. My ip address: ", eth.LocalIp)
End If
btn.Initialize(btn.A0, btn.MODE_INPUT_PULLUP)
btn.AddListener("Btn_StateChanged")
mqtt.Initialize(ethClient.Stream, serverIp, serverPort, "arduino", "Mqtt_MessageArrived", "Mqtt_Disconnected")
Connect(0)
End Sub
Sub Btn_StateChanged (State As Boolean)
If ethClient.Connected Then
Dim s As Byte
If State Then s = 1 Else s = 0
' astream.Write(Array As Byte(s))
mqtt.Publish("TEST",Array As Byte(s))
End If
End Sub
Sub Connect(unused As Byte)
If mqtt.Connect = False Then
Log("trying to connect again")
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected to broker")
mqtt.Subscribe("arduino", 0)
End Sub
'Sub Astream_NewData (Buffer() As Byte)
' Log("Received from server: ", Buffer) 'pass the array of bytes. Don't convert to string.
'End Sub
'
'Sub Astream_Error
' Log("error")
' ethClient.Close
' CallSubPlus("Connect", 1000, 0)
'End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived. Topic=", Topic, " payload: ", Payload)
End Sub
Sub Mqtt_Disconnected
Log("Disconnected")
mqtt.Close
Connect(0)
End Sub