调用 Web 服务时用 Raspberry Pi 点亮 LED(Flask,python)

Light a LED with a Raspberry Pi when a call is made to a web service (Flask, python)

每次在我们后端的网络服务上进行调用时,我都想使 LED 闪烁。

它在工作,但只工作了几个小时,之后就停止工作了。

每次我们 post 在这个 URL (http://IP_ADRESS:PORT/scan) 上,我们的 LED 都会闪烁。

我想知道为什么在一定时间后它停止工作..

这是完成 POST 时调用的代码:

@app.route('/scan', methods = ['POST'])
def scan():
    time.sleep(0.5)
    try:
            LED.rainbow()        
    finally:
           print("Scanned")
    return 'Scanned!!!'

可以找到 LED.rainbow() 代码 here。 整个项目的代码可以找到here.

当它停止工作时,我通过 raspberry Pi 上的 ssh 连接,我做了:

python
>>> import app
>>> app.scan()

这会使 LED 闪烁!!我不明白为什么它使用 ssh 而不是通过 http POST。

一开始以为是Flask server挂了,后来用CURL调用,一切正常:

$ http -f POST http://IP_ADDRESS:PORT/scan
HTTP/1.0 200 OK
Content-Length: 13
Content-Type: text/html; charset=utf-8
Date: Thu, 02 Jun 2016 20:11:02 GMT
Server: Werkzeug/0.11.9 Python/2.7.9

Scanned!!!

以下是我在 raspberry Pi 上启动 Flask 服务器的方式:

$ sudo crontab -e
# Add this line at the end of the file
@reboot python /home/pi/Desktop/Claudie/app.py &

我错过了什么?

感谢任何帮助!

谢谢!

发现错误!

问题出在 rainbow() 函数中。

这个函数被多次调用,在某个时间,它停止工作。

函数的作用如下:

def rainbow():
    RED, GREEN, BLUE = initializeGPIO() # setup the GPIO, outputs, and PWM

    # Light the LED

    GPIO.cleanup()

我注意到在循环中调用此函数(没有网络服务器)停止工作。

所以我删除了 cleanup()initializeGPIO(),现在一切正常:) 我真的不知道为什么初始化 GPIO(或清理)会使 LED 停止工作,如果有人知道,我会很高兴阅读它!