I've started working with Raspberry Pi and I've faced with architecture problem. I want to make python app which blink the LED using GPIO and have Web interface to start and stop blinking. In the Web there are a lot of examples of how to use Flask to make one action (e.g. enable LED, disable LED etc.) but I haven't found example how to run Flask web server and also run parallel "job" to blink LED or in general to run action by timer.
One solution is to have different applications for that 2 purposes. One for blinking. And one for Flask server. But this approach requires a database to share data between applications, I wouldn't like to do it in IoT device (Raspberry Pi). So I'd prefer single monolitic app.
Could anyone help me with idea how to run action by timer within Flask application? Here is pseudocode to show what I expect.
# main.py
import time
import Flask
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW)
app = Flask()
def blinking():
while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn on
time.sleep(1) # Sleep for 1 second
GPIO.output(8, GPIO.LOW) # Turn off
time.sleep(1)
if __name__ == 'main':
app.run_in_background()
blinking()
Thanks for any ideas!