基于树莓派的智能保温锅
来源:互联网 发布:讨厌知乎 编辑:程序博客网 时间:2024/04/28 02:06
project:code
基于树莓派的智能保温锅 ,采用Python-flask web开发框架,可以通过网页来控制电饭锅。
所需硬件:树莓派一个,继电器两个,杜邦线若干,电饭锅一个以及联网设备。
软件:sudo apt-get install python-flask
后台实现的代码:
#encoding:utf-8import RPi.GPIO as GPIOfrom flask import Flask, render_template, request,url_for,redirectimport sysreload(sys)sys.setdefaultencoding('utf8')app = Flask(__name__) # 创建一个名为app 的Flask 对象# 选择BCM编号方式,并创建一个名为pins 的字典对象,存放GPIO 接口编号、名字和状态信息。GPIO.setmode(GPIO.BCM)pins = { 24 : {'name' : '大火煮饭', 'state' : GPIO.LOW}, 25 : {'name' : '保 温', 'state' : GPIO.LOW} }# 把每个GPIO 接口都设为输出模式,并置为低电平。for pin in pins: GPIO.setup(pin, GPIO.OUT) GPIO.output(pin, GPIO.LOW)# 当用户访问树莓派的iP时,执行以下函数@app.route("/")def main(): for pin in pins: pins[pin]['state'] = GPIO.input(pin) # 把状态值存入pins 字典中的对应项 templateData = { 'pins' : pins # 把pins 字典对象放入模板数据字典中 } return render_template('main.html', **templateData) # 用模板数据字典渲染main.html 页面并把结果返回给用户@app.route('/logo.jpg')def logo(): return redirect(url_for('static', filename='logo.jpg'), code=301)@app.route('/on.png')def on_img_url(): return redirect(url_for('static', filename='on.ng'), code=302)@app.route('/off.png')def off_img_url(): return redirect(url_for('static', filename='off.ng'), code=303)# 当用户访问带有接口编号及操作的URL 时,执行下面的函数@app.route("/<changePin>/<action>")def action(changePin, action): changePin = int(changePin) deviceName = pins[changePin]['name'] if action == "on": GPIO.output(changePin, GPIO.HIGH) message = "Turned " + deviceName + " on." if action == "off": GPIO.output(changePin, GPIO.LOW) message = "Turned " + deviceName + " off." for pin in pins: pins[pin]['state'] = GPIO.input(pin) templateData = { #把pins 字典对象与表示状态的字符串放入模板数据字典中 'message' : message , 'pins' : pins } return render_template('main.html', **templateData)if __name__ == "__main__": app.run(host='0.0.0.0', port=80, debug=True) # 让服务器在80 端口上监听,并在出错时显示出错信息
网页代码
<!DOCTYPE html><head><title>智能保温锅</title> </head><body background="/static/bg.jpg"><center><h1>智能保温锅系统控制界面</h1> <img src="/static/logo.jpg" border="0" widgth="200" height="200"></a> <h2>学生姓名: <font size ="5" color="red" >张小红</font> </h2> {% for pin in pins %} <table border="0" width="250px height="50px"> <tr> <th width="100px" height="40px"> <font size ="4" color="blue" >{{ pins[pin].name }}: </font></th> <th width="150px" height="40px"><a href="/{{pin}}/on"><img src="/static/on.png" border="0" widgth="32" height="32"></a> <a href="/{{pin}}/off"><img src="/static/off.png" border="0" widgth="32" height="32"></a></th> </tr></table>{% endfor %}{% if message %} <h3>{{ message }}</h3>{% endif %}</center></body></html>
0 0
- 基于树莓派的智能保温锅
- 基于树莓派的定时智能豆浆机控制系统
- 基于wifi的智能开关
- 基于FPGA的智能窗户
- 基于模型的智能测试
- 基于DragonBord410C的智能遥控
- 基于DragonBoard410C的智能管家
- 基于ESP8266的智能插座
- 基于单片机的智能密码锁
- 基于树莓派的智能寝室终端(Python练手)1
- 基于树莓派的智能寝室终端(Python练手)2
- 基于树莓派的智能寝室终端(Python练手)4
- 基于windows系统的智能DNS
- 基于DS18B20的智能温度测量装置
- 基于自然语言对话的智能辅导系统
- 基于FPGA的智能温度采集控制器
- 基于 C8051F 的智能测量系统
- 基于MC9S12DGl28单片机的智能寻迹车设计
- (java)leetcode Move Zeros
- eclipse中xsd的验证问题cvc-complex-type error with xsd
- JSessionId谈cookie与session的区别和联系
- 黑马程序员————Java语法基础
- attrs文件.自定义组件
- 基于树莓派的智能保温锅
- PHP 实现简单的模板引擎
- Java读取csv文件
- IOS开发笔记22-block与@protocol
- C++继承与派生(一)
- PHP实现二分法查找(递归)
- 获取设备屏幕方向变化的代码
- 【Matlab开发】matlab删除数组中符合条件的元素与散点图绘制
- 二叉树模板