micropython 範例二十
用手機 V7RC app 透過 WIFI-UDP 來控制自走車
android / IOS 下需先安裝 V7RC app,手機要先用 WiFi 連上 ESP32 的網路來使用
# 範例二十:
# 用手機 V7RC app 透過 WIFI-UDP 來控制自走車
# 手機需安裝 V7RC app
# 需要的 module : motor、dgram、ble_uart,tools,const
# FB : https://www.facebook.com/mason.chen.1420
from motor import L9110S,Car
import network
import uasyncio
from dgram import UDPServer
import math
CH1 = None
CH2 = None
def cb(msg,adr):
CH1 = int(((msg).decode('utf-8')[3:7]),10)
CH2 = int(((msg).decode('utf-8')[7:11]),10)
print(CH1)
if math.fabs(CH1 - 1500) < 100 and math.fabs(CH2 - 1500) < 100:
car.stop()
elif math.fabs(CH1 - 1500) > math.fabs(CH2 - 1500):
if CH1 > 1500:
car.move(4)
else:
car.move(3)
else:
if CH2 > 1500:
car.move(1)
else:
car.move(2)
CH1 = 1500
CH2 = 1500
m1 = L9110S(25, 26)
m2 = L9110S(32, 33)
car=Car(m1,m2); car.wheel(1, 1)
wlan_ap = network.WLAN(network.AP_IF); wlan_ap.active(True); wlan_ap.config(essid='ESP32_V7RC',password='12341234')
s = UDPServer(); l= uasyncio.get_event_loop(); l.run_until_complete(s.serve(cb, '192.168.4.1',6188))