Getting Started with ESP32 Programming on GNU/Linux

Hello! It's been a while since the last time I posted something on this page. It's because things was rough lately and they demand a lot of attention, focus, and energy.

This time I'm gonna tell you about how to get started with ESP32 programming on GNU/Linux environment.

What is ESP32?

ESP32 is the next generation of microcontroller and many said that it is the successor of the infamous ESP8266, microcontroller unit with built-in WiFi support.

ESP32 is a lot better than ESP8266 in so many ways. But, it is still new which means many people haven't touched it yet. Here's the comparison table between ESP8266 and ESP32

The official framework for developing ESP32 program is called ESP IoT Development Framework and often abbreviated as ESP-IDF. ESP-IDF uses C and C++ as the programming language.

We can also use Arduino framework for ESP32 with Arduino IDE as the development environment. Even better, we can use both ESP-IDF and Arduino at the same time!

How? I'll write about it later.

Without further ado, let's get started by installing the required software for developing ESP32 program.

Installation

Debian-based distributions

sudo apt-get install git wget make libncurses-dev flex bison gperf python python-serial  

CentOS 7

sudo yum install git wget make ncurses-devel flex bison gperf python pyserial  

Achlinux

sudo pacman -S --needed gcc git make ncurses flex bison gperf python2-pyserial  
Installing the toolchain

If you have finished installing these required software, you need to install the toolchain for ESP32. Fortunately, you can't use the package manager for this, and that's where the fun begins.

For 64 bit client, the download link is

https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-61-gab8375a-5.2.0.tar.gz  

And for the 32 bit client

https://dl.espressif.com/dl/xtensa-esp32-elf-linux32-1.22.0-61-gab8375a-5.2.0.tar.gz  

The next step is to download the ESP32 toolchain. In this post, I'll use the 64 bit version.

Note: my terminal's $CWD is the ~ or the home folder.

Now, we create esp directory inside the home folder and move to the newly created directory.

mkdir esp  
cd esp  

Download the toolchain using wget.

$ wget -c https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-61-gab8375a-5.2.0.tar.gz

Next, extract the newly downloaded toolchain.

tar -xzf xtensa-esp32-elf-linux64-1.22.0-61-gab8375a-5.2.0.tar.gz  

The toolchain wil be extracted to ~/esp/xtensa-esp32-elf directory.

Now, you need to add the path to esp32 toolchain binaries to the PATH environment variable.

export PATH=$PATH:$HOME/esp/xtensa-esp32-elf/bin  

You should put that line above to .bash_profile or .profile if you're using bash as shell. If you're not using bash, please add the line above to the file with similar function as .bash_profile or .profile.

If you don't do that, you'll have to run the export command whenever you want to develop ESP32 firmware.

Installing ESP IoT Development Framework

After installing the toolchain, you need to install the framework as well.

First, we need to clone the ESP-IDF repository to the ~/esp/esp-idf directory. Inside the ~/esp directory, run:

git clone https://github.com/espressif/esp-idf.git --recursive  

Now, add the IDF_PATH environment variable that contains the path to ESP-IDF directory.

export IDF_PATH=$HOME/esp/esp-idf  

Same as before, you should add the above line to .bash_profile or .profile or files with similar function.

To confirm the ESP-IDF installation, restart the terminal and run

printenv IDF_PATH  

If you see nothing, you may want to recheck the installation process.

Hello World

Yay, we hanve installed the toolchain and framework to our system (or user scope). I will not explain any lines of code here. I'm just gonna explain how to build and flash the ESP32 program.

Ok, we will copy the hello_world example from the ESP-IDF examples to the ~/esp directory.

cp -r $IDF_PATH/examples/get-stated/hello_world .  

Next, connect your ESP32 to the USB port using USB cable. If you're using a development board such as Nano32, ESP32 dev kit, then you're all set.

Configure

Before building & flashing the code to the ESP32 board, we may need to change the default configuration values. In this case, we're going to change the ESP32 serial port and baud rate of the serial flasher.

Run

make menuconfig  

Use up & down arrow keys to navigate between menu items. Press enter to configure the selected menu and press esc to exit from the current configuration.

Select the Serial flasher config menu. Change the Default serial port configuration value to match the serial port of your ESP32.

Next, change the baud rate. Optionally, you can use the 2M baud rate which is a lot faster than the default (115200) baud rate.

Exit and save the configuration by pressing esc and select yes if the menuconfig ask you to save the configuration.

Flashing

Next, build and flash the program.

make flash  

Alternatively, you may add the -j flag with the number of your processor core as the flag value e.g

make -j4 flash  

This will create 4 jobs for compiling the source code.

Open the serial monitor

If everything went smoothly, you can open the serial monitor by running

make monitor  

You will see the log from the hello world program. Press CTRL + ] to exit the serial monitor.

What's Next?

You have installed the ESP32 development environment to your GNU/Linux system. So, what's next?

You can browse the esp32 forum at esp32.com to get more information about ESP32. You can also read the ESP-IDF documentation at esp-idf.readthedocs.io.

I'll post some more tutorials about ESP32 programming. Next post will be about manipulating GPIO.

Thanks for reading!