A code fix is required when importing an Arduino example into PlatformIO.
I was starting to convert from Arduino IDE to Platform IO on VSCode. I wanted to use the last project that I did as a first example to try out Platform IO. This project was the ESP32 CameraWebServer example discussed in the Smart Home v1.5 ebook in Module 11, and located in the Arduino IDE examples. Compiling and uploading this example project using the Arduino IDE works fine, but I wanted to do this in Platform IO, so I copied all the source files (CameraWebServer.ino, app_httpd.cpp, camera_index.h, Camera_pins.h, ci.json) from the Arduino IDE to a new project folder. I then went to VS code -> PlatformIO Home -> Import Arduino Project. I also elected NOT to use Arduino libraries when importing, because I want to run VS Code/PlatformIO independent of Arduino IDE.
Before compiling the code I noticed the following error flagged in VS Code:
In the file ‘app_httpd.cpp‘ in the very last lines of the code (lines 1321 to 1327) inside the function ‘setupLedFlash‘ the call to ledcAttach(pin, 5000, 8); is flagged as an error because there is no identifier ‘ledcAttach‘ referenced anywhere (see screenshot https://drive.google.com/file/d/1wgQHlPp_yQ_oZ1B5PgwdzVQ5-fL24EJW/view?usp=sharing ).
Upon compiling, VS Code catches the error and suggests using the function ‘ledcAttachPin‘ ( see screenshot https://drive.google.com/file/d/1_WjzDjcpgdC4GMa_cgc5tKzgJ434-Ewa/view?usp=sharing ).
But the definition of ledcAttachPin is
void ledcAttachPin(uint8_t pin, uint8_t chan)
So I changed the function to ledcAttachPin, but since it is only using two int variables, ‘pin‘ and ‘chan‘ I deleted the ‘5000‘ argument from the function call and recompiled. The compilation was successful after this change.
ledcAttachPin is found in the PlatformIO library ‘esp32-hal-ledc.c‘.
ledcAttach is found in the Arduino ESP32 library ‘esp32-hal-ledc.c’.
It’s definition is:
bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution) {
int free_channel = ~ledc_handle.used_channels & (ledc_handle.used_channels + 1);
if (free_channel == 0) {
log_e(“No more LEDC channels available! (maximum is %u channels)”, LEDC_CHANNELS);
return false;
}
uint8_t channel = __builtin_ctz(free_channel); // Convert the free_channel bit to channel number
return ledcAttachChannel(pin, freq, resolution, channel);
}
So these two libraries are different between Arduino and PlatformIO, and the functionality of the freq variable must be accounted for.