esp8266 and android
Posted: Tue May 14, 2019 1:31 pm
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:
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
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 all
try {
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