B4R Question read web page on www

Tayfur

Well-Known Member
Licensed User
Generally speaking I recommend using a Raspberry Pi if you need to interact with web pages. It will be simpler and much more powerful.

Example of sending a http request: https://www.b4x.com/android/forum/threads/ethernet-client-server.66038/#post-418138

The result will be available in the NewData event.


Hi @Erel;

I added your sample code in my code. But I cant read any data from web page.!!

Run code for web example.com

192.168.1.7/xxx

B4X:
Sub Process_Globals
    Private bc As ByteConverter
    Private Astream As AsyncStreams  
    Private server As WiFiServerSocket
    Private eeprom As EEPROM
    Private const MAGIC_EEPROM As Byte = 123
    Private ethClient As EthernetSocket
End Sub

Public Sub Start
    Log("StartAP: ", Main.WiFi.StartAccessPoint("esp"))
    Log("My AP ip: ", Main.WiFi.AccessPointIp)
    server.Initialize(80, "server_NewConnection")
    server.Listen
  
End Sub

Sub Connect(unused As Byte)
    Log("***********CONNECT*************")
   If ethClient.ConnectHost("example.com", 80) = False Then
     Log("trying to connect again")
     CallSubPlus("Connect", 1000, 0)
     Return
   End If
   Log("Connected to server")
   Astream.Initialize(ethClient.Stream, "Astream_NewData", "Astream_Error")
   Dim eol() As Byte = Array As Byte(13, 10)
   Astream.Write("GET / HTTP/1.1".GetBytes)
   Astream.Write(eol)
  Astream.Write("Host: www.example.com".GetBytes)
   Astream.Write(eol)
  Astream.Write("Connection: close".GetBytes)
   Astream.Write(eol)
   Astream.Write(eol)
   Log("***********CONNECT COMPLADE*************")
End Sub

Private Sub Server_NewConnection (NewSocket As WiFiSocket)
    Astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub

Private Sub Astream_NewData (Buffer() As Byte)
    Log("****data geldi*****")
    Log(Buffer)
    Log("****buffer end*****")
    If bc.IndexOf(Buffer, "GET") <> -1 Then
        If bc.IndexOf(Buffer, "/set") <> -1 Then
          
            Dim ssid = "", password  = "" As String
            Dim i1 As Int = 0
            Dim i2 As Int = 0
            For Each b1() As Byte In bc.Split(Buffer, " ")
                If i1 = 1 Then
                    For Each b2() As Byte In bc.Split(b1, "/")
                        Select i2
                            Case 2
                                ssid = bc.StringFromBytes(b2)
                            Case 3
                                password = bc.StringFromBytes(b2)
                        End Select
                        i2 = i2 + 1
                    Next
                  
                End If
                i1 = i1 + 1
            Next
            Log(StackBufferUsage)
            Astream.Write("HTTP/1.1 200").Write(CRLF)
            Astream.Write("Content-Type: text/html").Write(CRLF).Write(CRLF)
            Astream.Write("<script>setTimeout(function(){location.href=""http://192.168.4.1""} , 20000);</script>")
            Astream.Write("WiFi set to: ").Write(ssid).Write(", password: ").Write(password).Write("<br/>Please wait...")
            Main.SaveNetworkDetails(ssid, password)
            CallSubPlus("ConnectWifi", 500, 0)
        '-----------------------------------------------------
        Else If bc.IndexOf(Buffer, "/xxx ") <> -1 Then
            Astream.Write("HTTP/1.1 200").Write(CRLF).Write(CRLF)
            If Main.WiFi.IsConnected Then
                If Main.ww Then
                    Astream.Write("DENEME BAŞARILI").Write(CRLF)
                    Astream.Write("Kartek tube bağlanacak").Write(CRLF)
                    'Astream.Write("ESP8266 IP address: ").Write(Main.WiFi.LocalIp)
                    Log("***************KARTEK BAĞLANACAK********************************")
                    Main.ww=False
                    Connect(1)
                Else
                    Log("******* example waiting*********")
                End If
            Else
                Astream.Write("Not connected!")
            End If
        '---------------------------------------------  
        '-----------------WIFI Şİ------------------------------------
        Else If bc.IndexOf(Buffer, "/reset ") <> -1 Then
            Astream.Write("HTTP/1.1 200").Write(CRLF).Write(CRLF)
            If Main.WiFi.IsConnected Then
                Astream.Write("sistem resetlendi...").Write(CRLF)
                eeprom.WriteBytes("000", 0)
            Else
                Astream.Write("Not connected!")
            End If
        '---------------------------------------------  
        Else If bc.IndexOf(Buffer, " / ") <> -1 Then
            Astream.Write("HTTP/1.1 200").Write(CRLF).Write(CRLF)
            If Main.WiFi.IsConnected Then
                Astream.Write("Connected to network.").Write(CRLF)
                Astream.Write("ESP8266 IP address: ").Write(Main.WiFi.LocalIp)
            Else
                Astream.Write("Not connected!")
            End If
        Else
            Astream.Write("HTTP/1.1 404").Write(CRLF)
        End If
        CallSubPlus("CloseConnection", 200, 0)
    End If
End Sub

Private Sub ConnectWifi(u As Byte)
    Main.ConnectToNetwork
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

Private Sub AStream_Error
    Log("Disconnected")
    server.Listen
End Sub

Public Sub ReplaceString(Original() As Byte, SearchFor() As Byte, ReplaceWith() As Byte) As Byte()
    'count number of occurrences
    Dim bc2 As ByteConverter
    Dim c As Int = 0
    Dim i As Int
    If SearchFor.Length <> ReplaceWith.Length Then
        i = bc2.IndexOf(Original, SearchFor)
        Do While i > -1
            c = c + 1
            i = bc2.IndexOf2(Original, SearchFor, i + SearchFor.Length)
        Loop
    End If
    Dim result(Original.Length + c * (ReplaceWith.Length - SearchFor.Length)) As Byte
    Dim prevIndex As Int = 0
    Dim targetIndex As Int = 0
    i = bc2.IndexOf(Original, SearchFor)
    Do While i > -1
        bc2.ArrayCopy2(Original, prevIndex, result, targetIndex, i - prevIndex)
        targetIndex = targetIndex + i - prevIndex
        bc2.ArrayCopy2(ReplaceWith, 0, result, targetIndex, ReplaceWith.Length)
        targetIndex = targetIndex + ReplaceWith.Length
        prevIndex = i + SearchFor.Length
        i = bc2.IndexOf2(Original, SearchFor, prevIndex)
    Loop
    If prevIndex < Original.Length Then
        bc2.ArrayCopy2(Original, prevIndex, result, targetIndex, Original.Length - prevIndex)
    End If
    Return result
End Sub

LOgs
****data geldi*****
e-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko
****buffer end*****
close connection
Disconnected
****data geldi*****
GET /xxx HTTP/1.1
Host: 192.168.1.7
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Age
****buffer end*****
***************KARTEK BAĞLANACAK********************************
***********CONNECT*************
Connected to server
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
Disconnected
***********CONNECT COMPLADE*************
****data geldi*****
GET /xxx HTTP/1.1
Host: 192.168.1.7
Connection: keep-alive
Upgrade-Insecure-Requests: 1
User-Age
****buffer end*****
******* example waiting*********
****data geldi*****
nt: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116
****buffer end*****
close connection
Disconnected
close connection
 
Upvote 0

Tayfur

Well-Known Member
Licensed User
Last edited:
Upvote 0
Top