sumorobot-remote/main.py

44 lines
978 B
Python
Raw Permalink Normal View History

2024-11-03 13:29:43 +00:00
import socket
from machine import Pin, ADC
from utime import sleep_ms
import os
while not wlan.isconnected():
sleep_ms(100)
adc = ADC(0)
2024-11-03 18:38:26 +00:00
# Wemos D1 Mini (277010)
# Pin 16 forward pin 4 backwards
#forward = Pin(0, Pin.IN)
#backward = Pin(2, Pin.IN)
# Wemos D1 Normal Arduino
# Pin 16 forward pin 4 backwards
2024-11-03 13:29:43 +00:00
forward = Pin(16, Pin.IN)
backward = Pin(4, Pin.IN)
2024-11-03 18:38:26 +00:00
2024-11-03 13:29:43 +00:00
calibration = adc.read()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.bind(("", 37020))
while True:
try:
sockaddr = socket.getaddrinfo('sumo-%s' % config["remote-id"], 44444)[0][-1]
except OSError:
2024-11-03 15:54:59 +00:00
print("failed to resolve sumo-%s" % config["remote-id"])
2024-11-03 13:29:43 +00:00
sleep_ms(500)
else:
break
2024-11-03 15:54:59 +00:00
#sockaddr = "100.101.100.70", 44444
print("Sending packets to", sockaddr)
2024-11-03 13:29:43 +00:00
while True:
msg = "%d:%d:%d" % (adc.read()-calibration, not forward.value(), not backward.value())
2024-11-03 18:38:26 +00:00
print(msg)
2024-11-03 13:29:43 +00:00
sock.sendto(msg.encode("ascii"), sockaddr)
sleep_ms(30)