[Arduino 教學] Build ESP32 code on Ubuntu
先前在收到 ESP-CAM 這塊開發板時,曾經在 Ubuntu 下 build 過 ESP32 的 原始碼,
請參考這篇文章 ESP32-CAM ( Ai-Thinker Module )。
最近在玩 MicroPython (MP) ,想深入研究 MP,就必須自己 Build Code 研究,
上次 (ESP32-CAM ( Ai-Thinker Module )) 沒把過程詳細記入下來,
如今在 Ubuntu VM 下又實作一次,又是跌跌撞撞的,做到一半就想把過程步驟寫下來,
希望對日後有幫助。
這次改在 Ubuntu 18.04 VM 來建立開發環境,方便 Ubuntu 以及 Windows 10 同時操作
,文章也可即時撰寫。不廢話了,進入步驟描述吧!
步驟 :
1. Get ESP-IDF
1.1 Change directory to your working directory
1.2 Create an ESP folder at your working directory
sudo mkdir -p esp
cd esp
1.3 Download ESP-IDF under esp folder
sudo git clone --recursive https://github.com/espressif/esp-idf.git
1.4 做到 1.3 這一步,遇到一個 Error : "Command 'git' not found"
那是因為新裝好的 Ubuntu 18.04 VM,還沒裝任何 Package。
So, I need to install git first then run step 1.3 command again
sudo apt-get install git
Result from step 1
2. Setup ESP-IDF Tools
2.1 Change directory to /esp-idf by below commands
cd esp-idf
2.2 Execute below command to install ESP-IDF Tools
./install.sh
2.3 做到這一步,還是因為新裝好的 Ubuntu 18.04 VM,還沒裝任何 Package,
Python 以及 Python-pip 沒有安裝。
Please execute the below commands to install them。
sudo apt-get install python
sudo apt-get install python-pip
If you want to install Python3, run below commands
sudo apt-get install python3
sudo apt-get install python3-pip
After installed Python package if you still meet below error
/usr/bin/env: ‘python’: No such file or directory
Please execute below commands to create a symlink
sudo ln -s /usr/bin/python3 /usr/bin/python
Result from step 2
3. Setup the environment variables
3.1 Every time, you want to build code before, please run below
command to setup the environment variables
. ./export.sh
or
. $HOME/esp/esp-idf/export.sh
Result from step 3
4. Start a project by using built-in examples
4.1 Copy built-in example "Blink" under \esp folder
cd $IDF_PATH/..
sudo cp -r $IDF_PATH/examples/get-started/blink .
4.2 Need cmake tool before build, please run below command to install it,
otherwise you will get an error as below
sudo apt-get install cmake
5. Connect ESP32 board to PC
5.1 Run below command before and after to get serial port number
ls /dev/tty*
In my result, the serial port number from my two runs is
/dev/ttyUSB0
6. Configure then physical build
6.1 Configure target build by execute below command
idf.py set-target esp32
PS: Setting the target with idf.py set-target esp32 should be
done once, after opening a new project.
If the project contains some existing builds and configuration,
they will be cleared and initialized.
6.2 Execute below command to check build environment,
the following menu will appear if previous steps are correct.
idf.py menuconfig
6.3 Execute below command to physical build.
idf.py build
Build Complete Result
7. Flash code
7.1 From result screen, already tell us to run idf.py -p (port) flash command
to flash code。
Before flash code, please execute below command to get serial port right
sudo chmod 666 /dev/ttyUSB0
7.2 It's time to flash code now
idf.py -p /dev/ttyUSB0 flash
7.3 把 LED delay time 從 1000ms 改成 500ms,讓 LED 閃爍快些;
重新再Build & Flash 一次;看是否如預期。
後記 :
前面 ESP-IDF 的版本是使用 Master / 當時 Latest 的版本, 並非正式 Release 版本,
在 Build MicroPython Image 時遇到了問題,因為 MicroPython 只支援 ESP-IDF v3.0
以及 v4.0,所以我就重新安裝成 MicroPython 所支援的 v4.0。
重新再 Build 一次 Blink Project 發現 v4.0 和 Master 的差異有些大。
以下就是差異點 :
1. “idf.py set-target esp32” command is not support,直接下“idf.py menuconfig”
2. 下“idf.py menuconfig”command 後,會有一堆 Errors,那是因為還缺一些 packages
要再執行以下commands 安裝 packages
$ sudo apt-get install libncurses5-dev libncursesw5-dev
$ sudo apt install flex gperf bison
這樣才能順利執行 “idf.py menuconfig”以及“idf.py build”