B4R Question Simple Web Server - AStream_Error always triggered, why?

Discussion in 'B4R Questions' started by miker2069, Aug 11, 2017.

  1. miker2069

    miker2069 Member Licensed User

    I'm trying to implement a simple web-server using NodeMCU. It's very simple, one connection at any time. I'm porting a project from regular arduino C to B4R. So right now just a simple, receive some data and send a response. With the following code, it all seems to work with the exception that AStream_Error event is always triggered for every connection. I can get data and respond okay but every time AStream_Error is triggered. I figured I could use LastException but that doesn't seem like it's availble in B4R - I am using v2.2. What am I missing?

    I cobbled together this example from the forum and examples...thank you for any insight.

    Private Sub AppStart
    Dim ssid As String
    Dim password As String
        ssid = 
        password = 
    14, pin5.MODE_OUTPUT)
    If wifi.Connect2(ssid,password) Then 'change to your network SSID (use Connect2 if a password is required).
            Log("Connected to wireless network."  + ssid)
    Log("My ip: ", wifi.LocalIp)
    Log("Failed to connect.")
    End If
    'Timer1.Initialize("Timer1_Tick", 1000) '1000ms = 1 second
       'Timer1.Enabled = True
    End Sub

    Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")

    End Sub

    Sub AStream_NewData (Buffer() As Byte)
    Log("Received from server: ", Buffer)
    'astream.Write("HTTP/1.1 200 OK").write(CRLF)
        'astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
        'astream.Write("<b>Hello World</b><br/><a href=""https://www.b4x.com"">B4X</a>")
    Dim bc As ByteConverter
    Dim s As String
        s = 
    "HTTP/1.1 200 OK\r\n"
    Dim bytesa() As Byte
        bytesa = bc.ObjectToBytes(s,s.Length)
        s = 
    "Content-Type: text/html\r\n"
        bytesa = bc.ObjectToBytes(s,s.Length)
    '    Dim bc As ByteConverter
    'Dim data As String
    'data = bc.StringFromBytes(Buffer)
    'Dim be(10) As Object
        'Dim data() As Object = ser.ConvertBytesToArray(Buffer,be)
    'Log("-------->Received:" + data)
        'For Each o As Object In data
        '    Log(o)
    End Sub

    Sub CloseConnection(u As Byte)
    End Sub

    Sub AStream_Error

    End Sub
  2. Erel

    Erel Administrator Staff Member Licensed User

    1. You don't need to use bc.ObjectTypeBytes to convert a string to an array of bytes.
    2. There is no \r\n in B4R. Use CRLF instead.

    AStream_Error will be raised when the connection is closed.

    Why are you building a web server? Do you want to access it from the browser?
  3. miker2069

    miker2069 Member Licensed User

    I was also using:

    astream.Write("HTTP/1.1 200 OK").write(CRLF)
    astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)

    As well - my main question is that Astream_Error seemed to get triggered all the time. Which you answered it's called on closing the connection, which is fine, I can just ignore it :)

    I'm not trying to necessarily communicate with a browser (I know that's problematic since browsers can make multiple simultaneous connections to the ESP8266 which would cause issues). It was more proof of concept as I am porting over a couple Arduino projects to B4R and wanted to reproduce the current http connection approach I was using. I'm definitely going to use something much lighter since I really don't need it to use the http protocol.

    Thank You for your help!
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. miker2069

    miker2069 Member Licensed User

    Erel likes this.