Sub Process_Globals
Public Serial1 As Serial
Private MacAddress() As Byte = Array As Byte(0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
Private ServerIp() As Byte = Array As Byte(192, 168, 0, 6)
Private eth As Ethernet
Private ethSocket As EthernetSocket
Private mqtt As MqttClient
Private const SlaveAddress As Byte = 8
Private master As WireMaster
Private btnTimer As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
If eth.InitializeDHCP(MacAddress) = False Then
Log("Failed to get IP address")
Return
End If
mqtt.Initialize(ethSocket.Stream, ServerIp, 51042, "arduino1", "Mqtt_MessageArrived", _
"Mqtt_Disconnected")
btnTimer.Initialize("BtnTimer_Tick", 20)
master.Initialize
Connect (0)
End Sub
Sub Connect (Tag As Byte)
Log("Trying to connect")
If mqtt.Connect = False Then
'try to connect again after a second
CallSubPlus("Connect", 1000, 0)
Return
End If
Log("Connected!")
mqtt.Subscribe("arduino", 0)
btnTimer.Enabled = True
End Sub
Sub Mqtt_Disconnected
Log("disconnected")
btnTimer.Enabled = False
Connect(0)
End Sub
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
Log("Message arrived, ", Payload)
master.WriteTo(SlaveAddress, Payload)
End Sub
Sub BtnTimer_Tick
Dim b() As Byte = master.RequestFrom(SlaveAddress, 1)
If b.Length > 0 And b(0) <> 0 Then
'send the message (pressed button) to the android
mqtt.Publish("mobile", b)
End If
End Sub