#!/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. Linux 文本处理批量查找与替换

    # 搜索含有特定字符串在某个目录并打印出文件名grep -rl "www.baidu.com" /data* -r, --recursive like --directories= ...

  2. wget 快速下载 ftp 文件

    GNU Wget 1.17.1,非交互式的网络文件下载工具. 用法: wget [选项]... [URL]... 长选项所必须的参数在使用短选项时也是必须的. 启动: -V, --version 显示 ...

  3. [原题复现][CISCN 2019 初赛]WEB-Love Math(无参数RCE)[未完结]

    简介  原题复现:  考察知识点:无参数命令执行  线上平台:https://buuoj.cn(北京联合大学公开的CTF平台) 榆林学院内可使用信安协会内部的CTF训练平台找到此题 源码审计 代码 1 ...

  4. go返回json数据

    package main import ( "encoding/json" ) type Repay struct { Code uint64 `json:"code&q ...

  5. 深度分享:面试阿里,字节跳动,美团90%会被问到的HashMap知识

    一,HashTable 哈希表,它相比于hashMap结构简单点,它没有涉及红黑树,直接使用链表的方式解决哈希冲突. 我们看它的字段,和hashMap差不多,使用table存放元素 private t ...

  6. Java学习之Swing Gui编程

    Java学习之Swing Gui编程 0x00 前言 前面的使用的Gui是基于Awt 去进行实现,但是在现实写Gui中 AWT实际运用会比较少. 0x01 Swing 概述 AWT 和Swing 区别 ...

  7. MathType中怎么打约化普朗克常数ħ

    普朗克常数记为ħ,是一个物理常数,用以描述量子大小.在量子力学中占有重要的角色,马克斯·普朗克在1900年研究物体热辐射的规律时发现的.如果要打出关于约化普朗克常数ħ的公式,就需要用到专业的公式编辑器 ...

  8. 基于Koa2+mongoDB的后端博客框架

    主要框架:koa2全家桶+mongoose+pm2. 在阅读前建议将项目克隆到本地配合食用,否则将看得云里雾里. 项目地址:https://github.com/YogurtQ/koa-server. ...

  9. 3.深入Istio:Pilot配置规则ConfigController

    转载请声明出处哦~,本篇文章发布于luozhiyun的博客:https://www.luozhiyun.com 本文使用的Istio源码是 release 1.5. Config Controller ...

  10. CoProcessFunction实战三部曲之一:基本功能

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...