0

I'm simply trying to send String text from my android app to Nodemcu esp8266 and the esp8266 response with another text.

I can receive the text on esp8266 but on android app I'm not get any response text! and I don't knew what is the problem.

my android code is

public class MainActivity extends Activity {

TextView textResponse;
Button buttonConnect;
EditText welcomeMsg;
String IpAddress = "192.168.0.117";
int Port = 8090;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonConnect = (Button) findViewById(R.id.connect);
    textResponse = (TextView) findViewById(R.id.response);
    welcomeMsg = (EditText)findViewById(R.id.welcomemsg);

    buttonConnect.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            MyClientTask myClientTask = new MyClientTask(welcomeMsg.getText().toString());
            myClientTask.execute();
        }
    });
}

@SuppressLint("StaticFieldLeak")
public class MyClientTask extends AsyncTask<Void, Void, Void> {
    String response = "";
    String msgToServer;

    MyClientTask(String msgTo) {
        msgToServer = msgTo;
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;

        try {
            socket = new Socket(IpAddress, Port);
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            dataInputStream = new DataInputStream(socket.getInputStream());

            if(!msgToServer.equals(""))
                dataOutputStream.writeUTF(msgToServer + "$");

            response = dataInputStream.readUTF();

        } catch (IOException e) { }
        finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {}
            }
            if (dataOutputStream != null) {
                try {
                    dataOutputStream.close();
                } catch (IOException e) {}
            }
            if (dataInputStream != null) {
                try {
                    dataInputStream.close();
                } catch (IOException e) {}
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        textResponse.setText(response);
        super.onPostExecute(result);
    }
}
}

my esp8266 code is

#include <ESP8266WiFi.h>

WiFiServer server(8090);
IPAddress ip(192, 168, 0, 117);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

void setup() {
  Serial.begin(115200);
  WiFi.config(ip, gateway, subnet);
  WiFi.begin("TP-LINK", "55555555");
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
  }
  server.begin();
  Serial.println("Connected");
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  Serial.println(client.readStringUntil('$'));  
  String x = "esp8266";
  client.println(x);
  delay(100);
  client.flush();
}

and this is how my app locks like

app

anyone have any idea?

Nuwan Alawatta
  • 1,812
  • 21
  • 29
Ahmed Ibrahim
  • 13
  • 1
  • 4

1 Answers1

0

use the code bellow to send data:

example: sendData("http://192.168.0.117:8090/data");

private void sendData(final String requestURL) {
        reply = "";
        new AsyncTask<Object, Void, String>() {

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected String doInBackground(Object... params) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(requestURL);
                try {
                    HttpResponse response = httpclient.execute(httpget);
                    HttpEntity entity = response.getEntity();

                    if (entity != null) {
                        InputStream inputstream = entity.getContent();
                        BufferedReader bufferedreader =
                                new BufferedReader(new InputStreamReader(inputstream));
                        StringBuilder stringbuilder = new StringBuilder();

                        String currentline = null;
                        while ((currentline = bufferedreader.readLine()) != null) {
                            stringbuilder.append(currentline + "\n");
                        }
                        String result = stringbuilder.toString();
                        reply = result;
                        inputstream.close();
                    }
                } catch (NetworkOnMainThreadException ne) {
                    String err = (ne.getMessage() == null) ? "Network" : ne.getMessage();
                    reply = err;
                } catch (MalformedURLException me) {
                    String err = (me.getMessage() == null) ? "Malform" : me.getMessage();
                    reply = err;
                } catch (ProtocolException pe) {
                    String err = (pe.getMessage() == null) ? "Protocol" : pe.getMessage();
                    reply = err;
                } catch (IOException ioe) {
                    String err = (ioe.getMessage() == null) ? "IOError" : ioe.getMessage();
                    reply = err;
                }
                return reply;
            }

            @Override
            protected void onPostExecute(String result) {
              Log.v(reply);
            }
        }.execute();
    }