繁体   English   中英

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

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

使用Android应用程序控制Raspberry Pi的GPIO端口有哪些可用方法?

我已经研究过使用nodejs和简短的socketio-但是,关于如何实施该技术,真的没有一个明智的选择吗?

是否有人能够以更大的价格来解释该方法/建议替代方法/是否已有示例?

谢谢

我劝你,使树莓派一个网络服务器,通过使用瓶Web服务器,然后开发一个Android应用程序发送HTTP请求到web服务器来控制GPIO引脚。 您可以使用此类发出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();
}
}

例如,当您在应用程序中按下按钮时发出http请求。 您只需在函数内部编写:

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

在我的示例中,我假设应用程序和树莓派连接在同一网络中。

暂无
暂无

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

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