uc-wifi-rx-udp/src/main.py

70 lines
1.5 KiB
Python
Raw Normal View History

2021-07-14 00:54:27 +02:00
# main.py
import machine
import usocket as socket
from time import sleep
from esc import Esc
from wifi import WifiAP
from wifi import WifiSTA
# import web
PIN_M1_ENA = 5 # D1
PIN_M1_IN1 = 4 # D2
PIN_M1_IN2 = 0 # D3
PIN_M2_IN1 = 12 # D6
PIN_M2_IN2 = 13 # D7
PIN_M2_ENA = 15 # D8
esc = None
def main():
print("main")
global esc
wifi_sta = WifiSTA()
# wifi_ap = WifiAP()
esc = Esc(PIN_M1_ENA, PIN_M1_IN1, PIN_M1_IN2,
PIN_M2_ENA, PIN_M2_IN1, PIN_M2_IN2)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
s.bind(("", 80))
while True:
request = ""
try:
request, addr = s.recvfrom(256)
req = request.decode()
print("data: {}, from: {}".format(req, addr))
if len(request) == 0:
print("no data")
continue
except:
print("error: no data")
pass
if "fwd" in req:
print("--> fwd")
esc.fwd()
elif "left" in req:
print("--> left")
esc.left()
elif "rev" in req:
print("--> rev")
esc.rev()
elif "right" in req:
print("--> right")
esc.right()
elif "b1" in req:
print("--> b1")
esc.b1()
elif "b2" in req:
print("--> b2")
esc.b2()
if __name__ == "__main__":
main()