B4R Question Two ESP8266 Boards Client&Server sending and receiving a udp packet ?

KiloBravo

Active Member
Licensed User
Hi, looked thru a bunch of the examples but still can't find a simple example of a server and client (both esp8266 boards) receiving a udp packet.
I would like to set up one board as the server, then have the other board as the client. The client just needs to send a udp packet of 10 bytes in length.
I did get the example Erel had of the two boards using tcp-ip, but I can't figure out how to modify them to send and receive a udp packet.
Any help would be appreciated.
Thanks !
 

KiloBravo

Active Member
Licensed User
OK, I beginning to understand what I didn't understand. I was trying to have one esp8266 board be a server in AP mode. Then in the absence of any other router/network connection have the second board be in station mode. The station mode/client board would connect to the AP/server board and send it a udp packet of 10 bytes.

This may not be the best approach but I was getting reasonable times using the other tcp/ip examples. So I thought if I could send the payload as a udp packet it would have less latency.
 
Upvote 0

KiloBravo

Active Member
Licensed User
Well as it turns I forgot one line of code on the Client sending side ... RunNative("SetSTA", Null)
The packet data is not formatted correctly but the Client/Station is sending to the Server/AP.
A little bit of a learning curve for me, but I am getting there. Thanks !
 
Upvote 0

KiloBravo

Active Member
Licensed User
UDP Server
B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    Log(wifi.AccessPointIp)
    Log(wifi.LocalIp)
    wifi.StartAccessPoint("esp_server")
    RunNative("SetAP", Null)
    Log(wifi.AccessPointIp)
    Log(wifi.LocalIp)

    Log("Socket Init and Listening")
    udp.Initialize(51042, "udp_PacketArrived")
End Sub
'****************************************************
Sub Udp_PacketArrived (Buffer() As Byte, Ip() As Byte, Port As UInt)
    Log("PacketArrived ", Buffer.Length)
        If Buffer.Length =  0 Then
        Log("Packet Length 0 ")
        Return 'invalid message
    End If
' ********************************If Data.Length <= 18 Then Return
    If Buffer.Length > 18 Then
        Log("Packet Length > 18 ")
        Return 'invalid message
    End If
'************************************************************  
    For Each o As Object In Data
        Log(o)
    Next
End Sub
'************************************************************

#if C
void SetAP(B4R::Object* o) {
   WiFi.mode(WIFI_AP);
}
#end if
 
Upvote 0

KiloBravo

Active Member
Licensed User
Client Code
B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    If wifi.Connect("esp_server") = False Then
        Log("Error connecting to network")
        Return
    Else
        Log("Connected to network")
    End If

    RunNative("SetSTA", Null)
    usocket.Initialize(51042, "usocket_PacketArrived")

    Timer1.Initialize("Timer1_Tick", 50)
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Tick
    Timer1.Enabled = False 
' The Sub Below "Read Data" was called to Read the Device and Populate the Array that was sent to the Esp Server every 50 ms in Timer1_Tick
    Read_Data 
' ******************************** I used sizeof to determine it was 18 bytes and checked for the receiving side 
    usocket.BeginPacket(ip, port)
    usocket.Write(ser.ConvertArrayToBytes(Array(X_axis, Y_axis, C_button, Z_button, X_acc, Y_acc, Z_acc)))
    usocket.SendPacket
    Timer1.Enabled = True
End Sub

Private Sub usocket_PacketArrived (Data() As Byte, ip1() As Byte, port1 As UInt)
    'not used
    Log("Packet arrived")
End Sub

#if C
void SetSTA(B4R::Object* o) {
   WiFi.mode(WIFI_STA);
}
#end if
 
Upvote 0

KiloBravo

Active Member
Licensed User
I posted the main portions of my Client side and Server side UDP Code. This code was used to read data from a Nunchuk (Client side) and send it as an 18 byte array to the Server side. I used an esp8266 for the client side and another esp8266 for the server side. The Client sends the UDP Packet to Server every 50ms after reading the data from the Nunchuk and populating the array. It works great. I use the Client side to read the Nunchuk "joystick" and I am able to accurately and remotely control a hacked mobility scooter. The Server esp8266 controls the mobility scooter base (Server esp8266). Once I get it completed I will post more details in the project section and put a link to the full project here.

I should have posted the Process Global Section ....
Client Process Globals below

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private Timer1 As Timer
   Private usocket As WiFiUDP
   Private wifi As ESP8266WiFi
   Private ip() As Byte = Array As Byte(192,168,4,1)
   Private port As UInt = 51042
   Public ser As B4RSerializator
   Public bc As ByteConverter
End Sub

Server Process Globals Below ...

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private udp As WiFiUDP
    Public bc As ByteConverter
    Public ser As B4RSerializator
End Sub
 
Upvote 0
Top