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

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

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

我研究过使用nodejs并简要介绍了socketio - 但对于如何实现这项技术真的一无所知?

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

谢谢

最佳答案

我建议您使用 Bottle Web 服务器将树莓派设为 Web 服务器,然后开发一个 Android 应用程序,向 Web 服务器发送 HTTP 请求来控制 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");

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

关于android - 如何使用Android应用程序控制树莓派GPIO端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28417364/

相关文章:

android - 以编程方式添加 TextInputLayout

android - 空对象引用上的“java.lang.String android.content.Context.getPackageName()”

node.js - Azure Functions + Azure SQL + Node.js 和请求之间的连接池?

javascript - 我只是无法理解 Node.JS,有什么资源和教程可以帮助我吗?

node.js - 如何在 node.js 中的模块之间共享连接池?

android - SharedPreferences 不保存值(value)

android - 无法获取 'https://jcenter.bintray.com/com/android/tools/build/gradle/4.0.0/gradle-4.0.0.pom'

node.js - 使用 Socket.IO 和 NodeJS 实现音频聊天

node.js - Websocket 连接错误 : Does not return a 101 changing protocal when using from different path

javascript - 如何在客户端获取连接的 socket.id?