'a TCP client communicating with a server set on ESP8266-01

I'm setting up a TCP/IP communication between an android app and an ESP8266 module connected to Arduino uno. I'm using AT commands to set up the server as follows:

AT+CWMODE=1
AT+CIPMUX=1
AT+CIPSERVER=1,80
I get OK for each.

I want send an int to the app : 0 or 1 , the app read the int and then sends the text typed in the editText to the ESP8266

Now, here is my app code:

public class MainActivity extends AppCompatActivity {
TextView tv;
EditText txt;
EditText txt2;
Button b;
string response;
private static Socket s ;
private static PrintWriter printWriter;
String message="";
private static String ip="";

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.button);
    txt = (EditText) findViewById(R.id.editText);
    tv = (TextView) findViewById(R.id.textView) ;
    txt2=(EditText)findViewById(R.id.editText2);

 }
 class myTask extends AsyncTask<Void,Void,Void>{

    @Override
    protected Void doInBackground(Void... params)
    {
        try
        {        s= new Socket(ip,80);

                //READING THE INPUT

           BufferedReader in = new BufferedReader(new 
           InputStreamReader(s.getInputStream()));
            while(in!=null) {
                 response = in.readLine();
            }
                //SENDING THE MESSAGE
            printWriter= new PrintWriter(s.getOutputStream());
            printWriter.write(message);
            printWriter.flush();
            post_send();


            // closing all connections
           // printWriter.close();
           // in.close();
           // s.close();


        }catch(IOException e)
        {e.printStackTrace();}
           return null;
    }
  }
    public void send_text(View v)
    {
        message= txt.getText().toString();
        ip=txt2.getText().toString();
        myTask mt = new myTask();
        mt.execute();
        Toast.makeText(getApplicationContext(),"MT 
        LAUNCHED",Toast.LENGTH_LONG).show();

     }

     public void post_send(){
        Toast.makeText(getApplicationContext(),response 
         ,Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(),"Data 
        sent",Toast.LENGTH_LONG).show();
    }
   }

I have 1 button to create the socket and receive any data and then send a text.

When I click on the button I get the following message on the serial monitor:

 0, connected 

so I type:

 AT+CIPSEND=0,4

I get :

SEND OK

but none of the toasts show help me? What am I doing wrong ?



Solution 1:[1]

s = new Socket(ip, 80);

If you execute that code in the onClick() of an onClickListener you will get an NetworkOnMainThreadException. Which lets your app crash.

All internet code (also the connecting and reading) should be executed from a thread or AsyncTask.

Your writing is already in an AsyncTask. Which is the way to go.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1