配置ap。
1 2 3 |
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上传到板子上,重启。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
import machine pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 4, 5)] html = """ HTTP/1.0 200 OK <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"/> <title>www.simonzhang.net test</title> </head> <body> %s <p>GPIO0 <a href=\"?pin=ON0\"><button>ON</button></a> <a href=\"?pin=OFF0\"><button>OFF</button></a></p> <p>GPIO4 <a href=\"?pin=ON4\"><button>ON</button></a> <a href=\"?pin=OFF4\"><button>OFF</button></a></p> <p>GPIO5 <a href=\"?pin=ON5\"><button>ON</button></a> <a href=\"?pin=OFF5\"><button>OFF</button></a></p> </body> </html> """ 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 = ['<tr><td>%s</td><td>%d</td></tr>' % (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() |
连接ap,用浏览器登录192.168.4.1:8888端口
发表评论