Skip to content

Instantly share code, notes, and snippets.

@hkwi
Last active July 11, 2016 10:49
Show Gist options
  • Save hkwi/65e750aaefd1fa021e6434715f5078e2 to your computer and use it in GitHub Desktop.
Save hkwi/65e750aaefd1fa021e6434715f5078e2 to your computer and use it in GitHub Desktop.
from flask import Flask, request
app = Flask(__name__)
mac2gpio = dict(
"00:00:00:00:00:00": 17,
"00:00:00:00:00:00": 22,
)
if os.path.exists("/sys/class/gpio/gpio17"):
with open("/sys/class/gpio/unexport", "w") as w:
w.write("17\n")
with open("/sys/class/gpio/export", "w") as w:
w.write("17\n")
with open("/sys/class/gpio/gpio17/direction", "w") as w:
w.write("out\n")
if os.path.exists("/sys/class/gpio/gpio22"):
with open("/sys/class/gpio/unexport", "w") as w:
w.write("22\n")
with open("/sys/class/gpio/export", "w") as w:
w.write("22\n")
with open("/sys/class/gpio/gpio22/direction", "w") as w:
w.write("out\n")
def get_mac():
for l in open("/proc/net/arp"): # Linux required
e = l.split()
if e[0] == request.environ["REMOTE_ADDR"]:
return e[3].lower()
@app.route("/open/<battery>")
def hopen(battery):
pin = mac2gpio.get(mac())
if pin:
with open("/sys/class/gpio/gpio%d/value" % pin, "w") as w:
w.write("0\n")
return ""
@app.route("/close/<battery>")
def hclose(battery):
pin = mac2gpio.get(mac())
if pin:
with open("/sys/class/gpio/gpio%d/value" % pin, "w") as w:
w.write("1\n")
return ""
if __name__=="__main__":
app.run(port=80)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment