简体   繁体   English

PHP脚本点亮Raspberry Zero Pi W上的LED

[英]PHP script to lights up LED on Raspberry Zero Pi W

I do not know much about PHP, but I would like to ask how I should improve this script. 我对PHP不太了解,但是我想问一下我应该如何改进此脚本。

Adding a screenshot of my existing code, I will briefly describe. 我将简要描述一下添加现有代码的屏幕截图。

屏幕截图

The program causes the LED to turn on when the web button is pressed for the first time. 首次按下Web按钮时,程序使LED点亮。

A condition must be met where it is given that the file size must be equal to 2 bytes. 必须满足的条件是,文件大小必须等于2个字节。 When the first condition is executed, a larger 4-byte number is written to the txt file. 当执行第一个条件时,将更大的4字节数字写入txt文件。

By pressing the same button again, the second condition should be met, but there is an error that the file is overwritten, but the condition can not be fulfilled and I do not know why. 通过再次按下同一按钮,应该满足第二个条件,但是存在一个错误,即文件被覆盖,但是无法满足该条件,我也不知道为什么。

Do not you ask someone where there might be a problem or how would you possibly solve the problem you? 您是否不问某人哪里可能有问题,或者您将如何解决该问题?

Here is part of the code: 这是代码的一部分:

    if(!file_get_contents("soubor.txt", 0)) {
            $soubor = fopen("soubor.txt","w+");
            $funkceled1 = 10;
            fwrite ($soubor, $funkceled1);
            fclose ($soubor);
        }

     if (isset($_GET['on1']) && file_get_contents("soubor.txt", 2)) {
                shell_exec("/usr/local/bin/gpio -g write 14 1");
                $soubor = fopen("soubor.txt","w+");
                $funkceled1 = 1000;
                fwrite ($soubor, $funkceled1);
                fclose ($soubor);
            }
     else if (isset($_GET['on1']) && file_get_contents("soubor.txt", 4)) {
                shell_exec("/usr/local/bin/gpio -g write 14 0");
                $soubor = fopen("soubor.txt","w+");
                $funkceled1 = 10;
                fwrite ($soubor, $funkceled1);
                fclose ($soubor);

Your code could be rewritten this way: 您的代码可以这样重写:

$fileName = __DIR__.'/soubor.txt';
// if the file does not exist or does not contain '10' nor '1000', create it with default value '10'
if (!file_exists($fileName) || (file_get_contents($fileName) !== '10' && file_get_contents($fileName) !== '1000')) {
    file_put_contents($fileName, '10');
}
if (isset($_GET['on1']) && file_get_contents($fileName) === '10') {
    shell_exec("/usr/local/bin/gpio -g write 14 1");
    file_put_contents($fileName, '1000');
} else if (isset($_GET['on1']) && file_get_contents($fileName) === '1000') {
    shell_exec("/usr/local/bin/gpio -g write 14 0");
    file_put_contents($fileName, '10');
}

I don't know how your gpio binary works in detail, but maybe you can also read a value, besides writing one? 我不知道您的gpio二进制文件是如何工作的,但是也许您除了写一个值之外还可以读取一个值? Then you could make this super easy: 然后,您可以使此超级简单:

// read the current state from gpio (if 'read' is an option )
$current_state = shell_exec('/usr/local/bin/gpio -g read 14');

if (isset($_GET['on1']))  {
    // if it's off, turn it on, otherwise off.
    $new_state = (($current_state == '0') ? 1 : 0);
    shell_exec('/usr/local/bin/gpio -g write 14 '.$new_state);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM