SHA256
Compare commits
4
Commits
| Author | SHA256 | Date | |
|---|---|---|---|
|
|
9c21611c81 | ||
|
|
058aa39b9c | ||
|
|
5e55047884 | ||
|
|
20c3ecd30e |
No files matched your search
@@ -3,3 +3,4 @@ compile_commands.json
|
||||
.clangd
|
||||
.vscode
|
||||
.cache
|
||||
.venv
|
||||
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <WebSocketsServer.h>
|
||||
|
||||
void webSocketEvent( uint8_t num, WStype_t type, uint8_t * payload, size_t length );
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Network.h"
|
||||
#include <WiFi.h>
|
||||
|
||||
|
||||
extern volatile bool websocket_connected;
|
||||
|
||||
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
|
||||
);
|
||||
};
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "WiFiConf.h"
|
||||
|
||||
extern volatile bool websocket_connected;
|
||||
|
||||
void wifi_conf::handler_WiFi(arduino_event_id_t event, arduino_event_info_t info) {
|
||||
|
||||
switch(event){
|
||||
|
||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||
websocket_connected = true;
|
||||
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:
|
||||
websocket_connected = false;
|
||||
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);
|
||||
}
|
||||
+24
-87
@@ -1,101 +1,38 @@
|
||||
#include "Arduino.h"
|
||||
#include "esp_adc/adc_continuous.h"
|
||||
|
||||
// API ESP-IDF para ADC modo continuo (uso de DMA)
|
||||
#define MS_BLOQUE 10
|
||||
#define HZ_ADC 1000 // Limitado a 611 - 83333 para el ESP32-S3
|
||||
#define CANAL 3
|
||||
#include "WiFiConf.h"
|
||||
#include "WebSocketHandler.h"
|
||||
|
||||
const uint32_t MUESTRAS_BLOQUE = (HZ_ADC * MS_BLOQUE) / 1000;
|
||||
const uint32_t FRAME_SIZE = MUESTRAS_BLOQUE * 4; // Constante física SOC_ADC_DIGI_RESULT_BYTES
|
||||
const uint32_t POOL_SIZE = FRAME_SIZE * 4; // Buffer DMA de 4 veces el tamaño del FRAME_SIZE
|
||||
uint8_t dma_buffer[FRAME_SIZE] = {0};
|
||||
adc_channel_t channels[1] = {ADC_CHANNEL_3};
|
||||
#include <WebSocketsServer.h>
|
||||
|
||||
volatile bool dma_data_ready = false;
|
||||
adc_continuous_handle_t handle_ADC1_1 = NULL;
|
||||
// Conf de ESP32-S3 en modo STA
|
||||
const char* SSID="Tenda_32C850";
|
||||
const char* PASSWD="rhLcrMEcY^Xjvq66";
|
||||
IPAddress local_ip(192,168,0,220);
|
||||
IPAddress gateway(192,168,0,1);
|
||||
IPAddress subnet(255,255,255,0);
|
||||
IPAddress dns1(192,168,0,100);
|
||||
IPAddress dns2(1,1,1,1);
|
||||
volatile bool websocket_connected = false;
|
||||
|
||||
// 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){
|
||||
dma_data_ready = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void setupADC(adc_channel_t *canales, uint8_t numChannels){
|
||||
adc_continuous_handle_cfg_t ADC1_1_config = {
|
||||
.max_store_buf_size = POOL_SIZE,
|
||||
.conv_frame_size = FRAME_SIZE,
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_continuous_new_handle(&ADC1_1_config, &handle_ADC1_1));
|
||||
|
||||
adc_digi_pattern_config_t channel_config = {0};
|
||||
channel_config.channel = canales[0];
|
||||
channel_config.atten = ADC_ATTEN_DB_12;
|
||||
channel_config.bit_width = ADC_BITWIDTH_12;
|
||||
channel_config.unit = ADC_UNIT_1;
|
||||
|
||||
adc_continuous_config_t adc_config = {
|
||||
.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));
|
||||
|
||||
adc_continuous_evt_cbs_t cb_config = {
|
||||
.on_conv_done = adc_dma_callback,
|
||||
.on_pool_ovf = NULL
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle_ADC1_1, &cb_config, NULL));
|
||||
ESP_ERROR_CHECK(adc_continuous_start(handle_ADC1_1));
|
||||
Serial.println("DMA Iniciado con sistema de bandera.");
|
||||
}
|
||||
wifi_conf sta_esp32;
|
||||
WebSocketsServer webSocket(81);
|
||||
|
||||
void setup() {
|
||||
|
||||
Serial.begin(230400);
|
||||
setupADC(channels, 1);
|
||||
|
||||
sta_esp32.conectar(SSID, PASSWD, local_ip, gateway, subnet, dns1, dns2);
|
||||
|
||||
webSocket.begin();
|
||||
webSocket.onEvent(webSocketEvent);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (dma_data_ready) {
|
||||
dma_data_ready = false;
|
||||
uint32_t ret_num = 0;
|
||||
webSocket.loop();
|
||||
|
||||
// Leer los datos crudos del DMA
|
||||
esp_err_t ret = adc_continuous_read(handle_ADC1_1, dma_buffer, FRAME_SIZE, &ret_num, 0);
|
||||
uint8_t imagen = 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,26 @@
|
||||
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)}")
|
||||
|
||||
else:
|
||||
print(f"TXT: {data}")
|
||||
|
||||
print(f"DEBUG {i}")
|
||||
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("Cerrando conexión")
|
||||
|
||||
ws.close()
|
||||
Reference in new issue
Block a user