简体   繁体   English

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

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

Each time a call is made on a web service on our backend, I want to blink a LED. 每次在我们后端的Web服务上进行呼叫时,我都想闪烁LED。

It's working, but only for few hours, after that, it just stops working. 它正在工作,但只有几个小时,之后,它就停止工作了。

Each time we post on this URL ( http://IP_ADRESS:PORT/scan ), our LED is blinking. 每次我们发布此URL( http:// IP_ADRESS:PORT / scan )时,我们的LED都会闪烁。

I'm wondering why, after a certain amount of time it stops working.. 我想知道为什么,经过一段时间它停止工作..

Here is the code called when a POST is done: 以下是POST完成时调用的代码:

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

The LED.rainbow() code can be found here . 可以在这里找到LED.rainbow()代码。 The code of the whole project can be found here . 整个项目的代码可以在这里找到。

When it stops working, I connect via ssh on the raspberry Pi and I do: 当它停止工作时,我通过覆盆子Pi上的ssh连接,我做:

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

And this make the LED blinking!! 这使LED闪烁! I don't understand why it's working with ssh and not via the http POST. 我不明白为什么它使用ssh而不是通过http POST。

At the beginning, I thought the Flask server was crashed, but when I try to call it via CURL, everything is fine: 一开始,我认为Flask服务器崩溃了,但是当我尝试通过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!!!

Here is how I launch my Flask server on the raspberry Pi: 以下是我在raspberry Pi上启动Flask服务器的方法:

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

What am I missing? 我错过了什么?

Any help is appreciate! 任何帮助都很感激!

Thanks! 谢谢!

Found the bug! 发现了这个bug!

The problem is in the rainbow() function. 问题出在rainbow()函数中。

This function is called severals time, and at a certain time, it stops working. 此功能被称为多次,并且在某个时间停止工作。

Here is what the function do: 这是函数的功能:

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

    # Light the LED

    GPIO.cleanup()

I noticed that calling this function in a loop (without the web server) stops working. 我注意到在循环中调用此函数(没有Web服务器)停止工作。

So I removed the cleanup() and initializeGPIO() , and now everything is working perfectly :) I don't really know why initializing the GPIO (or cleanup) makes the LED stop working, if someone knows, I'll be happy to read it! 所以我删除了cleanup()initializeGPIO() ,现在一切都运行得很完美:)我真的不知道为什么初始化GPIO(或清理)会让LED停止工作,如果有人知道,我会很乐意阅读!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM