B4R Question Client Server with ESP8266

tonga

Member
Licensed User
Longtime User
Hi all,
I would like an ESP8266 to create its own network, another ESP8266 could be connected to this because it can exchange analog and digital data. Finally, I would also like an Android device to connect to this network to be able to see that data and connect it to the internet. How can I achieve all this?

Thanks!
 

tonga

Member
Licensed User
Longtime User
Hi Erel,
the first example works, but I can not understand how I can create my own network without using the home router. I also do not understand how to create an ESP8266 client.
In the second example: maybe I misunderstood, but I expected that by pressing SET the ESP8266 board would change name and IP.

Thanks
 
Upvote 0

tonga

Member
Licensed User
Longtime User
Hi XorAndOr,

thank you for the fast response. I ask you another help:
my reference is the "ESPConfigureWiFi-B4R" project. In this project where do I need to insert your lines of code? Do I have to delete other lines?

Thanks
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private astream As AsyncStreams
'    Private ser As B4RSerializator
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative( "SetIP" , Null )' STATIC IP 192.168.1.6 ---> code #if C   
'ACCESS POINT   
    If wifi.StartAccessPoint2("AccessPontESP","12345678") Then ' Router SSID e Passw.
        Log("Connected Access Point")
        Log("Module ESP-12 IP: ", wifi.LocalIp)
    Else
        Log("Failed to connect")
        Return
    End If   

    server.Initialize(51042, "server_NewConnection")
    server.Listen
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")
    
    astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub

Sub AStream_NewData (Buffer() As Byte)   
    
End Sub

Sub AStream_Error
    Log("Error")
    server.Listen
End Sub

'STATIC IP (Replace with desired IP)
#if C
  void SetIP(B4R::Object* o) {
  IPAddress ip(192, 168, 1, 6);  // (Replace with desired IP)
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);
  }
#end if
 
Upvote 0

tonga

Member
Licensed User
Longtime User
Hi XorAndOr,

now the client connect to server. I need to send data from server to client by timer, but I see that timer continuosly logs:

AppStart
Connected Access Point
Module ESP-12 IP: 192.168.1.6
Where are the errors?

Thanks

Server code:

B4X:
Sub Process_Globals
    Private TempSensorPin As Pin                 'Output pin connected from the TMP36 sensor
    Private TempSensorPinNumber As Byte = 0x00   'Pin number used is A0 (Analog)
    Private d1 As D1Pins
    Private timer1 As Timer
    Private pin As Pin
    
    
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private astream As AsyncStreams
    Private ser As B4RSerializator
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    
    'Init the pin with TMP36 connected
    TempSensorPin.Initialize(TempSensorPinNumber, TempSensorPin.MODE_OUTPUT)
    
    Log("AppStart")
    RunNative( "SetIP" , Null )' STATIC IP 192.168.1.6 ---> code #if C
    'ACCESS POINT
    If wifi.StartAccessPoint2("AccessPontESP","12345678") Then ' Router SSID e Passw.
        Log("Connected Access Point")
        Log("Module ESP-12 IP: ", wifi.LocalIp)
    Else
        Log("Failed to connect")
        Return
    End If

    timer1.Initialize("timer1_Tick", 1000)
    pin.Initialize(d1.D6, pin.MODE_INPUT)
    timer1.Enabled=True

    server.Initialize(80, "server_NewConnection")
    server.Listen

    
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")
    
    astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Dim be(10) As Object 'used as a storage buffer.
    Dim objects() As Object = ser.ConvertBytesToArray(Buffer, be)
    Log("Recieved:")
    For Each o As Object In objects
        Log(o)
    Next
    
    
    'Log("Received: ", Buffer)
End Sub

Sub AStream_Error
    Log("Error")
    server.Listen
    timer1.Enabled = False
End Sub

'STATIC IP (Replace with desired IP)
#if C
  void SetIP(B4R::Object* o) {
  IPAddress ip(192, 168, 1, 6);  // (Replace with desired IP)
  IPAddress gateway(192, 168, 1, 1);
  IPAddress subnet(255, 255, 255, 0);
  WiFi.config(ip, gateway, subnet);
  }
#end if

Sub Timer1_Tick
    astream.Write(ser.ConvertArrayToBytes(Array("Sent from ESP", Millis, TempSensorPin.AnalogRead, pin.DigitalRead)))

End Sub

Client code:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private server As WiFiServerSocket
    Private astream As AsyncStreams
    Private timer1 As Timer
    Private ser As B4RSerializator
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    ScanNetworks
    If wifi.Connect2("AccessPontESP","12345678") Then 'change to your network SSID (use Connect2 if a password is required).
        Log("Connected to wireless network.")
        Log("My ip: ", wifi.LocalIp)
    Else
        Log("Failed to connect.")
        Return
    End If
    timer1.Initialize("timer1_Tick", 1000)
    timer1.Enabled = True
    server.Initialize(51042, "server_NewConnection")
    server.Listen
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("Client connected")
    astream.Initialize(NewSocket.Stream, "astream_NewData", "astream_Error")
End Sub

Sub Timer1_Tick
    If server.Socket.Connected Then
        astream.Write(ser.ConvertArrayToBytes(Array("Time here is: ", Millis)))
        Log("timer on")
    End If
End Sub


Sub AStream_NewData (Buffer() As Byte)
    Dim be(10) As Object
    Dim data() As Object = ser.ConvertBytesToArray(Buffer, be)
    Log("Received:")
    For Each o As Object In data
        Log(o)
    Next
End Sub

Sub AStream_Error
    Log("Error")
    server.Listen
End Sub

Private Sub ScanNetworks 'ignore
    Dim numberOfNetworks As Byte = wifi.Scan
    Log("Found: ", numberOfNetworks, " networks.")
    For i = 0 To numberOfNetworks - 1
        Log(wifi.ScannedSSID(i))
    Next
End Sub
 
Upvote 0

tonga

Member
Licensed User
Longtime User
Hi XorAndOr,

I understand that, with the new correction, the server reply to a data sent by client: right?
Because in this case the client doesn't send nothing: may be that it's some errors in that code?

Thank you
 
Upvote 0

tonga

Member
Licensed User
Longtime User
Hi XorAndOr,
I apologize for this break due to unforeseen circumstances. I set server port and client port to 80.
When the server starts I see the local timer works only a time ("timer in action 1") after this I see the logs repeat what you see in jpg
 

Attachments

  • esp8266.jpg
    esp8266.jpg
    141.5 KB · Views: 297
Upvote 0

tonga

Member
Licensed User
Longtime User
With this client code:
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private wifi As ESP8266WiFi
    Private client As WiFiSocket
    Private astream As AsyncStreams
    Private serverIp() As Byte = Array As Byte(192, 168, 4, 1)
    Private ServerPort As UInt = 80
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    
    If wifi.Connect2("nety","12345678") Then ' fill in the correct data
        Log("Connected to wireless network.")
        Log(wifi.LocalIp)
    Else
        Log("Failed to connect.")
        Return
    End If
    Connect(0)
End Sub

Private Sub Connect(u As Byte)
    If client.ConnectIP(serverIp, ServerPort) Then

        Log("Connected to server.")
        astream.Initialize(client.Stream, "astream_NewData", "astream_Error")
    Else
        Log("Failed to connect to server")
        CallSubPlus("Connect", 1000, 0)
    End If
End Sub

Sub AStream_NewData (Buffer() As Byte)
    Log("Received: ", Buffer)
End Sub

Sub AStream_Error
    Log("Error")
    CallSubPlus("Connect", 1000, 0)
End Sub

Logs are:

Connected to wireless network.
192.168.4.3
Failed to connect to server
Failed to connect to server
Failed to connect to server......

Parameters of ssid, pw, port are right, so I don't understand why all this
 
Upvote 0

tonga

Member
Licensed User
Longtime User
To this server:
B4X:
Sub Process_Globals
    Private TempSensorPin As Pin                            'Output pin connected from the TMP36 sensor
    Private TempSensorPinNumber As Byte = 0x00   'Pin number used is A0 (Analog)
    Public Serial1 As Serial
    Private server As WiFiServerSocket
    Private d1 As D1Pins
    Private wifi As ESP8266WiFi
    Private astream As AsyncStreams
    Private timer1 As Timer
    Private pin As Pin
    Private ser As B4RSerializator
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    'Init the pin with TMP36 connected
    TempSensorPin.Initialize(TempSensorPinNumber, TempSensorPin.MODE_OUTPUT)
    Log("AppStart")
    
    If wifi.StartAccessPoint2("nety", "12345678") Then
        server.Initialize(80, "server_NewConnection")
        server.Listen
        Log("Waiting for connection.")
        Log("My ip: ", wifi.LocalIp)
    Else
        Log("Failed to connect to Wifi.")
    End If
    
    
    
    '    If wifi.Connect2("Linkem2.4GHz_B99714","dmqwl3ed") Then
    '        server.Initialize(80, "server_NewConnection")
    '        server.Listen
    '        Log("Waiting for connection.")
    '        Log("My ip: ", wifi.LocalIp)
    '    Else
    '        Log("Failed to connect to Wifi.")
    '    End If
    timer1.Initialize("timer1_Tick", 1000)
    pin.Initialize(d1.D6, pin.MODE_INPUT)
End Sub

Sub Server_NewConnection (NewSocket As WiFiSocket)
    Log("new connection")
    astream.InitializePrefix(NewSocket.Stream, False, "astream_NewData", "astream_Error")
    timer1.Enabled = True
End Sub

Sub Timer1_Tick
    astream.Write(ser.ConvertArrayToBytes(Array("Sent from ESP", Millis, TempSensorPin.AnalogRead, pin.DigitalRead)))
End Sub

Sub astream_NewData (Buffer() As Byte)
    Dim be(10) As Object 'used as a storage buffer.
    Dim objects() As Object = ser.ConvertBytesToArray(Buffer, be)
    Log("Recieved:")
    For Each o As Object In objects
        Log(o)
    Next
End Sub

Sub Astream_Error
    Log("Error.")
    server.Listen
    timer1.Enabled = False
End Sub

I wish send read the state of button and analog value of a esp8266 and send commands to the other esp8266
 
Upvote 0
Top