B4J Question ESP32 listener

Mostez

Well-Known Member
Licensed User
Longtime User
I want to build a listener or service that is always up, to receive simple text query/command from ESP32 board, then, that B4J listener exec this query or command and send a text reply back to ESP32 board. this service is running under windows, both computer and ESP32 are sharing the same wi-fi network.

for example, ESP sends letter 'T' to B4J listener, then B4J replies with current date and time of PC.

Your guide lines are appreciated.
TIA
 
Solution
point to any available code example or link

peacemaker

Expert
Licensed User
Longtime User
  • Web-server on Windows part under B4J: server's WebSocket app
  • WebSocket client in ESP32 B4R sketch
as a version...
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Take a look to this example, it could help you. It’s made with mqtt

 
Upvote 0

Mostez

Well-Known Member
Licensed User
Longtime User
Thank you all for help, I don't know which of them is better than the other, but I believe that B4J seems to be simpler and powerful too, I can make SQL server calls, access file, etc..
  • Web-server on Windows part under B4J: server's WebSocket app
  • WebSocket client in ESP32 B4R sketch

would you please point to any available code example or link?
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
point to any available code example or link
 
Upvote 0
Solution

Mostez

Well-Known Member
Licensed User
Longtime User
in this simple example ESP sends "T" command to server, if command matched then the server replies with date and time

B4R:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi   
    Private Client As WiFiSocket
    Dim WebSocket As WebSocketClient
    Private serverIp() As Byte = Array As Byte(192, 168, 1, 138)
    Private const serverPort As UInt = 51042
    Private path As String = "/ESP"   
    Private dt As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    dt.Initialize("dt_tick",1000)
    dt.Enabled = True
    Log("AppStart")
    RunNative( "SetIP" , Null )

    If wifi.Connect2("xxx","xxx") Then
        Log("Connected to wireless network.")
        Log("My ip: ", wifi.LocalIp)
    Else
        Log("Failed to connect to network")
        Return
    End If
    
    Delay(1000)   
    WebSocket.Initialize("websocket_NewMessage", "websocket_Disconnected")
    Connect(0)

End Sub

Sub Connect(unused As Byte)
    If WebSocket.ConnectIp(Client.Stream, serverIp, serverPort, path) Then
        Log("Connected...")
    Else
        Log("Connection failed")
        CallSubPlus("Connect", 1000, 0)
    End If
End Sub

Sub WebSocket_NewMessage (FunctionName As String, Params() As String)
    Select FunctionName
        Case "ServerTime"
            Log("Server time: ", Params(0))
    End Select
End Sub

Sub WebSocket_Disconnected
    Log("Disconnected")
    CallSubPlus("Connect", 1000, 0)
End Sub

private Sub dt_tick()
    If Client.Connected Then
        Log("Command sent")
        'raise GetESP_Message event in ESP class (on server side) every 1 sec and send CMD (command) with parameter T
        WebSocket.SendEventToServer("GetESP_Message", Array As String("CMD","T"))
    End If
End Sub

#if C
  void SetIP(B4R::Object* o) {
  WiFi.mode(WIFI_STA);
  IPAddress ip(192, 168, 1, 231);
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);
  }
  #end if

B4J Main:
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    Private srvr As Server   
    Dim MyESP As ESP
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("")
    srvr.Port = 51042
    srvr.AddWebSocket("/ESP", "ESP") 'dont forget to include class
    srvr.Start
    MyESP.Initialize
    StartMessageLoop
    
End Sub

B4J ESP Class:
Sub Class_Globals
    Private ws As WebSocket
    Private connected As Boolean 'not used so far
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub

Private Sub WebSocket_Connected (WebSocket1 As WebSocket)
    ws = WebSocket1
    connected = True
    Log("ESP Connected")
End Sub

Private Sub WebSocket_Disconnected
    connected = False
    Log("ESP Disconnected")
End Sub

Sub GetESP_Message (Params As Map)
    Dim cmd As String = Params.Get("CMD") 'get command and parameter
    Log("Recieved: " & cmd)   
    Select cmd.ToUpperCase
        Case "T" ' if T parameter, then reply with date and time
            ws.RunFunction("ServerTime", Array As String(DateTime.Time(DateTime.Now)))
    End Select

    ws.Flush
End Sub
 
Upvote 0
Top