Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed help implementing logic in an Android app with an Arduino website button.
    text
    copied!<h1>Background story:</h1> <p>I have an <a href="http://arduino.cc" rel="nofollow">Arduino</a> with a Wishield from <a href="http://asynclabs.com/" rel="nofollow">Async Labs</a> wired up to a garage door opener. The Arduino throws up a website with a button. You push the button and the page refreshes with the new status of the button (on or off). The only problem is the pin (and by association the garage door remote) is set to on all the time. I made an Android app that gets the data from this web page and "pushes" the button from the app. </p> <h1>Problem:</h1> <p>Everything in the Arduino and my Android app works fine, what I need is some kind of logic inside the app. I just can't get my head around how or where to implement the logic. I'd like to be able to push the button once and it open the door, open the circuit, and then close the circuit. Right now it works like this:</p> <pre><code>------------------------------------------------------------------- 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 </code></pre> <p>I think it should work like this:</p> <pre><code>--------------------------------------------------------------------- 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 </code></pre> <h2>Relevant code:</h2> <pre><code>//Http Task private class httpTask extends AsyncTask&lt;Void, Void, Void&gt; { 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\"&gt;Door Closed")){ //"\"Opener 2\"&gt;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\"&gt;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(); } } } } </code></pre> <p>If anyone would like to make their own version of this I can post the full Android and Arduino code. Thanks for your help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload