bobo | NPC | 创造力: | 帖子: | 发消息 | 串个门 | 加好友 | 打招呼
2014-7-1 21:50:09 [显示全部楼层]
17677浏览
查看: 17677|回复: 2

[教程] 《边学边用树莓派 -11》Raspberry Pi 做Web服务器-上

[复制链接]
前面我们聊过用 树莓派从Internet 服务器上获取信息。下面我们来试试让树莓派作为服务器对外提供信息。

我的住所养了四盆植物,老是忘记浇水,而奄奄一息。有必要开发一款自动浇花系统。希望实现以下功能:
1. 每盆植物都具备土壤湿度传感器,当土壤变干,达到一个值的时候自动实现浇花。并且可以在Web界面显示各个花盆的土壤湿度数据。
2. 具备室内温湿度传感器,可以在Web界面显示。同时这个数据也可以做一些逻辑,例如温度达到多少的时候不能浇花。(土壤温度太高浇水容易损害植物) 。
3. Web界面具备手动浇花的功能,有时候看看土壤湿度,手动浇浇花,还是很有乐趣的,你也可以将权限开放给朋友让他试试浇花。
4. 具备一个摄像头,能将当前的花园图片上传到Web界面显示, 朋友们也可以看看你的花园呢。
5. 技术方面,登录界面需要有用户名和密码输入校验功能。可以实现外网控制。

下面是需要的一些材料:
1. 树莓派主板     1只
2. DFRobot 新推出的Raspberry Pi Meet Arduino 扩展板  1只
3. 数字继电器模块   4只
4. 土壤湿度传感器  4只
5. Si7021温湿度传感器  1只
6.带水管潜水泵      4只

Raspberry Pi 基于Liunx 可以安装多种 Web框架,他将Web界面的设计和 Python代码分离开来,通过Web框架来编写Web项目非常方便。
Python 可以使用的Web框架有 Django、Web2PY、Web.py、Flask。
在这里使用Flask (http://flask.pocoo.org),它是一个简洁的Web框架。

安装Flask需要安装pip。如果没有安装可以用下面的命令安装:
  1. root@raspberrypi:/home/pi# apt-get install python-pip
复制代码
安装Flask:
  1. root@raspberrypi:/home/pi# pip install flask
复制代码
建立一个名为  webwatering.py 的文件
  1. #!usr /bin/python
  2. import RPi.GPIO as GPIO   
  3. from flask import Flask,render_template,request
  4. app = Flask(__name__)
  5. GPIO.setmode(GPIO.BCM)
  6. pins = {
  7.     24 : {'name' : 'banian','state' : GPIO.LOW},
  8.     25 : {'name' : 'jasmine','state' : GPIO.LOW}
  9. }
  10. for pin in pins:
  11.     GPIO.setup(pin,GPIO.OUT)
  12.     GPIO.output(pin,GPIO.LOW)
  13. @app.route("/")
  14. def main():
  15.     for pin in pins:
  16.     GPIO.setup(pin,GPIO.OUT)
  17.     GPIO.output(pin,GPIO.LOW)
  18. @app.route("/")
  19. def main():
  20.     for pin in pins:
  21.         pins[pin]['state'] = GPIO.input(pin)
  22.     templateData = {
  23.         'pins' : pins
  24.     }
  25.     return render_template("main.html",**templateData)
  26. @app.route("/<changePin>/<action>")
  27. def action(changePin,action):
  28.     changePin = int(changePin)
  29.     deviceName = pins[changePin]['name']
  30.     if action == "on":
  31.         GPIO.output(changePin,GPIO.HIGH)
  32.         message = " Truned " + deviceName + " on. "
  33.     if action == "off":
  34.         GPIO.output(changePin,GPIO.LOW)
  35.         message = " Turned " + deviceName + " off. "
  36.     if action == "toggle":
  37.         GPIO.output(changePin,not GPIO.input(changePin))
  38.         message = "Toggled " + deviceName + "."
  39.     for pin in pins:
  40.         pins[pin]['state'] = GPIO.input(pin)
  41.     templateData = {
  42.         'mesage' : message,
  43.         'pins'   : pins
  44.     }
  45.     return render_template("main.html",**templateData)
  46. if __name__ == "__main__":
  47.     app.run(host="192.168.1.118",port=80,debug=True)
复制代码
在 webwatering.py 同目录建立一个 templates 文件夹 ,在里面建立一个main.html的文件,内容如下:
  1. <!DOCTYPE html>
  2.   <head>
  3.     <title>Web Watering Status</title>
  4.   </head>
  5.   <body>
  6.     <h1>Web Watering Status</h1>
  7.     {% for pin in pins %}
  8.     <p>The {{ pins[pin].name }}
  9.     {% if pins[pin].state == true %}
  10.       is currently on (<a href="/{{pin}}/off">turn off</a>)
  11.     {% else %}
  12.       is currently off (<a href="/{{pin}}/on">turn on</a>)
  13.     {% endif %}
  14.     </p>
  15.     {% endfor %}
  16.     {% if message %}
  17.     <h2>{{ message }}</h2>
  18.     {% endif %}
  19.   </body>
  20. </html>
复制代码
运行 webwatering.py  
  1. root@raspberrypi:/home/pi/webwatering# python webwatering.py
  2. webwatering.py:14: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  3.   GPIO.setup(pin,GPIO.OUT)
  4. * Running on http://192.168.1.118:80/
  5. * Restarting with reloader
  6. webwatering.py:14: RuntimeWarning: This channel is already in use, continuing anyway.  Use GPIO.setwarnings(False) to disable warnings.
  7.   GPIO.setup(pin,GPIO.OUT)
  8. 192.168.1.207 - - [22/Jun/2014 21:16:19] "GET /25/on HTTP/1.1" 200 -
  9. 192.168.1.207 - - [22/Jun/2014 21:16:22] "GET /25/off HTTP/1.1" 200 -
  10. 192.168.1.207 - - [22/Jun/2014 21:16:22] "GET /favicon.ico HTTP/1.1" 404 -
  11. 192.168.1.207 - - [22/Jun/2014 21:16:22] "GET /favicon.ico HTTP/1.1" 404 -
  12. 192.168.1.207 - - [22/Jun/2014 21:16:23] "GET /24/on HTTP/1.1" 200 -
  13. 192.168.1.207 - - [22/Jun/2014 21:16:24] "GET /24/off HTTP/1.1" 200 -
复制代码

在浏览器输入192.168.1.118:80  点击按钮即可对 链接到GPIO24和GPIO25上的两个继电器模块进行控制
《边学边用树莓派 -11》Raspberry Pi 做Web服务器-上图1
未完待续............

librax  学徒

发表于 2014-8-6 09:53:27

不错,期待更新。。。
回复

使用道具 举报

rulioo  学徒

发表于 2014-9-1 13:38:46

:)有点半途而废
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail