The Raspberry Pi is a series of small single-board computers. https://www.raspberrypi.org/
-
gpiozero: A simple interface to everyday GPIO components used with Raspberry Pi. (https://gpiozero.readthedocs.io/en/stable/index.html)
-
LED control example: https://gpiozero.readthedocs.io/en/stable/index.html
- LED light on/off button: following example below. And set up circuit as below: https://www.raspberrypi.org/learning/python-quick-reaction-game/worksheet/
- implement call back function when button for LED light on/off
- when LED on/off triggered, a message will be sent to EC2 server (described below)
- server process request with LED light status
- web browser/apps query server for LED light status and display to end user
- ngnix server up
- flask server up
- mongod up
sudo pip install Flask
sudo pip install flask-pymongo
cd /path/to/code
export FLASK_DEBUG=1
export FLASK_APP=server.py
flask run --host=0.0.0.0
- spin up ec2 instance (Amazon Linux)
- get
.pem
key ssh -i /Users/xinr/Downloads/sapHackathon.pem [email protected]
- setup Mongo on centOS: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/
(@note
: change .pem file to 600: chmod 600 /Users/xinr/Downloads/sapHackathon.pem
)
(@note
: user name is root
, not centos
. aws turotial is wrong at http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html)
http://flask.pocoo.org/docs/0.12/deploying/#deployment
sudo yum install nginx
sudo /etc/init.d/nginx start
- nginx will redirect requests to localhost(127.0.0.1)
Ref: http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/
http://localhost:5000/status?udid=d1
http://localhost:5000/update?udid=d1&status=closed
@note
: I didn't do param checking or exeception handling, here is just a super simple demo
sudo python -m pip install pymongo
https://api.mongodb.com/python/current/tutorial.html
sudo mkdir -p /Users/xinr/Downloads/mongodb/
mongod --dbpath /Users/xinr/Downloads/mongodb/
use local
db.status.insert({'deviceId':'d1', 'status':'open'})
db.status.insert({'deviceId':'d2', 'status':'occupied'})
> db.status.find().pretty()
{
"_id" : ObjectId("58e9681d2533a670f43efb17"),
"deviceId" : "d1",
"status" : "open"
}
{
"_id" : ObjectId("58e9683c2533a670f43efb18"),
"deviceId" : "d2",
"status" : "occupied"
}
- click 'Add' to add a green circle
- Drag circle to map
- click 'Refresh'
- Green circle: room not occupied
- Red circle: room is occupied
http://ec2-34-209-46-58.us-west-2.compute.amazonaws.com:5000/
- create a folder called
templates
- and use built-in
render_template
def hello(name=None):
return render_template('hello.html', name='name')
Ref: http://flask.pocoo.org/docs/0.12/quickstart/#rendering-templates
- need to have a
static
folder setup (for css/js files) - then:
<link rel= "stylesheet" type= "text/css" href= "{{ url_for('static',filename='styles/mainpage.css') }}">
Ref: http://stackoverflow.com/questions/22259847/application-not-picking-up-css-file-flask-python