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/platformio.ini b/platformio.ini index ffbadc1..257e0f5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -23,3 +23,4 @@ board_build.partitions = default_16MB.csv board_build.extra_flags = -DBOARD_HAS_PSRAM -DCORE_DEBUG_LEVEL=5 +lib_deps = links2004/WebSockets@^2.7.3 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 00f1798..016cab5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,10 @@ #include "freertos/ringbuf.h" #include "ADCconf.h" +#include "WiFiConf.h" +#include "WebSocketHandler.h" + +#include // API ESP-IDF para ADC modo continuo (uso de DMA) #define MS_BLOQUE 10 @@ -14,6 +18,19 @@ const uint32_t SIZE_BUFFER = MUESTRAS_BLOQUE*10; ADC_BME adc1_3; + +// Conf de ESP32-S3 en modo STA +const char* SSID="debian"; +const char* PASSWD="aH2T9GFA"; +IPAddress local_ip(10,42,0,100); +IPAddress gateway(40,42,0,1); +IPAddress subnet(255,255,255,0); +IPAddress dns1(10,42,0,1); +IPAddress dns2(1,1,1,1); + +wifi_conf sta_esp32; +WebSocketsServer webSocket(81); + // FreeRTOS TaskHandle_t ADCTaskHandle = NULL; TaskHandle_t WebSocketTaskHandle = NULL; @@ -25,6 +42,11 @@ void WebSocketTask(void *parameter); void setup(){ Serial.begin(230400); + sta_esp32.conectar(SSID, PASSWD, local_ip, gateway, subnet, dns1, dns2); + + webSocket.begin(); + webSocket.onEvent(webSocketEvent); + adc1_3.config(MUESTRAS_BLOQUE, CHANNEL, HZ_ADC, dma_buffer); adc1_3.setup(); @@ -55,15 +77,15 @@ void ADCTask(void *parameter){ void WebSocketTask(void *parameter){ for(;;) { + + webSocket.loop(); + size_t receivedMessageSize; uint16_t* receivedMessage = (uint16_t*)xRingbufferReceive(bufferHandle, &receivedMessageSize, pdMS_TO_TICKS(1000)); // 6. Check if the receive operation was successful if (receivedMessage != NULL) { - size_t samples = receivedMessageSize / sizeof(uint16_t); - for(size_t i = 0; i < samples; i++) { - Serial.printf("%u ", receivedMessage[i]);} - Serial.println(); + webSocket.broadcastBIN((uint8_t*)receivedMessage, receivedMessageSize); vRingbufferReturnItem(bufferHandle, (void*)receivedMessage); } else { Serial.println("Failed t+o receive message from ring buffer!"); diff --git a/test/Cliente-Websocket/cliente.py b/test/Cliente-Websocket/cliente.py new file mode 100644 index 0000000..81327d0 --- /dev/null +++ b/test/Cliente-Websocket/cliente.py @@ -0,0 +1,29 @@ +import struct + +from websocket import create_connection + +ws = create_connection("ws://10.42.0.100:81") + +print("Conectado") + +i = 0 + +try: + while True: + data = ws.recv() + i += 1 + + if isinstance(data, bytes): + # Convertir bytes -> uint16_t + samples = struct.unpack(f"<{len(data) // 2}H", data) + + print("ADC:", samples) + + else: + print("TXT:", data) + + print(f"DEBUG {i}") + +except KeyboardInterrupt: + print("Cerrando conexión") + ws.close()