zoukankan      html  css  js  c++  java
  • 【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()
        
  • 相关阅读:
    土豆案例(display:none和block的应用)
    显示和隐藏
    鼠标经过提高层级案例(margin,相对定位,z-index)
    垂直对齐vertical-align
    表单初始化
    使用定位隐式转换为行内块元素
    清除浮动的方法
    定位的盒子叠放顺序z-index
    FreeRTOS-为什么关中断之后切换进程?
    PowerPC-关闭中断后,还能报sc中断?
  • 原文地址:https://www.cnblogs.com/colipso/p/3533412.html
Copyright © 2011-2022 走看看