Serial Monitor outputs unreadable characters

Hello there !

Today I thought of moving from Arduino IDE to PlatformIO IDE, because I’ve found some things that don’t work in Arduino IDE but do in PlatformIO IDE. Also I’ve used VC Studio in the past for C# and I loved it.

I just finished building my first project in Platformio IDE and I am left with some questions.

  1. Where can I find settings for ESP32 board ? Like the ones in Arduino IDE ? Image

  2. Should it be easy to use ? I don’t find it easy to use and the documentation makes it even harder to understand.

2.1 What do I do with -b , --baud . Where should I write these ? Why doesn’t it tell me where to write this code ?
2.2 What do I do with this ?

[env:custom_monitor_port]
...
; Unix
monitor_port = /dev/ttyUSB1

; Windows
monitor_port = COM3 

I didn’t understand a thing from this:
docs.platformio.()org/en/latest/projectconf/section_env_monitor.html#monitor-speed
[Remove the brackets () .New users can’t post more than 2 links in a post]

Shouldn’t the documentation be easier ? Or am I a bigger idiot that I thought I was ?

I posted this because I have a problem with the Serial Monitor. It outputs ? characters. Image

#include "Arduino.h"
#include <SPIFFS.h>
#include <WiFi.h>

String v[2];
 
void setup() {
  
  Serial.begin(115200);

   
  if (!SPIFFS.begin(true)) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }
 
 
    //---------- Read file
    File fileToRead = SPIFFS.open("/inputs.txt");
 
    if(!fileToRead){
        Serial.println("Failed to open file for reading");
        return;
    }
 
    Serial.println("File Content: ");

    int i = 0;
    while(fileToRead.available()){
        String line= fileToRead.readStringUntil('\n');        
        v[i] = line;
        i++;        
        String string2 = String("Linia ") + i + ": " + line;
        Serial.println(string2);
        Serial.println();      
    }
 
    fileToRead.close();
    
    for(i = 0; i<2; i++) {
        Serial.println(v[i]);
      }

  WiFi.begin(v[0].c_str(),v[1].c_str());  

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to WiFi..");
  }
  Serial.println((String)"Connected to " + v[0] + " with IP addres: " + WiFi.localIP().toString() );
  
  Serial.println(SPIFFS.exists("/inputs.txt"));

} 
void loop() {}

This works in Arduino IDE. It should work in PlatformIO IDE, but it probably doesn’t because I have Serial.begin(115200); and the Serial Monitor’s Speed is at 9600.

Thank you !

See the documentation.

Which page tells you to write which code?

What exact part do you not understand?

You’re using a 115200 baud in your sketch but the default mointor_spped is 9600, thus you only get garbled characters. Open your project’s platformio.ini and add the line monitor_speed = 115200. See the documentation.

I suspect the OP is was reading the documentation for the platformIO CLI, instead of the platformio.ini configuration now that he’s using vscode + the platformio extension. Easy to do, when the docs for platformio.ini monitor_speed says “A monitor speed baud rate. See platformio device monitor --baud .”

@GeorgeFlorian When using the platformio extension / integration in VSCode, most of the CLI settings can now be specified in the platformio.ini, for things ranging from the board and environment code is being compiled for, upload ports, upload speeds, monitor port, monitor speed, library dependencies, etc, etc.

Once you get used to how PlatformIO does things, it is much quicker at compiling code, at compiling code for multiple board types, for being able to switch between boards and projects. Instead of having a rigid set of menus, you have the platformio.ini file that is used to configure each project, which is then transportable with your code. The monitor_port parameter is for setting which port the serial monitor will attach to, and you have the examples of what it would look like for linux/mac or windows. Once you have specified your port, naturally, you need to set the baud rate or speed, if it’s not the default 9600… so monitor_speed needs to be set also.

So, because this is all located in the “platformio.ini” (Project Configuration File) part of the documentation, this all goes in your platformio.ini file :wink: Now, for the esp32 dev board, these are the default settings… (although I needed to check the mentioned json file to check the flash_mode default, which is dio, not qio like in your screenshot), so a platformio.ini for you could look like:

[env:esp32dev]
platform = espressif32
board = esp32dev
board_build.flash_mode = qio
upload_port = /dev/ttyUSB0
upload_speed = 115200
monitor_port = /dev/ttyUSB0
monitor_speed = 115200

And naturally add more board_build parameters as you move away from the defaults, or even to set to the default, so there’s no confusion later. All of the relevant configuration options you can change are located here.

Clear as mud?

Hello ! Thank you for your reply @maxgerhardt !

I read some pages of documentation but things are not clearly explained there. I got stuck on some pages of it and I had no idea how to even search.

This:

Port, a number or a device name. See [ platformio device monitor --port ].

Example:

[env:custom_monitor_port] … ; Unix monitor_port = /dev/ttyUSB1 ; Windows monitor_port = COM3

If I click on platformio device monitor --port it goes to a page where I got -b and --baud code from without telling me where to write it, what to do with it.

This page tells me

Please platformio run --target the next command to erase the entire flash chip (all data replaced with 0xFF bytes):

> platformio run --target erase #
or short version
> pio run -t erase

I am sorry, but I don’t know where to write “the next command” and I assume the next command is either > platformio run --target erase # or > pio run -t erase. With or without > ?

The point I was trying to make is that the documentation is too quick to refer you to more advanced code, instead of limiting you (me, the noob) to the basics: like opening the project folder where the platformio.ini is and edit it with a .txt editor.

Here is how I got into trouble:

  1. I oppened this page:
    docs.platformio.()org/en/latest/ide/vscode.html#quick-start

  2. I read until I got to 8. Serial Port Monitor and I clicked on it.

  3. It redirected me here:
    docs.platformio.()org/en/latest/userguide/cmd_device.html#cmd-device-monitor

  4. Here is where I had no idea what to do with -b and --baud

  5. Then I just scrolled up and down with no idea on how to search for what I needed (mainly because I had no idea what was I looking for)

  6. I posted here in hope that some enlightenment will come

  7. It came:
    docs.platformio.()org/en/latest/platforms/espressif32.html#configuration

There are two ways to set the baude rate of your serial monitor in the VSCode IDE.

  1. By setting it in the platformio.ini of your project. This file can be found in your projects folder. It should be shown in VSCode’s explorer if you have the project opened. Ther you have to add the line
    monitor_speed = 115200

  2. You can manually start the serial from a console. Under windows just open one with win+r and type cmd and then enter. There you can start the monitor with the start it with pio device monitor. You can also do that from the console in VSCode. Start a new console in VSCode by pressing ctrl+shift+p and there type PlatformIO: New Terminal. In this terminal you can also use pio device monitor. To now manually change the bauderate pass the argument discussed int he docs pio device monitor -b 115200.

1 Like

Hello ! Thank you for you reply @pfeerick !

Here is how I got into trouble:

  1. I oppened this page:
    docs.platformio.()org/en/latest/ide/vscode.html#quick-start
  2. I read until I got to 8. Serial Port Monitor and I clicked on it.
  3. It redirected me here:
    docs.platformio.()org/en/latest/userguide/cmd_device.html#cmd-device-monitor
  4. Here is where I had no idea what to do with -b and --baud
  5. Then I just scrolled up and down with no idea on how to search for what I needed (mainly because I had no idea what was I looking for)
  6. I posted here in hope that some enlightenment will come

What is the difference between [env:esp32dev] and [env:esp32doit-devkit-v1]. Aren’t they the same ?

This is how my platformio.ini looks like:

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
board_build.flash_mode = qio
upload_port = /dev/ttyUSB0
upload_speed = 115200
monitor_port = /dev/ttyUSB0
monitor_speed = 115200

I would like to ask more questions about this. Does this mean that PlatformIO has those default settings when choosing ESP32 as environment and when I make changes to platformio.ini it overwrites some of those settings ?

Also, when do I write board_build and when do I not ?
This board_build.flash_mode = qio is the only setting that has board_build in front.

Hello ! Thank you for your reply @Thomseeen !

I found the platformio.ini file and made some changes.

I am using Linux Mint, will those commands work for it ?

In VSCode ctrl+shift+p and then typing PlatformIO: New Terminal should be the same. Starting a “normal” console in Mint, idk. Probably just a shortcut on your desktop?

1 Like

The [env:] bit is just the name you assign the board, your build environment. The board = bit is the important one, but in this particular case, the only difference between the two configurations as far as PlatformIO is concerned is the… vendor and board names… so it’s cosmetic :slight_smile:

Yes, spot on… the settings in the json are the board defaults, and setting the board_build flags override those settings. Normally you don’t use the board_build bit, other than when you are overriding some board default configuration. In your case, the flash_mode settings was the only board config setting I overrode… everything else was left at defaults, but it was some of the other platformio defaults that needed changing or customising.

ctrl+alt+p does nothing in VSCode. What should happen ?

I think that was meant to be Ctrl+Shift+P :wink:

1 Like

My bad… typo :smiley:

1 Like

I think I’ve got bigger problems than that right now.

  1. I made the changes in the platformio.ini as follows:

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
board_build.flash_mode = qio
upload_port = /dev/ttyUSB0
upload_speed = 115200
monitor_port = /dev/ttyUSB0
monitor_speed = 115200

and I have no output on the terminal and 3 problems:

Also, why does it take so long for it to upload ? It stops at this part everytime. Pressing BOOT does not help.
Wrote 691216 bytes (411061 compressed) at 0x00010000 in 36.5 seconds (effective 151.5 kbit/s)...

When I select Serial Monitor I get a pop-up saying There are task errors. See the output for details. I click on it and it goes to the OUTPUT window and there I have multiple Error: no handler found .

  1. With these settings:

[env:esp32dev]
platform = espressif32
board = esp32dev
board_build.flash_mode = qio
upload_port = /dev/ttyUSB0
upload_speed = 115200
monitor_port = /dev/ttyUSB0
monitor_speed = 115200

it doesn’t find any library. Should I include the libraries from Arduino ? If I use libraries from Arduino IDE why am I bothering with PlatformIO IDE ?

The codelinter complains about unused variables in ESP-IDFs framework code. You can safely ignore that which does not come from your code.

You put your upload baud at 115200 (approx. bits per second), which is slow when you want to upload ~410 kilobytes :wink: Depending on the USB-to-serial chip on your devboard you can safely increase the upload_speed in your platformio.ini to e.g. 460800 (for common baudrates see here).

This is an error within Visual Studio code, see the thread In VSC Task Window always popping up: "Error: No handler found" - #48 by mrred128.

1 Like

Upload speed and monitor speed are two seperate things; One is the upload speed used for uploading the firmware via bootloader, the monitor speed is the baud rate at which miniterm.py is started in order to read your serial prints, so this has to match the Serial.begin() statement.

Right, because if you delete those lines, the defaults will take over. Since the upload speed for this board is by default 921600 baud, it will be very fast. See platform-espressif32/boards/esp32doit-devkit-v1.json at develop · platformio/platform-espressif32 · GitHub. As I said, this depends on the board used. Your board supports this very fast upload speed.

1 Like

Ok ! Thank you !

Deleting upload_speed= 115200 made it upload way faster.

Restarting Serial Monitor one or two times finally got it to show what it was supposed to.

I hope I won’t have these newb problems in the future.

Can I get a link about libraries in the documentation ? Or a little explaining about how do they work ?

In Arduino IDE if I needed a library that didn’t come with the IDE or the ESP32 Core I had to download it and move it to ~/Arduino/hardware/espressif/esp32/libraries, or include them using the option menu from the Arduino IDE.

How do I proceed here ? Thank you !

You use the library manager (and the library dependency finder, LDF). See documentation. Basically you can browse the libraries at the library registry, download them using the VSCode GUI (section ‘libraries’) or directly declare their usage in the platformio.ini through the lib_deps = <lib-name-here> directive.

Thank you ! I managed to install a library from the library manager, but also added one in platformio.ini using lib_deps = ....

The “problem” I am facing now is that Serial Monitor works only when he wants to. It’s random. It never works on the first go, it sometimes works with the first or second restart, but sometimes it doesn’t matter how many times I wait or restart, I get nothing.
When using Arduino IDE I found that keeping the Serial Monitor windows open when uploading helped me getting an output all the time.

Is there any fix-around ? I lose a lot of time uploading every time the Serial Monitor doesn’t work.

Why reupload, first try to press the reset button. It might be that the serial monitor opens slightly too late after the upload and the ESP32 has already started the code execution. You can also add a small delay(1000); in setup() to combat that.

1 Like

Ok. I will try that. Thank you !

Man this community is awesome ! I didn’t expect to get answers so fast.
I am used to wait days for an answer on https://www.esp32.com/ , GtiHub or StackOverflow.

2 Likes