树莓派3B+ 手机h5网页控制GPIO

ezgif-4-5d181e9b42.gif

本文介绍如何在H5页面控制树莓派GPIO

仅实现以BCM编码方式控制GPIO,其他方式未做.

演示地址:http://gpio.exmll.cn/ 点击BCM编码列即可控制对应GPIO

1.部署准备

1.树莓派3b+
2.webpy
3.杜邦线\LED灯若干

2.安装webpy

    2.1 pip install web.py
    2.2 如果2.1安装失败 
        使用sudo wget http://webpy.org/static/web.py-0.38.tar.gz
        sudo tar -zxvf  web.py-0.38.tar.gz
        cd web.py-0.38
        sudo python setup.py install

3.新建code.py文件

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import web
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)
try:
    import RPi.GPIO as GPIO
except RuntimeError:
    print("导入 RPi.GPIO 时出现错误!这可能由于没有超级用户权限造成的。您可以使用 'sudo' 来运行您的脚本。")
urls = (
    '/', 'index',
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index:
    def GET(self):
        name = '树莓派40pin引脚对照表'
        footnote = '欢迎收藏'
        wiringPi = 'wiringPi编码'
        BCM = 'BCM编码'
        fun = '功能名'
        pin = '物理引脚'
        web.header('Content-Type', 'text/html; charset=UTF-8')
        return (render.index(name,footnote,wiringPi,BCM,fun,pin))
    def POST(self):
        i = web.input()
        channel = i.channel
        mode = i.mode
        state = i.st
        print(i)
        stateStr = ''
        if state == '1':
            stateStr = '高电平'
        else:
            stateStr = '低电平'
        setGPIO(mode,int(channel),int(state))
        return ("SUCCEED:以BCM方式设置GPIO%s为%s" % (channel,stateStr))

def setGPIO(mode,channel,state):
    if mode == 'BOARD':
        GPIO.setmode(GPIO.BOARD)
    if mode == 'BCM':
        GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    GPIO.setup(channel, GPIO.OUT)
    GPIO.output(channel, state)


if __name__ == "__main__":
    # web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
    app.run()

4.新建templates文件夹 并在文件下新建index.html

$def with (name,footnote,wiringPi,BCM,fun,pin)
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>$name</title>


    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://apps.bdimg.com/libs/jquerymobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
</head>
<script>
         jQuery(document).ready(function(){
              jQuery("li").click(function(){
                  state = 1;
                  if(jQuery(this).hasClass("bcm"))
                  {
                     if(jQuery(this).hasClass("outColor"))
                     {
                       state = 0;
                       jQuery(this).removeClass("outColor");
                     }else
                     {
                       state = 1;
                      jQuery(this).addClass("important outColor");
                     }
                     jQuery.post("/",{
                          channel:jQuery(this).text(),
                          mode:"BCM",
                          st:state
                     },
                      function(data,status){
                            alert(data);
                      });
                  }

                   if(jQuery(this).hasClass("bord"))
                  {
                     alert(jQuery(this).text()+"bcm");
                  }

             });
         });

    </script>
<style>
   body ul li{
     text-align:center;
     height:40px;
     line-height:40px;
   }
    .outColor{
       color: red;
    }
    .threev{
        background-color:rgb(255,160,32);
    }
    .firvev{
        background-color:rgb(255,33,21);
        color: white;
        border: 1px solid black;
    }
    .gray{
        background-color:rgb(190,192,191);
         border: 1px solid black;
    }
    .white{
        background-color:rgb(255,255,255);
        border: 1px solid black;
    }
    .bcm{
        background-color:rgb(255,239,176);
         border: 1px solid black;
    }
    .wiringpi{
        background-color:rgb(199,237,160);

         border: 1px solid black;
    }
    .darkblue{
        background-color:rgb(5,51,255);
        color: white;
        border: 1px solid black;
    }
    .blue{
        background-color:rgb(1,255,255);
        color: black;
        border: 1px solid black;
    }
    .black{
         background-color:rgb(0,0,0);
         color: white;
          border: 1px solid black;
    }
    .grassgreen{
        background-color:rgb(148,222,73);
         border: 1px solid black;
    }
    .purple
    {
        background-color:rgb(157,68,185);
        color: white;
         border: 1px solid black;
    }
</style>
</head>
<body>
<div data-role="page" id="pageone" data-theme="f" >
    <div data-role="header">
        <h1>$name</h1>
    </div>
    <div  class="ui-grid-a mgin">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">$wiringPi</li>
                    <li class="bcm">$BCM</li>
                    <li class="white">$fun</li>
                    <li class="gray">$pin</li>
                </ul>
            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">$pin</li>
                    <li class="white">$fun</li>
                    <li class="bcm">$BCM</li>
                    <li class="wiringpi">$wiringPi</li>

                </ul>
            </div>
        </div>
    </div>



    <div class="ui-grid-a mgin">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi"></li>
                    <li class="bcm"></li>
                    <li class="threev">3.3V</li>
                    <li class="gray ">1</li>
                </ul>
            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">2</li>
                    <li class="firvev">5V</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>
            </div>
        </div>
    </div>
    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">8</li>
                    <li class="bcm">2</li>
                    <li class="blue">SDA.1</li>
                    <li class="gray ">3</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">4</li>
                    <li class="firvev">5V</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>

            </div>
        </div>
    </div>

    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">9</li>
                    <li class="bcm">3</li>
                    <li class="blue">SCL.1</li>
                    <li class="gray ">5</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c"  data-role="navbar">
                <ul>
                    <li class="gray">6</li>
                    <li class="black">GND</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>

            </div>
        </div>
    </div>

    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c"  data-role="navbar">
                <ul>
                    <li class="wiringpi">7</li>
                    <li class="bcm">4</li>
                    <li class="grassgreen"> GPIO7</li>
                    <li class="gray ">7</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">8</li>
                    <li class="darkblue">TXD</li>
                    <li class="bcm">14</li>
                    <li class="wiringpi">15</li>
                </ul>

            </div>
        </div>
    </div>

    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi"></li>
                    <li class="bcm"></li>
                    <li class="black">GND</li>
                    <li class="gray ">9</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">10</li>
                    <li class="darkblue">RXD</li>
                    <li class="bcm">15</li>
                    <li class="wiringpi">16</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">0</li>
                    <li class="bcm">17</li>
                    <li class="grassgreen">GPIO0</li>
                    <li class="gray ">11</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">12</li>
                    <li class="grassgreen">GPIO1</li>
                    <li class="bcm">18</li>
                    <li class="wiringpi">1</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">2</li>
                    <li class="bcm">27</li>
                    <li class="grassgreen">GPIO2</li>
                    <li class="gray">13</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>

                    <li class="gray">14</li>
                    <li class="black">GND</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">3</li>
                    <li class="bcm">22</li>
                    <li class="grassgreen">GPIO3</li>
                    <li class="gray">15</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">16</li>
                    <li class="grassgreen">GPIO4</li>
                    <li class="bcm">23</li>
                    <li class="wiringpi">4</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi"></li>
                    <li class="bcm"></li>
                    <li class="threev">3.3V</li>
                    <li class="gray ">17</li>
                </ul>
            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">18</li>
                    <li class="grassgreen">GPIO5</li>
                    <li class="bcm">24</li>
                    <li class="wiringpi">5</li>
                </ul>

            </div>
        </div>
    </div>



    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c"  data-role="navbar">
                <ul>
                    <li class="wiringpi">12</li>
                    <li class="bcm">10</li>
                    <li class="purple">MOSI</li>
                    <li class="gray ">19</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c"  data-role="navbar">
                <ul>
                    <li class="gray">20</li>
                    <li class="black">GND</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>

            </div>
        </div>
    </div>



    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">13</li>
                    <li class="bcm">9</li>
                    <li class="purple">MISO</li>
                    <li class="gray ">21</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">22</li>
                    <li class="grassgreen">GPIO6</li>
                    <li class="bcm">25</li>
                    <li class="wiringpi">6</li>
                </ul>

            </div>
        </div>
    </div>



    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">14</li>
                    <li class="bcm">11</li>
                    <li class="purple">SCLK</li>
                    <li class="gray ">23</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">24</li>
                    <li class="purple">CEO</li>
                    <li class="bcm">8</li>
                    <li class="wiringpi">10</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi"></li>
                    <li class="bcm"></li>
                    <li class="black">GND</li>
                    <li class="gray ">25</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">26</li>
                    <li class="purple">CE1</li>
                    <li class="bcm">7</li>
                    <li class="wiringpi">11</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">30</li>
                    <li class="bcm">0</li>
                    <li class="blue">SDA.0</li>
                    <li class="gray ">27</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">28</li>
                    <li class="blue">SCL.0</li>
                    <li class="bcm">1</li>
                    <li class="wiringpi">31</li>
                </ul>

            </div>
        </div>
    </div>



    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">21</li>
                    <li class="bcm">5</li>
                    <li class="grassgreen">GPIO21</li>
                    <li class="gray ">29</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">30</li>
                    <li class="black">GND</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">22</li>
                    <li class="bcm">6</li>
                    <li class="grassgreen">GPIO22</li>
                    <li class="gray ">31</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">32</li>
                    <li class="grassgreen">GPIO26</li>
                    <li class="bcm">12</li>
                    <li class="wiringpi">26</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c"  data-role="navbar">
                <ul>
                    <li class="wiringpi">23</li>
                    <li class="bcm">13</li>
                    <li class="grassgreen">GPIO23</li>
                    <li class="gray ">33</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">34</li>
                    <li class=" black">GND</li>
                    <li class="bcm"></li>
                    <li class="wiringpi"></li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">24</li>
                    <li class="bcm">19</li>
                    <li class="grassgreen">GPIO24</li>
                    <li class="gray ">35</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">36</li>
                    <li class="grassgreen">GPIO27</li>
                    <li class="bcm">16</li>
                    <li class="wiringpi">27</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi">25</li>
                    <li class="bcm">26</li>
                    <li class="grassgreen">GPIO25</li>
                    <li class="gray ">37</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">38</li>
                    <li class="grassgreen">GPIO28</li>
                    <li class="bcm">20</li>
                    <li class="wiringpi">28</li>
                </ul>

            </div>
        </div>
    </div>


    <div class="ui-grid-a">
        <div class="ui-block-a">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="wiringpi"></li>
                    <li class="bcm"></li>
                    <li class="black">GND</li>
                    <li class="gray ">39</li>
                </ul>

            </div>
        </div>
        <div class="ui-block-b">
            <div class="ui-grid-c" data-role="navbar">
                <ul>
                    <li class="gray">40</li>
                    <li class="grassgreen">GPIO29</li>
                    <li class="bcm">21</li>
                    <li class="wiringpi">29</li>
                </ul>

            </div>
        </div>
    </div>

    <div data-role="footer">
        <h1>$footnote</h1>
    </div>

</div>


</body>
</html>

5.运行程式 sudo python code.py 8080

6.源码下载:https://github.com/guiguihao/RaspberryWeb

在手机上浏览器打开 http://你的ip:8080
在电脑上打开可能会丢失css样式,显示不完整,看我的另外两遍文章即可解决
//www.greatytc.com/p/99dd0ece88cc//www.greatytc.com/p/c76aba56abe2

7.推荐一个linux命令行:https://rootopen.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容