PHP Lighttpd submit button issue Raspberry pi GPIO

A

Anonymous

Guest
Hi ,

I am new to PHP programming .Here i am trying to run GPIO pins of raspberry pi with php . I have taken the code from : http://www.raspberry-pi-geek.com/Archive/2014/07/PHP-on-Raspberry-Pi. to start

I want to have multiple buttons for GPIO ON/OFF like for example
button1,button2,button3

1. if i press submit button1 for on - the display should show the result in Textbox as " ON ".
2. if i press submit button2 for on - the display should show the result in Textbox as " ON ".
3. if i press submit button3 for off -the display should show the result in Textbox as " OFF ".


Problem:

what should i do to keep the textbox text unchanged after clicking on any buttons or reloading of same page.

if light is ON means its ON it should not show OFF after reloading of same page.


HTML

<form method="get" action="gpio.php">
<input type="submit" value="ON" name="on">
<input type="submit" value="OFF" name="off">
</form>

PHP

<?php
$setmode17 = shell_exec("/usr/local/bin/gpio -g mode 17 out");
if(isset($_GET['on'])){
$gpio_on = shell_exec("/usr/local/bin/gpio -g write 17 1");
echo "LED is on";
}
else if(isset($_GET['off'])){
$gpio_off = shell_exec("/usr/local/bin/gpio -g write 17 0");
echo "LED is off";
}
?>


Thanks
Atila
 
Hi,
Welcome to PHP! I love the [idea of] the Pi, and I've owned a few, I just haven't got round to using it for anything, so I'm afraid my Pi knowledge is scant :)

You probably won't get many answers to your post in this section which is the 'alternate db' section - it might be better off in the general forum. However, I will try to help.

As far as I can see, adding a second 'on' button would not change the logic of this simple program, so I'm not sure what the problem is. Please post the actual code you want to work, not just bits of it. I suggest you comment out the gpio code, and then your problem becomes a php problem, not a Pi problem :)

-A
 
Thanks for the response. please find the attached image. I am trying to do this. Objective is to switch ON and OFF. if i press Green it should show OFF and when i select Red it should show status as ON..and the status of the switches must be unchanged until i click on the buttons .

But status changes to default after page reload.(after clicking on any button)

I am good at C++ but beginner at Php. can you please let me know what things i need to take care to achieve this. like jquery etc..


Atila
 

Attachments

  • switch.png
    switch.png
    236.9 KB · Views: 6,928
Hi,
One way is to store the state of each switch within a hidden field e.g.
Code:
<?php
// See if hidden input had a value last time, and if so, store it
if (isset($_REQUEST["switch1"])) {
	$sSwitch1 = $_REQUEST["switch1"];
} else {
	$sSwitch1 = 'OFF';
}
...
// Now add some code to change the state
if (isset($_REQUEST["on1"])) {
	$sSwitch1 = $_REQUEST["on1"]; // Will be on or off
}
...
?>
...
<input type="hidden" name="switch1" value="<?=$sSwitch1?>">
<input type="hidden" name="switch2" value="<?=$sSwitch2 ?>">

<?
...
// Display button state
echo "1=" . $sSwitch1 . '<br>';
echo "2=" . $sSwitch2 . '<br>';
...
?>
...
<input type="submit" value="ON" name="on1">
<input type="submit" value="OFF" name="on1">
<input type="submit" value="ON" name="on2">
<input type="submit" value="OFF" name="on2">
//...
- A
 
Back
Top