使用 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/