Erino242
Erino242

Reputation: 1

How to send data to ESP8266?

I want to send data to ESP8266 to work with my motors. I just need send string 00 or 10 or 01 it depends what button I will press. So it have to look like this: http://ipaddress:port/00 So I found this code. I changed it so it can work with my program. So I install app to my phone and I connect to ESP8266´s hotspot. Then I send(click on button) the value(00 or 10 or 01). The Esp receive this string. BUT when I want click second button(to change string) it WILL NOT send it. The code is stuck and I thing it is waiting for response or something like that. But I don´t want resposne from my Arduino(ESP8266) I just want send datas imadiately...

View.OnClickListener btnSetClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String value;
            if (view== btnSet) {
               value = "/10";
            } else if(view == btnMinus) {
                value = "/01";
            } else {
                value = "/00";
            }
            Toast.makeText(SettingsActivity.this, "OnClick", Toast.LENGTH_LONG).show();
            String PORT = editPort.getText().toString();
            String serverIP = editIp.getText().toString() + ":" + PORT + value;
            textInfo1.setText(serverIP);

            TaskEsp taskEsp = new TaskEsp(serverIP);
            taskEsp.execute();

private class TaskEsp extends AsyncTask<Void, Void, String> {

    String server;

    TaskEsp(String server){
        this.server = server;
    }

    @Override
    protected String doInBackground(Void... params) {

        final String p = "http://"+server;

        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                textInfo1.setText(p);
            }
        });

        String serverResponse = "";
        String port = null;
        //HttpURLConnection httpURLConnection = p;

        try {


            HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(p).openConnection());
            if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    textView.setText("Try");
                }
            });
                InputStream inputStream = null;
                inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader =
                        new BufferedReader(new InputStreamReader(inputStream));
                serverResponse = bufferedReader.readLine();
                httpURLConnection.disconnect();
                inputStream.close();
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
        }
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                textView.setText("return");
            }
        });
        return serverResponse;
    }

    @Override
    protected void onPostExecute(String s) {
        textInfo2.setText(s);
        textView.setText("PostExc");
    }
}

Upvotes: 0

Views: 3193

Answers (2)

vaibhav sharma
vaibhav sharma

Reputation: 172

I am doing the same thing but with different approach,

In this case I am creating a JSON object where I am adding all the click responses as the values to the JSON and this JSON is posted to the web server hosted by ESP8266.

on the ESP side I am parsing this JSON from WebServer and getting the values from it.

I've a similar kind of project where I am controlling the relays from android application You can go to this LINK for detail discription.

Upvotes: 1

FrancescoAzzola
FrancescoAzzola

Reputation: 2654

you can use a different approach to this:

  1. You can use aRest library. It helps you to expose arduino/ESP functions as API
  2. Implement a simple Android app that connects to ESP and sends the required data.

I've made a tutorial explaining how to send data from an android app. You can give a look here and here

If you look for an example how to implement a web server on ESP and implement an Android app to exchange data you can refer to my post here

Upvotes: 1

Related Questions