-1

I have using an arduino with an ESP8266 module and I am currently controlling it using a web browser (or an android app(that calls a web browser)) url commands eg."http://192.168.1.65/pin=10"(Call a motor to turn it on).

I want to have an android button which requests that url without opening it in the browser. Is that possible and how would i do it?

Onelarian
  • 1
  • 2
  • It is trivial in android. [refer this](http://stackoverflow.com/questions/8654876/http-get-using-android-httpurlconnection) –  Apr 19 '17 at 07:19

3 Answers3

0

I wrote a function for my self, that can request any URL. You can also define the method (POST, GET, etc. ) and the body of data, if you have something to send.

public static String requestURL(URL url, String method, String body){
        String result = "";
        HttpURLConnection httpCon = null;
        try {
            httpCon = (HttpURLConnection) url.openConnection();

            assert httpCon != null;
            httpCon.setDoOutput(true);

            httpCon.setRequestMethod(method);

            OutputStreamWriter out = null;
            out = new OutputStreamWriter(httpCon.getOutputStream());

            assert out != null;
            out.write(body);

            out.close();

            httpCon.getInputStream();

            InputStream is = null;
            result = "";

            is = new BufferedInputStream(httpCon.getInputStream());

            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String inputLine = "";
            while ((inputLine = br.readLine()) != null) {
                result += inputLine;
            }
            return result;
        }catch (Exception e){
            return "{\"nothing\":\"nothing\"}";
        }
    }

I am using the HTTPURLConnection to accomplish this.

PS. Sorry for my bad code, but this was something, which works every time for me, even in my earliest states of programming experience for Android.

Julian Schmuckli
  • 3,681
  • 11
  • 37
  • 64
0

It is totally possible, but your question is a bit vague. What do you mean by "Android Button"? Do you mean you want to write a program using Android SDK? Or do you want to put a button in a webpage?

The answer to both is using Sockets. You should open a socket and send a GET request. You may want to use WebSockets if you are going to write a webapp, or use TCPClient or HttpURLConnection class in Java API. Further answering depends on your choice.

Also, the api you have (http://192.168.1.65/pin=10), is not a good one, you should have a packet parser on your embedded side, and parse a GET request (which is really easy and there are hundreds of ready to use implementations available). Then you can have many arguments in a much more flexible and maintainable way.

arashka
  • 1,226
  • 3
  • 17
  • 30
0

You can use the volley library to send the request to your arduino, it's quite easy to use. See https://developer.android.com/training/volley/simple.html for an example.

Add a onlick listener to your button and make the request with it

Denny
  • 1,766
  • 3
  • 17
  • 37