B4R Question websocket to webserverq web site

yaniv hanya

Active Member
Licensed User
i"m trying to build an esp32 app that will get order with websocket from a WWW located site.

i saw the toturial of websocket, that has a mac address and local IP.

what should i change so that the sockect will connect to the internet? preferably a URL?
 

Daestrum

Expert
Licensed User
Longtime User
Not 100% certain but a quick look (not tried it) you need to do something like the following. (The server side must be specially crafted as we dont want webpages sent to us)
B4X:
Dim wsc As WebSocketClient
Dim st As Stream
...
Private Sub AppStart
...
'Connect to wifi as normal ssd and pword etc
...
    wsc.Initialize("incoming","loggedOff")  ' routines to handle the data we get or handle disconnection
    ' set up with stream, web address, the port, the path for the server handler
    wsc.ConnectHost(st,"www.myLittleServer.com",80,"/thingy1")    ' connect to the webserver
    ' set up some data we want to send
    Dim datamap() As String = Array As String("key1","data1")
    ' send it to the server
    wsc.SendEventToServer("_getSomething",datamap)  ' run the routine _getSomething on the server with the supplied datamap.
...
End Sub

' handle the server talking to us
Sub incoming (FunctionName As String, Params() As String)
    ' I guess FunctionName is the routine we call in our app with the data supplied (Params)
     ' do something with FunctionName - possible
     ...
End Sub

' handle being disconnected
Sub loggedOff
    Log("disconnected")
End Sub

Of course this could all be wrong - I am sure someone can correct it.
 
Upvote 0

yaniv hanya

Active Member
Licensed User
i can't make it work. i get an error messages-
i tried port 80 and 443...

Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d2f8d PS : 0x00060a30 A0 : 0x800d2555 A1 : 0x3ffb1f00


....
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Sorry, maybe it needs Erel to cast his eye over it.

It could be that it's only able to interact with a B4J server as you can control exactly what is sent in response. Plus it mentions running a sub on the server, which sort of implies it's a B4J server.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
ok - got them talking now
B4R code
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Dim wsc As WebSocketClient
    Dim st As WiFiServerSocket
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    'example of connecting to a local network
    If wifi.Connect2("******", "*******") Then
        Log("Connected to network")
    Else
        Log("Failed to connect to network")
    End If

    wsc.Initialize("incoming","loggedOff")  ' routines to handle the data we get or handle disconnection
    Log("after init")
    connect(0)
    Log("after host connect")
    ' set up some data we want to send
    Dim datamap() As String = Array As String("key1","data1") ' can use this or just use Array As String(...)
    Log("after datamap")
    ' send it to the server
     ' the function to run on server MUST have an underscore in name   XXXXX_YYYYYY one at the start like _fred won't work
    wsc.SendEventToServer("hello_there",Array As String("key1","value1"))  ' run the sub hello_there on the server with the supplied datamap.
    Log("after send to server")
End Sub

Sub connect(unused As Byte)
    ' the sneaky st.Socket.Stream caught me out - stop hiding things so deep   lol
    If wsc.ConnectHost(st.Socket.Stream,"192.168.0.12" , 51042, "/test") Then   ' must be real address of server
        Log("Connected...")
    Else
        Log("Connection failed")
        CallSubPlus("Connect", 1000, 0)
    End If
End Sub

public Sub incoming (FunctionName As String, Params() As String)

' handle the server talking to us

    ' I guess FunctionName is the routine we call in our app with the data supplied (Params)
    ' do something with FunctionName  eg if FunctionName = "doThis" then doThis(Params)

    Log(FunctionName)
    Log(Params(0))
End Sub

' handle being disconnected
public Sub loggedOff

'Sub loggedOff
    Log("disconnected")
End Sub

B4J Code for socket - pretty standard ws handler
B4X:
'Class module
Sub Class_Globals
    Private ws As WebSocket
End Sub

Public Sub Initialize

End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    Log("Arduino connected")
    Main.tme = Me ' not sure what this does ???
End Sub

public Sub hello_there (data As Map)
    Log("in hello")
    Log(data)
    ' what we send to the arduino - FunctionName then the data
    ws.RunFunction("servTalk", Array As String("arduino_led", "True"))
    ws.Flush
End Sub

Private Sub WebSocket_Disconnected
    Log("Arduino disconnected")
End Sub

B4J main server code
B4X:
Sub Process_Globals
    Private srvr As Server
    Public tme As testMe
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("srvr")
    srvr.Port = 51042
    srvr.AddWebSocket("/test", "testMe")
    srvr.Start
    tme.Initialize
    StartMessageLoop
End Sub

log from B4R


Log from server
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…