#!/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. 粉丝少的UP主如何赚大钱

    常逛B站的小伙伴应该知道,B站官方经常会推出各类征稿活动,奖金池也非常高,少则几万,多则上百万,可以说非常受UP主们的欢迎. 图1:B站各类活动 要知道,除了少数头部UP主可能因为没(有)有(钱)看( ...

  2. FL studio系列教程(十六):FL Studio查看菜单讲解

    FL Studio中每个窗口的显示.隐藏和布局命令都在查看菜单中.其中它被分为窗口.布局和浏览器3个部分,各项名称都有其单独的作用.窗口部分主要是软件的显示的一些菜单这里就不详细讲解了,接下来我们重点 ...

  3. 「LOJ 541」「LibreOJ NOIP Round #1」七曜圣贤

    description 题面很长,这里给出题目链接 solution 用队列维护扔掉的红茶,同时若后扔出的红茶比先扔出的红茶编号更小,那么先扔出的红茶不可能成为答案,所以可以用单调队列维护 故每次询问 ...

  4. 蓝桥杯-RP大冒险-未解决

    RP大冒险 问题描述 请尽情使用各种各样的函数来测试你的RP吧~~~ 输入格式 一个数N表示测点编号. 输出格式 一个0~9的数. 样例输入 0 样例输出 X {当且仅当输出仅有一个数X且X为0~9的 ...

  5. 大白话详解大数据HBase核心知识点,老刘真的很用心(2)

    前言:老刘目前为明年校招而努力,写文章主要是想用大白话把自己复习的大数据知识点详细解释出来,拒绝资料上的生搬硬套,做到有自己的理解! 01 HBase知识点 第6点:HRegionServer架构 为 ...

  6. Docker实战 | 第一篇:Centos8 安装 Docker

    1. 安装依赖包 yum install -y yum-utils device-mapper-persistent-data lvm2 2. 配置镜像源 yum config-manager --a ...

  7. 20201101_Python的虚拟环境问题

    虚拟环境使用总结: 1. 安装创建虚拟环境要使用的工具virtualenv  pip install virtualenv -i https://pypi.douban.com/simple/ #使用 ...

  8. python模块wifi使用小记

    安装命令 pip install wifi 连接命令 sudo wifi connect --add-hoc ssid,使用该命令会修改/etc/network/interfaces配置文件,导致启动 ...

  9. Python中repr(变量)和str(变量)的返回值有什么区别和联系

    Python中repr(变量)和str(变量)都返回一个描述对象的字符串,二者有关联又有不同.由于Python3.0后都是新式类,我们的分析也是基于新式类进行的.基于object派生的新式类中二者之间 ...

  10. PyQt(Python+Qt)学习随笔:Qt Designer中toolBar的toolButtonStyle属性

    tooButtonStyle属性保存主工具栏按钮的样式设置,用来表示工具栏按钮的文字和图标怎么显示. 该属性的可设置值类型为枚举类型Qt.ToolButtonStyle,它包含如下值: 该属性的缺省值 ...