Main Content

Run a C Language Interpreter on Your ESP32

Details the Implementation and use of a C Language Interpreter with a browser Interface running on an ESP32

Introduction
I’ve spent considerable time in the Arduino IDE waiting for the sketch to compile and upload only to discover that the sketch did not behave as I expected and to then consider where to add Serial.printx statements to the sketch, wait again for compile and upload, and lose hair trying to get the results I desired. What could be better than putting an if (j==3 && k>11) Debug(); statement into your program and to be able to get debug information out from the program when the variables meet the conditional requirements?

So… I put together a C language interpreter for the ESP32 based boards that allows me to use their built-in file system for persistent program storage, to edit and Interpret my programs within a browser page without needing a compile and upload cycle. Better yet, I built in support for the common I/O Arduino functions such as analogWrite, digitalRead/Write, and supplied highly accurate servo positioning functions. But maybe the best part is that I added a debugger to the Interpreter that supports conditional Debug tracing, and conditional Watch functions that report variable values as the program executes. This article gives credit to those who built tools that I used for the Interpreter and also explains how to use it on your ESP32.

Background
There has been continuing discussion of Interpreted versus Compiled code and the virtues and disadvantages of each approach. Suffice it to say that interpreted code will run more slowly than compiled code and that many interpreters offer less than brilliant error descriptions. But the Interpreter is known for its value as a prototyping tool and provides a rapid turnaround code environment. Another useful Interpreter feature is the ease with which it introduces novice programmers to creating useful programs.

Zik Saleeba created the picoc Interpreter and its source is available from https://gitlab.com/zsaleeba/picoc. It is a solid foundation for a C Language interpreter and I tip my hat to him for its creation.

Much of the web interface is inspired by, and liberally copied from work by Michael Molinari for his esp8266 Basic available from https://github.com/esp8266/Basic.

Using the Code
The references at the top of the article are for the files needed to compile and upload the Interpreter to your ESP32. The first thing that you will notice when you have it in the Arduino IDE is, I bet, that you have never seen an Arduino sketch with so many tabs. Much of this is due to keeping the file structure of the picoc interpreter in place within the Arduino IDE, and to simplify the segregation of C and C++ code sections. The good news is that the sketch provides a very thorough exercise of the WebServer Arduino object, a completely transparent look into the bowels of an Interpreter, illustrates how to use C language code within the Arduino IDE, and can provide countless hours of code browsing to see how this all works.

But, I hope that you really aren’t here to understand the internals of a C Interpreter. I think that you are reading this because you want to explore running the C Interpreter on your ESP32. The Interpreters use is documented in the following paragraphs. If you want even more detail, you can consult ESP32_picoc_C_Language_Interpreter.pdf.

Before you upload the sketch to your ESP32 board, use the Tools/ESP32 Sketch Data Upload menu item to put the contents of the data directory within the sketch folder onto your ESP32. You will also need to edit the ESP32Program/data/data/WIFIname.dat and WIFIpass.dat files before their upload to match your WiFi environment. When you have uploaded the ESP2Program sketch to your ESP32 board, it will be running on the Serial port at 500,000 baud. Consulting the Serial Monitor after a board reset will give you the IP address that the board is using. The ESP32Program.ino file contained in the zip file has a few #define statements at its beginning that are important.”

Link to article

Related Content