ESP32 - Arduino or micropython? + Arduino PlatformIO problems

Hi, I've seen some ideas about this topic, yet, I would like an opinion of someone with more experience than me.

My project consists of controlling three stepper motors in an automatic sequence. Then there are buttons that control run/reset/pause. On top of that, reading values from time-of-flight sensor and running web, on which you can control the run as well as with the buttons.

Is this too much to run with micropython on ESP32 board? And if it's not and it can run on just one board, is it better for me to go with micropython, arduino or micropython and then swap to arduino for better performance?

Thank you for ideas.

This page here seems to suggest that MicroPython is slower by almost two orders of magnitude compared to C++ (which is a more fitting description than "Arduino"), reducing the performance of a 96MHz ARM controller to less than C++ on a 16 MHz Atmega328.
With the slightly faster ESP32 compared to their ARM, we are then about in the order of magnitude of such an 8bit microcontroller.

What you describe seems to be doable for such a microcontroller, but do you really want to deliberately cripple yourself?

Do you know Python?
Do you know C++?
Are you willing to accept the somewhat flatter learning curve of C++ compared to Python?
Are there libraries available only in one language?
Are you planning to to do any more and potential more complex projects after this?

ElCaron:
This page here seems to suggest that MicroPython is slower by almost two orders of magnitude compared to C++ (which is a more fitting description than "Arduino"), reducing the performance of a 96MHz ARM controller to less than C++ on a 16 MHz Atmega328.
With the slightly faster ESP32 compared to their ARM, we are then about in the order of magnitude of such an 8bit microcontroller.

What you describe seems to be doable for such a microcontroller, but do you really want to deliberately cripple yourself?

Do you know Python?
Do you know C++?
Are you willing to accept the somewhat flatter learning curve of C++ compared to Python?
Are there libraries available only in one language?
Are you planning to to do any more and potential more complex projects after this?

Well, arduino is programmed not in C++ but in C.
Programming in python is much easier than C, especially in my case (Classes would shorten my program).
My experience with larger codes is better with python.
There's no problem with libraries.
Only thing I need to decide is if using "slightly" faster ESP32 will run better with C or micropython.

1 Like

Well, arduino is programmed not in C++ but in C.

Wrong

I have a couple of ESP32s and I downloaded MicroPython but I have not uploaded it to the ESP32 yet so I will be interested in this discussion.

EDIT to add ...
I find Python much more convenient for programs that manipulate text and large quantities of data but I suspect I would have little use for that sort of program on an ESP32

...R

darren41448:
(Classes would shorten my program).

The good news is that C++ encourages the use of OOP too.

darren41448:
Well, arduino is programmed not in C++ but in C.

That is, as you where told, plainly wrong. It is C++, and essentially all libraries that I came across are more or less object oriented (though often just a single object).
I am currently writing a [completely object oriented controller[/ur] for my floor heating for the ESP32, with support for MQTT, filters for the temperatur values, differenent thermostat and valve backends, OTA with telnet debug ...
For this, I get

DATA:    [=         ]  14.4% (used 47148 bytes from 327680 bytes)
PROGRAM: [======    ]  62.9% (used 824422 bytes from 1310720 bytes)

The bottleneck for speed is currently the update of the display, because I failed to wire the right pins for HW SPI. Be VERY careful with the pins you choose. There are SPI pins that you essentiall cannot use because they are used for the internal flash, there are SPI pins advertised that are actually for slave mode, there are input-only pins and there are IOs that need to be pulled HIGH or LOW during startup. Errors can very easily happen.
I just ordered one of these to be able to first breadboard the exact SMD module I am going to place on the PCB.

darren41448:
Programming in python is much easier than C, especially in my case (Classes would shorten my program).
My experience with larger codes is better with python.

That is why I asked what you know and what you are willing to invest.

darren41448:
Only thing I need to decide is if using "slightly" faster ESP32 will run better with C or micropython.

It will obviously run better with C++, because MicroPython is a horrible bloat, despite the name. As you can see with the Teensy 96MHz example. The 180MHz dual core ESP32 will give you more headroom, and that may be sufficient, but not more.

So again:

  • You are not able or not willing to code or learn C++ -> Try micropython.
  • You are -> The proper way is C++.

Also, if you decide to use C++ drop the ArduinoIDE. It is completely useless for a larger project or anything more than playing around with LEDs. I am quite happy with Visual Code+PlatformIO, but you can also use it with Atom or the commandline and your favorite editor.](GitHub - kvoit/FloorHeatingController_SW)

Robin2:
I find Python much more convenient for programs that manipulate text and large quantities of data but I suspect I would have little use for that sort of program on an ESP32

That may well be because we don't use the higher level String classes on microcontrollers, e.g. because they use too much dynamic memory. It is not like this would be better with MicroPython.

ElCaron:
That is, as you where told, plainly wrong. It is C++, and essentially all libraries that I came across are more or less object oriented (though often just a single object).
I am currently writing a completely object oriented controller[/ur] for my floor heating for the ESP32, with support for MQTT, filters for the temperatur values, differenent thermostat and valve backends, OTA with telnet debug ...
For this, I get

DATA:    [=         ]  14.4% (used 47148 bytes from 327680 bytes)

PROGRAM: [======    ]  62.9% (used 824422 bytes from 1310720 bytes)



[
The bottleneck for speed is currently the update of the display, because I failed to wire the right pins for HW SPI. Be VERY careful with the pins you choose. There are SPI pins that you essentiall cannot use because they are used for the internal flash, there are SPI pins advertised that are actually for slave mode, there are input-only pins and there are IOs that need to be pulled HIGH or LOW during startup. Errors can very easily happen.
I just ordered one of ](https://github.com/kvoit/FloorHeatingController_SW)[these](https://www.aliexpress.com/item/32967199184.html) to be able to first breadboard the exact SMD module I am going to place on the PCB.
That is why I asked what you know and what you are willing to invest.
It will obviously run better with C++, because MicroPython is a horrible bloat, despite the name. As you can see with the Teensy 96MHz example. The 180MHz dual core ESP32 will give you more headroom, and that may be sufficient, but not more.

So again:
- You are not able or not willing to code or learn C++ -> Try micropython.
- You are -> The proper way is C++.

Also, if you decide to use C++ drop the ArduinoIDE. It is completely useless for a larger project or anything more than playing around with LEDs. I am quite happy with Visual Code+PlatformIO, but you can also use it with Atom or the commandline and your favorite editor.



Edit: I was wrong about C only in arduino.

I already have https://s.click.aliexpress.com/e/cJsZ7Peu

I know a little from both C and python (maybe more of python). I will learn along with work on the project. So I want to choose correctly now.

To summarize what I read here, I should not flesh micropython on the board, instead download better arduino editor and learn C++?
Will I absolutely achieve faster performence of the code and possibly overall better and faster solution?

AFAIK MicroPython does not compile into a "real" executable binary, but into something called "byte code" (as known from JAVA). Byte code requires a runtime (or interpreter) in order to be executable and this makes the binary both bulkier and slower (*).

C(++) code compiles into a stand alone executable which is much leaner and faster and I would not even consider to use Python for any MCU, unless it was a requirement.

(*) Correct me if I'm wrong! :slight_smile:

Since you know C, it's trivial to learn enough C++ to program an Arduino. You'll come across objects like serial in almost every example that make it clear how to use them and for the most part, that's all you need. You're unlikely to use templates or write your own classes on day one of your Arduino adventures.

Danois90:
(*) Correct me if I'm wrong! :slight_smile:

That sounds correct.

The potential advantage of using Python is that it makes the programming easier and avoids the compile process. Whether the convenience offsets the slower performance depends on the individual application. Nobody would use C++ on a PC project unless it was essential to achieve the performance.

For projects that are primarily about internet communication the Python performance may be perfectly adequate.

Also I think it would be wrong to see the Python performance purely in terms of byte-code because some of the byte-code instructions probably call big chunks of C code to do the actual work.

...R

I've used the ESP32 (Plus the Teensy 3.6 and 4.0 plus the STM32) and the thing I find troubling is that there are so many flavors of the ESP32. I'm working on another book and we are using some of the Arduino family, but also including the above-mentioned processors. The ESP32 is a little weird in that some pins are input-only and the configurations are different. There are 30, 36, and 38 pin versions available and the pinouts seem to vary even within those constraints. That adds another bump in the path to writing about them. On the plus side, it has a very fast clock speed (240MHz), up to 4MB of flash and 500K of SRAM, builtin Wifi/Bluetooth, multi-core, and a host of other nice stuff. I've not used the Python runtime interpreter, but the Arduino GNU compiler works fine for me.

econjack:
up to 4MB

16MB
There is also a version with 8MB PSRAM.

If using Python on a dual core ESP32 does not allow the use of freeRTOS and the ESP32 API using Python is a thing to not do.

So I've tried programming using C/C++ for now.

I just can't get CSC PlatformIO to work. Same simple code, doesn't matter which (for example, blink from examples), in arduino ide works, here, it doesn't.

Any ideas?

*** [.pio\build\esp32dev\src\main.cpp.o] Error 1/code]

I am sure that is not the only output.

Hot guess: The Arduino IDE implicitly includes Arduino.h, while does not (and also does not include anythink else like Wifi.h that would include it)

src\main.cpp:16:12: error: cannot declare variable 'server' to be of abstract type 'WiFiServer'
 WiFiServer server(80);
            ^
In file included from C:\Users\filip\.platformio\lib\WiFi_ID870\src/WiFi.h:32:0,
                 from src\main.cpp:9:
C:\Users\filip\.platformio\lib\WiFi_ID870\src/WiFiServer.h:31:7: note:   because the following virtual functions are pure within 'WiFiServer':
 class WiFiServer : public Server {
       ^
In file included from C:\Users\filip\.platformio\packages\framework-arduinoespressif32\cores\esp32/Arduino.h:152:0,
                 from src\main.cpp:1:
C:\Users\filip\.platformio\packages\framework-arduinoespressif32\cores\esp32/Server.h:28:18: note:      virtual void Server::begin(uint16_t)
     virtual void begin(uint16_t port=0) =0;
                  ^
src\main.cpp: In function 'void setup()':
src\main.cpp:41:28: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
   WiFi.begin(ssid, password);
                            ^
In file included from src\main.cpp:9:0:
C:\Users\filip\.platformio\lib\WiFi_ID870\src/WiFi.h:79:9: note:   initializing argument 1 of 'int WiFiClass::begin(char*, const char*)'
     int begin(char* ssid, const char *passphrase);
         ^
*** [.pio\build\esp32dev\src\main.cpp.o] Error 1
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp: In static member function 'static char SpiDrv::spiTransfer(char)':
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:36:78: warning: integer overflow in expression [-Woverflow]
 #define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); } while (++ii < (X*F_CPU/16000000)); }
                                                                              ^
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:37:26: note: in expansion of macro 'DELAY_SPI'
 #define DELAY_TRANSFER() DELAY_SPI(10)
                          ^
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:76:5: note: in expansion of macro 'DELAY_TRANSFER'
     DELAY_TRANSFER();
     ^
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp: In static member function 'static void SpiDrv::getParam(uint8_t*)':
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:36:78: warning: integer overflow in expression [-Woverflow]
 #define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); } while (++ii < (X*F_CPU/16000000)); }
                                                                              ^
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:37:26: note: in expansion of macro 'DELAY_SPI'
 #define DELAY_TRANSFER() DELAY_SPI(10)
                          ^
C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:148:5: note: in expansion of macro 'DELAY_TRANSFER'
     DELAY_TRANSFER();
     ^
In file included from C:\Users\filip\.platformio\lib\WiFi_ID870\src\utility\spi_drv.cpp:22:0:
C:\Users\filip\.platformio\lib\WiFi_ID870\src/utility/spi_drv.h: At global scope:
C:\Users\filip\.platformio\lib\WiFi_ID870\src/utility/spi_drv.h:41:13: warning: 'initialized' defined but not used [-Wunused-variable]
 static bool initialized = false;

This is all. I didn't forget to include Arduino.h, but if there is anything else ArduinoIDE does include, that platformio doesn't then I didn't include it.

The code:

#include <Arduino.h>

// Load Wi-Fi library
#include <WiFi.h>

// Replace with your network credentials
const char* ssid     = "XX";
const char* password = "YY";

// Set web server port number to 80
WiFiServer server(80);

// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";

// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;

void setup() {
  Serial.begin(115200);
  // Initialize the output variables as outputs
  pinMode(output26, OUTPUT);
  pinMode(output27, OUTPUT);
  // Set outputs to LOW
  digitalWrite(output26, LOW);
  digitalWrite(output27, LOW);

  etc... (too many characters)
 the rest of the code: https://raw.githubusercontent.com/RuiSantosdotme/ESP32-Course/master/code/WiFi_Web_Server_Outputs/WiFi_Web_Server_Outputs.ino
  }
}

There is examples that should be in the Arduino IDE for ESP32 and WiFi, have you looked through the examples for the ESP32 WiFI?

Idahowalker:
There is examples that should be in the Arduino IDE for ESP32 and WiFi, have you looked through the examples for the ESP32 WiFI?

-Do you mean that there are examples in arduinoIDE or in platformIO - for esp32? (I know about arduino IDE examples, not about other ones, but that is not the point here anyways).

This code works perfectly in arduino ide, but not in platformIO. I am trying to find out why.