Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Private WiFi As ESP8266WiFi
Private mqtt As MqttClient
Private ServerIP() As Byte = Array As Byte(192, 168, 1, 111)
Private const ServerPort As UInt = 1883
Private client As WiFiSocket
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
ConnectToNetwork
mqtt.Initialize(client.Stream, ServerIP,ServerPort, "huzzah", "Mqtt_MessageArrived", "Mqtt_Disconnected")
Connect(0)
End Sub
Public Sub ConnectToNetwork
WiFi.Disconnect
Dim SSID As String = "ITM"
Dim Password As String = "400westwhitmandrive"
Log("Trying to connect to: ", SSID, " password: ", Password)
If WiFi.Connect2(SSID, Password) Then
Log("Connected successfully to: ", SSID)
Log(WiFi.LocalIp)
Else
Log("Failed to connect.")
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")
Dim pub As String = JoinStrings(Array As String("{","test","}"))
mqtt.Publish("dev/test",pub.GetBytes)
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