B4R Question ESP8266 as Webserver

rwblinn

Well-Known Member
Licensed User
Longtime User
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).

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
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't think that implementing a web server on ESP8266 is the best idea. It will be much simpler to use RPi and create a powerful server instead.
The server cannot update connected clients. This is not a websocket server. WebSocketClient library will not help.
Clients will need to send requests every few seconds.

ESP8266 is better suited for lower level network solutions such as MQTT or sockets (B4RSerializator will help with both cases).

You can see an example of a web server here: https://www.b4x.com/android/forum/threads/esp8266-wifi-remote-configuration.68596/#content
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks for clarification and direction = do concur.

It was a thought for a small experiment to show that an ESP can act as a true (and cheap) webserver ... might be in the (near) future (evolving ESP32 etc.) ... but indeed not at the forefront (yet)
 
Upvote 0
Top