On ESP32 within Arduino IDE, can I run a script from a string?

642 Views Asked by At

I know that I can load MicroPython or Espruino(JS) on the ESP32 and run a script from there but (for several reasons outside the scope of this question) I can't do it that way.

What I want is a framework written in the Arduino IDE that:

1: Loads an encrypted string from an external source (I can do this)

2: Decrypt the string (I can do this)

3: Run the string as a script and return a value (no clue how to do this)

Any ideas on how to start? The script can be written in any language and does not need to interface with IO on the chip or anything fancy.

2

There are 2 best solutions below

3
On

Arduino programs are written in C++. C++ is a "compiled" language - the compiler runs on your computer and turns the program source code into a machine code binary that's then copied to the flash memory on the ESP32 and executed there.

MicroPython (Python) and Espruino (Javascript) are both interpreted languages. The interpreter is a program written in C or C++, compiled, and then copied to flash memory on the ESP32. It's the interpreter that reads the strings that represent the program and then executes them. That's why MicroPython and Espruino both are able to execute a script loaded externally.

Arduino programs can't do this, at least not for C or C++ programs, because the C or C++ compiler would have to run on the ESP32, and then the binary code would have to be linked against the code already running on the ESP32. This is all quite a bit beyond the capabilities of the CPU and the Arduino framework (and ESP-IDF, which it's built using).

So you're not going to have your C++ program that runs on an ESP32 load and compile another C++ program, execute it, and use its return value.

What you can do is use an embedded interpreter for another language to interpreter and execute code in that language. You could even write your own if you want.

These interpreters will only be suitable for running small, simple programs. If you want to do anything more then you're best off sticking with MicroPython, CircuitPython or Espruino, which I understand you said you can't do, but that's your only choice in this area.

You can find examples of embedded interpreters by searching the web for "esp32 embedded interpreter". The languages Forth, Lua and even BASIC may work. There is also an "embedded C interpreter" called "picoc", which will be limited compared to true C but might do what you need.

0
On

Found it. This is a script that looks like C++ and is an interpreted language running inside Arduino IDE. It runs perfectly on an ESP32

https://github.com/jingoro2112/wrench