基于树莓派的智能保温锅

project:code

基于树莓派的智能保温锅 ,采用Python-flask web开发框架,可以通过网页来控制电饭锅。

所需硬件:树莓派一个,继电器两个,杜邦线若干,电饭锅一个以及联网设备。

软件:sudo apt-get install python-flask

后台实现的代码:

#encoding:utf-8
import RPi.GPIO as GPIO
from flask import Flask, render_template, request,url_for,redirect
import sys
reload(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>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值