#!/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. 漏洞利用-FTP漏洞利用

    一.环境说明 目标IP: 本人虚拟机 192.168.80.134 ,使用 metasploit2 攻击IP: 本人虚拟机 192.168.80.129 ,使用 kali 二.匿名用户登录 root@ ...

  2. bWAPP----PHP Code Injection

    PHP Code Injection 主要代码 1 <div id="main"> 2 3 <h1>PHP Code Injection</h1> ...

  3. ppt-1 操作界面与基本操作

    1.Ctrl+N快速建立新文档 2.新模板:文件--新建--可免费搜索.下载新模板 3.恢复未保存的演示文稿 文件--打开(首先看到的是近期使用的演示文稿,)--鼠标滚动至末尾,可看到"恢复 ...

  4. H5系列之drag拖放

    H5中, 有个属性,draggable="true", 这个属性呢(默认false),需要加在标签上,加上去该标签就可以拖动了, 看下gif图吧 默认的标签,是不能拖动的,但是有两 ...

  5. day008|python之函数

    函数 目录 函数 1 Type hinting 2 函数参数 2.1 概述 2.2 参数详解 2.3 参数的使用 2.4 可变长函数-->*与**的应用 2.6 命名关键字形参 3 函数对象 3 ...

  6. 蓝桥杯——快速排序(2018JavaB组第5题9分)

    快速排序(18JavaB5,9') 以下代码可以从数组a[]中找出第k小的元素. 它使用了类似快速排序中的分治算法,期望时间复杂度是O(N)的. 请仔细阅读分析源码,填写划线部分缺失的内容. impo ...

  7. High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis

    论文来源:CVPR 2017 摘要 之前方法的缺点:之前的方法是基于语义和上下文信息的,在填充较大holes的表现得很好,能够捕获更高级的图像特征,但是由于内存限制和难以训练网络的因素,只能处理分辨率 ...

  8. python办公入门7:xlwt

    xlwt写入excel步骤 创建工作簿 创建工作表 填充工作表内容 保存文件 1 import xlwt 2 3 #创建工作簿 4 wb=xlwt.Workbook() 5 #创建工作表 6 ws=w ...

  9. 使用RestTemplate,显示请求信息,响应信息

    使用RestTemplate,显示请求信息,响应信息 这里不讲怎么用RestTemplate具体细节用法,就是一个学习中的过程记录 一个简单的例子 public class App { public ...

  10. 软件工程与UML第三次作业

    博客班级 软件工程与UML2班 作业要求 本次作业要求 作业目标 <给至少5名同学提他的代码issue并用博客记录;根据收到的issue修改自己的代码> 作业源代码 我的码云仓库 学号 & ...