From 058aa39b9ca04831218442291abecdfb354978f8a7610e7e92cc8d513a108586 Mon Sep 17 00:00:00 2001 From: Sebastian Mariano Flores Date: Wed, 20 May 2026 20:45:24 -0600 Subject: [PATCH] Feature: Funcional --- include/WebSocketHandler.h | 6 +++ include/WiFiConf.h | 24 ++++++++++++ src/WebSocketHandler.cpp | 17 +++++++++ src/WiFiConf.cpp | 48 ++++++++++++++++++++++++ src/main.cpp | 61 +++---------------------------- test/Cliente-Websocket/cliente.py | 5 ++- 6 files changed, 104 insertions(+), 57 deletions(-) create mode 100644 include/WebSocketHandler.h create mode 100644 include/WiFiConf.h create mode 100644 src/WebSocketHandler.cpp create mode 100644 src/WiFiConf.cpp diff --git a/include/WebSocketHandler.h b/include/WebSocketHandler.h new file mode 100644 index 0000000..0a6d6ff --- /dev/null +++ b/include/WebSocketHandler.h @@ -0,0 +1,6 @@ +#pragma once + +#include "Arduino.h" +#include + +void webSocketEvent( uint8_t num, WStype_t type, uint8_t * payload, size_t length ); diff --git a/include/WiFiConf.h b/include/WiFiConf.h new file mode 100644 index 0000000..9d2f0f2 --- /dev/null +++ b/include/WiFiConf.h @@ -0,0 +1,24 @@ +#pragma once + +#include "Arduino.h" +#include "Network.h" +#include + +class wifi_conf { + private: + const char* _ssid; + const char* _passwd; + + void handler_WiFi(arduino_event_id_t event, arduino_event_info_t info); + + public: + void conectar( + const char* ssid, + const char* passwd, + IPAddress local_ip, + IPAddress gateway, + IPAddress subnet, + IPAddress dns1, + IPAddress dns2 + ); +}; diff --git a/src/WebSocketHandler.cpp b/src/WebSocketHandler.cpp new file mode 100644 index 0000000..3da5639 --- /dev/null +++ b/src/WebSocketHandler.cpp @@ -0,0 +1,17 @@ +#include "WebSocketHandler.h" + +void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { + switch(type) { + case WStype_CONNECTED: + Serial.printf("[%u] Cliente conectado\n", num); + break; + + case WStype_DISCONNECTED: + Serial.printf("[%u] Cliente desconectado\n", num); + break; + + case WStype_TEXT: + Serial.printf("[%u] Mensaje: %s\n", num, payload); + break; + } +} diff --git a/src/WiFiConf.cpp b/src/WiFiConf.cpp new file mode 100644 index 0000000..3f06ecb --- /dev/null +++ b/src/WiFiConf.cpp @@ -0,0 +1,48 @@ +#include "WiFiConf.h" + +void wifi_conf::handler_WiFi(arduino_event_id_t event, arduino_event_info_t info) { + + switch(event){ + + case ARDUINO_EVENT_WIFI_STA_CONNECTED: + Serial.printf("Wi-Fi conectado\n"); + break; + + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + Serial.printf( "IP Address: %s\n", IPAddress(info.got_ip.ip_info.ip.addr).toString().c_str() ); + break; + + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + Serial.printf("WiFi: Desconectado\n"); + Serial.printf("DEBUG: Reconectando...\n"); + + WiFi.STA.connect(_ssid,_passwd); + break; + } +} + +void wifi_conf::conectar( + const char* ssid, + const char* passwd, + IPAddress local_ip, + IPAddress gateway, + IPAddress subnet, + IPAddress dns1, + IPAddress dns2 +) { + + _ssid = ssid; + _passwd = passwd; + + Network.begin(); + + Network.onEvent([this](arduino_event_id_t event, arduino_event_info_t info){ + handler_WiFi(event, info); + }); + + WiFi.STA.begin(); + + WiFi.STA.config( local_ip, gateway, subnet, dns1, dns2); + + WiFi.STA.connect(_ssid, _passwd); +} diff --git a/src/main.cpp b/src/main.cpp index b094dac..1ff70a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,8 @@ #include "Arduino.h" -#include "Network.h" -#include + +#include "WiFiConf.h" +#include "WebSocketHandler.h" + #include // Conf de ESP32-S3 en modo STA @@ -13,59 +15,6 @@ IPAddress dns1(192,168,0,100); IPAddress dns2(1,1,1,1); -void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) { - switch(type) { - case WStype_CONNECTED: - Serial.printf("[%u] Cliente conectado\n", num); - break; - - case WStype_DISCONNECTED: - Serial.printf("[%u] Cliente desconectado\n", num); - break; - - case WStype_TEXT: - Serial.printf("[%u] Mensaje: %s\n", num, payload); - break; - } -} - -// Clase para manejo de Wi-Fi -class wifi_conf { - private: - const char* _ssid; - const char* _passwd; - - void handler_WiFi(arduino_event_id_t event, arduino_event_info_t info) { - switch(event){ - case ARDUINO_EVENT_WIFI_STA_CONNECTED: - Serial.printf("Wi-Fi conectado\n"); - break; - case ARDUINO_EVENT_WIFI_STA_GOT_IP: - Serial.printf("IP Address: %s\n", IPAddress(info.got_ip.ip_info.ip.addr).toString().c_str()); - break; - case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - Serial.printf("WiFi: Desconectado\n"); - Serial.printf("DEBUG: Reconectando...\n"); - WiFi.STA.connect(_ssid,_passwd); - break; - } - } - public: - void conectar(const char* ssid, const char* passwd){ - _ssid=ssid; - _passwd=passwd; - - Network.begin(); - - Network.onEvent([this](arduino_event_id_t event, arduino_event_info_t info){ - handler_WiFi(event, info); - }); - - WiFi.STA.begin(); - WiFi.STA.config(local_ip,gateway,subnet,dns1,dns2); - WiFi.STA.connect(_ssid, _passwd); - } -}; wifi_conf sta_esp32; WebSocketsServer webSocket(81); @@ -73,7 +22,7 @@ void setup() { Serial.begin(230400); - sta_esp32.conectar(SSID, PASSWD); + sta_esp32.conectar(SSID, PASSWD, local_ip, gateway, subnet, dns1, dns2); webSocket.begin(); webSocket.onEvent(webSocketEvent); diff --git a/test/Cliente-Websocket/cliente.py b/test/Cliente-Websocket/cliente.py index dadfd81..8725657 100644 --- a/test/Cliente-Websocket/cliente.py +++ b/test/Cliente-Websocket/cliente.py @@ -3,12 +3,13 @@ import time from websocket import create_connection ws = create_connection("ws://192.168.0.220:81") - +i = 0 print("Conectado") try: while True: data = ws.recv() + i += 1 if isinstance(data, bytes): print(f"BIN: {list(data)}") @@ -16,6 +17,8 @@ try: else: print(f"TXT: {data}") + print(f"DEBUG {i}") + except KeyboardInterrupt: print("Cerrando conexión")