Run multiple python functions using flask
Question: **2nd UPDATE**

Almost there!! But getting a "ValueError: Attempting to use a port that is not
open"

>
>     File "c:\Python27\lib\site-packages\flask\app.py", line 1701, in
> __call__
>     return self.wsgi_app(environ, start_response)
>  
>     File "c:\Python27\lib\site-packages\flask\app.py", line 1689, in
> wsgi_app
>     response = self.make_response(self.handle_exception(e))
>  
>     File "c:\Python27\lib\site-packages\flask\app.py", line 1687, in
> wsgi_app
>     response = self.full_dispatch_request()
>  
>     File "c:\Python27\lib\site-packages\flask\app.py", line 1360, in
> full_dispatch_request
>     rv = self.handle_user_exception(e)
>  
>     File "c:\Python27\lib\site-packages\flask\app.py", line 1358, in
> full_dispatch_request
>     rv = self.dispatch_request()
>  
>     File "c:\Python27\lib\site-packages\flask\app.py", line 1344, in
> dispatch_request
>     return self.view_functions[rule.endpoint](**req.view_args)
>  
>     File "G:\OverAir\arduino\server.py", line 19, in light_off
>     board.output([pin])
>  
>     File "G:\OverAir\arduino\arduino.py", line 16, in output
>     self.__sendData(len(pinArray))
>  
>     File "G:\OverAir\arduino\arduino.py", line 56, in __sendData
>     while(self.__getData()[0] != "w"):
>  
>     File "G:\OverAir\arduino\arduino.py", line 61, in __getData
>     return self.serial.readline().rstrip('\n')
>  
>     File "c:\Python27\lib\site-packages\serial\serialwin32.py", line 221, in
> read
>     if not self.hComPort: raise portNotOpenError
>  
>     ValueError: Attempting to use a port that is not open
>  
>
> UPDATE
>
> The code from @Blender answer below is probably correct. The issue now I am
> having is I'm getting **"SerialException: could not open port COM5: [Error
> 5] Access is denied."**
>
> However, if I run my original, on.py or off.py script individually, it works
> fine...I don't see how the new code is not working...unless does it try to
> open COM5 twice??
>
> END UPDATE

I trying to run multiple python functions, or separate .py scripts, using
Flask; in order to generate separate URLs. Just trying to turn the Arduino
ON/OFF running it locally.

For example, I have on.py and off.py and want to build sever.py. So, when I
open 127.0.0.1:5000/on it runs on.py, similarly 127.0.0.1:5000/off runs
off.py,

After a few hours, I just decided to add both .py functions into the
server.py, but of course it doesn't work...I think I just don't understand the
parameters usage.

Really thank you in advance!

    
    
    ## Open a serial connection with Arduino.
    
    from time import sleep
    from arduino import Arduino
    
    from flask import Flask
    
    
    b = Arduino("COM5",9600)
    pin = 13
    num = 5
    
    ON = Flask("Light_ON")
    OFF = Flask("Light_OFF")
    
    
    def LightON():
        #declare output pins as a list/tuple
        b.output([pin])
        b.setHigh(pin)
        b.close()
        return 'Light is ON'
    
    def LightOFF():
        #declare output pins as a list/tuple
        b.output([pin])
        b.setLow(pin)
        return 'Light is OFF'
    
    @ON.route('/ON')
    @OFF.route('/OFF')
    
    ON.run()
    OFF.run()  //I know this doesn't run, just not sure how to include it.
    


Answer: Something like this should work:

    
    
    from arduino import Arduino
    from flask import Flask
    
    app = Flask('light_control')
    board = Arduino('COM5', 9600)
    pin = 13
    
    @app.route('/on')
    def light_on():
        board.output([pin])
        board.setHigh(pin)
        board.close()
    
        return 'Light is ON'
    
    @app.route('/off')
    def light_off():
        board.output([pin])
        board.setLow(pin)
        # board.close() ???
    
        return 'Light is OFF'
    
    if __name__ == '__main__':
        app.debug = True
        app.run()
    

