0

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?

CalvT
  • 3,123
  • 6
  • 37
  • 54
Petr Jelínek
  • 110
  • 2
  • 11

1 Answers1

0

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">';
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yes, but when I press it on one device and load page on another, image is not visible on another device. – Petr Jelínek Dec 18 '18 at 19:01
  • Simplyfing code works good, thanks for that, but still, when I press button once on computer, image will appear, but when I go on my phone, refresh site, there is no image or wrong one (with led off) not right one. – Petr Jelínek Dec 18 '18 at 19:40
  • I fixed it. I wasn't flipping the `$current_state` variable correctly. – Barmar Dec 18 '18 at 20:36
  • Very nice! It works! And do you thing thats posible to put image somewhere into HMTL code? Because I have my HTML code above my PHP, so I dont know if I can display images somewheer else than echo does. – Petr Jelínek Dec 18 '18 at 20:54
  • Can you please explain me this part of the code? `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;` I dont really understand condition where must be $current_state not equals to 0 and 1, when I press button, there will be 0 after that, because there is this: $current_state = 1 - $current_state; So, please, how exactly this works? – Petr Jelínek Dec 18 '18 at 21:14
  • Maybe you can just move this code up, so it's before you display the HTML. – Barmar Dec 18 '18 at 21:17
  • The `if` is just like your code `(file_get_contents($fileName) !== '1' && file_get_contents($fileName) !== '0')` I just put the contents into the variable instead of reading the file over and over. – Barmar Dec 18 '18 at 21:18
  • `$current_state = 1 - $current_state` does this: If it's currently 1, then 1-1 is 0. If it's currently 0, 1-0 is 1. So it just flips between those two values. – Barmar Dec 18 '18 at 21:19