Compiling for Wemos D1 Mini AND C3 Mini

Hello, I am relatively new to PlatformIO but have grasped the nettle and am making good progress.

I have some code (simple code) which writes to the Serial Pin using Serial.Println (on Wemos D1) but to get the output from the TX pin on the C3 Mini I have to write to Serial0 - thus Serial0.println etc.etc.etc.

Is there some simple method that I can use to make the same code work for both devices? I have been advised that I can do something in the platformio.ini file that will make the compile process interpret Serial as Serial0 (or perhaps Serial0 as Serial) but everything I have tried thus far has failed - it was also several weeks ago and has been lost from my ageing mind.

Can anyone advise as to whether there is a simple method I could employ to achieve this feat?

  • Richard

Hello @richard2

Which board do you use exactly?
Please show the content of your platformio.ini

Here is my platformio.ini:

[platformio]
default_envs = d1_mini, lolin_c3_mini
;default_envs = lolin_c3_mini
[env]
lib_deps =
jchristensen/Timezone@^1.2.4
adafruit/Adafruit BME280 Library@^2.2.4
adafruit/Adafruit BMP280 Library@^2.6.8
adafruit/Adafruit Unified Sensor@^1.1.14
alanswx/ESPAsyncWiFiManager@^0.31
me-no-dev/AsyncTCP@^1.1.1
ESP Async WebServer=https://github.com/judge2005/ESPAsyncWebServer.git

[env:d1_mini]
platform = espressif8266 @ 4.2.1
board = d1_mini
framework = arduino
build_flags =
-D d1_mini
platform_packages = framework-arduinoespressif8266 @ 3.30102.0
lib_deps =
${env.lib_deps}
[env:lolin_c3_mini]
platform = espressif32 @ 6.5.0
board = lolin_c3_mini
framework = arduino
platform_packages = framework-arduinoespressif32 @ 3.20014.231204
build_flags =
-D lolin_c3_mini
lib_deps =
${env.lib_deps}
upload_port = COM[10]

I had some help getting the libraries right.
The two boards I want to compile for are:
Lolin Wemos D1 Mini ESP8266
Lolin Wemos C3 mini ESP32
They share the same footprint

  • Richard

The Serial object is controlled by the flag ARDUINO_USB_CDC_ON_BOOT (and ARDUINO_USB_MODE) - See ESP32-S3 native USB interface and Serial Monitor missing first messages - #10 by sivar2311

The lolin_c3_mini.json sets ARDUINO_USB_CDC_ON_BOOT=1.

To clear this flag, add to your platformio.ini:

build_unflags=
  -DARDUINO_USB_CDC_ON_BOOT

If you now hover over the Serial object in your code, you should see HardwareSerial Serial instead of HWCDC Serial.

1 Like

Thank you.
I added the ‘unflag’ and then changed all my ‘Serial0’ references back to ‘Serial’ and got a compile OK. In fact I set it to compile for both platforms and it compiled both OK.
I will read that thread next as I have to get my head around this - I will also drop this code on to a micro and test it.
Am I right in thinking that, by making this change, the ‘Serial’ object that references the TX pin on the D1 Mini will also reference the TX pin on the C3 mini (where I previously had to use ‘Serial0’) ? And furthermore - will the serial output be echoed to the monitor via the USB port as it does be default on the D1 Mini?

  • Richard

By changing ARDUINO_USB_CDC_ON_BOOT=0, the output of Serial is routed to the UART port (RX/TX pins) and no longer to the built-in USB port (HWCDC).

To output to the built-in USB port, you must use the USBSerial object.

// print to UART
Serial.println("Hello UART");

// print to builtin USB
USBSerial.println("Hello USB");
1 Like

Got It, I understand, thank you very much for your guidance.

  • Richard
1 Like

Here comes a bonus if you want output to UART and USB at the simultaneously:

#include <Arduino.h>

class MySerial : public Print {
  public:
    size_t write(uint8_t c) {
        Serial.write(c);
        USBSerial.write(c);
        return 1;
    }
};

MySerial mySerial;

void setup() {
  Serial.begin(115200);
  USBSerial.begin(115200);
}

void loop() {
  Serial.println("Hello UART");
  USBSerial.println("Hello USB");
  mySerial.println("Hello UART & USB!");
}

1 Like

Wow - now that very much is a bonus - thank you for that.

  • Richard