借鉴自:树莓派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


