Android应用程序接收ESP-01 8266发送的数据

时间:2016-12-19 08:21:23

标签: android http android-wifi wireless esp8266

我想编写一个接收ESP-01 8266发送的数据的应用程序,并且ESP-01连接到同一个手机热点。实现这一目标的最佳方法是什么?

enter image description here

3 个答案:

答案 0 :(得分:1)

首先,您需要将ESP设置为客户端(ST_MODE)。如果你不知道该怎么做,你应该阅读ESP8266 module tutorial first。然后,在您的Android设备上,您可以安装名为Pushbullet的应用程序。之后,在pushbullet网站上免费设置一个帐户,设置一个新的通知渠道,并开始从ESP8266-01向该频道发送HTTP请求。您应该在智能手机上实时接收ESP8266发送的数据作为通知。这是最快的方式:)

答案 1 :(得分:0)

我使用UDP将数据传输到ESP8266的应用程序。 使用UDP意味着数据可能会丢失,但编码要求更容易(没有手抖动)。

查看此页面,其中包含所有步骤。 UDP on the ESP01

您将需要更改部分" UDP接收器"因为它显示了Windows UDP接收器的代码,您需要创建一个Android UDP接收器。

答案 2 :(得分:0)

我使用了一个从Android手机到ESP8266的简单httprequest。 打开/关闭门应用程序。

Android Java代码详细信息如下:

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();

}

调用函数示例:

openCloseDoor("http://your domain or ip:port/command");

ESP8266代码:

#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"); 
}