树莓派+php实现定时鱼缸计划

树莓派+php定时开关过滤器和龟龟晒背灯
leorain, 技术life09-14 21:32
php-ryrqd

高端的设备加上简单的代码,让龟龟过上神仙都羡慕的生活🤣,更多功能开发中

<?php

namespace App\Console\Commands;

use App\Models\GpioStatus;
use Illuminate\Console\Command;
use PhpGpio\Gpio;

class GpioController extends Command
{
    const HIGH = 1;
    const LOW = 0;
    const TURN_PIN = 17;
    const SUN_PIN = 18;
    const SIGNAL = 23;
    const LIGHT = 8;
    const BLACK = 2;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'gpio:control';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $startTime = " 08:30:00";
        $endTime = " 21:30:00";
        // 判断时间
        $now = now();
        $nowTime = now();
        $dayStart = date("Y-m-d", $now->startOfDay()->timestamp) . $startTime;
        $dayEnd = date("Y-m-d", $now->startOfDay()->timestamp) . $endTime;
        $gpioOn = (int)$nowTime->between($dayStart, $dayEnd);
        $gpio = new Gpio();
        $gpioSunNumber = self::SUN_PIN;
        $gpioTurnNumber = self::TURN_PIN;
        $gpioPinArray = [
            $gpioSunNumber,
            $gpioTurnNumber
        ];
        $this->signal_blink(3);
        // 处理引脚状态数据
        foreach ($gpioPinArray as $pin) {
            $gpioStatus = GpioStatus::firstOrCreate(['pin' => $pin]);
            $gpioStatus->times = ($gpioStatus->times ?? 0) + 1;
            // 晒背灯额外处理
            if ($pin === self::SUN_PIN && $gpioOn) {
                /*
                 * 实现的功能是
                 * 亮5分钟,熄灭1分钟降温
                 * 每分钟 times 参数加 1 ,
                 * 当 times  <= 5 的时候,亮,
                 * 当 times % ( 5 + 1 ) == 0 的时候,开始
                 */
                if ($gpioStatus->times <= self::LIGHT) {
                    $gpio->setup($pin, "out");
                    $gpio->output($pin, self::HIGH);
                } elseif ($gpioStatus->times < self::LIGHT + self::BLACK) {
                    $gpio->setup($pin, "out");
                    $gpio->output($pin, self::LOW);
                } else {
                    $gpioStatus->times = 0;
                }
            } else {
                $gpio->setup($pin, "out");
                $gpio->output($pin, $gpioOn);
                $gpioStatus->times = 0;
            }
            $gpioStatus->save();
        }
        $this->signal_blink(5);
    }

    protected function signal_blink(int $times = 3)
    {
        $gpio = new \PhpGpio\Gpio();
        // 信号灯闪烁提示
        $gpio->setup(self::SIGNAL, 'out');
        foreach (range(0, $times * 2 - 1) as $item) {
            $gpio->output(self::SIGNAL, $item % 2);
            usleep(300000);
        }
        $gpio->output(self::SIGNAL, self::HIGH);
    }
}

原创文章,转载请注明出处~ 以上就是本文的全部内容啦,有什么疑问欢迎在下方评论区留言嗷,收到通知会及时回复~
文章浏览总量:573 (非即时 )
跳转到顶部