SHA256
API:WiFi -> Error de handler
This commit is contained in:
1 parent
80a703673f
commit
6ca5f7f68d
7 files changed
+151
-4
No files matched your search
@@ -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,24 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Arduino.h"
|
||||||
|
#include "Network.h"
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
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 =
|
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
|
||||||
@@ -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,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);
|
||||||
|
}
|
||||||
+26
-4
@@ -2,6 +2,10 @@
|
|||||||
#include "freertos/ringbuf.h"
|
#include "freertos/ringbuf.h"
|
||||||
|
|
||||||
#include "ADCconf.h"
|
#include "ADCconf.h"
|
||||||
|
#include "WiFiConf.h"
|
||||||
|
#include "WebSocketHandler.h"
|
||||||
|
|
||||||
|
#include <WebSocketsServer.h>
|
||||||
|
|
||||||
// API ESP-IDF para ADC modo continuo (uso de DMA)
|
// API ESP-IDF para ADC modo continuo (uso de DMA)
|
||||||
#define MS_BLOQUE 10
|
#define MS_BLOQUE 10
|
||||||
@@ -14,6 +18,19 @@ const uint32_t SIZE_BUFFER = MUESTRAS_BLOQUE*10;
|
|||||||
|
|
||||||
ADC_BME adc1_3;
|
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
|
// FreeRTOS
|
||||||
TaskHandle_t ADCTaskHandle = NULL;
|
TaskHandle_t ADCTaskHandle = NULL;
|
||||||
TaskHandle_t WebSocketTaskHandle = NULL;
|
TaskHandle_t WebSocketTaskHandle = NULL;
|
||||||
@@ -25,6 +42,11 @@ void WebSocketTask(void *parameter);
|
|||||||
void setup(){
|
void setup(){
|
||||||
Serial.begin(230400);
|
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.config(MUESTRAS_BLOQUE, CHANNEL, HZ_ADC, dma_buffer);
|
||||||
adc1_3.setup();
|
adc1_3.setup();
|
||||||
|
|
||||||
@@ -55,15 +77,15 @@ void ADCTask(void *parameter){
|
|||||||
|
|
||||||
void WebSocketTask(void *parameter){
|
void WebSocketTask(void *parameter){
|
||||||
for(;;) {
|
for(;;) {
|
||||||
|
|
||||||
|
webSocket.loop();
|
||||||
|
|
||||||
size_t receivedMessageSize;
|
size_t receivedMessageSize;
|
||||||
uint16_t* receivedMessage = (uint16_t*)xRingbufferReceive(bufferHandle, &receivedMessageSize, pdMS_TO_TICKS(1000));
|
uint16_t* receivedMessage = (uint16_t*)xRingbufferReceive(bufferHandle, &receivedMessageSize, pdMS_TO_TICKS(1000));
|
||||||
|
|
||||||
// 6. Check if the receive operation was successful
|
// 6. Check if the receive operation was successful
|
||||||
if (receivedMessage != NULL) {
|
if (receivedMessage != NULL) {
|
||||||
size_t samples = receivedMessageSize / sizeof(uint16_t);
|
webSocket.broadcastBIN((uint8_t*)receivedMessage, receivedMessageSize);
|
||||||
for(size_t i = 0; i < samples; i++) {
|
|
||||||
Serial.printf("%u ", receivedMessage[i]);}
|
|
||||||
Serial.println();
|
|
||||||
vRingbufferReturnItem(bufferHandle, (void*)receivedMessage);
|
vRingbufferReturnItem(bufferHandle, (void*)receivedMessage);
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Failed t+o receive message from ring buffer!");
|
Serial.println("Failed t+o receive message from ring buffer!");
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in new issue
Block a user