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
The code for the ESP8266 module
Mogens
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: