【问题标题】:Need help implementing logic in an Android app with an Arduino website button.需要帮助使用 Arduino 网站按钮在 Android 应用程序中实现逻辑。
【发布时间】:2012-04-13 06:35:39
【问题描述】:

背景故事:

我有一个 Arduino 和一个来自 Async Labs 的 Wishield 连接到车库开门器。 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 代码。谢谢你的帮助。

【问题讨论】:

    标签: android logic arduino


    【解决方案1】:

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

    在每次按下按钮时启动倒数计时器,可能会起作用。 看,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 套件:)

    【讨论】:

    • 我喜欢 Arduino。如果您需要帮助,我的第一个项目登陆 HackADay.com(搜索 daft punk 咖啡桌)......我不是专家,但我愿意尝试。无论如何......所以当它完成后,我可以调用 onButtonPressed();功能,对吧?它会“按下”按钮并关闭电路。但是我必须有某种计数器,以便我的按钮的图像在关闭电路时不会显示“关闭”......或者我可以跳过第二次更新照片。谢谢你。今晚我会进一步研究。
    • 它似乎可以工作,但我似乎无法正确实现它。我有一个计数器设置,在第一次运行时它会改变背景图像并做它的事情。在第二次运行时它做了所有事情,但更新了图像(因为图像显示门是打开还是关闭而不是电路状态)我遇到的问题是第二次运行的检查总是正确的,所以每 30几秒钟后它会自行激活,应用程序最终会挂起并强制关闭。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    相关资源
    最近更新 更多