The use of the ESP8266 in the world of IoT

User avatar
By GastonMelo
#82292 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 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://" + btn_id.getTag().toString()
                        Toast.makeText(MainActivity.this, btn_id.getTag().toString(), Toast.LENGTH_SHORT).show();
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                        }catch(Exception e){};

                    }catch (Exception e){

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.

User avatar
By survivingwithandroid
#82948 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)) )
  tv.setText("Sending data to Arduino...");
  client.newCall(req).enqueue( new Callback() {
  public void onFailure(Call call, IOException e) { // Handle call failure }
  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:

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
Let me know if you need more details.