This guide will walk you through the process of transitioning your existing PlatformIO project with a single main file to an ESP8266 RTOS SDK project.
First, ensure you have the ESP8266 RTOS SDK toolchain installed.
- Download the toolchain (v8.4.0) from the official Espressif website.
- Extract the downloaded zip file to a directory of your choice (e.g.,
C:\esp\
).
- Open a terminal window.
- Run the following commands:
mkdir -p ~/esp
cd ~/esp
wget https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-macos.tar.gz
tar -xzf xtensa-lx106-elf-gcc8_4_0-esp-2020r3-macos.tar.gz
Note: Replace the URL with the appropriate one for your operating system (Linux 64-bit or 32-bit).
Add the toolchain's bin
directory to your PATH environment variable.
- Open the Start menu and search for "Environment Variables".
- Click on "Edit the system environment variables".
- Click the "Environment Variables" button.
- Under "System variables", find the "Path" variable and click "Edit".
- Click "New" and add the path to your toolchain's
bin
directory (e.g.,C:\esp\xtensa-lx106-elf\bin
). - Click "OK" to save the changes.
Add the following line to your ~/.profile
or ~/.bash_profile
file:
export PATH="$PATH:$HOME/esp/xtensa-lx106-elf/bin"
Then, reload the profile by running:
source ~/.profile
Download the ESP8266 RTOS SDK:
cd ~/esp
git clone https://github.com/espressif/ESP8266_RTOS_SDK.git
Set the IDF_PATH
environment variable to point to the ESP8266_RTOS_SDK directory:
- Follow the same steps as adding to PATH, but instead of editing "Path", create a new system variable.
- Set the variable name as
IDF_PATH
and the value as the path to your SDK directory (e.g.,C:\esp\ESP8266_RTOS_SDK
).
Add the following line to your ~/.profile
or ~/.bash_profile
file:
export IDF_PATH="$HOME/esp/ESP8266_RTOS_SDK"
Then, reload the profile:
source ~/.profile
Now, let's create a new ESP8266 RTOS SDK project and migrate your PlatformIO code:
- Create a new directory for your ESP8266 RTOS SDK project:
mkdir ~/esp/my_esp_project
cd ~/esp/my_esp_project
- Copy the example project structure:
cp -r $IDF_PATH/examples/get-started/hello_world/* .
- Replace the contents of
main/hello_world_main.c
with your PlatformIO main file:
cp /path/to/your/platformio/project/src/main.cpp main/hello_world_main.c
- Modify your main file:
- Change the file extension from
.cpp
to.c
if you're using C++. - Replace
#include <Arduino.h>
with#include "esp_common.h"
and#include "esp_system.h"
. - Replace Arduino-specific functions with ESP8266 RTOS SDK equivalents. For example:
Serial.begin(115200);
becomesuart_init_new();
Serial.println();
becomesprintf();
- Wrap your
setup()
andloop()
functions in a task:
- Change the file extension from
void app_main(void)
{
// Your setup code here
while(1) {
// Your loop code here
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
-
Update
CMakeLists.txt
in themain
directory:- If you've added any new source files, add them to the
SRCS
list. - If you're using C++, change
hello_world_main.c
tohello_world_main.cpp
.
- If you've added any new source files, add them to the
-
Configure your project:
make menuconfig
- Build the project:
make all
- Flash the project to your ESP8266 (replace
/dev/ttyUSB0
with your actual serial port):
make flash ESPPORT=/dev/ttyUSB0
- Monitor the output:
make monitor ESPPORT=/dev/ttyUSB0
If your PlatformIO project uses external libraries, you'll need to port them to work with ESP8266 RTOS SDK:
-
Check if the library is available for ESP8266 RTOS SDK. If so, you can include it in your project's
components
directory. -
If not, you may need to adapt the library yourself:
- Copy the library files into a new directory under
components/
. - Create a
CMakeLists.txt
file in the library directory to define it as a component. - Modify the library code to use ESP8266 RTOS SDK APIs instead of Arduino APIs.
- Copy the library files into a new directory under
-
Update your main code to include the adapted library headers.
Remember that migrating from PlatformIO to ESP8266 RTOS SDK often requires significant code changes due to differences in the underlying frameworks. You may need to rewrite portions of your code to work with the ESP8266 RTOS SDK APIs.
This process provides a starting point for migrating your project. Depending on the complexity of your PlatformIO project, you may need to make additional adjustments to fully port your application to the ESP8266 RTOS SDK environment.