栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

借鉴自:树莓派3B+ 手机h5网页控制GPIO - 简书 (jianshu.com)

源码亲测有效,有细微的问题需要注意。

1.安装web.py

我用的是python3,所以需改为pip3

pip3 install web.py

成功安装界面如下

 

2.新建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()

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

$def with (name,footnote,wiringPi,BCM,fun,pin)


    
    $name


    
    
    
    



   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;
    }




    
        $name
    
    
        
            
                
  • $wiringPi
  • $BCM
  • $fun
  • $pin
  • $pin
  • $fun
  • $BCM
  • $wiringPi
  • 3.3V
  • 1
  • 2
  • 5V
  • 8
  • 2
  • SDA.1
  • 3
  • 4
  • 5V
  • 9
  • 3
  • SCL.1
  • 5
  • 6
  • GND
  • 7
  • 4
  • GPIO7
  • 7
  • 8
  • TXD
  • 14
  • 15
  • GND
  • 9
  • 10
  • RXD
  • 15
  • 16
  • 0
  • 17
  • GPIO0
  • 11
  • 12
  • GPIO1
  • 18
  • 1
  • 2
  • 27
  • GPIO2
  • 13
  • 14
  • GND
  • 3
  • 22
  • GPIO3
  • 15
  • 16
  • GPIO4
  • 23
  • 4
  • 3.3V
  • 17
  • 18
  • GPIO5
  • 24
  • 5
  • 12
  • 10
  • MOSI
  • 19
  • 20
  • GND
  • 13
  • 9
  • MISO
  • 21
  • 22
  • GPIO6
  • 25
  • 6
  • 14
  • 11
  • SCLK
  • 23
  • 24
  • CEO
  • 8
  • 10
  • GND
  • 25
  • 26
  • CE1
  • 7
  • 11
  • 30
  • 0
  • SDA.0
  • 27
  • 28
  • SCL.0
  • 1
  • 31
  • 21
  • 5
  • GPIO21
  • 29
  • 30
  • GND
  • 22
  • 6
  • GPIO22
  • 31
  • 32
  • GPIO26
  • 12
  • 26
  • 23
  • 13
  • GPIO23
  • 33
  • 34
  • GND
  • 24
  • 19
  • GPIO24
  • 35
  • 36
  • GPIO27
  • 16
  • 27
  • 25
  • 26
  • GPIO25
  • 37
  • 38
  • GPIO28
  • 20
  • 28
  • GND
  • 39
  • 40
  • GPIO29
  • 21
  • 29
$footnote

4.在此基础上。树莓派硬件如图搭建简单电路。

5.在终端运行可输入如下指令,或者用树莓派自带Thonny Python IDE.

python3 code.py

 运行结果如下 

可在手机上浏览器输入树莓派ip+端口号8080(192.168.137.146:8080),即可出现如下

点击bcm部分,会变红,同时相应gpio口会变成高电平,会点亮led灯,并会弹出窗口提示。

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649925.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号