实验环境:PT8.2
实验目标:通过实验了解基于PT8.2的LED灯远程控制实验
实验拓扑:
![](https://img-blog.csdnimg.cn/img_convert/008ce8e8c68d595c3a045a26c0b797f8.png)
实验过程:
设备准备
(1)设备连接
如实验拓扑图所示,设备连接。
(2)设备IP地址配置
SBC0增加一个以太网口。
PC0 | SBC0 | |
IP地址 | 192.168.25.2 | 192.168.25.1 |
子网掩码 | 255.255.255.0 | 255.255.255.0 |
程序编写
本实验通过PC远程控制LED灯的开关。通过程序在SBC实现HTTP服务和LED控制。程序如下:
main.py
from gpio import *
from time import *
from http import *
#callback triggered with the root address.(1.1.1.1)
def onRouterRoot(url,res):
print("Request for /")
res.send(getHTML());
#callback trigged when the On button is pressed.
def onRouterOn(url,res):
print("ON")
digitalWrite(0,HIGH);
#callback trggered when the off button is pressed.
def onRouterOff(url,res):
print("off")
digitalWrite(0,LOW)
#setup for the root, on and off actions.
def setup():
HTTPServer.route("/",onRouterRoot)
HTTPServer.route("/on",onRouterOn)
HTTPServer.route("/off",onRouterOff)
#start server on port 80
HTTPServer.start(80)
def getHTML():
html="welocme PT8.2<br>"
html+="""<form method="get" action="/on"><button type="submit">LED ON</button></form>"""
html+="""<form method="get" action="/off"><button type="submit">LED OFF</button></form>"""
return html
def main():
setup()
while True:
delay(1000)
if __name__=="__main__":
main()
设备控制
(1)在PC浏览器访问192.168.25.1
![](https://img-blog.csdnimg.cn/img_convert/5d5ab5bf4fe8d2fbad43951e44ef1338.png)
(2)点击页面中的按钮观察LED变化
![](https://img-blog.csdnimg.cn/img_convert/bbba3bad17eff1e1bd8179d798fc2df9.png)