1

I have a web server running a simple php page with a few buttons (12345678) These buttons control the lights in my house through an arduino.

Now I want to create a simple app to control these buttons. The PHP script is posted below:

<?php if (isset($_POST['button1'])) { shell_exec('screen -S led -X stuff 1'); } ?>
<?php if (isset($_POST['button2'])) { shell_exec('screen -S led -X stuff 2'); } ?>
<?php if (isset($_POST['button3'])) { shell_exec('screen -S led -X stuff 3'); } ?>
<?php if (isset($_POST['button4'])) { shell_exec('screen -S led -X stuff 4'); } ?>
<?php if (isset($_POST['button5'])) { shell_exec('screen -S led -X stuff 5'); } ?>
<?php if (isset($_POST['button6'])) { shell_exec('screen -S led -X stuff 6'); } ?>
<?php if (isset($_POST['button7'])) { shell_exec('screen -S led -X stuff 7'); } ?>
<?php if (isset($_POST['button8'])) { shell_exec('screen -S led -X stuff 8'); } ?>

<form action="" method="post">
<button type="submit" name="button1">AV Rack</button>
<br>
<button type="submit" name="button2">Scherm bureau + LEDs</button>
<br>
<button type="submit" name="button4">Woonkamer LEDs + Beamer</button>
<br>
<button type="submit" name="button3">Buiten Achter</button>
<br>
<button type="submit" name="button5">Red LED test</button>
<br>
<button type="submit" name="button6">Error test</button>
<br>
<button type="submit" name="button7">Alles aan</button>
<br>
<button type="submit" name="button8">Alles uit</button>
</form>

Could someone point me in the right direction?

EDIT:

I really did get somewhere but not all the way.

App is working and sending commands as it should. The web interface is still up and running

I think my server (ubuntu) doesn't accept the info/command I send it. I think I'm doing something wrong from the command end.

index.php?action=""&method="post"&type="submit"&name="button1"

If i put that in my browser url bar or any other string that I tried doesn't give me a result but just the page with the buttons.

Any suggestions?

  • 1 form 1 submit button – Fabio May 31 '13 at 13:41
  • The fact that the page is generated by php is not relevant; all that matters is that it is an HTML page and your server expects HTTP requests. That done, maybe you want to try a more standard way of HTTP messaging (SOAP, JSON, REST) (I do not know if Arduino supports them, though). – SJuan76 May 31 '13 at 13:41

2 Answers2

2

You don't control these buttons, actually. You perform a POST-Request (you might want to look into the HTTP-Standard), and as the code looks, as long as you pass anything into a variable the key of which is buttonX, it will accomplish what you want. For POST-Requests there is an HTTP library in Java that will do most of the work for you.

[Edit:] This tutorial might be helpful. It's sadly not as straightforward as it could be to sent such a simple request, especially since you don't even care for the answer. But having this code at hand, I am confident you will be able to figure it out.

[Edit2:] Look into @FoamyGuy's answer if you just want to get it done. That class looks nice, and I posted a comment that, in combination with the class, should get you there in no time.

Janis F
  • 2,637
  • 1
  • 25
  • 36
2

I like to use the HttpRequest object given in this answer

If you download a copy of that class and put it in your project you can send a request like this:

HttpRequest mReq = new HttpRequest();
mReq.sendPost(urlToYourServer, data);

You need to fill in the url to your sever, and a url encoded name value pair list for the data to send. Im not particularly fluent in php so I don't quite understand what parameters if any your server is expecting, but if so data is where you'll put them. If not you can just give it a blank String.

Community
  • 1
  • 1
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • His PHP script expects either one of the variables button1 through button8 to _exist_. If data is a String here, executing the command behind Button1 would require the data-String to look somewhat like this: `"Button1=execute"`, where execute could be anything, really, since the script only checks for the variable being available, and doesn't care about contents. – Janis F May 31 '13 at 14:00
  • I'm quite new to android programming and have no clue where to put the snippet you just posted. I have the class, changed parameters to my needs and I made the buttons in activity_main.xml. How do I go on with connecting these components? – Stein van Broekhoven May 31 '13 at 14:25
  • you would put it in a click listener inside your activity so that it will send the request whenever you click a button. To test though you can just stick it inside of onCreate if you want, that way it will send the post when the app launches\ – FoamyGuy May 31 '13 at 14:30