From 310a057795c09b6dc03d91596811025e8b19cb1da2e74048d480f13fc894b83d Mon Sep 17 00:00:00 2001 From: Sebastian Mariano Flores Date: Fri, 8 May 2026 11:15:58 -0600 Subject: [PATCH] UDP-router --- src/main.cpp | 22 +++++----- src/python/udp_client.py | 86 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 src/python/udp_client.py diff --git a/src/main.cpp b/src/main.cpp index 4565840..639592f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,8 +14,9 @@ IPAddress subnet(255,255,255,0); IPAddress dns1(192,168,0,100); IPAddress dns2(1,1,1,1); -NetworkServer server(8080); -NetworkClient client = server.accept(); +IPAddress IP_C(192,168,0,134); +#define PORT_C 8080 +NetworkUDP udp; uint8_t i=0; @@ -40,14 +41,13 @@ void setup() { delay(500); Serial.print("."); } + udp.begin(8080); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); - server.begin(); - // Iniciar y desbloquear la bandera timerSemaphore = xSemaphoreCreateBinary(); xSemaphoreGive(timerSemaphore); @@ -60,14 +60,10 @@ void setup() { } void loop() { - if (!client || !client.connected()) { - client = server.accept(); - } - if (xSemaphoreTake(timerSemaphore, portMAX_DELAY) == pdTRUE) { - if (client && client.connected()) { - uint8_t imagen=i; - client.printf("%03u",imagen); - } + uint8_t imagen = i; + udp.beginPacket(IP_C,PORT_C); + udp.write(imagen); + udp.endPacket(); } -} +} \ No newline at end of file diff --git a/src/python/udp_client.py b/src/python/udp_client.py new file mode 100644 index 0000000..e498011 --- /dev/null +++ b/src/python/udp_client.py @@ -0,0 +1,86 @@ +import csv +import time +import threading +import matplotlib.pyplot as plt +import socket +import time +from websocket import create_connection + +HOST="0.0.0.0" +PORT=8080 +CSV_NOMBRE = "Router_STA_UDP.csv" +PNG_NOMBRE = "ROUTER_STA_UDP.png" + +row_data=[] + +inicio=time.perf_counter() +duracion=300 + +lock = threading.Lock() +running = True + + +def graficar(): + plt.ion() + fig, ax1 = plt.subplots() + ax2 = ax1.twinx() + + line_data, = ax2.plot([], []) + + ax1.set_ylim(0, 100) + ax2.set_ylim(0, 100) + + while running: + with lock: + + data_copy = row_data.copy() + + if len(data_copy) > 1: + + t = [i / 1000 for i in range(len(data_copy))] # aprox 1kHz + + datos = [x[0] for x in data_copy] + lag = [x[1] for x in data_copy] + + + t = t[-1000:] + datos = datos[-1000:] + lag = lag[-1000:] + + line_data.set_data(t, datos) + + ax1.set_xlim(t[0], t[-1]) + + plt.pause(0.01) + + plt.savefig(PNG_NOMBRE, dpi=500) + + +# lanzar hilo de gráfica +thread_plot = threading.Thread(target=graficar) +thread_plot.start() +t_prev = time.perf_counter() + + +client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +client.bind((HOST,PORT)) + +while time.perf_counter() - inicio < duracion: + row,address=client.recvfrom(1) + data=row[0] + t_now = time.perf_counter() + delta = (t_now - t_prev) * 1000 # ms + t_prev = t_now + + with lock: + row_data.append([data, delta]) + +running = False +thread_plot.join() + +data_final = [[d, round(l, 3)] for d, l in row_data] +with open(CSV_NOMBRE, 'w', newline='', encoding='utf-8') as archivo: + write = csv.writer(archivo, quoting=csv.QUOTE_NONE, escapechar='\\') + write.writerows(data_final) + +print(f"Datos recibidos:{len(row_data)}") \ No newline at end of file