Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private Client As WiFiSocket
Dim WebSocket As WebSocketClient
Private serverIp() As Byte = Array As Byte(192, 168, 1, 138)
Private const serverPort As UInt = 51042
Private path As String = "/ESP"
Private dt As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
dt.Initialize("dt_tick",1000)
dt.Enabled = True
Log("AppStart")
RunNative( "SetIP" , Null )
If wifi.Connect2("xxx","xxx") Then
Log("Connected to wireless network.")
Log("My ip: ", wifi.LocalIp)
Else
Log("Failed to connect to network")
Return
End If
Delay(1000)
WebSocket.Initialize("websocket_NewMessage", "websocket_Disconnected")
Connect(0)
End Sub
Sub Connect(unused As Byte)
If WebSocket.ConnectIp(Client.Stream, serverIp, serverPort, path) Then
Log("Connected...")
Else
Log("Connection failed")
CallSubPlus("Connect", 1000, 0)
End If
End Sub
Sub WebSocket_NewMessage (FunctionName As String, Params() As String)
Select FunctionName
Case "ServerTime"
Log("Server time: ", Params(0))
End Select
End Sub
Sub WebSocket_Disconnected
Log("Disconnected")
CallSubPlus("Connect", 1000, 0)
End Sub
private Sub dt_tick()
If Client.Connected Then
Log("Command sent")
'raise GetESP_Message event in ESP class (on server side) every 1 sec and send CMD (command) with parameter T
WebSocket.SendEventToServer("GetESP_Message", Array As String("CMD","T"))
End If
End Sub
#if C
void SetIP(B4R::Object* o) {
WiFi.mode(WIFI_STA);
IPAddress ip(192, 168, 1, 231);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);
}
#end if