it's actually my first time asking a question here, and really hoping someone could give solution to my problem.
I've been working on my Design Project, and one of the feature is that it could communicate from ESP8266 NodeMCU to Android, without the NodeMCU connecting to a network. So it's like the Android connecting directly to the Wifi Module, setting up the NodeMCU as both Access Point and WebServer.
I've established a simple communication, but I want it constant and simultaneous, since i'm using this connection to display the reading of the sensor from the NodeMCU to Android. My problem here is that, it won't constantly give the values, it would only give the first data and then nothing.
Here's my Code in Android:
First is the Client Class
public class Client extends AsyncTask<Void, Void, String> {
String dstAddress;
int dstPort;
String response = "";
TextView textResponse;
Socket socket = null;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.textResponse = textResponse;
}
@Override
protected String doInBackground(Void... arg0) {
Log.d("Status_2", "Sample_2");
try {
if(socket == null)
{
socket = new Socket(dstAddress, dstPort);
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice: inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
} /*finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*/
//textResponse.setText(response);
return response;
}
@Override
protected void onPostExecute(String result)
{
Log.d("Status_1", "sample");
textResponse.setText(response);
super.onPostExecute(result);
}
}
Here is from the Main Activity
public class MainActivity extends Activity {
TextView response;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear, buttonStop;
int check = 0;
public static Client myClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAddress = findViewById(R.id.addressEditText);
editTextPort = findViewById(R.id.portEditText);
buttonConnect = findViewById(R.id.connectButton);
buttonClear = findViewById(R.id.clearButton);
buttonStop = findViewById(R.id.buttonStop);
response = findViewById(R.id.responseTextView);
myClient = new Client(editTextAddress.getText()
.toString(), Integer.parseInt(editTextPort
.getText().toString()), response);
buttonStop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
check = 0;
}
});
buttonConnect.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
check = 1;
myClient.execute();
}
});
buttonClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
response.setText("");
}
});
}
}
And here is the code from the Arduino:
int status = WL_IDLE_STATUS;
boolean alreadyConnected = false; // whether or not the client was connected
previously
WiFiServer server(8080);
void setup()
{
delay(1000);
Serial.begin(115200);
Serial.println();
// MotionSensor_Setup();
Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid,password);
delay(10000);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Serial.println("\nStarting server...");
// start the server:
server.begin();
}
void loop()
{
// wait for a new client:
WiFiClient client = server.available();
// when the client sends the first byte, say hello:
if(client)
{
if (!alreadyConnected)
{
// clead out the input buffer:
client.flush();
Serial.println(analogRead(A0));
Serial.println();
client.println("Hello, client!");
client.println(analogRead(A0));
delay(500);
alreadyConnected = true;
}
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
}
}
Eventually I've tried this code here in the void loop() method.
void loop()
{
// wait for a new client:
WiFiClient client = server.available();
// when the client sends the first byte, say hello:
while(client.connected())
{
client.println(analogRead(A0));
delay(1000);
Serial.println(analogRead(A0));
}
}
it got inside the while condition and displayed data in the Serial Monitor, but didn't displayed in the Android.
I've tried several techniques and still no luck.
I would really appreciate your reply guys, Thank you!