3469| 2
|
[入门教程] 掌控WIFI--AP模式下遥控小车 |
之前有写过掌控下连接已有的wifi,分配到一个ip地址,制作网页控制灯的点亮。这次尝试利用掌控的WIFI--AP模式(“接入点”或“热点”),摆脱局域网的束缚。 外加另一个帖子提到的控制舵机,结合喵家的360舵机,iobit扩展板,能量模块的18650电池仓,搭了个小车,手机连接掌控的AP热点,浏览器登录分配的ip,通过设置好的网页按钮控制小车运动。 代码如下: [mw_shl_code=python,true]import socket import network from mpython import * ap = network.WLAN(network.AP_IF) ap.config(essid = 'ZK_demo') ap.active(True) CONTENT = """<!DOCTYPE HTML> <html> <head><meta charset = "utf-8"> <title>ZK-CAR</title> </head> <body> <h1>掌控小车控制</h1> <p><a href="/?car=go"><button class="button">GO</button></a></p> <p><a href="/?car=stop"><button class="button button2">STOP</button></a></p> <p><a href="/?car=back"><button class="button">BACK</button></a></p> </body> </html> """ addr = socket.getaddrinfo(ap.ifconfig()[0], 80)[0][-1] s = socket.socket() s.bind(addr) s.listen(1) oled.fill(0) oled.DispChar('Connect on', 0, 0) oled.DispChar(str(addr), 0, 16) oled.show() while True: conn, addr = s.accept() print('Connection from %s' %str(addr)) request = conn.recv(1024) request = str(request) print('Content = %s' %request) car_go = request.find('/?car=go') car_stop = request.find('/?car=stop') car_back = request.find('/?car=back') servo_1 = PWM(Pin(Pin.P1), freq=50) servo_2 = PWM(Pin(Pin.P8), freq=50) if car_go == 6: print('GO') servo_1.duty(int(0.5/20*1023)) servo_2.duty(int(2.5/20*1023)) if car_stop == 6: print('STOP') servo_1.duty(int(1.5/20*1023)) servo_2.duty(int(1.5/20*1023)) if car_back == 6: print('BACK') servo_1.duty(int(2.5/20*1023)) servo_2.duty(int(0.5/20*1023)) conn.send(CONTENT) conn.close()[/mw_shl_code] 效果如下: 手机WIFI连接掌控板提供的名为"ZK_demo"的无线。浏览器登录"192.168.4.1",就会出来如上图所示的掌控小车控制界面。通过三个按钮来实现小车的前进、停止、后退 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed