简体   繁体   English

如何使用Android应用程序控制树莓派GPIO端口?

[英]How to control raspberry pi gpio ports using android app?

What methods are available in controlling the GPIO ports of a Raspberry Pi using an Android application? 使用Android应用程序控制Raspberry Pi的GPIO端口有哪些可用方法?

I've looked at using nodejs and briefly socketio - But really am none the wiser as to how to go about implementing this technology? 我已经研究过使用nodejs和简短的socketio-但是,关于如何实施该技术,真的没有一个明智的选择吗?

Is anybody able to explain the approach in greater deal/suggest an alternative / have existing examples? 是否有人能够以更大的价格来解释该方法/建议替代方法/是否已有示例?

Thanks 谢谢

I advise you to make the raspberry pi a webserver by using Bottle web server, then develop an android app that sends HTTP requests to the web server to control the GPIO pins. 我劝你,使树莓派一个网络服务器,通过使用瓶Web服务器,然后开发一个Android应用程序发送HTTP请求到web服务器来控制GPIO引脚。 You can use this class to make http requests: 您可以使用此类发出http请求:

class RequestTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... uri) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpResponse response;
    String responseString = null;
    try {
        response = httpclient.execute(new HttpGet(uri[0]));
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpStatus.SC_OK){
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else{
            //Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        //TODO Handle problems..
    } catch (IOException e) {
        //TODO Handle problems..
    }
    return responseString;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);

    Toast.makeText(getApplicationContext(), result, 0).show();
}
}

For example, to make http request when you press a button in your app. 例如,当您在应用程序中按下按钮时发出http请求。 You just write inside the function: 您只需在函数内部编写:

    new RequestTask().execute("http://192.168.1.145:80/3");

In my example, I am assuming that the app and the raspberry pi are connected in the same network. 在我的示例中,我假设应用程序和树莓派连接在同一网络中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM