B4R Question ESP8266 Sockets

Discussion in 'B4R Questions' started by heromedx, Dec 11, 2018.

  1. heromedx

    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 ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. heromedx

    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:

    Code:
    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(19216803), 51042Then
            
    Log("connected")
            astream.InitializePrefix(
    socket.Stream, False"astream_NewData""astream_Error")
            astream.Write(
    "hello!!!")
        
    Else
            CallSubPlus(
    "Connect"10000)
        
    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:

    Code:
    #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: Dec 12, 2018
  4. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    You shouldn't use prefix mode as the server doesn't support it.
     
  5. heromedx

    heromedx Member

    Sorry...

    I have edited it ...
     
  6. heromedx

    heromedx Member

    As suggested I have changed:
    Code:
    astream.InitializePrefix(socket.Stream, False"astream_NewData""astream_Error")
    to
    Code:
    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 ?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload the project (File - Export as zip)?
     
  8. heromedx

    heromedx Member

    Uploaded Client.zip
     

    Attached Files:

  9. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice