I want to write an app that receives data sent by ESP-01 8266 and that ESP-01 is connected to the same phone hotspot. What is best way to achieve this?
Android app to receive data sent by ESP-01 8266
3.3k views Asked by Muhammad_08 At
3
There are 3 answers
1
On
First, you need to setup your ESP as a client (ST_MODE). If you don't know how to do it, you should read this ESP8266 module tutorial first. Then, on your Android device, you can install an application called Pushbullet. After that, setup an account free on pushbullet website, setup a new notification channel and start sending HTTP requests to that channel from your ESP8266-01. You should receive data sent from ESP8266 as notifications in real time on your smartphone. This is the fastest way :)
0
On
I have used a simple httprequest from Android phone to ESP8266. Open/close door application.
Android Java code detail as follow:
private void openCloseDoor(final String requestURL) {
reply = "";
new AsyncTask<Object, Void, String>() {
@Override
protected void onPreExecute() {
dialog.setProgressStyle(android.R.style.Widget_ProgressBar_Small);
dialog.setMessage("Send command to door ...");
dialog.show();
}
@Override
protected String doInBackground(Object... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(requestURL);
try {
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputstream = entity.getContent();
BufferedReader bufferedreader =
new BufferedReader(new InputStreamReader(inputstream));
StringBuilder stringbuilder = new StringBuilder();
String currentline = null;
while ((currentline = bufferedreader.readLine()) != null) {
stringbuilder.append(currentline + "\n");
}
String result = stringbuilder.toString();
reply = result;
inputstream.close();
}
} catch (NetworkOnMainThreadException ne) {
String err = (ne.getMessage() == null) ? "Network" : ne.getMessage();
Log.e("openDoor", err);
reply = err;
} catch (MalformedURLException me) {
String err = (me.getMessage() == null) ? "Malform" : me.getMessage();
Log.e("openDoor", err);
reply = err;
} catch (ProtocolException pe) {
String err = (pe.getMessage() == null) ? "Protocol" : pe.getMessage();
Log.e("openDoor", err);
reply = err;
} catch (IOException ioe) {
String err = (ioe.getMessage() == null) ? "IOError" : ioe.getMessage();
Log.e("openDoor", err);
reply = err;
}
return reply;
}
@Override
protected void onPostExecute(String result) {
if (dialog.isShowing()) {
Log.v("openDoor", reply);
statust.setText(reply);
statust.setTextColor(getResources().getColor(R.color.lightGreen));
dialog.dismiss();
}
}
}.execute();
}
Call function example:
openCloseDoor("http://your domain or ip:port/command");
ESP8266 code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
const int LED_PIN = 16;
IPAddress ip(192, 168, 0, 32);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
WebServer server(9999);
void handleRoot();
void handleNotFound();
void handleRelay();
void setup(void){
WiFi.config(ip, gateway, subnet);
WiFi.begin("...","...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
server.on("/", HTTP_GET, handleRoot);
server.on("/<id>", HTTP_GET, handleDoor);
server.onNotFound(handleNotFound);
server.begin();
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(115200);
}
void loop(void){
server.handleClient();
}
void handleDoor() {
digitalWrite(LED_PIN, HIGH);
// Send door code ....
server.send(200,"text/plan","OK");
delay(500);
digitalWrite(LED_PIN, LOW);
}
void handleRoot() {
digitalWrite(LED_PIN, HIGH);
server.send(200, "text/plain", "Ready");
digitalWrite(LED_PIN, LOW);
}
void handleNotFound(){
server.send(404, "text/plain", "404: Not found");
}
I have used UDP to transmit data to apps from ESP8266. Using UDP means the data may get lost but the coding requirements are much easier (no hand shakes).
Have a look at this page which goes through all the steps. UDP on the ESP01
You wil lneed to change the section "UDP Receiver" as that shows code for a Windows UDP receiver and you would need to create an Android UDP receiver.