mirror of
https://github.com/adrigongv23/G26---Telemetry-Software.git
synced 2026-08-25 11:33:17 +02:00
Telemetria WiFi+SD
This commit is contained in:
parent
7c069efa14
commit
4b58b1ec97
6 changed files with 181 additions and 152 deletions
|
|
@ -1,91 +1,105 @@
|
|||
#include "include/data_processor.hpp"
|
||||
#include "include/can.hpp"
|
||||
#include "include/common_libraries.hpp"
|
||||
#include "include/common_libraries.hpp"
|
||||
|
||||
DataProcessor dataProcessor;
|
||||
CAN canController;
|
||||
|
||||
// Objeto UDP para manejar la conexión UDP
|
||||
// SD
|
||||
SPIClass spiSD(HSPI);
|
||||
SdFat sd;
|
||||
SdFile logFile;
|
||||
|
||||
// UDP
|
||||
WiFiUDP udp;
|
||||
|
||||
//Envio de datos a través de UDP
|
||||
void TaskUdpSender(void *pvParameters){
|
||||
// --- Tarea UDP (Nucleo 0) ---
|
||||
void TaskUdpSender(void *pvParameters) {
|
||||
Serial.println("Iniciando tarea de envio UDP...");
|
||||
|
||||
Serial.println("Iniciando tarea de envío...");
|
||||
while (true) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
int tempActual = dataProcessor.current_ect_value;
|
||||
int rpmActual = dataProcessor.current_rpm_value;
|
||||
float battActual = dataProcessor.current_vbatt_value;
|
||||
float tpsActual = dataProcessor.current_tps_value;
|
||||
float frenoDelActual = dataProcessor.current_freno_del_value;
|
||||
float pcombActual = dataProcessor.current_pcomb_value;
|
||||
float taceiteActual = dataProcessor.current_taceite_value;
|
||||
float paceiteActual = dataProcessor.current_paceite_value;
|
||||
float mapActual = dataProcessor.current_map_value;
|
||||
float lambdaActual = dataProcessor.current_lambda_value;
|
||||
float lambdaObjActual = dataProcessor.current_lambda_obj_value;
|
||||
|
||||
while (true){
|
||||
if(WiFi.status() == WL_CONNECTED){
|
||||
char mensaje[256];
|
||||
snprintf(mensaje, sizeof(mensaje),
|
||||
"ect=%d;rpm=%d;vbatt=%.2f;tps=%.1f;freno_del=%.1f;"
|
||||
"pcomb=%.2f;taceite=%.1f;paceite=%.2f;map=%.1f;lambda=%.3f;lambda_obj=%.3f",
|
||||
tempActual, rpmActual, battActual, tpsActual, frenoDelActual,
|
||||
pcombActual, taceiteActual, paceiteActual, mapActual, lambdaActual, lambdaObjActual);
|
||||
|
||||
//Obtenemos los datos actuales
|
||||
int tempActual = dataProcessor.current_ect_value;
|
||||
int rpmActual = dataProcessor.current_rpm_value;
|
||||
float battActual = dataProcessor.current_vbatt_value;
|
||||
udp.beginPacket(IPAddress(255, 255, 255, 255), UDP_PORT);
|
||||
udp.print(mensaje);
|
||||
udp.endPacket();
|
||||
} else {
|
||||
Serial.println("[WIFI] Desconectado...");
|
||||
WiFi.disconnect();
|
||||
WiFi.reconnect();
|
||||
}
|
||||
|
||||
float tpsActual = dataProcessor.current_tps_value;
|
||||
float frenoDelActual = dataProcessor.current_freno_del_value; // freno delantero (instalado)
|
||||
float pcombActual = dataProcessor.current_pcomb_value;
|
||||
float taceiteActual = dataProcessor.current_taceite_value;
|
||||
float paceiteActual = dataProcessor.current_paceite_value;
|
||||
float mapActual = dataProcessor.current_map_value;
|
||||
float lambdaActual = dataProcessor.current_lambda_value;
|
||||
float lambdaObjActual = dataProcessor.current_lambda_obj_value;
|
||||
// Pendientes de montar/configurar en el coche. Cuando se instalen, descomentar aquí
|
||||
// y añadir su campo al snprintf; mientras no lleguen, el monitor los muestra como "--".
|
||||
//float velocidadActual = dataProcessor.current_velocidad_value;
|
||||
//float frenoTraActual = dataProcessor.current_freno_tra_value; // freno trasero
|
||||
|
||||
//Formato "clave=valor" separado por ';'
|
||||
char mensaje[256];
|
||||
snprintf(mensaje, sizeof(mensaje),
|
||||
"ect=%d;rpm=%d;vbatt=%.2f;tps=%.1f;freno_del=%.1f;"
|
||||
"pcomb=%.2f;taceite=%.1f;paceite=%.2f;map=%.1f;lambda=%.3f;lambda_obj=%.3f",
|
||||
tempActual, rpmActual, battActual, tpsActual, frenoDelActual,
|
||||
pcombActual, taceiteActual, paceiteActual, mapActual, lambdaActual, lambdaObjActual);
|
||||
// Al añadir los pendientes: sumar ";velocidad=%.0f;freno_tra=%.1f" al formato
|
||||
// y velocidadActual, frenoTraActual al final de los argumentos.
|
||||
|
||||
//Enviamos el paquete por broadcast a toda la red local (no hace falta conocer la IP del portátil)
|
||||
udp.beginPacket(IPAddress(255,255,255,255), UDP_PORT);
|
||||
udp.print(mensaje);
|
||||
udp.endPacket();
|
||||
|
||||
//Para comprobar que el paquete se está enviado correctamente podemos usar estos prints
|
||||
//Serial.print("UDP Enviado: ");
|
||||
//Serial.println(mensaje);
|
||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
else {
|
||||
//Si no hay Wifi o no se consigue conectar, lo intentamos reconectar
|
||||
Serial.println("[WIFI] Desconectado...");
|
||||
WiFi.disconnect();
|
||||
WiFi.reconnect();
|
||||
}
|
||||
|
||||
//Ponemos de velocidad de envio 50ms, es ajustable
|
||||
vTaskDelay(50 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// 1. INICIAR CAN Y PANTALLA
|
||||
// Pasamos el puntero de dataProcessor al controlador CAN
|
||||
canController.set_data_proccessor(&dataProcessor);
|
||||
delay(1000);
|
||||
Serial.println("\n--- G26 TELEMETRY: INICIO DE SISTEMA ---");
|
||||
|
||||
// 1. INICIALIZACION SD
|
||||
spiSD.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
|
||||
|
||||
if (!sd.begin(SdSpiConfig(SD_CS, DEDICATED_SPI, SD_SCK_MHZ(1), &spiSD))) {
|
||||
Serial.println("[FALLO] SD no detectada. El sistema continuara sin Datalogging.");
|
||||
} else {
|
||||
char filename[24];
|
||||
int session = 1;
|
||||
bool opened = false;
|
||||
|
||||
while (!opened && session < 100000) {
|
||||
snprintf(filename, sizeof(filename), "G26-%d.csv", session);
|
||||
if (logFile.open(filename, O_RDWR | O_CREAT | O_EXCL)) {
|
||||
opened = true;
|
||||
Serial.printf("[OK] Nueva sesion: %s\n", filename);
|
||||
} else {
|
||||
session++;
|
||||
}
|
||||
}
|
||||
|
||||
if (opened) {
|
||||
logFile.println("Time,ECT,RPM,TPS,VBATT,FRENO_DEL,PCOMB,TACEITE,PACEITE,MAP,LAMBDA,LAMBDA_OBJ");
|
||||
logFile.sync();
|
||||
} else {
|
||||
Serial.println("[ERROR] No se pudo abrir archivo de sesion");
|
||||
}
|
||||
}
|
||||
|
||||
dataProcessor.setLogSystem(&sd, &logFile);
|
||||
|
||||
// 2. INICIAR CAN
|
||||
canController.set_data_proccessor(&dataProcessor);
|
||||
canController.start();
|
||||
canController.start_listening_task();
|
||||
|
||||
// 2. INICIAR WIFI
|
||||
|
||||
// 3. INICIAR WIFI
|
||||
Serial.println("--- CONECTANDO WIFI ---");
|
||||
|
||||
//IP estática dentro de la red del CPE (DHCP desactivado en el CPE)
|
||||
IPAddress local_IP(192, 168, 0, 50);
|
||||
IPAddress gateway(192, 168, 0, 254);
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
|
||||
if (!WiFi.config(local_IP, gateway, subnet)) {
|
||||
Serial.println("[ERR] Fallo al configurar IP estática");
|
||||
Serial.println("[ERR] Fallo al configurar IP estatica");
|
||||
}
|
||||
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
|
|
@ -97,27 +111,26 @@ void setup() {
|
|||
intentos++;
|
||||
}
|
||||
|
||||
if(WiFi.status() == WL_CONNECTED){
|
||||
Serial.println("\n[OK] WiFi Conectado.");
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.println("\n[OK] WiFi Conectado.");
|
||||
} else {
|
||||
Serial.println("\n[ERR] No se pudo conectar WiFi (Continuando offline).");
|
||||
Serial.println("\n[ERR] No se pudo conectar WiFi (Continuando offline).");
|
||||
}
|
||||
|
||||
//Creamos la tarea UDP para el envio de datos
|
||||
//Usaremos el núcleo 1 o 0 ya que la ESP32 es Dual Core
|
||||
// 4. TAREA UDP
|
||||
xTaskCreatePinnedToCore(
|
||||
TaskUdpSender, // Función que debe de ejecutar
|
||||
"UdpSender", // Nombre de la tarea
|
||||
4096, // Stack size
|
||||
NULL, // Parámetros extras?
|
||||
1, // Prioridad (1 = Baja, 10 = Alta, la ponemos 1 ya que el CAN debe de tener más prioridad)
|
||||
NULL, // Handle
|
||||
0 // Núcleo
|
||||
TaskUdpSender,
|
||||
"UdpSender",
|
||||
4096,
|
||||
NULL,
|
||||
1,
|
||||
NULL,
|
||||
0
|
||||
);
|
||||
|
||||
Serial.println("OK CAN + UDP Sender ");
|
||||
Serial.println("[OK] Sistema ONLINE (CAN + SD + WiFi)");
|
||||
}
|
||||
|
||||
void loop(){
|
||||
void loop() {
|
||||
vTaskDelay(5 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue