e210 engineering cyber -physical systems (spring 2021) flask

30
Flask Bryce Himebaugh E210 Engineering Cyber-Physical Systems (Spring 2021)

Upload: others

Post on 04-Jan-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

Flask

Bryce Hime baugh

E210 Engineering Cyber-Physical Systems (Spring 2021)

Page 2: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

https://engr210.github.io/

Page 3: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTONINDIANA UNIVERSITY BLOOMINGTON

Raspberry PI/Basys3 Link

Page 4: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

Flask Overview

Page 5: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Flask1. Python Micro Web Framework

– Minimalist compared to Django Framework

2. First Release in 2010

3. Hammer vs toolbox

4. Used in projects as the API …

Page 6: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

System Architecture

Silo Pi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA Board

Pivot.iuiot.orgMQTT Broker

MQTT Messages

Your Flask Web Server

MQTT Messages

Browser Access to PI Systems

Page 7: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

Minimal Example

Page 8: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

from flask import Flaskapp = Flask(__name__)@app.route('/')def index():

return 'Hello world'if __name__ == '__main__':

app.run(debug=True, port=55346, host='0.0.0.0')

Page 9: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

from flask import Flaskapp = Flask(__name__)@app.route('/')def index():

return 'Hello world'if __name__ == '__main__':

app.run(debug=True, port=55346, host='0.0.0.0')

Page 10: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

from flask import Flaskapp = Flask(__name__)@app.route('/')def index():

return 'Hello world'if __name__ == '__main__':

app.run(debug=True, port=55346, host='0.0.0.0')

Page 11: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

from flask import Flaskapp = Flask(__name__)@app.route('/')def index():

return 'Hello world'if __name__ == '__main__':

app.run(debug=True, port=55346, host='0.0.0.0')

Page 12: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

from flask import Flaskapp = Flask(__name__)@app.route('/')def index():

return 'Hello world'if __name__ == '__main__':

app.run(debug=True, port=55346, host='0.0.0.0')

Page 13: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

Python Function Decorators

Page 14: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Functions

https://realpython.com/primer-on-python-decorators/#functions

Page 15: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Functions can be Passed as Arguments

https://realpython.com/primer-on-python-decorators/#functions

Page 16: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Functions within Functions (Inner Functions)

https://realpython.com/primer-on-python-decorators/#functions

first_child andsecond_child only exist inside of parent

Page 17: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Returning Functions

https://realpython.com/primer-on-python-decorators/#functions

Function Reference Returned

Can call these functions like any other function

Page 18: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Decorators

https://realpython.com/primer-on-python-decorators/#functions

Decorators wrap the original function

Page 19: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Dynamic Behavior

https://realpython.com/primer-on-python-decorators/#functions

When called after bedtime …

Page 20: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

@ symbol

Same as say_whee = my_decorator(say_whee)

https://realpython.com/primer-on-python-decorators/#functions

Page 21: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

Back to the Example …

Page 22: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

from flask import Flaskapp = Flask(__name__)@app.route('/')def index():

return 'Hello world'if __name__ == '__main__':

app.run(debug=True, port=55346, host='0.0.0.0')

Page 23: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

Running the Code on Silo

Page 24: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Find an Open Port

Page 25: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Screen Command1. Versatile Linux Utility

2. Allows User Processes to Continue to Run after Terminal Closes

– Detach and Reattach

3. Key Bindings: https://www.gnu.org/software/screen/manual/html_node/Default-Key-Bindings.html

Page 26: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Running Screen

New Screen Process

Start ServerDetach with ServerStill Running

Page 27: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Page 28: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

Reattaching Right back where we left off when the detach was issued

Page 29: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

CTRL-C to stop

Page 30: E210 Engineering Cyber -Physical Systems (Spring 2021) Flask

INDIANA UNIVERSITY BLOOMINGTON

System Architecture

Silo Pi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA BoardPi

Press/Temp Sensor

FPGA Board

Pivot.iuiot.orgMQTT Broker

MQTT Messages

Your Flask Web Server

MQTT Messages

Browser Access to PI Systems