com.example.arduinoesp.MainActivity
package com.example.arduinoesp;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
public class MainActivity extends ActionBarActivity {
EditText editIp;
Button btnOn, btnOff;
TextView textInfo1, textInfo2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editIp = (EditText)findViewById(R.id.ip);
btnOn = (Button)findViewById(R.id.bon);
btnOff = (Button)findViewById(R.id.boff);
textInfo1 = (TextView)findViewById(R.id.info1);
textInfo2 = (TextView)findViewById(R.id.info2);
btnOn.setOnClickListener(btnOnOffClickListener);
btnOff.setOnClickListener(btnOnOffClickListener);
}
View.OnClickListener btnOnOffClickListener = new View.OnClickListener(){
@Override
public void onClick(View v) {
String onoff;
if(v==btnOn){
onoff="1";
}else{
onoff="0";
}
btnOn.setEnabled(false);
btnOff.setEnabled(false);
String serverIP = editIp.getText().toString()+":80";
TaskEsp taskEsp = new TaskEsp(serverIP);
taskEsp.execute(onoff);
}
};
private class TaskEsp extends AsyncTask<String, Void, String> {
String server;
TaskEsp(String server){
this.server = server;
}
@Override
protected String doInBackground(String... params) {
String val = params[0];
final String p = "http://"+server+"?led="+val;
runOnUiThread(new Runnable(){
@Override
public void run() {
textInfo1.setText(p);
}
});
String serverResponse = "";
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpGet = new HttpGet();
httpGet.setURI(new URI(p));
HttpResponse httpResponse = httpclient.execute(httpGet);
InputStream inputStream = null;
inputStream = httpResponse.getEntity().getContent();
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
serverResponse = bufferedReader.readLine();
inputStream.close();
} catch (URISyntaxException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (ClientProtocolException e) {
e.printStackTrace();
serverResponse = e.getMessage();
} catch (IOException e) {
e.printStackTrace();
serverResponse = e.getMessage();
}
return serverResponse;
}
@Override
protected void onPostExecute(String s) {
textInfo2.setText(s);
btnOn.setEnabled(true);
btnOff.setEnabled(true);
}
}
}
layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<EditText
android:id="@+id/ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="192.168.4.1" />
<Button
android:id="@+id/bon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LED ON" />
<Button
android:id="@+id/boff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LED OFF" />
<TextView
android:id="@+id/info1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"/>
<TextView
android:id="@+id/info2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
</LinearLayout>
Make sure to add uses-permission of "android.permission.INTERNET" in src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arduinoesp" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Download the files (Android Studio Format).
19 comments:
is this possible with esp8266-12 wifi module?
please reply asap.thank you!
As you see in the video, I was using ESP8266-12.
But have to take caution on the firmware, sometime I have code work on this firmware, then I update the ESP8266 firmware, the code become not work!!!
THANKS A LOT!!!
Thank You Very Helpfull
can this work on the esp8266 01
Anonymous,
suppose it can work on ESP8266 01 also. Very depends on the firmware version of ESP8266.
hey i am using ESP8266-12E-MOD....i want to know if i can use your android app and your arduino code to communicate with module?????
hello Anonymous,
suppose it can.
Thanks a lot , it's very helpfull in my project, one question: why the application is slow after sending 3 or 4 request,but it work fine when I use browser in my PC to send : http://myadressip:80/led=1 for test to know if my ESP are a problem
can u refer me link how to connect esp8266 access point ( socket server ) with an android app ( socket client ) toon off gpio led ??
i want to receive data on it
help
i got error message .. connection refused, i was using esp8266 01. can u help me?
Can you please post your Arduino code? thad would be great help, Thanks.
Hello Abhijeet Tapase,
http://arduino-er.blogspot.com/2015/05/arduino-due-esp8266-web-server.html
Thank you.
Thank You for Arduino Code.
I will work on this and inform you if it works.
Thanks again.....
Hi Andr.oid Eric, thanks to your android code i am able to communicate with WiFi shield. Though I dont have Arduino Due (i have Uno,mega,pro). I am not able to read the string from serial monitor. Please post if you can help. Thanks again.
Hi! Great article :)
How should i modify arduino code if a want to control to leds?
android code link is not opening.
Post a Comment