【Raspberry Pi】webpy+mysql+GPIO 实现手机控制

1、mysql

http://dev.mysql.com/doc/refman/5.5/en/index.html

安装

sudo apt-get install update

sudo apt-get install mysql-server-5.5 mysql-client-5.5

2、安装web.py

sudo apt-get install python-pip

sudo pip install web.py

html模板

$def with(todos)
    $if todos=='me':
        <html>
        <body>
        <h1>My test</h1>
        <p>Hello World</p>
        <hr />
        <form name='input' action="www.baidu.com" method="get">
        <input type="text" name="user">
        <input type="submit" value="sub_mit">
        </form>
        </body>
        </html>
    $else:
        <html>
        <body>
        <ul heigh="">
        $for word in todos:
            <li id="$word.id">$word.title</li>
        </ul>
        <hr />
        <form method="post" action="add">
        <input type="text" name="title"/>
        <input type="submit" value="Add" />
        </form>
        <hr />
        <h1>control LED</h1>
        <form method="post" action="led">
        <input type="radio" name='open_close' value="1" /> OPEN LED
        <br />
        <br />
        <br />
        <input type="radio" name='open_close' value="0" /> CLOSE LED
        <br />
        <br />
        <br />
        <input type="submit" value="submit" />
        </form>
        </body>
        </html>

主程序

# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 03:08:25 2014

@author: pi
"""

import web
import RPi.GPIO as gpio

gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(7,gpio.OUT)
gpio.setup(11,gpio.OUT)

gpio.output(7,gpio.HIGH)
gpio.output(11,gpio.HIGH)

render=web.template.render('templates/')

urls = (
'/','index',
'/add','add',
'/led','led'
)

app = web.application(urls,globals())

db=web.database(dbn='mysql',user='root',pw='******',db='myweb')

class index:
    def GET(self):
        todos=db.select('todo')
        return render.index(todos)
        
class add:
    def POST(self):
        i=web.input()
        if i.title=='open':
            gpio.output(7,gpio.LOW)
        elif i.title=="close":
            gpio.output(7,gpio.HIGH)

        db.insert('todo',title=i.title)
        raise web.seeother('/')

class led:
    def POST(self):
        i=web.input()
        if i.open_close=="1":
            gpio.output(11,gpio.LOW)
        if i.open_close=="0":
            gpio.output(11,gpio.HIGH)
        raise web.seeother("/")    
        
if __name__=="__main__":
    app.run()
    

 

你可能感兴趣的:(mysql)