背景故事:
我有一个带有Async Labs的 Wishield的Arduino ,连接到车库门开启器。Arduino 会弹出一个带有按钮的网站。您按下按钮,页面会以按钮的新状态(打开或关闭)刷新。唯一的问题是别针(以及车库门遥控器)一直设置为开启状态。我制作了一个 Android 应用程序,它从这个网页获取数据并从应用程序中“按下”按钮。
问题:
Arduino 和我的 Android 应用程序中的一切都运行良好,我需要的是应用程序内部的某种逻辑。我只是不知道如何或在哪里实现逻辑。我希望能够按下按钮一次,它会打开门,打开电路,然后关闭电路。现在它的工作原理是这样的:
-------------------------------------------------------------------
Door | Circuit
closed | off -- default: door closed circuit off
open | on -- button pushed 1st time: door open circuit on
open | off -- button pushed 2nd time: door open circuit off
closed | on -- button pushed 3rd time: door closed circuit on
closed | off -- button pushed 4th time: door closed circuit off
我认为它应该像这样工作:
---------------------------------------------------------------------
Door | Circuit
closed | off -- default: door closed circuit off
open | on -- button pushed 1st time: door open circuit on
open | off -- after a set time app automatically closes circuit
closed | on -- button pushed 2nd time: door closed circuit on
closed | off -- after a set time app automatically closes circuit
相关代码:
//Http Task
private class httpTask extends AsyncTask<Void, Void, Void> {
HttpResponse responseGet = null;
HttpEntity resEntityGet = null;
@Override
protected void onPreExecute() {
EditText01 = (TextView) findViewById(R.id.EditText01);
GetMethod test = new GetMethod();
String returned;
try {
returned = test.getInternetData();
if (pinCheck == 2){
if(returned.toString().contains("\"Opener 2\">Door Closed")){ //"\"Opener 2\">Door Closed"
Toast.makeText(getBaseContext(), "Opening the door.", Toast.LENGTH_SHORT).show();
findViewById(R.id.button1).setBackgroundResource(R.drawable.dooropen);
}
if(returned.toString().contains("\"Opener 2\">Door Open")){
Toast.makeText(getBaseContext(), "Closing the door.", Toast.LENGTH_SHORT).show();
findViewById(R.id.button1).setBackgroundResource(R.drawable.doorclosed);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dialog = new ProgressDialog(GarageDoorOpenerActivity.this);
dialog.setMessage("Working");
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(customURL[0] + URL + customURL[1] + pin);
responseGet = client.execute(get);
resEntityGet = responseGet.getEntity();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void params) {
if(dialog.isShowing())
dialog.dismiss();
if (resEntityGet != null) {
Toast.makeText(getBaseContext(), "Done. ", Toast.LENGTH_SHORT).show();
resEntityGet = null;
}
}
}
public class GetMethod {
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI url = new URI(customURL[0] + URL + customURL[1]); //customURL[0] + URL + customURL[1]
HttpGet request = new HttpGet();
request.setURI(url);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally{
if (in !=null){
try{
in.close();
return data;
} catch (Exception e){
e.printStackTrace();
}
}
}
}
如果有人想制作自己的版本,我可以发布完整的 Android 和 Arduino 代码。谢谢你的帮助。