SHA256
POO:WiFi-STA|Basic:WebSocket
This commit is contained in:
1 parent
082995d264
commit
20c3ecd30e
3 files changed
+94
-82
No files matched your search
@@ -23,3 +23,4 @@ board_build.partitions = default_16MB.csv
|
|||||||
board_build.extra_flags =
|
board_build.extra_flags =
|
||||||
-DBOARD_HAS_PSRAM
|
-DBOARD_HAS_PSRAM
|
||||||
-DCORE_DEBUG_LEVEL=5
|
-DCORE_DEBUG_LEVEL=5
|
||||||
|
lib_deps = links2004/WebSockets@^2.7.3
|
||||||
+70
-82
@@ -1,101 +1,89 @@
|
|||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "esp_adc/adc_continuous.h"
|
#include "Network.h"
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebSocketsServer.h>
|
||||||
|
|
||||||
// API ESP-IDF para ADC modo continuo (uso de DMA)
|
// Conf de ESP32-S3 en modo STA
|
||||||
#define MS_BLOQUE 10
|
const char* SSID="Tenda_32C850";
|
||||||
#define HZ_ADC 1000 // Limitado a 611 - 83333 para el ESP32-S3
|
const char* PASSWD="rhLcrMEcY^Xjvq66";
|
||||||
#define CANAL 3
|
IPAddress local_ip(192,168,0,220);
|
||||||
|
IPAddress gateway(192,168,0,1);
|
||||||
const uint32_t MUESTRAS_BLOQUE = (HZ_ADC * MS_BLOQUE) / 1000;
|
IPAddress subnet(255,255,255,0);
|
||||||
const uint32_t FRAME_SIZE = MUESTRAS_BLOQUE * 4; // Constante física SOC_ADC_DIGI_RESULT_BYTES
|
IPAddress dns1(192,168,0,100);
|
||||||
const uint32_t POOL_SIZE = FRAME_SIZE * 4; // Buffer DMA de 4 veces el tamaño del FRAME_SIZE
|
IPAddress dns2(1,1,1,1);
|
||||||
uint8_t dma_buffer[FRAME_SIZE] = {0};
|
|
||||||
adc_channel_t channels[1] = {ADC_CHANNEL_3};
|
|
||||||
|
|
||||||
volatile bool dma_data_ready = false;
|
|
||||||
adc_continuous_handle_t handle_ADC1_1 = NULL;
|
|
||||||
|
|
||||||
// DEBUG
|
|
||||||
uint16_t DRAM_contador = 0;
|
|
||||||
|
|
||||||
|
|
||||||
bool IRAM_ATTR adc_dma_callback(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data){
|
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
|
||||||
dma_data_ready = true;
|
switch(type) {
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupADC(adc_channel_t *canales, uint8_t numChannels){
|
// Clase para manejo de Wi-Fi
|
||||||
adc_continuous_handle_cfg_t ADC1_1_config = {
|
class wifi_conf {
|
||||||
.max_store_buf_size = POOL_SIZE,
|
private:
|
||||||
.conv_frame_size = FRAME_SIZE,
|
const char* _ssid;
|
||||||
};
|
const char* _passwd;
|
||||||
ESP_ERROR_CHECK(adc_continuous_new_handle(&ADC1_1_config, &handle_ADC1_1));
|
|
||||||
|
|
||||||
adc_digi_pattern_config_t channel_config = {0};
|
void handler_WiFi(arduino_event_id_t event, arduino_event_info_t info) {
|
||||||
channel_config.channel = canales[0];
|
switch(event){
|
||||||
channel_config.atten = ADC_ATTEN_DB_12;
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
channel_config.bit_width = ADC_BITWIDTH_12;
|
Serial.printf("Wi-Fi conectado\n");
|
||||||
channel_config.unit = ADC_UNIT_1;
|
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;
|
||||||
|
|
||||||
adc_continuous_config_t adc_config = {
|
Network.begin();
|
||||||
.pattern_num = numChannels,
|
|
||||||
.adc_pattern = &channel_config,
|
|
||||||
.sample_freq_hz = HZ_ADC,
|
|
||||||
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
|
|
||||||
};
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(adc_continuous_config(handle_ADC1_1, &adc_config));
|
Network.onEvent([this](arduino_event_id_t event, arduino_event_info_t info){
|
||||||
|
handler_WiFi(event, info);
|
||||||
|
});
|
||||||
|
|
||||||
adc_continuous_evt_cbs_t cb_config = {
|
WiFi.STA.begin();
|
||||||
.on_conv_done = adc_dma_callback,
|
WiFi.STA.config(local_ip,gateway,subnet,dns1,dns2);
|
||||||
.on_pool_ovf = NULL
|
WiFi.STA.connect(_ssid, _passwd);
|
||||||
};
|
}
|
||||||
|
};
|
||||||
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle_ADC1_1, &cb_config, NULL));
|
wifi_conf sta_esp32;
|
||||||
ESP_ERROR_CHECK(adc_continuous_start(handle_ADC1_1));
|
WebSocketsServer webSocket(81);
|
||||||
Serial.println("DMA Iniciado con sistema de bandera.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(230400);
|
Serial.begin(230400);
|
||||||
setupADC(channels, 1);
|
|
||||||
|
sta_esp32.conectar(SSID, PASSWD);
|
||||||
|
|
||||||
|
webSocket.begin();
|
||||||
|
webSocket.onEvent(webSocketEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (dma_data_ready) {
|
webSocket.loop();
|
||||||
dma_data_ready = false;
|
|
||||||
uint32_t ret_num = 0;
|
|
||||||
|
|
||||||
// Leer los datos crudos del DMA
|
uint8_t imagen = 0;
|
||||||
esp_err_t ret = adc_continuous_read(handle_ADC1_1, dma_buffer, FRAME_SIZE, &ret_num, 0);
|
webSocket.broadcastBIN(&imagen, 1);
|
||||||
|
delay(10);
|
||||||
|
|
||||||
if (ret == ESP_OK) {
|
|
||||||
adc_continuous_data_t parsed_data[MUESTRAS_BLOQUE];
|
|
||||||
uint32_t parsed_count = 0;
|
|
||||||
|
|
||||||
//API oficial documentada para extraer la información (reemplaza a los p->type2)
|
|
||||||
esp_err_t parse_ret = adc_continuous_parse_data(handle_ADC1_1,
|
|
||||||
dma_buffer,
|
|
||||||
ret_num,
|
|
||||||
parsed_data,
|
|
||||||
&parsed_count);
|
|
||||||
|
|
||||||
if (parse_ret == ESP_OK) {
|
|
||||||
for (uint32_t i = 0; i < parsed_count; i++) {
|
|
||||||
if (parsed_data[i].valid) {
|
|
||||||
DRAM_contador++;
|
|
||||||
|
|
||||||
// Imprimir resultados
|
|
||||||
Serial.printf("Unidad: ADC%u | Canal: %u | Valor: %04lu | Debug: %u\n",
|
|
||||||
parsed_data[i].unit + 1,
|
|
||||||
parsed_data[i].channel,
|
|
||||||
parsed_data[i].raw_data,
|
|
||||||
DRAM_contador);
|
|
||||||
|
|
||||||
DRAM_contador = (DRAM_contador >= HZ_ADC) ? 0 : DRAM_contador;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
from websocket import create_connection
|
||||||
|
|
||||||
|
ws = create_connection("ws://192.168.0.220:81")
|
||||||
|
|
||||||
|
print("Conectado")
|
||||||
|
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
data = ws.recv()
|
||||||
|
|
||||||
|
if isinstance(data, bytes):
|
||||||
|
print(f"BIN: {list(data)}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f"TXT: {data}")
|
||||||
|
|
||||||
|
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print("Cerrando conexión")
|
||||||
|
|
||||||
|
ws.close()
|
||||||
Reference in new issue
Block a user