【问题标题】:Python Socket Server and parallel timed TasksPython Socket 服务器和并行定时任务
【发布时间】:2020-06-02 07:02:11
【问题描述】:

我对 python 还很陌生,只是遇到了以下问题。

我正在尝试构建一个植物浇水自动化系统。作为基础,使用 Tornado 套接字服务器来侦听客户端轮询并传递数据以进行可视化。

对于浇水自动化,我创建了一个 ContinuousCallback(每 30 分钟),我想在其中检查湿度并控制泵(GPIO 输出)。

任务:如果回调启动并且它测量到低湿度,它应该通过在定义的时间内切换 GPIO 引脚来开始抽水。 (时间在 SQLITE DB 中定义)

问题:我该怎么做,我进入回调并启动泵操作(我有 4 个泵)并使其在定义的时间后结束? (Timer?, Timer Interrupt?) 不影响 Socket Server IOloop?

.

.

谢谢你和最良好的祝愿

塞巴斯蒂安

【问题讨论】:

  • 我认为定时器应该可以工作......
  • 您有可以帮助我的代码 sn-p 吗?如何使用计时器归档 4 个不同的运行时 (GPIO.HIGH)?

标签: python sockets timer raspberry-pi tornado


【解决方案1】:

我猜

def on_button_or_whatever():
    setGPIO_HIGH()
    timer = threading.Timer(target=setGPIO_LOW,5.0)
    timer.start() 

【讨论】:

  • 谢谢。我让它运行了:)
【解决方案2】:

感谢 Joran,我成功了。

这是我的代码。

GPIO_PUMP_1 = 17
GPIO_PUMP_2 = 27
GPIO_PUMP_3 = 22
GPIO_PUMP_4 = 10

#function which switches the relais off (low-active)
def setGPIO_HIGH(pin):
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin,     GPIO.OUT)
    GPIO.output(pin,    GPIO.LOW)
    return True


#function which switches the relais on (low-active)
def setGPIO_LOW(pin):
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pin,     GPIO.OUT)
    GPIO.output(pin,    GPIO.HIGH)
    return True



#main function for controlling the watering logic
def doStuff():
    setGPIO_HIGH(GPIO_PUMP_1)
    setGPIO_HIGH(GPIO_PUMP_2)
    setGPIO_HIGH(GPIO_PUMP_3)
    setGPIO_HIGH(GPIO_PUMP_4)

    timer1 = threading.Timer(5.0, setGPIO_LOW, [GPIO_PUMP_1])
    timer1.start()
    timer2 = threading.Timer(6.0, setGPIO_LOW, [GPIO_PUMP_2])
    timer2.start()
    timer3 = threading.Timer(7.0, setGPIO_LOW, [GPIO_PUMP_3])
    timer3.start()
    timer4 = threading.Timer(8.0, setGPIO_LOW, [GPIO_PUMP_4])
    timer4.start()

    WebSocketHandler.broadcast("all done")
    return True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多