#!/usr/bin/env python3
# -*- coding: utf-8 -*- import RPi.GPIO as GPIO
from flask import Flask, request, jsonify app = Flask(__name__)
pwm_dict = {} # set the mode of raspberry pi
# when mod = 10 set the mode as GPIO.BOARD
# when mod = 11 set the mode as GPIO.BCM
@app.route('/setmode')
def setmode():
mod = request.args.get('mod')
try:
GPIO.setmode(int(mod))
return jsonify({"status": 1, "message": "success"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # get the mode of raspberry pi
# when mod = 10 the mode is GPIO.BOARD
# when mod = 11 the mode is GPIO.BCM
@app.route('/getmode')
def getmode():
try:
return jsonify({"status": 1, "message": "success", "value": GPIO.getmode()})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # set the pin mode of raspberry pi
# pin is relay on the raspberry pi board hardware and the mode of raspberry pi
# when mod = 1 set the pin mode as GPIO.IN
# when mod = 0 set the pin mode as GPIO.OUT
@app.route('/setup')
def setup():
pin = request.args.get('pin')
mod = request.args.get('mod')
try:
GPIO.setup(int(pin), int(mod))
return jsonify({"status": 1, "message": "success"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # clean the pin mode settings of raspberry pi
@app.route('/cleanup')
def cleanup():
try:
GPIO.cleanup()
return jsonify({"status": 1, "message": "success"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # set warning behavior of raspberry pi
# when val = 1 focus on the warning of changing default values
# when val = 0 ignore the warning of changing defualt values
@app.route('/setwarnings')
def setwarnings():
val = request.args.get('val')
try:
GPIO.setwarnings(int(val))
return jsonify({"status": 1, "message": "success"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # set the value of pin
# pin is relay on the raspberry pi board hardware and the mode of raspberry pi
# when val = 1 set the pin as high power
# when val = 0 set the pin as low power
@app.route('/output')
def output():
pin = request.args.get('pin')
val = request.args.get('val')
try:
GPIO.output(int(pin), int(val))
return jsonify({"status": 1, "message": "success"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # get the value of pin
# pin is relay on the raspberry pi board hardware and the mode of raspberry pi
# when val = 1 the pin is high power
# when val = 0 the pin is low power
@app.route('/input')
def input():
pin = request.args.get('pin')
try:
return jsonify({"status": 1, "message": "success", "value": GPIO.input(pin)})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # create a pwm of pin
# key is the identity of the pwm instance
# pin is relay on the raspberry pi board hardware and the mode of raspberry pi
# freq is the frequency of the pwm instance
@app.route('/create_pwm')
def create_pwm():
key = request.args.get('key')
pin = request.args.get('pin')
freq = request.args.get('freq')
try:
if pwm_dict.has_key(key):
return jsonify({"status": 0, "message": "key has been exsisted"})
else:
pwm = GPIO.PWM(pin, freq)
pwm_dict[key] = pwm
return jsonify({"status": 1, "message": "success"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # start an exsist pwm
# key is the identity of the pwm instance
# dtc is the duty cycle of the pwm instance
@app.route('/start_pwm')
def start_pwm():
key = request.args.get('key')
dtc = request.args.get('dtc')
try:
if pwm_dict.has_key(key):
pwm_dict[key].start(dtc)
return jsonify({"status": 1, "message": "success"})
else:
return jsonify({"status": 0, "message": "key is not exsisted"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # stop an exsist pwm
# key is the identity of the pwm instance
@app.route('/stop_pwm')
def stop_pwm():
key = request.args.get('key')
try:
if pwm_dict.has_key(key):
pwm_dict[key].stop()
del pwm_dict[key]
return jsonify({"status": 1, "message": "success"})
else:
return jsonify({"status": 0, "message": "key is not exsisted"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # change the frequency of the pwm instance
# key is the identity of the pwm instance
# freq is the new frequency set to the pwm instance
@app.route('/change_pwm_freq')
def change_pwm_freq():
key = request.args.get('key')
freq = request.args.get('freq')
try:
if pwm_dict.has_key(key):
pwm_dict[key].ChangeFrequency(freq)
return jsonify({"status": 1, "message": "success"})
else:
return jsonify({"status": 0, "message": "key is not exsisted"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) # change the duty cycle of the pwm instance
# key is the identity of the pwm instance
# dtc is the new duty cycle set to the pwm instance
@app.route('/change_pwm_dtc')
def change_pwm_dtc():
key = request.args.get('key')
dtc = request.args.get('dtc')
try:
if pwm_dict.has_key(key):
pwm_dict[key].ChangeDutyCycle(dtc)
return jsonify({"status": 1, "message": "success"})
else:
return jsonify({"status": 0, "message": "key is not exsisted"})
except Exception as e:
app.logger.error(e)
return jsonify({"status": 0, "message": "failed"}) if __name__ == '__main__':
app.config['DEBUG'] = True
app.run(host='0.0.0', port=59154)
for pwm in pwm_dict:
pwm.stop()
pwm_dict.clear()
GPIO.cleanup()

简易封装,打字不易,转载请注明出处!感谢!

树莓派RPi.GPIO+Flask构建WebApi实现远程控制的更多相关文章

  1. 树莓派 - RPi.GPIO

    RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口. 这是关于RPi.GPIO项目的介绍. 其中提到了有python ...

  2. 树莓派搭建基于flask的web服务器-通过移动端控制LED

    1.概述 在局域网内,基于flask搭建web服务,从而可以使用移动客户端访问该web服务.由于是flask新手,所以本次实现的web服务功能较为简单,即控制LED灯的开/关及闪烁. 2.准备工作 2 ...

  3. 树莓派高级GPIO库,wiringpi2 for python使用笔记(一)安装

    网上的教程,一般Python用RPi.GPIO来控制树莓派的GPIO,而C/C++一般用wringpi库来操作GPIO,RPi.GPIO过于简单,很多高级功能不支持,比如i2c/SPI库等,也缺乏高精 ...

  4. 【玩转开源】BananaPi R2——移植RPi.GPIO 到 R2

    1. 首先给大家介绍一下什么是RPi.GPIO. 简单去讲,RPi.GPIO就是一个运行在树莓派开发板上可以通过Python去控制GPIO的一个中间件. 现在我这边做了一个基础功能的移植,接下来大家可 ...

  5. 关于RPi.GPIO、BCM2835 c library、WiringPi、Gertboard

    1.RPi.GPIO//RPi.GPIO-0.5.5.tar.gz 开发者:python官网:https://www.python.org/ 官网:https://pypi.python.org/py ...

  6. nanopi NEO2 学习笔记 3:python 安装 RPi.GPIO

    如果我要用python控制NEO2的各种引脚,i2c 或 spi ,RPi.GPIO模块是个非常好的选择 这个第三方模块是来自树莓派的,好像友善之臂的工程师稍作修改移植到了NEO2上,就放在 /roo ...

  7. RPi.GPIO 和 HM

    后续笔记不再记录导入的模块和硬件的连接方法,请根据关键词自行搜索. RPi.GPIO模块 GPIO:General Purpose Input Output 即 通用输入/输出 RPi.GPIO是一个 ...

  8. 树莓派的GPIO编程

    作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...

  9. 树莓派控制GPIO(Python)

    如果你的raspi没有安装python那么先   sudo apt-get update sudo apt-get install python-dev   例如想要控制35管脚的亮灭: 先建一个文本 ...

随机推荐

  1. 攻防世界-Web_php_include (四种解法)

    攻防世界-Web_php_include   (考察的是文件包含) 打开页面是这样一段代码从代码中得知page中带有php://的都会被替换成空 str_replace()以其他字符替换字符串中的一些 ...

  2. 实时检测微信域名防红拦截检测API系统,最新腾讯域名屏蔽检测官方接口

    最近手里有个项目需要检测域名在微信里是否可以打开,如果被微信拦截,则需要进行下一步操作,所以需要判断域名的状态,但是微信官方并没有提供相关查询的方法,最后在网上找到了这个接口地址,分享给有需要的朋友. ...

  3. linux常见目录

  4. Mac 安装Homebrew慢的问题解决

    一开始安装,在官网上的命令: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/ma ...

  5. 干货分享:盘点那些最常用的Linux命令,需熟记!

  6. pyhon的6大基本数据类型

    1.数字型(Number) 1.1 整型(int) 整型包括所有的正整数,负整数还有0. 在python中所有的整型数据全部默认采用十进制进行表示,但我们还可以手动表示其他进制的整型,具体表示如下: ...

  7. Cloud-Native! 实战 Helm 3 部署 Traefik 2

    介绍 Traefik 是什么? Traefik, The Cloud Native Edge Router Traefik 是一种现代 HTTP 反向代理和负载均衡器,用于轻松部署微服务. 这篇文章对 ...

  8. B. Irreducible Anagrams【CF 1290B】

    思路: 设tx为t类别字符的个数. ①对于长度小于2的t明显是"YES"②对于字符类别只有1个的t明显是"YES"③对于字符类别有2个的t,如左上图:如果str ...

  9. 解决 win7 win10 等 64位 支持access数据库问题

    好多年不写ASP+ACCESS了,这两天帮朋友做个网站,碰到这个问题: ADODB.Connection 错误 '800a0e78' 对象关闭时,不允许操作. 记录一下. 以下内容转载自:https: ...

  10. 在Python实现print标准输出sys.stdout、stderr重定向及捕获的简单办法

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 Python中的标准输出和错误输出由sys模块的stdout.stde ...