UPDATE: Thank you for the thoughts. I have debugged the issue I believe down to the connection between the android to the ESP8266. I believe there may be something going on with the ESP because when I connect the android to my pc it seems to connect and work well. There are some very minor delays but nothing like when it is connected to the ESP. Therefore I am thinking the ESP is causing the delay. That being said I am not sure what could cause this. I have tried using the ESP8266 2.3.0 ver of the libraries in Arduino IDE 1.6.8 but can't seem to figure out why the delay. I did connect the android app to my internal router and then send to the PC with no issues. However it doesn't seem to work with the ESP. I can't believe I am the only one that is seeing this... If anyone else has this working please let me know how you did it. My eternal gratitude would be yours.
Here is the ESP Arduino code:
WiFiServer server(2390); // TCP/IP
SoftwareSerial mySerial( 4, 5); // RX, TX
void setup( )
{
Serial.begin( 115200 );
WiFi.mode(WIFI_STA);
WiFi.softAP( "mySSID", "myPASS");
server.begin();// START TCP/IP SERVER
}
WiFiClient client;
void loop( )
{
if (client)
{
while (client.connected())
{
if (client.available())
{
char command = client.read();
Serial.println(itoa(command,buffer,10));
}
}
}
else
{
client = server.available();
}
}
I am trying to get almost instantaneous communication using TCP/IP sockets between android (Java) and ESP8266. The problem I am seeing is a significant delay between when I press the button to when the android will actually send the data. It seems to sometimes send right when I tap the Go Button but after the first time it will not send for 5-10 seconds. When it is stuck I tap the Go button a number of times and after the delay they all seem to send at once or start sending in spurts. I look at the network monitor in Android Studio and the delay is noticable from the time I tap the button until the traffic shows up on the network panel in the Monitors tab.
I am using the Android Studio Debugger to test the code.
Is there something that I am not doing correctly? Or perhaps is there a better implementation for near instant communication between devices?
Thank you for your assistance in advance!
Android Device: Samsung Note 4 - v5.1.1 Android v2.0
Android Studio
ESP Device: ESP8266 using Arduino Studio 1.6.8
Here is my Code:
public class MainActivity extends AppCompatActivity
{
private Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view)
{
try
{
new AsyncTaskRunner().execute("");
}
catch (Exception e)
{
e.printStackTrace();
}
}
private class AsyncTaskRunner extends AsyncTask<String, String, String>
{
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params)
{
try
{
DataOutputStream dataOut;
Socket s = new Socket("192.168.4.1", 2390);
dataOut=new DataOutputStream(s.getOutputStream());
byte[] outputData = new byte[9];
outputData[0] = 1;
outputData[1] = 2;
outputData[2] = 3;
outputData[3] = 4;
outputData[4] = 5;
outputData[5] = 6;
outputData[6] = 7;
outputData[7] = 8;
outputData[8] = 9;
dataOut.write(outputData); // Red
dataOut.flush();
dataOut.close();
s.close();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onProgressUpdate(String... text) {
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jered.networktest.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go"
android:onClick="onClick"/>
</RelativeLayout>