0

背景故事:

我有一个带有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 代码。谢谢你的帮助。

4

1 回答 1

0

根据我对您的描述的了解,您已准备就绪,除了在每次按下按钮后关闭电路的定时任务。

在每次按下按钮时启动倒数计时器可能会起作用。见,http://developer.android.com/reference/android/os/CountDownTimer.html

这是一个例子

new CountDownTimer(30000, 1000) { //30 seconds count-down

 public void onTick(long millisUntilFinished) {
     //no need to do anything here
 }

 public void onFinish() {
     turnOffCircuit(); 
 }
}.start();

Arduino 很酷的东西。实际上昨天买了我的第一个 Arduino 套件 :)

于 2012-04-13T07:32:31.190 回答