PHP Lighttpd submit button issue Raspberry pi GPIO

Do you have questions regarding other database enginges (not MySQL) -- ask here!

Moderators: egami, macek, gesf

Post Reply
Atila
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Jun 05, 2017 12:30 am

Mon Jun 05, 2017 12:56 am

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/Archiv ... spberry-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

landi
php-forum Active User
php-forum Active User
Posts: 388
Joined: Thu Mar 15, 2012 3:59 pm

Mon Jun 05, 2017 2:44 am

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

Atila
New php-forum User
New php-forum User
Posts: 2
Joined: Mon Jun 05, 2017 12:30 am

Mon Jun 05, 2017 10:24 am

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.85 KiB) Viewed 664 times

landi
php-forum Active User
php-forum Active User
Posts: 388
Joined: Thu Mar 15, 2012 3:59 pm

Mon Jun 05, 2017 10:50 am

Hi,
One way is to store the state of each switch within a hidden field e.g.

Code: Select all

<?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

Post Reply