I am new to B4R, so please excuse my mistakes.
I would be very gratefull for some help with this code. It sort of works but I have to press the button twice to turn on or off the LED.
Many thanks
I would be very gratefull for some help with this code. It sort of works but I have to press the button twice to turn on or off the LED.
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 1200
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
'Wemos D1 R2
' Pin GPIO Function
'____ ______ ___________
' D0 GPIO16
' D1 GPIO5 SCL
' D2 GPIO4 SDA
' D3 GPIO0
' D4 GPIO2 BUILTIN_LED
' D5 GPIO14 SCK
' D6 GPIO12 MISO
' D7 GPIO13 MOSI
' D8 GPIO15
' A0 A0
Private bc As ByteConverter
Private Serial1 As Serial
Private wifi As ESP8266WiFi
Private server As WiFiServerSocket
Private astream As AsyncStreams
Private LEDPin As Pin
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Log(wifi.StartAccessPoint("Wemos_Server"))
Log(wifi.AccessPointIp)
RunNative("SetAP", Null)
Log(wifi.AccessPointIp)
Log(wifi.LocalIp)
server.Initialize(80, "server_NewConnection")
LEDPin.Initialize(2, LEDPin.MODE_OUTPUT)
server.Listen
End Sub
#if C
void SetAP(B4R::Object* o) {
WiFi.mode(WIFI_AP);
}
#end if
Sub Server_NewConnection (NewSocket As WiFiSocket)
astream.Initialize( NewSocket.Stream, "astream_NewData", "astream_Error")
Log("New connection..")
'DisplayWebpage
End Sub
Sub astream_NewData (Buffer() As Byte)
Log("new data: ", Buffer)
If bc.IndexOf(Buffer, "GET") <> -1 Then 'Browser opened and logged in
End If
If bc.IndexOf(Buffer, "LEDON") <> -1 Then
LEDPin.DigitalWrite(False)
Else if bc.IndexOf(Buffer, "LEDOFF") <> -1 Then
LEDPin.DigitalWrite(True)
End If
DisplayWebpage
End Sub
Sub DisplayWebpage
Log("Sending webpage")
Dim html1, html2, html3 As String
'Prepare default strings for webpage
html1="<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width, initial-scale=1.0'/><meta charset='utf-8'><style>body {font-size:140%;} #main {display: table; margin: auto; padding: 0 10px 0 10px; } h2,{text-align:center; } .button { padding:10px 10px 10px 10px; width:100%; background-color: #4CAF50; font-size: 120%;}</style><title>LED Control</title></head><body><div id='main'><h2>LED Control</h2>"
html2=""
html3="</div></body></html>"
Dim LEDStatus As Boolean=LEDPin.DigitalRead
If LEDStatus=True Then
' the LED is off so the button needs To say turn it on
html2 = "<form id='F1' action='LEDON'><input class='button' type='submit' value='Turn on the LED' ></form><br>"
Else
' the LED is on so the button needs To say turn it off
html2 = "<form id='F1' action='LEDOFF'><input class='button' type='submit' value='Turn off the LED' ></form><br>"
End If
Log("***********************************************************")
Log(html1)
Log(html2)
Log(html3)
Log("***********************************************************")
astream.Write("HTTP/1.1 200").Write(CRLF).Write("Content-Type: text/html").Write(CRLF).Write(CRLF).Write(html1).Write(CRLF).Write(html2).Write(CRLF).Write(html3).Write(CRLF)
'astream.Write("HTTP/1.1 200 OK").Write(CRLF).Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
'astream.Write(html1).Write(CRLF)
'astream.Write(html2).Write(CRLF)
'astream.Write(html3).Write(CRLF)
CallSubPlus("CloseConnection", 200, 0)
End Sub
Sub astream_Error
Log("Astream error")
server.Listen
End Sub
Private Sub CloseConnection(u As Byte)
Log("Close connection")
If server.Socket.Connected Then
server.Socket.Stream.Flush
server.Socket.Close
End If
End Sub
Many thanks