Sub Process_Globals
Public Serial1 As Serial
Public WiFi As ESP8266WiFi
Private Astream As AsyncStreams
Private server As WiFiServerSocket
Private bc As ByteConverter
Private Pin14 As Pin
Private Pin1 As Pin
End Sub
Sub AppStart
Serial1.Initialize(115200)
Pin14.Initialize(14, Pin14.MODE_OUTPUT) ': Pin14.DigitalWrite(True)
Pin1.Initialize(1, Pin1.MODE_OUTPUT) ': Pin1.DigitalWrite(True)
WiFi.Connect2("RNIC", "*****")
Log("Connected to: ", "RNIC, IP: ", WiFi.LocalIp)
server.Initialize(80, "server_NewConnection")
server.Listen
End Sub
Sub Server_NewConnection (NewSocket As WiFiSocket)
Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub
Public Sub Astream_NewData (Buffer() As Byte)
Log(Buffer)
If bc.IndexOf(Buffer, "/set") <> -1 Then
Dim Result As String
If bc.IndexOf(Buffer, "/set/1/0") <> -1 Then
Pin1.DigitalWrite(False)
Result = Pin1.DigitalRead
End If
If bc.IndexOf(Buffer, "/set/1/1") <> -1 Then
Pin1.DigitalWrite(True)
Result = Pin1.DigitalRead
End If
If bc.IndexOf(Buffer, "/set/14/0") <> -1 Then
Pin14.DigitalWrite(False)
Result = Pin14.DigitalRead
End If
If bc.IndexOf(Buffer, "/set/14/1") <> -1 Then
Pin14.DigitalWrite(True)
Result = Pin14.DigitalRead
End If
Astream.Write("SET:").Write(Result)
End If
If bc.IndexOf(Buffer, "/status") <> -1 Then
Dim p1 As String = Pin1.DigitalRead
Dim p14 As String = Pin14.DigitalRead
Astream.Write("Pin 1: ").Write(p1).Write(CRLF)
Astream.Write("Pin 14: ").Write(p14).Write(CRLF)
Astream.Write("Temperature: ").Write(t).Write(CRLF)
Astream.Write("Humidity: ").Write(h)
End If
CallSubPlus("CloseConnection", 200, 0)
End Sub
Sub CloseConnection(u As Byte)
If server.Socket.Connected Then
server.Socket.Stream.Flush
server.Socket.Close
End If
End Sub
Sub AStream_Error
server.Listen
End Sub