esp8266做AP启动WEB服务器控制led

配置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 OK



    
    
    www.simonzhang.net test
    

    %s
    

GPIO0  

GPIO4  

GPIO5  

""" 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 = ['%s%d' % (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()

esp8266we控制led源码

连接ap,用浏览器登录192.168.4.1:8888端口


控制led成功。

发表评论

电子邮件地址不会被公开。 必填项已用*标注