B4R Question ESP8266 Sockets

heromedx

Member
I am new to B4R, have a Python and Delphi background...

Some questions regarding 8266 communications using B4R:
1) Is there a Simple Socket Library ?
2) Is it possible to write a Socket Server using B4R ?
3) Is it possible to write a Socket Client using B4R ?
4) Is there an example for 2 or 3 ?
 

heromedx

Member
Thanks Erel for your prompt reply...

I have tried the Client example copied from https://www.b4x.com/android/forum/threads/89726/#content
running on a NodeMCU (8266), with a server running on a Win10 PC and written in Python.

Unfortunately, the Client could not connect, displaying the following:

AppStart
0
0.0.0.0
Trying to connect
Trying to connect
Trying to connect
Trying to connect
...

I wonder if you have any idea what the problem is ?

This is The Client Code:

B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private socket As WiFiSocket
    Private astream As AsyncStreams
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("SetSTA", Null)
    Log(wifi.Connect("esp_server"))
    Log(wifi.LocalIp)
    Connect(0)
End Sub

Sub Connect(u As Byte)
    Log("Trying to connect")
    If socket.ConnectIP(Array As Byte(192, 168, 0, 3), 51042) Then
        Log("connected")
        astream.InitializePrefix(socket.Stream, False, "astream_NewData", "astream_Error")
        astream.Write("hello!!!")
    Else
        CallSubPlus("Connect", 1000, 0)
    End If
End Sub

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

Sub astream_NewData (Buffer() As Byte)
    Log("new data: ", Buffer)
End Sub

Sub astream_Error
    Log("error")
End Sub


This is the server code:

B4X:
#TempServer
import sys
import socket

port = 51042
num = 0
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind( ('', port) )
s.listen(1)
print("Waiting on Port ", port)
c, a = s.accept()
print ("Server connected to " + str(a))

while True:
    rep = c.recv(1024)
    rep = rep.decode('Utf-8')
    print(rep)
    if rep == 'end':
        sys.exit() 
    num = num + 1
    mess = str(num)
    mess = mess.encode('Utf-8')
    c.send(mess)
Regards

Bob Stanton
 
Last edited:
Upvote 0

heromedx

Member
As suggested I have changed:
B4X:
astream.InitializePrefix(socket.Stream, False, "astream_NewData", "astream_Error")
to
B4X:
astream.Initialize(socket.Stream,  "astream_NewData", "astream_Error")

But now I am getting the following compilation error:
B4R Version: 2.51
Parsing code. (0.00s)
Compiling code. (0.18s)
Building project (0.40s)
Compiling & deploying Ino project (NodeMCU 1.0 (ESP-12E Module) - COM5) Error
Loading configuration...
Initializing packages...
Preparing boards...
Verifying...
C:\Users\Bob\Objects\bin\sketch\b4r_main.cpp: In function 'void SetSTA(B4R::Object*)':
b4r_main.cpp:14: error: 'wifi' was not declared in this scope
wifi.mode(WIFI_STA);
^
exit status 1

I am using the following Libraries:
rCore (Version: 2.00)
rESP8266WiFi (Version: 1.33)
rRandomAccessFile (Version: 1.90)

Can you Help ?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…