Created
February 21, 2015 12:03
-
-
Save artizirk/0f02df46a10bc0e4e145 to your computer and use it in GitHub Desktop.
blink a led at tartu raspberry pi workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from time import sleep | |
from flask import Flask, redirect, request | |
app = Flask(__name__) | |
@app.route("/") | |
def hello(): | |
head="""<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">""" | |
style="""<style></style>""" | |
if request.remote_addr == "192.168.10.100": | |
style += "nope" | |
return head+style+'<a href="/on" class="btn btn-success">on</a> <a href="/off" class="btn btn-danger">off</a> <a href="/blink" class="btn btn-default">blink</a>' | |
@app.route("/on") | |
def on(): | |
if request.remote_addr == "192.168.10.100": | |
return "nope" | |
led(0) | |
return redirect("/") | |
@app.route("/off") | |
def off(): | |
if request.remote_addr == "192.168.10.100": | |
return "nope" | |
led(1) | |
return redirect("/") | |
@app.route("/blink") | |
def blink(): | |
if request.remote_addr == "192.168.10.100": | |
return "nope" | |
led(0) | |
sleep(0.1) | |
led(1) | |
sleep(0.1) | |
led(0) | |
sleep(0.1) | |
led(1) | |
sleep(0.1) | |
return redirect("/") | |
def setup(): | |
print "setup" | |
if not os.path.exists("/sys/class/gpio/gpio2"): | |
with open("/sys/class/gpio/export", "w") as f: | |
f.write("2\n") | |
sleep(0.5) | |
with open("/sys/class/gpio/gpio2/direction", "w") as f: | |
f.write("out\n") | |
def led(value): | |
print "led val={}".format(value) | |
with open("/sys/class/gpio/gpio2/value", "w") as f: | |
f.write("{}\n".format(value)) | |
if __name__ == "__main__": | |
setup() | |
app.run(host="0.0.0.0", port=80) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment