B4R Question Question about NewSocket.Stream

Cesar_Morisco

Active Member
Talk about weight
All good
I have a question about NewSocket.Stream
Doesn't work, locks app data
I can't receive the data from my application, can anyone give me some guidance?
If I add the second line it works with 50%
B4R:
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
   
    Astream.Initialize(NewSocket.Stream, "astream_NewData","astream_Error")    <<<<<<<<<<< ERROR
    Astream.Initialize(NewSocket.Stream, "astream1_NewData","astream2_Error") <<<<<<<<<<<< OK + -
   
    Astream.Write("<html lang=""en"" class="" "">")
    Astream.Write(  "<head>")
    Astream.Write("    <meta charset='utf-8'><meta name=""viewport"" content=""width=device-width,initial-scale=1,user-scalable=no""/>")
    Astream.Write("    <style>div,fieldset,input,select{padding:5px;font-size:1em;}fieldset{background:#4f4f4f;}p{margin:0.5em 0;}input{width:100%;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;background:#dddddd;color:#000000;}input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px;}input[type=range]{width:99%;}select{width:100%;background:#dddddd;color:#000000;}textarea{resize:none;width:98%;height:318px;padding:5px;overflow:auto;background:#1f1f1f;color:#65c115;}body{text-align:center;font-family:verdana,sans-serif;background:#252525;}td{padding:0px;}button{border:0;border-radius:0.3rem;background:#1fa3ec;color:#faffff;line-height:2.4rem;font-size:1.2rem;width:100%;-webkit-transition-duration:0.4s;transition-duration:0.4s;cursor:pointer;}button:hover{background:#0e70a4;}.bred{background:#d43535;}.bred:hover{background:#931f1f;}.bgrn{background:#47c266;}.bgrn:hover{background:#5aaf6f;}a{color:#1fa3ec;text-decoration:none;}.p{float:left;text-align:left;}.q{float:right;text-align:right;}.r{border-radius:0.3em;padding:2px;margin:6px 2px;}</style>")
    Astream.Write("  </head>")
    Astream.Write("  <body>")
    Astream.Write("    <div style='text-align:left;display:inline-block;color:#eaeaea;min-width:340px;'>")
    Astream.Write("    <div style='text-align:center;color:#eaeaea;'>")
    Dim numberofnetwork As Byte = WiFi.Scan
    For i = 0 To numberofnetwork - 1
    Astream.Write(" <div style='text-align:left;color:#0000FF;'>")
    Astream.Write("Rede: ").Write(Main.wifi.ScannedSSID(i)).Write("")
    Astream.Write("</div>")
    Next
    Astream.Write(" <h3>ESP8266 ESP-12F Modulo</h3>")
    Astream.Write(" </div>")
    Astream.Write(" <fieldset>")
    Astream.Write(" <form action='save' method='get'>")
    Astream.Write(" <div><label For='_nwk'>SSID:</label><br><input name='_nwk' id='_nwk' value='").write(bc.StringFromBytes(GStore1.slot0)).write("'></div>")
    Astream.Write(" <div><label For='_key'>SSID Key:</label><br><input name='_key' id='_key' value='").write(bc.StringFromBytes(GStore1.slot1)).write("'></div>")
    Astream.Write(" <div><label For='_mth'>MQTT Urlm:</label><br><input name='_mth' id='_mth' value='").write(bc.StringFromBytes(GStore1.slot2)).write("'></div>")
    Astream.Write(" <div><label For='_mtp'>MQTT Porta:</label><br><input name='_mtp' id='_mtp' value='").write(bc.StringFromBytes(GStore1.slot3)).write("'></div>")
    Astream.Write(" <div><label For='_mtc'>MQTT ClientID:</label><br><input name='_mtc' id='_mtc' value='").write(bc.StringFromBytes(GStore1.slot4)).write("'></div>")
    Astream.Write(" <div><label For='_mtu'>MQTT Nome:</label><br><input name='_mtu' id='_mtu' value='").write(bc.StringFromBytes(GStore1.slot5)).write("'></div>")
    Astream.Write(" <div><label For='_mtk'>MQTT Senha:</label><br><input name='_mtk' id='_mtk' value='").write(bc.StringFromBytes(GStore1.slot6)).write("'></div>")
    Astream.Write("    <div><br><button name='_save'>Salvar</button></div>")
    Astream.Write("    </form>")
    Astream.Write(" <form action='_abort' method='get'>")
    Astream.Write(" <div><button onclick=""location.href='/';"" name='_clear'>Apagar</button></div>")
    Astream.Write(" </fieldset>")
    Astream.Write(" </body>")
    Astream.Write(" </html>")  
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    If bc.IndexOf(Buffer, "save?_nwk=") <> -1 Then  
        Dim init(8) As Object
         Dim Pt1,Pt2 As Int
        Dim nb As Int = 0
        Dim nkay() As String = Array As String("&_key=","&_mth=","&_mtp=","&_mtc=","&_mtu=","&_mtk=","&_save=")
        Pt1 = bc.IndexOf(Buffer, "save?_nwk=")+10
        For Each n As String In nkay
            Pt2 = bc.IndexOf(Buffer, n)
            Dim temp1() As Byte =bc.substring2(Buffer,Pt1,Pt2)
            temp1 = ReplaceString(temp1,"+".getbytes,"%20".getbytes)  'recovery of Space
            temp1 = convertunicode(temp1)
            bc.ObjectSet(temp1,init(nb))
            Log("Valor Nb= ",init(nb))
            Pt1 = Pt2+6      
            nb = nb+1
        Next
        CloseConnection(0)
        For Each o As Object In init
            Log("Html= ",o)
        Next
        Log("Htmldata:",sr.ConvertArrayToBytes(init))
        Main.SaveNetworkDetails(sr.ConvertArrayToBytes(init))
        CloseConnection(0)
    Else
        If bc.IndexOf(Buffer, "/_abort?_abort=") <> -1 Then
            Log("====> abort input")
            CloseConnection(0)
        Else
            If bc.IndexOf(Buffer, "/_abort?_clear=") <> -1 Then
            Log("====> clear eeprow")
            Main.ClearStoredDataLength
            End If
            CloseConnection(9)
         End If  
    End If  

End Sub

Private Sub Astream1_NewData (Buffer() As Byte)
    'Dim value As String = bc.StringFromBytes(Buffer)
    Log(Buffer)
End Sub

Private Sub CloseConnection(u As Byte)
    If server.Socket.Connected Then
        server.Socket.Stream.Flush
        server.Socket.Close      
    End If
End Sub

Private Sub AStream_Error
    server.Socket.Close
    server.Listen
End Sub

Private Sub AStream2_Error
    server.Socket.Close
    server.Listen
End Sub

Ícone Verificada pela comunidade






Abrir no Google Tradutor

Feedback

Resultado da Web com links de sites​

 

hatzisn

Well-Known Member
Licensed User
Longtime User
Talk about weight
All good
I have a question about NewSocket.Stream
Doesn't work, locks app data
I can't receive the data from my application, can anyone give me some guidance?
If I add the second line it works with 50%
B4R:
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
 
    Astream.Initialize(NewSocket.Stream, "astream_NewData","astream_Error")    <<<<<<<<<<< ERROR
    Astream.Initialize(NewSocket.Stream, "astream1_NewData","astream2_Error") <<<<<<<<<<<< OK + -
 
    Astream.Write("<html lang=""en"" class="" "">")
    Astream.Write(  "<head>")
    Astream.Write("    <meta charset='utf-8'><meta name=""viewport"" content=""width=device-width,initial-scale=1,user-scalable=no""/>")
    Astream.Write("    <style>div,fieldset,input,select{padding:5px;font-size:1em;}fieldset{background:#4f4f4f;}p{margin:0.5em 0;}input{width:100%;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;background:#dddddd;color:#000000;}input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px;}input[type=range]{width:99%;}select{width:100%;background:#dddddd;color:#000000;}textarea{resize:none;width:98%;height:318px;padding:5px;overflow:auto;background:#1f1f1f;color:#65c115;}body{text-align:center;font-family:verdana,sans-serif;background:#252525;}td{padding:0px;}button{border:0;border-radius:0.3rem;background:#1fa3ec;color:#faffff;line-height:2.4rem;font-size:1.2rem;width:100%;-webkit-transition-duration:0.4s;transition-duration:0.4s;cursor:pointer;}button:hover{background:#0e70a4;}.bred{background:#d43535;}.bred:hover{background:#931f1f;}.bgrn{background:#47c266;}.bgrn:hover{background:#5aaf6f;}a{color:#1fa3ec;text-decoration:none;}.p{float:left;text-align:left;}.q{float:right;text-align:right;}.r{border-radius:0.3em;padding:2px;margin:6px 2px;}</style>")
    Astream.Write("  </head>")
    Astream.Write("  <body>")
    Astream.Write("    <div style='text-align:left;display:inline-block;color:#eaeaea;min-width:340px;'>")
    Astream.Write("    <div style='text-align:center;color:#eaeaea;'>")
    Dim numberofnetwork As Byte = WiFi.Scan
    For i = 0 To numberofnetwork - 1
    Astream.Write(" <div style='text-align:left;color:#0000FF;'>")
    Astream.Write("Rede: ").Write(Main.wifi.ScannedSSID(i)).Write("")
    Astream.Write("</div>")
    Next
    Astream.Write(" <h3>ESP8266 ESP-12F Modulo</h3>")
    Astream.Write(" </div>")
    Astream.Write(" <fieldset>")
    Astream.Write(" <form action='save' method='get'>")
    Astream.Write(" <div><label For='_nwk'>SSID:</label><br><input name='_nwk' id='_nwk' value='").write(bc.StringFromBytes(GStore1.slot0)).write("'></div>")
    Astream.Write(" <div><label For='_key'>SSID Key:</label><br><input name='_key' id='_key' value='").write(bc.StringFromBytes(GStore1.slot1)).write("'></div>")
    Astream.Write(" <div><label For='_mth'>MQTT Urlm:</label><br><input name='_mth' id='_mth' value='").write(bc.StringFromBytes(GStore1.slot2)).write("'></div>")
    Astream.Write(" <div><label For='_mtp'>MQTT Porta:</label><br><input name='_mtp' id='_mtp' value='").write(bc.StringFromBytes(GStore1.slot3)).write("'></div>")
    Astream.Write(" <div><label For='_mtc'>MQTT ClientID:</label><br><input name='_mtc' id='_mtc' value='").write(bc.StringFromBytes(GStore1.slot4)).write("'></div>")
    Astream.Write(" <div><label For='_mtu'>MQTT Nome:</label><br><input name='_mtu' id='_mtu' value='").write(bc.StringFromBytes(GStore1.slot5)).write("'></div>")
    Astream.Write(" <div><label For='_mtk'>MQTT Senha:</label><br><input name='_mtk' id='_mtk' value='").write(bc.StringFromBytes(GStore1.slot6)).write("'></div>")
    Astream.Write("    <div><br><button name='_save'>Salvar</button></div>")
    Astream.Write("    </form>")
    Astream.Write(" <form action='_abort' method='get'>")
    Astream.Write(" <div><button onclick=""location.href='/';"" name='_clear'>Apagar</button></div>")
    Astream.Write(" </fieldset>")
    Astream.Write(" </body>")
    Astream.Write(" </html>")
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    If bc.IndexOf(Buffer, "save?_nwk=") <> -1 Then
        Dim init(8) As Object
         Dim Pt1,Pt2 As Int
        Dim nb As Int = 0
        Dim nkay() As String = Array As String("&_key=","&_mth=","&_mtp=","&_mtc=","&_mtu=","&_mtk=","&_save=")
        Pt1 = bc.IndexOf(Buffer, "save?_nwk=")+10
        For Each n As String In nkay
            Pt2 = bc.IndexOf(Buffer, n)
            Dim temp1() As Byte =bc.substring2(Buffer,Pt1,Pt2)
            temp1 = ReplaceString(temp1,"+".getbytes,"%20".getbytes)  'recovery of Space
            temp1 = convertunicode(temp1)
            bc.ObjectSet(temp1,init(nb))
            Log("Valor Nb= ",init(nb))
            Pt1 = Pt2+6    
            nb = nb+1
        Next
        CloseConnection(0)
        For Each o As Object In init
            Log("Html= ",o)
        Next
        Log("Htmldata:",sr.ConvertArrayToBytes(init))
        Main.SaveNetworkDetails(sr.ConvertArrayToBytes(init))
        CloseConnection(0)
    Else
        If bc.IndexOf(Buffer, "/_abort?_abort=") <> -1 Then
            Log("====> abort input")
            CloseConnection(0)
        Else
            If bc.IndexOf(Buffer, "/_abort?_clear=") <> -1 Then
            Log("====> clear eeprow")
            Main.ClearStoredDataLength
            End If
            CloseConnection(9)
         End If
    End If

End Sub

Private Sub Astream1_NewData (Buffer() As Byte)
    'Dim value As String = bc.StringFromBytes(Buffer)
    Log(Buffer)
End Sub

Private Sub CloseConnection(u As Byte)
    If server.Socket.Connected Then
        server.Socket.Stream.Flush
        server.Socket.Close    
    End If
End Sub

Private Sub AStream_Error
    server.Socket.Close
    server.Listen
End Sub

Private Sub AStream2_Error
    server.Socket.Close
    server.Listen
End Sub

View attachment 150084





Abrir no Google Tradutor

Feedback

Resultado da Web com links de sites​


Go to my signature and find the rDebug library in my contributions. Add some break points and this will tell you where does it break.
 
Last edited:
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Also add these two lines in the beggining of the Astream.write... code:

B4X:
    Astream.Write("HTTP/1.1 200").Write(CRLF)
    Astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Ok, what is your problem? Does it continue to do the same?
 
Upvote 0

Cesar_Morisco

Active Member
Hey guys
Can anyone help me resolve this issue?
I can't receive data from my application via tcp WiFiSocket I think and because of the network configuration web.
I have no idea how to solve it.
That's thanks
I am using the code examples
Thread Candide

B4R:
Private Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("new connexion", NewSocket.RemoteIp)
    
    Dim ObjectsBuffer(16) As Object
    Dim SSID, PASS As String
    Dim datas() As Byte = Main.GetStoredData
        Dim ObjectsBuffer(2) As Object
        Dim Objects() As Object = sr.ConvertBytesToArray(datas, ObjectsBuffer)
        SSID = bc.StringFromBytes(Objects(0))
        PASS = bc.StringFromBytes(Objects(1))
        Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
        
        
        Astream.Write("HTTP/1.1 200").Write(CRLF)
        Astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
        Astream.Write("<html lang=""en"" class="" "">")
        Astream.Write(  "<head>")
        Astream.Write("    <meta charset='utf-8'><meta name=""viewport"" content=""width=device-width,initial-scale=1,user-scalable=no""/>")
        Astream.Write("    <style>div,fieldset,input,select{padding:5px;font-size:1em;}fieldset{background:#4f4f4f;}p{margin:0.5em 0;}input{width:100%;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;background:#dddddd;color:#000000;}input[type=checkbox],input[type=radio]{width:1em;margin-right:6px;vertical-align:-1px;}input[type=range]{width:99%;}select{width:100%;background:#dddddd;color:#000000;}textarea{resize:none;width:98%;height:318px;padding:5px;overflow:auto;background:#1f1f1f;color:#65c115;}body{text-align:center;font-family:verdana,sans-serif;background:#252525;}td{padding:0px;}button{border:0;border-radius:0.3rem;background:#1fa3ec;color:#faffff;line-height:2.4rem;font-size:1.2rem;width:100%;-webkit-transition-duration:0.4s;transition-duration:0.4s;cursor:pointer;}button:hover{background:#0e70a4;}.bred{background:#d43535;}.bred:hover{background:#931f1f;}.bgrn{background:#47c266;}.bgrn:hover{background:#5aaf6f;}a{color:#1fa3ec;text-decoration:none;}.p{float:left;text-align:left;}.q{float:right;text-align:right;}.r{border-radius:0.3em;padding:2px;margin:6px 2px;}</style>")
        Astream.Write("  </head>")
        Astream.Write("  <body>")
        Astream.Write("    <div style='text-align:left;display:inline-block;color:#eaeaea;min-width:340px;'>")
        Astream.Write("    <div style='text-align:center;color:#eaeaea;'>")
        Astream.Write("      <h3>ESP8266 ESP-12F Module</h3>")
        Astream.Write("    </div>")
        Astream.Write("  <fieldset>")
        Astream.Write(" <form action='save' method='get'>")
        Astream.Write(" <div><label For='_nwk'>SSID</label><br><input name='_nwk' id='_nwk' value='").write(SSID).write("'></div>")
        Astream.Write(" <div><label For='_key'>SSID Key</label><br><input name='_key' id='_key' value='").write(PASS).write("'></div>")
        Astream.Write("    <div><br><button name='_save'>Save</button></div>")
        Astream.Write("    </form>")
        Astream.Write("<form action='_abort' method='get'>")
        Astream.Write("<div><button  name='_clear'   >Clear eeprom</button></div>")
        Astream.Write("<div><br><button  name='_reset'   >Reset esp</button></div>")
        Astream.Write("</form>")
        Astream.Write("<button onclick=""location.href='/';"">Reload data</button>")
        Astream.Write("</fieldset>")
        Astream.Write(" </body>")
        Astream.Write("</html>")
        Astream.Write("HTTP/1.1 200 OK").Write(CRLF).Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
End    Sub

Private Sub Astream_NewData (Buffer() As Byte)
    
    CallSubPlus("CloseConnection",250,0)
    If bc.IndexOf(Buffer,"Connection:".GetBytes) > -1 Then
        Dim buff() As Byte = bc.SubString2(Buffer,0,bc.IndexOf(Buffer,"Connection:".GetBytes))
    Else
        Dim buff() As Byte
    End If         
        If bc.IndexOf(buff, "save?_nwk=") <> -1 Then   
            Log("====> save?_nwk=")
            Dim init(2) As Object
             Dim pt1,pt2 As Int             
            Dim nb As Int = 0
            Dim nkay() As String = Array As String("&_key=","&_save=")
            pt1 = bc.IndexOf(buff, "save?_nwk=")+10
            
            For Each n As String In nkay
                pt2 = bc.IndexOf(buff, n)
                Dim temp1() As Byte =bc.substring2(buff,pt1,pt2)
                    temp1 = ReplaceString(temp1,"+".getbytes,"%20".getbytes)  'recovery of Space
                    temp1 = convertunicode(temp1)                           
                bc.ObjectSet(temp1, init(nb))
                pt1 = pt2+6       
                nb = nb +1
            Next
            Main.SaveNetworkDetails(sr.ConvertArrayToBytes(init))
        Else
            If bc.IndexOf(buff, "/_abort?_reset=") <> -1 Then
                Log("====> Reset esp")
                Main.esp_reset(0)   
            Else
                If bc.IndexOf(buff, "/_abort?_clear=") <> -1 Then
                Log("====> clear eeprow")
                Main.ClearStoredDataLength
            End If
         End If
    End If   

End Sub
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Have a look at my signature in my contributions. Go to my signature in tutorials and find in B4R section the "full responsive B4R webapp with web sockets". Read the tutorial and download the code changing it according to the tutorial. I manipulated the existing code to create something new. You can do it too.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Remove also the last line after Async.Write("</html>")
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Also move <fieldset> and it's closing tag inside form.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Although it is unlikely to be responsible add also async.write("CRLF) in all html lines and why do you close connection on async_newdata?
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Move also Dim buff() as byte outside if clause and just set it inside if
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Also what you do with buttons in _abort is wrong. Use a hidden field which you will set with JavaScript with the buttons and check the value of the hidden field.
 
Upvote 0

hatzisn

Well-Known Member
Licensed User
Longtime User
Also what you do with buttons in _abort is wrong. Use a hidden field which you will set with JavaScript with the buttons and check the value of the hidden field.

Now I understood what you were doing. Anyway. I corrected the html you can send, and added bootstrap.
Check the attached file. Tell us if it works or, if not, post the error message.
 

Attachments

  • cesarmorisco.zip
    1.3 KB · Views: 33
Last edited:
Upvote 0

Cesar_Morisco

Active Member
Now I understood what you were doing. Anyway. I corrected the html you can send, and added bootstrap.
Check the attached file. Tell us if it works or, if not, post the error message.
Hello budy, how are you
for your help.
It worked here
This was a problem"CallSubPlus("CloseConnection", 200, 0)"I was closing the terminal
Like I said I'm starting with these tools now
Thank you very much from the bottom of my heart
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Also be ware that the astream.newdata buffer has a default value of only 100 bytes
You may need to increase this in order to get the data in a single even, instead of parts of it in multiple events.

Olá Cesar, o aStream.NewData them um buffer com um valor por defeito de 100 bytes, o que é muito baixo.
Eu tive que aumentar o meu para 800 bytes (num esp32) para poder receger os Dados num só evento em vez de repartido em varios pedaços.

 
Last edited:
Upvote 0
Top