Hi,
its not quite clear how to setup the ESP8266 as a mini webserver which updates connected webbrowser clients. Tried following code (snippet to show data to a single webbrowser client) but the ESP8266 resets when connecting via webbrowser = probably because of the WiFiSocket can not handle webbrowser clients. Then tried with WebSocketClient but not sure how to apply (as no path and index.html file).
its not quite clear how to setup the ESP8266 as a mini webserver which updates connected webbrowser clients. Tried following code (snippet to show data to a single webbrowser client) but the ESP8266 resets when connecting via webbrowser = probably because of the WiFiSocket can not handle webbrowser clients. Then tried with WebSocketClient but not sure how to apply (as no path and index.html file).
B4X:
Sub Process_Globals
Public Serial1 As Serial
Private wifi As ESP8266WiFi
Private server As WiFiServerSocket
Private serverport As UInt = 51042
Private astream As AsyncStreams
Private ser As B4RSerializator
Private timer1 As Timer
Private bmp180 As SFE_BMP180 ' ensure to use rSFE_BMP180 v1.10 or higher
Private Temperature As Double
Private Pressure As Double
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
bmp180.Initialize
If wifi.Connect2("SSID", "******") Then
Log("Connected to wireless network with IP: ", wifi.LocalIp)
Else
Log("Failed to connect.")
Return
End If
timer1.Initialize("timer1_Tick", 2000)
timer1.Enabled = True
server.Initialize(serverport, "Server_NewConnection")
server.Listen
End Sub
Sub Timer1_Tick
GetBMP180Data
' UPDATE CONNECTED CLIENTS BUT HOW?
End Sub
Sub GetBMP180Data
If Not(bmp180.GetTemperature) Then
Log("Error retrieving the temperature.")
Else
Dim buffer(8) As Byte
Dim raf As RandomAccessFile
raf.Initialize(buffer, True)
raf.WriteDouble32(bmp180.LastResult, raf.CurrentPosition)
Temperature = bmp180.LastResult
bmp180.GetPressure(0, bmp180.LastResult)
raf.WriteDouble32(bmp180.LastResult, raf.CurrentPosition)
Pressure = bmp180.LastResult
Log("T=", Temperature, "P=", Pressure)
End If
End Sub
Sub Server_NewConnection (NewSocket As WiFiSocket)
Log("New Server Client connected.")
Dim s As String
astream.Initialize(NewSocket.Stream, Null, "AStream_Error")
If server.Socket.Connected Then
'astream.Write(ser.ConvertArrayToBytes(Array("T=", Temperature, "P=", Pressure)))
astream.Write("text/html".GetBytes)
astream.Write("<h1>B4R HowTo ESP8266 Web Server</h1>")
s = Temperature
astream.Write("Temperature:".GetBytes).Write(s).Write("<hr>")
s = Pressure
astream.Write("Pressure:".GetBytes).Write(s).Write("<hr>")
End If
End Sub
Sub AStream_NewData (Buffer() As Byte)
' NOT USED
End Sub
Sub AStream_Error
Log("Error")
server.Listen
End Sub