【问题标题】:How to control raspberry pi gpio ports using android app?如何使用 Android 应用程序控制树莓派 gpio 端口?
【发布时间】:2015-02-09 19:06:44
【问题描述】:

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

我已经研究过使用 nodejs 和简单的 socketio - 但是对于如何实施这项技术真的一点都不明智吗?

是否有人能够更详细地解释该方法/建议替代方法/现有示例?

谢谢

【问题讨论】:

    标签: android node.js socket.io raspberry-pi gpio


    【解决方案1】:

    我建议你通过使用 Bottle 网络服务器将树莓派设置为网络服务器,然后开发一个向网络服务器发送 HTTP 请求以控制 GPIO 引脚的 Android 应用程序。您可以使用此类发出 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");
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-05
      • 2020-01-16
      • 1970-01-01
      • 2019-10-28
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2022-12-05
      相关资源
      最近更新 更多