2015-02-09 107 views
2

使用Android應用程序控制Raspberry Pi的GPIO端口有哪些方法可用?如何使用android應用程序控制樹莓pi gpio端口?

我已經看過使用nodejs和簡短的socketio - 但是,對於如何實施這項技術,我真的沒有更聰明的辦法嗎?

是否有人能夠更大程度地解釋該方法/建議替代/現有示例?

感謝

回答

0

我勸你,使樹莓派一個網絡服務器,通過使用瓶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"); 

在我的例子中,我假設應用程序和樹莓派連接在同一個網絡。