python Raspberry Pi基于定时阀门控制器。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Raspberry Pi基于定时阀门控制器。相关的知识,希望对你有一定的参考价值。

#!/usr/bin/python
# Ben Fasoli
import datetime
import RPi.GPIO as GPIO
from time import sleep

std_pins = [11, 13, 15, 29]
qc_pins  = [11, 13, 15, 31, 33]
all_pins = [11, 13, 15, 29, 31, 33]
t = datetime.datetime.utcnow()

# Initialize GPIO
GPIO.setmode(GPIO.BOARD)
for pin in all_pins: GPIO.setup(pin, GPIO.OUT)

def set_pin(num):
    not_pin = list(set(all_pins) - set([num]))
    for pin in not_pin: GPIO.output(pin, 1)
    GPIO.output(num, 0)
    outstr = t.strftime('%Y-%m-%d %H:%M:%S.%f') + ', ' + str(num) + '\n'
    print outstr
    with (open('/home/pi/valve_log.dat', 'a+')) as f:
        f.write(outstr)


def std_routine():
    if t.minute % 10 is 0 and t.second is 0:
        pin_num = std_pins[pin_idx]
        set_pin(pin_num)
        sleep(1)
        return(True)
    else: return(False)

def qc_routine():
    if t.minute % 2 is 0 and t.second is 0:
        pin_num = qc_pins[qcpin_idx]
        set_pin(pin_num)
        sleep(1)
        return(True)
    else: return(False)


pin_idx = 0
qcpin_idx = 0
pin_num = std_pins[3]
set_pin(pin_num)
while True:
    t = datetime.datetime.utcnow()
    if t.hour % 6 is 0 and t.minute >= 20 and t.minute < 30:
        if qc_routine():
            pin_idx = 0
            qcpin_idx = qcpin_idx + 1
    else:
        if std_routine():
            qcpin_idx = 0
            pin_idx = pin_idx + 1
    if pin_idx > 3: pin_idx = 0
    sleep(0.05)

以上是关于python Raspberry Pi基于定时阀门控制器。的主要内容,如果未能解决你的问题,请参考以下文章

python 基于Python Bottle的简单应用程序,用于控制Raspberry Pi相机模块

慢响应Firebase + Python + Raspberry pi

在Raspberry Pi上使用python3进行Bash

在raspberry pi中安装缺少的python包

并行计算Python / Raspberry Pi

python 用于控制Raspberry Pi汽车的Python脚本。