Sub Process_Globals
Public Serial1 As Serial
Private eth As Ethernet
Private ethClient As EthernetSocket
Private btn As Pin
Private serverIp() As Byte = Array As Byte(192, 168, 1, 105 )
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private const serverPort As UInt = 50000 '51042
Private mqtt As MqttClient
'Private astream As AsyncStreams
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("pc" , Array As Byte(s) )
End If
End Sub
'============mqtt
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 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