Created
September 15, 2016 17:42
-
-
Save benyaminsalimi/7e47fa53e404aa1bfa45d9a0bb13b7f5 to your computer and use it in GitHub Desktop.
simple RestApi for RaspberryPi RPIO & camera model with flask
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
# BPi restFull-api by @BenyaminSalimi | |
# you can see log of this program in "BPi.log" | |
from flask import Flask,jsonify,send_file | |
import picamera | |
import time | |
import RPi.GPIO as GPIO | |
app = Flask(__name__) | |
GPIO.setmode(GPIO.BCM) | |
### index page | |
@app.route("/") | |
def hello(): | |
return "BPi Api is running " | |
### info | |
@app.route("/info") | |
def info(): | |
return jsonify(GPIO.RPI_INFO) # can be some of this info or any thing else ! prove connection is perfect | |
#### Stat | |
@app.route("/pinstat/<pin>") | |
def readPin(pin): | |
try: | |
GPIO.setup(int(pin), GPIO.IN) # setup gpio | |
if GPIO.input(int(pin)) == True: | |
response = "high" # this is for high voltage | |
else: | |
response = "low" # this is for low voltage | |
except: | |
response = "Erorr" # this is erorr | |
return jsonify(pin=pin,stat=response) | |
### Actions | |
@app.route("/action/<act>/<pin>") | |
def acting(act,pin): | |
pin=int(pin) | |
GPIO.setup(pin,GPIO.OUT) | |
if act== "high": | |
GPIO.output(pin, GPIO.HIGH) | |
return jsonify(pin=pin,action="High") | |
elif act== "low": | |
GPIO.output(pin, GPIO.LOW) | |
return jsonify(pin=pin, action="low") | |
elif act== "tog": | |
GPIO.output(pin, not GPIO.input(pin)) | |
return jsonify(pin=pin, action="toggle") | |
else: | |
return jsonify(pin=pin, action="Not valid Commend") | |
### set all pin high or low | |
@app.route("/setall/<stat>") | |
def setAll(stat): | |
if stat== "high": | |
try: | |
GPIO.setup(range(0,26),GPIO.OUT) | |
GPIO.output(range(0,26), GPIO.HIGH) | |
return jsonify(pin="All", action="high") | |
except: | |
return jsonify(Erorr="Can not be high") | |
elif stat == "low": | |
try: | |
GPIO.setup(range(0,26),GPIO.OUT) | |
GPIO.output(range(0,26), GPIO.LOW) | |
return jsonify(pin="All", action="low") | |
except: | |
return jsonify(Erorr="Can not be low!") | |
### GPIO checking function | |
@app.route("/showfunction/<pin>") | |
def showPinFunc(pin): | |
GPIO.setmode(GPIO.BOARD) | |
return jsonify(pin=pin,function= GPIO.gpio_function(pin)) | |
### taking pic of now | |
@app.route("/shot") | |
def take_pic(): | |
## this is happing when camera not connected or in use by other app :-/ | |
try: | |
pic = picamera.PiCamera() | |
except: | |
return jsonify(erorr="can not acess camera!") | |
# you can change the resolution ! | |
pic.resolution = (1280, 720) | |
#you can change the format, as you see y= years and other is the same ;) | |
picName = time.strftime('bpi-%Y-%m-%d-%H%M%S')+'.jpg' | |
pic.capture(picName) | |
return send_file(picName, mimetype='image/jpg') | |
### Server | |
if __name__ == "__main__": | |
## logging to file "BPI.log" | |
import logging | |
logging.basicConfig(filename='BPi.log', level=logging.DEBUG) | |
## running option | |
app.run(host='0.0.0.0', port=80, debug=True) # any where access | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment