我的问题对您来说可能很简单,但是我才刚刚开始,所以请帮助我。
我正在运行家庭自动化脚本,并尝试将其与Tornado Web服务器结合使用。
最好的方法是什么?
基本的龙卷风服务器:
import tornado.ioloop
import tornado.web
import os.path
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render('index.html')
application = tornado.web.Application([
(r"/", MainHandler)
application = tornado.web.Application([
(r"/check", MainHandler)
])
if __name__ == "__main__":
print 'Starting Server'
print 'Press ctrl+c to close'
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
和一个Python脚本:
import RPi.GPIO as GPIO
import time
import os
motion = 14
relay = 2
GPIO.setmode(GPIO.BCM)
GPIO.setup(motion, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setup(relay, GPIO.OUT)
GPIO.output(relay, GPIO.HIGH)
previous = False
current = False
while True:
time.sleep(1)
previous = current
current = GPIO.input(motion)
if current != previous:
new = "HIGH"
GPIO.output(relay, GPIO.LOW)
print("GPIO pin %s is %s" % (motion, new))
os.system("sudo omxplayer ring.mp3 &")
time.sleep(5)
else:
GPIO.output(relay, GPIO.HIGH)
print("No motions")
如果您的脚本是作为函数编写的,则可以导入并调用它。但是,由于它包含同步代码,因此您需要使用aThreadPoolExecutor
从Tornado进行调用。
或者,最好将它作为子进程启动。请参阅tornado.process.Subprocess
和标准库的subprocess
模块。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句