Using Arduino IDE for the ESP32 in a LXD container

The ESP32 is a dual-core microcontroller that has both WiFi and Bluetooth. The ESP32 is the next generation of the older single-core microcontroller ESP8266 that only had WiFi. You can perform most tasks that you did with the various Arduinos but with the ESP32 (or ESP8266) and you also have WiFi embedded into the microcontroller. You can even use the Arduino IDE to write programs for the ESP32 (or ESP8266).

In the previous post, Setting up the ESP32 in LXD on Ubuntu, we saw how to connect the ESP32 to a PC running a Linux distribution, connect to the serial console, get this working to in a LXD container, setting up the ESP32 toolchain and ESP-IDF development environment, compiling the samples and finally flashing them onto the ESP32 board.

In this post we setup the Arduino IDE into a LXD container and set it up to develop software for the ESP32 board. That is, instead of messing our desktop Linux with the ESP32 development packages, we create a development LXD container for ESP32 and put them all inside there.

Creating the LXD container

The Arduino IDE has a graphical user-interface. The LXD container needs some extra support in order to show the window of Arduino IDE. We do that by following the instructions at How to easily run graphics-accelerated GUI apps in LXD containers. These instructions tell us to create a LXD profile that has the necessary instructions to launch LXD containers for GUI applications (X11). After you have set up this GUI LXD profile, come back to continue.

Let’s create the container and name it esp32gui.

$ lxc launch ubuntu:18.04 --profile default --profile gui esp32gui
Creating esp32gui
Starting esp32gui

Then, share the device /dev/ttyUSB0 into the container. The command creates a Unix character file into the container and links it to the one on the host.

$ lxc config device add esp32gui ttyUSB0 unix-char source=/dev/ttyUSB0 path=/dev/ttyUSB0 major=188 minor=0 uid=0 gid=20 mode=666
Device ttyUSB0 added to esp32gui

We are ready to enter the container and do stuff.

Setting up the esp32gui LXD container

Let’s get a shell into the LXD container.

$ lxc exec esp32gui -- sudo --user ubuntu --login

Run again the screen command as before, to test that the ESP32 board is accessible in the LXD container.

After you have done that, we can move onto installing the Arduino IDE.

Installing the Arduino IDE

We are going to use Ubuntu Make to install the Arduino IDE in the LXD container. We also install the GTK 2.0 library because it’s a dependency for the Arduino IDE (based on Java, and the default setup needs GTK-2.0 for theming purposes). Finally, we install Python 2 with Python Serial and Python PIP, which are required by the ESP32 flashing utility.

ubuntu@esp32gui:~$ sudo apt update
ubuntu@esp32gui:~$ sudo apt install ubuntu-make
ubuntu@esp32gui:~$ sudo apt install libgtk2.0-0
ubuntu@esp32gui:~$ sudo apt install python python-serial python-pip

Ubuntu Make has the command umake that can be used to install various development packages. The Arduino IDE is part of the ide group, therefore, we install with umake ide arduino. You will be prompted for the location to install the Arduino IDE. Simply press Enter to accept the default location.

ubuntu@esp32gui:~$ umake ide arduino
Choose installation path: /home/ubuntu/.local/share/umake/ide/arduino
Downloading and installing requirements |
100% |#############################################################|
Installing Arduino
|#############################################################|
Installation done
ubuntu@esp32gui:~$

Finally, we need to logout and log in again so that the new shell gets updated with the new $PATH of the Arduino IDE.

ubuntu@esp32gui:~$ logout
$ lxc ubuntu esp32gui
ubuntu@esp32gui:~$

And, that’s it. Now we can launch the Arduino IDE and set it up to work with the ESP32. Here it is,

ubuntu@esp32gui:~$ arduino
The Arduino IDE running from inside a GUI LXD container.

Setting up the Arduino IDE for the ESP32

The ESP32 support for the Arduino IDE is developed as an add-on. Here are the instructions on how to add ESP32 board support to Arduino IDE.

First, in the Arduino IDE, click on File→Preferences. Append https://dl.espressif.com/dl/package_esp32_index.json into the Additional Boards Manager URLs text box as shown in the screenshot and press OK.

Added the ESP32 board into the “Additional Boards Manager URLs” text box.

Then, click on Tools → Board: ???? → Boards Manager… You will be presented with the following screen. Locate the entry for esp32 (hint: it’s at the very end) and click to Install.

The esp32 entry in the Boards Manager of the Arduino IDE.
Installing the esp32 support in the Boards Manager of the Arduino IDE.

After the esp32 has been installed, we are ready to select the appropriate board from the list Board list in the Arduino IDE. Indeed, it’s a long list. My no-name ESP32 board says ESP32 DEVKITV1 so I selected ESP32 Dev Module. Each entry has information on what is supported in the hardware of your specific board. If something does not work later on, we might need to revisit here to change the model to something else.

Finally, we need to select the port (/dev/ttyUSB0) of our ESP32 board. Here is how it looks. We have selected an ESP32 board and the rest of the configuration has been auto-filled for us. The Port is something that we have to select. Note that LXD supports hot-plugging, which means that if you connect now your ESP32 board to your computer, the proper port device /dev/ttyUSB0 will appear at one.

We do not need to setup any other option at this point. The “Programmer: AVRISP mkll” is not used on the ESP32 boards, therefore we do not need to bother with that value.

We are ready to compile and flash any demo. Time for the Blink demo. Open up the examples and select the Blinker (more advanced version of the standard Blink application).

When you select it, it will get loaded in a new Arduino IDE window. Note that we changed the LED_PIN value from 21 to 2 (the correct value for the embedded LED on this ESP32 board).

The Arduino IDE with the Blinker sample program. We have changed the LED_PIN value to 2, which is the appropriate value for the embedded LED pin of this ESP32 board.

Finally, we are ready to upload to the ESP32 board. Press the button shown in the screenshot (mustard color, arrow points to the right). This will do what is required to compile the sample and upload (flash) to the ESP32.

If everything went OK, you will be able to see the blue LED of the ESP32 board blinking. It is not the typical blinking, there is variation of fast and slow blinking. Blinking bliss.

The Arduino IDE works with the ESP32 board. Now let’s create an application shortcut to launch the Arduino IDE from the LXD container.

Creating a launcher for Arduino IDE

The command to run the Arduino IDE from the LXD container is the following. It gets a shell as user ubuntu in the container, then runs arduino. That’s the command to add to our launcher.

$ lxc exec esp32gui -- sudo --user ubuntu --login arduino

There is already a launcher for the Arduino IDE, but it is inside the container. We are copying that launcher on the host, and then change the command to the above one. Here we go.

$ lxc file pull esp32gui/home/ubuntu/.local/share/applications/arduino.desktop ~/.local/share/applications/arduino.desktop

$ lxc file pull esp32gui/home/ubuntu/.local/share/umake/ide/arduino/lib/arduino_icon.ico ~/.local/share/icons/arduino_icon.ico

Then, edit the arduino.desktop file with your favorite editor (i.e. gedit ~/.local/share/applications/arduino.desktop) and adapt to the following. Save and that’s it. You can now find the Arduino IDE in the list of your applications.

[Desktop Entry]
Version=1.0
Type=Application
Name=Arduino
Icon=/home/REPLACE_WITH_USERNAME/.local/share/icons/arduino_icon.ico
Exec=lxc exec esp32gui -- sudo --user ubuntu --login arduino %f
Comment=The Arduino Software IDE
Categories=Development;IDE;
Terminal=false

Conclusion

We have seen how to set up a LXD container in order to run the Android IDE development environment with support for the ESP32. You can then try some more samples, including those about WiFi (you can get the ESP32 board to scan for available networks) and Bluetooth.

I think I have covered most of the issues. If there is something missing, or you have a query, fill free to ask in a comment.

Permanent link to this article: https://blog.simos.info/using-arduino-ide-for-the-esp32-in-a-lxd-container/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.