I have a PHP script in which I control LEDs over wifi on Raspberry.
$fileName = __DIR__.'/txt/led2.txt';
if (!file_exists($fileName) || (file_get_contents($fileName) !== '1' && file_get_contents($fileName) !== '0')) {
file_put_contents($fileName, '1');
}
if (isset($_GET['on4']) && file_get_contents($fileName) === '1') {
shell_exec("/usr/local/bin/gpio -g write 15 1");
file_put_contents($fileName, '0');
}
else if (isset($_GET['on4']) && file_get_contents($fileName) === '0') {
shell_exec("/usr/local/bin/gpio -g write 15 0");
file_put_contents($fileName, '1');
}
Basically If I press button, script will replace variable in file, when it is on, variable in file = 0 and when it is off, its equals to 1.
I need to show the current status of the LEDs on my web UI. Is it posible? Now I only know if the echo shows 1 or 0, could not it be replaced by some picture or text?
Add an echo
statement that displays an image that depends on the state of the LED.
You can also simplify the code by noticing all the repetition and using a variable
$fileName = __DIR__.'/txt/led2.txt';
if (file_exists($fileName)) {
$current_state = file_get_contents($fileName);
if ($current_state != "0" && $current_state != "1") {
file_put_contents($fileName, '1');
$current_state = 1;
}
} else {
$current_state = 1;
file_put_contents($fileName, $current_state);
}
if (isset($_GET['on4'])) {
shell_exec("/usr/local/bin/gpio -g write 15 $current_state");
$current_state = 1 - $current_state;
file_put_contents($fileName, $current_state);
}
echo $current_state == 1 ? '<img src="images/ledOff.png">' : '<img src="images/ledOn.png">';