配置ap。
import network ap_if = network.WLAN(network.AP_IF) ap_if.config(essid="simonzhang.net", authmode=network.AUTH_WPA_WPA2_PSK, password="simon")
代码部分,保存为main.py上传到板子上,重启。
import machine pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 4, 5)] html = """ HTTP/1.0 200 OKwww.simonzhang.net test %s """ def indexHandle(rtype, argv): if rtype == 'GET': try: num = argv.split('=')[1] except: num = 9999 if num == 9999: pass else: num1 = num[:-1] num2 = num[-1] if num1 == "OFF": machine.Pin(int(num2), machine.Pin.OUT, value=0) elif num1 == "ON": machine.Pin(int(num2), machine.Pin.OUT, value=1) rows = ['' % (str(p), p.value()) for p in pins] return html%'\n'.join(rows) def pinHandle(rtype, argv): print(rtype) print(argv) def route(getData): rtype = getData[0].split()[0] path = getData[0].split()[1] if path.find('?') > -1: urlPath = path.split('?')[0] argv = path.split('?')[1] else: urlPath = path argv = '' rou = {'/' : indexHandle(rtype, argv)} try: htmlContext = rou[urlPath] except: htmlContext = html%'error' return htmlContext import socket addr = socket.getaddrinfo('0.0.0.0', 8888)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) while True: cl, addr = s.accept() cl_file = cl.makefile('rwb', 0) tmp = '' line = '' while True: line = cl_file.readline() tmp = '%s%s' % (tmp, line.decode('utf-8')) if not line or line == b'\r\n': break cl.send(route(tmp.split('\r\n'))) cl.close() %s %d
连接ap,用浏览器登录192.168.4.1:8888端口
发表评论