#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 3000
#End Region
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
Public wifi As ESP8266WiFi
Public Astream As AsyncStreams
Public server As WiFiServerSocket
Public st As ULong
End Sub
Private Sub AppStart
st=Millis
Serial1.Initialize(115200)
Log("AppStart")
Log("after start time: ",Millis-st)
RunNative("SetSTA", Null)
Log("Before connect time: ",Millis-st)
If Not(wifi.IsConnected) Then wifi.Connect2("xyz","12345678")
Log("After connect time: ",Millis-st)
Log("IP: ",wifi.LocalIp)
server.Initialize(80, "Server_NewConnection")
server.Listen
Log("After server start time: ",Millis-st)
End Sub
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
Log("NEW server CONNECTION ********** at: ", Millis-st)
Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
Astream.MaxBufferSize=1000
End Sub
Private Sub Astream_NewData (Buffer() As Byte)
Log("Astream Data -------------------------------------- ", Millis-st)
Log("Length: ",Buffer.Length,CRLF)
Log(Buffer)
Log(CRLF)
Log("Time: ",Millis-st)
Astream.Write("HTTP/1.1 200").Write(CRLF)
Astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
Astream.Write("Hello World").Write(CRLF)
CallSubPlus("CloseConnection",200,0)
End Sub
Private Sub AStream_Error
Log("Disconnected at Astream Error sub")
server.Listen
End Sub
Private Sub CloseConnection(u As Byte)
Log("close connection sub at: ", Millis-st)
If server.Socket.Connected Then
Log("server was connected")
server.Socket.Stream.Flush
server.Socket.Close
Else
Log("server was not connected")
End If
End Sub
#if C
void SetSTA(B4R::Object* o) {
WiFi.mode(WIFI_STA);
}
#end if