-->
Page 1 of 1

esp8266 and android

PostPosted: Tue May 14, 2019 1:31 pm
by GastonMelo
Hi to all, i have an esp8266 as webserver to turn on and off a lamp. The classic example sending the /on to the IP (in this example http://192.168.0.6/on) the lamp is turn on. I can do it in the web browser of pc without problems.
I was thinking in do it by android app and I read (by google search) using the HttpURLConnection in android, an so far i Got this:

Code: Select alltry {
                        URL url = new URL("http://192.168.0.6/on");//"http://" + btn_id.getTag().toString()
                        Toast.makeText(MainActivity.this, btn_id.getTag().toString(), Toast.LENGTH_SHORT).show();
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        try{
                            connection.setDoOutput(true);
                            connection.setInstanceFollowRedirects(false);
                            //connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                            connection.setRequestMethod("GET");
                            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                            connection.connect();
                            wr.flush();
                            wr.close();
                        }catch(Exception e){};

                    }catch (Exception e){
                        e.printStackTrace();
                    }


I tried and it doesnt work. If this approach correct or do I need to use socket?. Im totally new to android, thanks for your help.

Gastón

Re: esp8266 and android

PostPosted: Thu May 16, 2019 6:09 am
by GastonMelo
using android Sockets is the answer.

Re: esp8266 and android

PostPosted: Fri Jun 28, 2019 5:53 am
by survivingwithandroid
Hi,

when connecting ESP8266 to Android there are two different scenarios you should use:

- Implement a Web server on ESP8266 and send data from Android
- Use aRest library

In the first scenario you have to implement a web server on ESP8266 side and invoke it from android app. The easiest way is using OkHTTP library. The code below shows how to do it:

Code: Select all Request req = new Request.Builder() .url(URL)
      .post( RequestBody.create(JSON, createJSON(r,g,b)) )
      .build();
 
  tv.setText("Sending data to Arduino...");
  client.newCall(req).enqueue( new Callback() {
 
  @Override
  public void onFailure(Call call, IOException e) { // Handle call failure }
 
  @Override
  public void onResponse(Call call, Response response) throws IOException {
    // OK Response...inform the user }
  });


It sends JSON data to ESP8266. If you want to have more information I wrote a post on my blog about it: https://www.survivingwithandroid.com/2016/03/connect-android-arduino-using-http-web-server.html

If you use the second scenario you have to use aRest Library and invoke the API from ESP8266 in Android. The Android client is almost the same while on ESP8266 you have to define the API.
You can give a look at my post about how to implement it https://www.survivingwithandroid.com/2016/05/arduino-rest-api-iot.html
Let me know if you need more details.
F.