树莓派RPi.GPIO+Flask构建WebApi实现远程控制
#!/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实现远程控制的更多相关文章
- 树莓派 - RPi.GPIO
RPi.GPIO是通过Python/C API实现的,C代码操作底层寄存器, python通过Python/C API调用这些C接口. 这是关于RPi.GPIO项目的介绍. 其中提到了有python ...
- 树莓派搭建基于flask的web服务器-通过移动端控制LED
1.概述 在局域网内,基于flask搭建web服务,从而可以使用移动客户端访问该web服务.由于是flask新手,所以本次实现的web服务功能较为简单,即控制LED灯的开/关及闪烁. 2.准备工作 2 ...
- 树莓派高级GPIO库,wiringpi2 for python使用笔记(一)安装
网上的教程,一般Python用RPi.GPIO来控制树莓派的GPIO,而C/C++一般用wringpi库来操作GPIO,RPi.GPIO过于简单,很多高级功能不支持,比如i2c/SPI库等,也缺乏高精 ...
- 【玩转开源】BananaPi R2——移植RPi.GPIO 到 R2
1. 首先给大家介绍一下什么是RPi.GPIO. 简单去讲,RPi.GPIO就是一个运行在树莓派开发板上可以通过Python去控制GPIO的一个中间件. 现在我这边做了一个基础功能的移植,接下来大家可 ...
- 关于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 ...
- nanopi NEO2 学习笔记 3:python 安装 RPi.GPIO
如果我要用python控制NEO2的各种引脚,i2c 或 spi ,RPi.GPIO模块是个非常好的选择 这个第三方模块是来自树莓派的,好像友善之臂的工程师稍作修改移植到了NEO2上,就放在 /roo ...
- RPi.GPIO 和 HM
后续笔记不再记录导入的模块和硬件的连接方法,请根据关键词自行搜索. RPi.GPIO模块 GPIO:General Purpose Input Output 即 通用输入/输出 RPi.GPIO是一个 ...
- 树莓派的GPIO编程
作者:Vamei 出处:http://www.cnblogs.com/vamei 严禁转载. 树莓派除了提供常见的网口和USB接口 ,还提供了一组GPIO(General Purpose Input/ ...
- 树莓派控制GPIO(Python)
如果你的raspi没有安装python那么先 sudo apt-get update sudo apt-get install python-dev 例如想要控制35管脚的亮灭: 先建一个文本 ...
随机推荐
- 我的开源经历:为了方便处理三方 HTTP 接口而写的 Java 框架
缘起 我以前公司需要在 Java 后台调用许多第三方 HTTP 接口,比如微信支付.友盟等等第三方平台. 公司内部还有很多服务是用世界最好语言写的,接口自然也只能通过 HTTP 接口来调用.于是日积月 ...
- SSL加密原理
对称加密算法 对称加密算法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密. 非对称加密算法 非对称加密算法(RSA)是内容加密的一类算法,它有两个秘钥:公钥与私钥 ...
- [原题复现+审计][BJDCTF2020]Mark loves cat($$导致的变量覆盖问题)
简介 原题复现:https://gitee.com/xiaohua1998/BJDCTF2020_January 考察知识点:$$导致的变量覆盖问题 线上平台:https://buuoj.cn( ...
- 已安装的nginx添加其他模块
总体操作就是添加新模块并重新编译源码,然后把编译后的nginx可执行文件覆盖原来的那个即可.1 查看已安装的参数nginx -V拷贝那些巴拉巴拉的参数,后面编译的时候使用 2 下载相同版本号的源码,解 ...
- CorelDRAW常用工具之涂抹工具
CDR作为绘图软件或者说平面设计软件使用频繁的功能之一,就是为绘制好的图片进行涂抹混色. 1.基本操作 CorelDRAW平面设计软件的涂抹工具是在形状工具组里的,打开左侧工具栏"形状&qu ...
- guitar pro系列教程(四): 详解Guitar Pro主音量自动化设置
让我们继续进行guitar pro 7系列教程 在上一章节中我们讲到插入速度自动化设置,本章节我们将采用图文结合的方式详细的讲解guitar pro 7主音量的相关自动化设置分别是:插入主音量自动化, ...
- kafka对接Rancher日志
kafka对接Rancher日志 目录 kafka对接Rancher日志 概述 环境准备 正常对接kafka集群 1.helm添加bitnami库 2.下载 kafka 对应的chart压缩文件 3. ...
- Springboot整合WebSocket实现网页版聊天,快来围观!
- Verilog 分频器
verilog设计进阶 时间:2014年5月6日星期二 主要收获: 1. 自己动手写了第一个verilog程序. 题目: 利用10M的时钟,设计一个单周期形状如下的周期波形. 思考: 最开始的想法是: ...
- Beta冲刺随笔——Day_Six
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta 冲刺 这个作业的目标 团队进行Beta冲刺 作业正文 正文 其他参考文献 无 今日事今日毕 林涛: ...