B4R Question ESP8266 Serial data problem

mrossen

Active Member
Licensed User
Longtime User
Hi,

I am trying to make a setup that a app can send and receive text data to a terminal program on my pc.

I have made a small test app in B4A there send data over tcp to the esp8266 module.

I want to have the esp8266 to receive this data and send them out on its serial port.
When esp 8266 receive data on its serial port its have to send it on the WiFi port.

Does this make sense?

The app connect to the esp8166 but esp8266 will not recieve data?

And the esp8266 restart all the time. This i probably because the esp8266 has a program fail.

I dont know if am on the right track here.

The code for the app

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Dim WiFi_Socket As Socket
    Dim TcpStreams As AsyncStreams
    Dim ServerSocket1 As ServerSocket
   
   

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
    Dim RecievedText, txtSend As String
   
    Private Button2 As Button
    Private Button3 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub WiFi_Socket_Connected (Connected As Boolean)
   
    Dim Buffer() As Byte
    ToastMessageShow("Connected", False)
    If Connected = True Then
        Log("Connected")
        ToastMessageShow("Forbindelse oprettet",True)
        TcpStreams.Initialize(WiFi_Socket.InputStream,WiFi_Socket.OutputStream,"tcpStreams")
       
        Sleep(500)
       
        Buffer = "*".GetBytes("UTF8")
        TcpStreams.Write(Buffer)
    Else
        ToastMessageShow("Server not connected",True)
    End If
       
End Sub

Sub TcpStreams_NewData (Buffer() As Byte)
   
    Dim msg As String
   
    msg = BytesToString(Buffer, 0, Buffer.Length, "ISO-8859-1")
    RecievedText = RecievedText & msg
    Log("New" & RecievedText)
   
End Sub


Sub Button1_Click
   
    If TcpStreams.IsInitialized = True Then
        ToastMessageShow("Der er forbindelse",True)
        Return
    End If
   
    Dim ServerIp As String = "192.168.4.1" 'Access point IP
    Dim Port As Int = 80                   'Set til port 80
    WiFi_Socket.Initialize("WiFi_Socket")
    WiFi_Socket.Connect(ServerIp,Port,5000)
End Sub

Sub Button2_Click
   
    Dim Buffer() As Byte
   
    If TcpStreams.IsInitialized = False Then
        ToastMessageShow("Der er ikke forbindelse",True)
        Return
    End If
   
    Buffer = "+".GetBytes("UTF8")
    TcpStreams.Write(Buffer)
   
    Sleep(500)
   
    WiFi_Socket.Close
    TcpStreams.Close
    ToastMessageShow("Forbindelse afbrudt",True)  
   
End Sub

Sub Button3_Click
   
    Dim Buffer() As Byte
   
    If TcpStreams.IsInitialized = False Then
        ToastMessageShow("Der er ikke forbindelse",True)
        Return
    End If
   
    Buffer = "+".GetBytes("UTF8")
    TcpStreams.Write(Buffer)
   
    ToastMessageShow("+", False)
   
End Sub

The code for the ESP8266 module

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 2000
#End Region
Sub Process_Globals
    Public Serial1 As Serial
    Public SerialCom As Serial
    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.StartAccessPoint("WiFi"))
    Log(wifi.AccessPointIp)
    RunNative("SetAP", Null)
    Log(wifi.AccessPointIp)
    Log(wifi.LocalIp)
    server.Initialize(80, "server_NewConnection")
    server.Listen
   
    SerialCom.Initialize(9600)
    astreamCom.Initialize(SerialCom.Stream, "astreamCom_NewData", "astreamCom_Error")
   
End Sub
#if C
void SetAP(B4R::Object* o) {
   WiFi.mode(WIFI_AP);
}
#end if
Sub server_NewConnection(NewSocket As WiFiSocket)
    astreamWiFi.InitializePrefix(NewSocket.Stream, False, "astreamWiFi_NewData", "astreamWiFi_Error")
    Log("new connection")
End Sub
Sub astreamWiFi_NewData(Buffer() As Byte)
    astreamComData(Buffer)
    server.Socket.Close 'disconnect to allow new connections
End Sub
Sub astreamWiFiData(data() As Byte)
    astreamWiFi.Write(data)
End Sub
Sub astreamWiFi_Error
    server.Listen
End Sub
Sub astreamCom_NewData(Buffer() As Byte)
    If server.Socket.Connected Then
        astreamWiFiData(Buffer)
    End If
End Sub
Sub astreamComData(data() As Byte)
    astreamCom.Write(data)
End Sub
Sub astreamCom_Error
    'Log("error")
End Sub

Mogens
 
Last edited:

mrossen

Active Member
Licensed User
Longtime User
Hi,

The buffer sized do not change anything.

I figured out why the program crashed.

The com port tried to send before it was initialized. :(

This is fixed with:

B4X:
If server.Socket.Connected Then
        astreamWiFiData(Buffer)
    End If

But I still have no connection on the WiFi.

I have tested the app part with a standard ESP8266 module. It transmit as expected.

I expect that when I connect ex. my phone to to esp8266 this sub will raise?

B4X:
Sub server_NewConnection(NewSocket As WiFiSocket)
    astreamWiFi.InitializePrefix(NewSocket.Stream, False, "astreamWiFi_NewData", "astreamWiFi_Error")
    Log("new connection")
End Sub

But it is not.

Anyone have a clue ?

Mogens
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

The Android device is connected to the esp8266 wifi module.

The android device logs connected when it gets its connection.

In this sub

B4X:
Sub WiFi_Socket_Connected (Connected As Boolean)
  
    Dim Buffer() As Byte
    ToastMessageShow("Connected", False)
    If Connected = True Then
        Log("Connected")
        ToastMessageShow("Forbindelse oprettet",True)
        TcpStreams.Initialize(WiFi_Socket.InputStream,WiFi_Socket.OutputStream,"tcpStreams")
      
        Sleep(500)
      
        Buffer = "*".GetBytes("UTF8")
        TcpStreams.Write(Buffer)
    Else
        ToastMessageShow("Server not connected",True)
    End If
      
End Sub

But still the esp8266 module not raise the Sub server_NewConnection(NewSocket As WiFiSocket)?

I use the port 80. I have also tried port 51042 as you use, but there can not get connection.

Mogens
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi Erel,

The problem is this line:

B4X:
SerialCom.Initialize(9600)

When I rem this line (and the other releated to this) out it raises "new connection" and the com over WiFi works.

But I need to send it out again on the com port.

Mogens
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
What if I disconnect the logging?

I use the AI-Thinker esp8266. The is only 8 pins and only one RX/TX
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

Now I have a module that works. :)

Thank you Erel.

I want to share the code with you if anyone can use it.

B4X:
#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("WiFi", "00000000"))
    Log(wifi.AccessPointIp)
    RunNative("SetAP", Null)
    Log(wifi.AccessPointIp)
    Log(wifi.LocalIp)
   
    server.Initialize(80, "server_NewConnection")
    server.Listen

    SerialCom.Initialize(9600, 2, 0)
    astreamCom.Initialize(SerialCom.Stream,"astreamCom_NewData", "astreamCom_Error")
   
End Sub

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

Sub server_NewConnection(NewSocket As WiFiSocket)
    Log("New connection")
    astreamWiFi.Initializeprefix(NewSocket.Stream, False, "astreamWiFi_NewData", "astreamWiFi_Error")
End Sub

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

Sub astreamWiFiData(data() As Byte)
    astreamWiFi.Write(data)
End Sub

Sub astreamWiFi_Error
    server.Listen
End Sub

Sub astreamCom_NewData(Buffer() As Byte)
    If server.Socket.Connected Then
        astreamWiFiData(Buffer)
        Log(Buffer)
    End If
End Sub

Sub astreamComData(data() As Byte)
    astreamCom.Write(data)
End Sub

Sub astreamCom_Error
    'Log("error")
End Sub
 
Upvote 0

mrossen

Active Member
Licensed User
Longtime User
Hi,

I have made another scenario where I use astream.Initializeprefix on both the WiFi and Serial streams.

B4X:
SerialCom.Initialize(9600, 2, 0)
    astreamCom.Initializeprefix(SerialCom.Stream, False, "astreamCom_NewData", "astreamCom_Error")

If I do that I get the 4 prefix bytes in the begining when I receive data from serial to WiFi.

I am then missing the 4 last!

I dont know why?

Anyone can explain that?

Mogens
 
Last edited:
Upvote 0
Top