I Hhave a error, Compilation error: usb/cdc_acm_host.h: No such file or directory
i have the latest version of arduino ide and esp32s3 by espressif
Using capital letters in chats, forums, etc., means that you are shouting at someone. Why are you shouting?
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Tu librería LCD, que ya funciona
// Incluye las cabeceras necesarias para USB Host CDC
// Estas son parte del ESP32 Arduino core para S3, usando TinyUSB
#include "usb/usb_host.h"
#include "usb/cdc_acm_host.h" // Para dispositivos CDC-ACM (serial virtual)
// Configuración de la LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Revisa la dirección I2C si no funciona (podría ser 0x3F)
// Variables globales para USB Host
static cdc_acm_dev_hdl_t cdc_dev_hdl = NULL; // Manejador para el dispositivo CDC-ACM
static usb_host_client_handle_t client_hdl = NULL; // Manejador para el cliente USB host
// Buffers para recibir datos
#define RX_BUF_SIZE 256
static uint8_t rx_buf[RX_BUF_SIZE];
String incomingUsbData = ""; // Para acumular datos parciales del puerto USB
// Declaraciones anticipadas para los manejadores de eventos
void client_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg);
void cdc_acm_device_event_cb(const cdc_acm_host_dev_event_data_t *event_data);
void setup() {
Serial.begin(115200);
Serial.println("Iniciando configuracion...");
// Inicialización de la LCD
Wire.begin(8, 9); // Pines SDA=GPIO8, SCL=GPIO9 para I2C
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Iniciando USB...");
// Configurar e instalar el USB Host
usb_host_config_t host_config = {
.skip_background_task = false, // Permitir que la tarea USB Host se ejecute en segundo plano
.max_clients = 1,
.async_callback = true, // Usar callbacks asíncronos
.client_event_callback = client_event_cb, // Registrar el callback de eventos del cliente
.reserves_special_clients = false,
};
ESP_ERROR_CHECK(usb_host_install(&host_config)); // Instalar el driver del USB Host
// Registrar el driver y cliente CDC-ACM host
cdc_acm_host_driver_config_t cdc_config = {
.driver_task_stack_size = 4096, // Tamaño de pila para la tarea del driver CDC
.driver_task_priority = 10,
.pipe_buffer_size = 512, // Tamaño del buffer para los pipes
.callback = cdc_acm_device_event_cb, // Registrar el callback de eventos del dispositivo CDC-ACM
.xQueue = NULL, // No se usa cola FreeRTOS para eventos aquí, se usa callback
};
ESP_ERROR_CHECK(cdc_acm_host_install(&cdc_config)); // Instalar el driver del host CDC-ACM
Serial.println("Esperando que la bascula se conecte...");
lcd.setCursor(0, 1);
lcd.print("Conecta Bascula!");
}
void loop() {
delay(1); // Pequeño retraso para permitir que otras tareas de fondo se ejecuten
// Si hay un dispositivo CDC-ACM conectado
if (cdc_dev_hdl != NULL) {
size_t bytes_read = 0;
esp_err_t err = cdc_acm_host_read(cdc_dev_hdl, rx_buf, RX_BUF_SIZE, &bytes_read, 10); // Leer con 10ms de timeout
if (err == ESP_OK && bytes_read > 0) {
// Acumular los bytes recibidos
for (size_t i = 0; i < bytes_read; i++) {
incomingUsbData += (char)rx_buf[i];
}
// Procesar la línea completa cuando se detecta el terminador (CRLF)
int newlineIndex = incomingUsbData.indexOf('\n');
if (newlineIndex != -1) {
String dataLine = incomingUsbData.substring(0, newlineIndex);
incomingUsbData = incomingUsbData.substring(newlineIndex + 1); // Quitar la línea procesada del buffer
dataLine.trim(); // Elimina espacios en blanco al inicio/final, incluyendo el CR
if (dataLine.length() > 0) {
Serial.print("Datos recibidos: '");
Serial.print(dataLine);
Serial.println("'");
// Parsear los datos de la báscula Navigator (basado en el formato del manual)
String pesoStr = dataLine.substring(0, 10);
pesoStr.trim();
int unidadStart = 11;
String unidadStr = "";
for (int i = 0; i < 5 && (unidadStart + i) < dataLine.length(); ++i) {
if (dataLine.charAt(unidadStart + i) == ' ') break;
unidadStr += dataLine.charAt(unidadStart + i);
}
unidadStr.trim();
char estabilidadChar = ' ';
if (dataLine.length() >= 18) {
estabilidadChar = dataLine.charAt(17);
}
String estabilidad = (estabilidadChar == '?') ? "Inestable" : "Estable";
Serial.print("Peso: "); Serial.println(pesoStr);
Serial.print("Unidad: "); Serial.println(unidadStr);
Serial.print("Estabilidad: "); Serial.println(estabilidad);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Peso: " + pesoStr);
lcd.setCursor(0, 1);
lcd.print(unidadStr + " - " + estabilidad);
}
}
} else if (err != ESP_ERR_TIMEOUT && err != ESP_FAIL) {
Serial.printf("Error al leer CDC: %d\n", err);
}
} else {
static bool deviceDisconnected = false;
if (!deviceDisconnected) {
Serial.println("Esperando que la bascula se conecte...");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Esperando Bascula");
lcd.setCursor(0, 1);
lcd.print("Conecta USB OTG");
deviceDisconnected = true;
}
}
}
// *** MANEJADORES DE EVENTOS USB HOST ***
void client_event_cb(const usb_host_client_event_msg_t *event_msg, void *arg) {
if (event_msg->event == USB_HOST_CLIENT_EVENT_CONNECTED) {
Serial.println("USB Host Client Connected");
} else if (event_msg->event == USB_HOST_CLIENT_EVENT_DISCONNECTED) {
Serial.println("USB Host Client Disconnected");
if (cdc_dev_hdl != NULL) {
cdc_acm_host_close(cdc_dev_hdl);
cdc_dev_hdl = NULL;
}
}
}
void cdc_acm_device_event_cb(const cdc_acm_host_dev_event_data_t *event_data) {
if (event_data->event == CDC_ACM_HOST_DEV_EVENT_CONNECTED) {
Serial.printf("CDC-ACM Device Connected, address: %d\n", event_data->new_dev.address);
esp_err_t err = cdc_acm_host_open(event_data->new_dev.address, &cdc_dev_hdl);
if (err == ESP_OK) {
Serial.println("CDC-ACM device opened.");
cdc_acm_host_set_line_coding(cdc_dev_hdl, 9600, CDC_ACM_DATA_BITS_8, CDC_ACM_PARITY_NONE, CDC_ACM_STOP_BITS_1);
Serial.println("Baud rate configurado a 9600.");
} else {
Serial.printf("Error al abrir dispositivo CDC-ACM: %d\n", err);
}
} else if (event_data->event == CDC_ACM_HOST_DEV_EVENT_DISCONNECTED) {
Serial.printf("CDC-ACM Device Desconnected, address: %d\n", event_data->dis_dev.address);
if (cdc_dev_hdl != NULL) {
cdc_acm_host_close(cdc_dev_hdl);
cdc_dev_hdl = NULL;
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bascula Desconect");
lcd.setCursor(0, 1);
lcd.print("Reconecta USB OTG");
}
}
here is the code i dont know whats wrong
Have you instaled the libraries:
usb/usb_host
and
usb/cdc_acm_host ?
If so,
change " " to < >
try this syntaxe:
#include <usb/usb_host.h>
#include <usb/cdc_acm_host.h>
now i have this : Compilation error: #include expects "FILENAME" or
Did you write this code?
Using tags like code,
show complete error msg.
whith ia: C:\Users\andre\OneDrive\Documentos\Arduino\sketch_jun27a\sketch_jun27a.ino:7:10: fatal error: usb/cdc_acm_host.h: No such file or directory
7 | #include <usb/cdc_acm_host.h> // Para dispositivos CDC-ACM (serial virtual)
| ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
Compilation error: usb/cdc_acm_host.h: No such file or directory
same error with " or <
Hi @andyyy29 ,
this is older idf code..
original notation was correct ""..
for this to compile as is, you will need to downgrade your esp32 core to some version 2..
espressif/esp-idf/tree/master/components/usb
version 3 of the esp32 core, changes allot and breaks allot..
they have a migration guide..
good luck.. ~q
same error but this time the sdk folder exist
interesting..
what version do you have installed??
here you can see I have version 3.0.0-a installed..
~q
i have 2.0.14
I've dug into this a bit..
found this post..
don't think it's finished yet but looks like esp is working on adding this for Arduino core..
so it's the reverse from what we normally see here, you need the latest core version, but again, it's still open..
espressif/arduino-esp32/milestone
curious, where did you get this code??
i'm thinking they are using platformio..
good luck.. ~q