B4R Question ESP8266 connect with PC

Amateurtje

Member
Licensed User
Longtime User
Hello,

I have made an succesful connection between my android app (B4A) and my uC through the ESP8266, based on the script from https://www.b4x.com/android/forum/threads/esp8266-getting-started.68740/ . This works great. Now I want to connect the ESP8266 to a pc. I installed several TCP interfaces and I notice that it makes a connection to the ESP8266 socket and it seems that it receives something. But is does not write any information/buffer to the serial port. I think it is a kind of mismatch in some settings or something but I cna not find it. Anybody any ideas?
 

Amateurtje

Member
Licensed User
Longtime User
Sorry I did not give te code. I went back to the most simple code I started with but even with this code, I do not get it to work:
ESP8266:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 2000
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Public SerialCom As SoftwareSerial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private astreamWiFi As AsyncStreams
    Private astreamCom As AsyncStreams


End Sub

Private Sub AppStart

    Serial1.Initialize(115200)
   
    Log("AppStart")
   
    Log(wifi.StartAccessPoint2("WIIIFIII", "06112006"))
    Log("Wifi AP IP",wifi.AccessPointIp)
    RunNative("SetAP", Null)
    Log("Wifi AP IP",wifi.AccessPointIp)
    Log("Wifi is connected:",wifi.IsConnected)
 

    server.Initialize(80, "server_NewConnection")
    server.Listen
    Log("Server is listening")


    SerialCom.Initialize(9600, 2, 0)
    astreamCom.Initialize(SerialCom.Stream,"astreamCom_NewData", "astreamCom_Error")
   Log("Serialport is listening")
    Log("Wifi is connected:",wifi.IsConnected)
End Sub

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

Sub WiFi_Socket_Connected (Connected As Boolean)
    Log("Wifi Socket connected")
End Sub

Sub server_NewConnection(NewSocket As WiFiSocket)
    Log("New connection")
    astreamWiFi.Initializeprefix(NewSocket.Stream, False, "astreamWiFi_NewData", "astreamWiFi_Error")
    Log("remoteIp " , NewSocket.RemoteIp)
    Log("remoteport " ,NewSocket.RemotePort)
    Log("remoteconnected: " ,NewSocket.Connected)
    Log("Wifi is connected:",wifi.IsConnected)
    Log("Established New connection")
End Sub

Sub astreamWiFi_NewData(Buffer() As Byte)
    Log("WifiStreamReceived")
    astreamComData(Buffer)
    Log(Buffer)
End Sub

Sub astreamWiFiData(data() As Byte)
    Log("Send Serial Data to Wifi")
    astreamWiFi.Write(data)
End Sub

Sub astreamWiFi_Error
    Log("a Wifi Data error")
    ''server.Initialize(80, "server_NewConnection")''??
    server.Listen
End Sub

Sub astreamCom_NewData(Buffer() As Byte)
    Log("Serial Data received")
    If server.Socket.Connected Then
        Log("and Socket connected")
        astreamWiFiData(Buffer)
        Log(Buffer)
    End If
End Sub

Sub astreamComData(data() As Byte)
    Log("Send Wifistream to Comport")
    astreamCom.Write(data)
End Sub

Sub astreamCom_Error
    'Log("error")
End Sub


I see that there is a connection with the PC, a New connection is made but if I try to send data, nothing arrives... jsut to start and test, What program do you use to test the connection? So I can start from something that works. I use realterm as interface to send data to the socket.

Some last rules from the log file:

remoteIp 192.168.4.2
remoteport 5467
remoteconnected: 1
Wifi is connected:0
Established New connection
a Wifi Data error
New connection
remoteIp 192.168.4.2
remoteport 5499
remoteconnected: 1
Wifi is connected:0
Established New connection

It connects etc but When sending data, I do not receive anything.
 
Last edited:
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
You have to support the same protocol in the receiving end.

Thanks for the help.

You mean the sending side (the pc)?.

In the end, I want to make a vb.net application to also communicate with the ESP. Therefore Realterm is not a "must" to use but just to test.
I can see two directions/solutions:
- Change the existing communication between the ESP and existing Android app so that I can also implement the vb.net .
So, use something else than : astreamWiFi.Initializeprefix ... Which prefix to use in this case?

Or to find a test program and a vb.net script which can work with this prefix...

Any good ideas for the way to go? Any good idea to change this prefix or what to change for vb.net to match this prefix?
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
it all seems to work with using the normal initialize command instead of the initialiseprefix... it works in realterm now..I assume that it will work in vb.net now also.. Going to try this tomorrow ?

I do not know exactly what is the difference (besides it has something to do as a setting/mode of the socket??) .

thanks for the Pointer, Jan ? . ( I assume that is your name :))
 
Upvote 0

Amateurtje

Member
Licensed User
Longtime User
Every block is "prefixed" with block length, while the normal sends stream as-is.

You can mimic the prefix in vb. Just add before data block in vb. I don't remember the format, probably a 16 bits integer.

Thanks for the info. Without the prefix I get exactly what I need but it came from an example. being fresh, I did not realise that.. Thanks all for the expanaition.!
 
Upvote 0
Top