tx-udp: initial commit
This commit is contained in:
commit
8075579496
10 changed files with 328 additions and 0 deletions
1
src/boot.py
Normal file
1
src/boot.py
Normal file
|
@ -0,0 +1 @@
|
|||
# boot.py - - runs on boot-up
|
75
src/main.py
Normal file
75
src/main.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
# main.py
|
||||
import machine
|
||||
import socket
|
||||
from time import sleep
|
||||
from wifi import WifiSTA
|
||||
|
||||
|
||||
## Pins
|
||||
PIN_UP = 14 # D5
|
||||
PIN_LE = 12 # D6
|
||||
PIN_RI = 13 # D7
|
||||
# PIN_DN = 16 # D0 - does not support pull
|
||||
PIN_DN = 2 # D4
|
||||
PIN_B1 = 0 # D2
|
||||
PIN_B2 = 4 # D3
|
||||
## Delay
|
||||
DELAY_CMD = 0.1
|
||||
## HOST
|
||||
RX_ADDR = ("192.168.11.220", 80)
|
||||
# RX_ADDR = ("192.168.0.1", 80)
|
||||
|
||||
ts_cmd = 0
|
||||
s = None
|
||||
|
||||
def handle_btn_up(pin):
|
||||
send_cmd("fwd")
|
||||
|
||||
def handle_btn_dn(pin):
|
||||
send_cmd("rev")
|
||||
|
||||
def handle_btn_le(pin):
|
||||
send_cmd("left")
|
||||
|
||||
def handle_btn_ri(pin):
|
||||
send_cmd("right")
|
||||
|
||||
def handle_btn_b1(pin):
|
||||
send_cmd("b1")
|
||||
|
||||
def handle_btn_b2(pin):
|
||||
send_cmd("b2")
|
||||
|
||||
def send_cmd(cmd):
|
||||
print("cmd: {}".format(cmd))
|
||||
global s, ts_cmd
|
||||
# if time.time() > (ts_cmd + DELAY_CMD):
|
||||
# ts_cmd = time.time()
|
||||
try:
|
||||
s.sendto(cmd.encode(), RX_ADDR)
|
||||
except Exception as ex:
|
||||
print("Exception Type:{}, args: {}".format(type(ex).__name__, ex.args))
|
||||
|
||||
def main():
|
||||
global s
|
||||
wifiSTA = WifiSTA()
|
||||
## initialize buttons and IRQs
|
||||
btn_up = machine.Pin(PIN_UP, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||
btn_up.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_up)
|
||||
btn_dn = machine.Pin(PIN_DN, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||
btn_dn.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_dn)
|
||||
btn_le = machine.Pin(PIN_LE, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||
btn_le.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_le)
|
||||
btn_ri = machine.Pin(PIN_RI, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||
btn_ri.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_ri)
|
||||
btn_b1 = machine.Pin(PIN_B1, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||
btn_b1.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_b1)
|
||||
btn_b2 = machine.Pin(PIN_B2, machine.Pin.IN, machine.Pin.PULL_UP)
|
||||
btn_b2.irq(trigger=machine.Pin.IRQ_FALLING, handler=handle_btn_b2)
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
while True:
|
||||
sleep(0.01)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
43
src/wifi.py
Normal file
43
src/wifi.py
Normal file
|
@ -0,0 +1,43 @@
|
|||
from credentials import *
|
||||
import network
|
||||
import time
|
||||
|
||||
address = "192.168.0.1"
|
||||
netmask = "255.255.255.0"
|
||||
gateway = "192.168.0.1"
|
||||
dns = "192.168.0.1"
|
||||
|
||||
|
||||
class WifiAP:
|
||||
def __init__(self):
|
||||
self.if_ap = network.WLAN(network.AP_IF)
|
||||
self.if_sta = network.WLAN(network.STA_IF)
|
||||
self.if_sta.active(False)
|
||||
self.if_ap.active(True)
|
||||
self.if_ap.ifconfig([address, netmask, gateway, dns])
|
||||
self.if_ap.config(essid=SSID, password=PASS)
|
||||
print("network config {}".format(self.if_ap.ifconfig()))
|
||||
|
||||
class WifiSTA:
|
||||
def __init__(self):
|
||||
self.if_ap = network.WLAN(network.AP_IF)
|
||||
self.if_sta = network.WLAN(network.STA_IF)
|
||||
self.if_ap.active(False)
|
||||
self.if_sta.active(True)
|
||||
self.if_sta.connect(SSID, PASS)
|
||||
# self.wifi_thread = threading.Thread(target=self.wifi_thread_fn)
|
||||
while not self.if_sta.isconnected():
|
||||
time.sleep(0.1)
|
||||
print("network config {}".format(self.if_sta.ifconfig()))
|
||||
# self.wifi_thread.start()
|
||||
|
||||
# def wifi_thread_fn(self):
|
||||
# while True:
|
||||
# if not self.if_sta.isconnected():
|
||||
# print("reconnecting...")
|
||||
# self.if_sta.connect(SSID, PASS)
|
||||
# while not self.if_sta.isconnected():
|
||||
# time.sleep(0.1)
|
||||
# print("network config {}".format(self.if_sta.ifconfig()))
|
||||
# else:
|
||||
# time.sleep(0.1)
|
Loading…
Add table
Add a link
Reference in a new issue