ESP32: manage multiple Serial and logging for debugging – 3

Spread the love

ESP32 multiple Serial UART and Logging levels
ESP32 multiple Serial UART and Logging levels

As you can see in the pinout diagram esp32 can have multiple UART, some are market as RX2 and TX2.

You can find ESP32 used for this test on ESP32 Dev Kit v1 - TTGO T-Display 1.14 ESP32 - NodeMCU V3 V2 ESP8266 Lolin32 - NodeMCU ESP-32S - WeMos Lolin32 - WeMos Lolin32 mini - ESP32-CAM programmer - ESP32-CAM bundle - ESP32-WROOM-32 - ESP32-S

We are going to see an ESP32 DOIT DEV KIT v1

esp32 dev kit pinout v1
esp32 dev kit pinout v1

And a better ESP32 the WeMos LOLIN32.

ESP32 WeMos LOLIN32 pinout
ESP32 WeMos LOLIN32 pinout

This pins can be used at to transmit core debug information or like a classic serial named Serial2.

To connect, naturally, you must use a USB to TTL converter, you can find it at 1$.

Exists more expensive FT232RL or FT232 module, but a CH340G or CH340 working very good.

CH340G Module It is my forced choice

You can find here USB to TTL CH340G - USB to TTL FT232RL


FT232RL Module

Connection schema is very simple, here for DOIT DEV KIT v1

esp32 DOIT DEV KIT v1 Serial2 UART2 ttl to usb schema
esp32 DOIT DEV KIT v1 Serial2 UART2 ttl to usb schema

and here for WeMos LOLIN32

esp32 WeMos LOLIN32 Serial2 UART2 ttl to usb schema
esp32 WeMos LOLIN32 Serial2 UART2 ttl to usb schema

Example

/*
 *  ESP32 DOIT DEV KIT v1 - ESP32 WeMos LOLIN32
 *  Debug on standard Serial and Serial2 on GPIO17 pin
 *  by Mischianti Renzo <https://mischianti.org>
 *
 *  https://mischianti.org/
 *
 */
#include "Arduino.h"

void setup()
{
	Serial.begin(9600);
	Serial2.begin(9600);
}

int i = 0;
void loop()
{
	i++;
	Serial.print("Hi, I'm Serial on standard TX RX pin! --> ");
	Serial.println(i);
	Serial2.print("Hi, I'm Serial2 on GPIO17! --> ");
	Serial2.println(i);
	delay(1000);
}

Here the serial monitor on standard UART (USB connection).

Hi, I'm Serial on standard TX RX pin! --> 1
Hi, I'm Serial on standard TX RX pin! --> 2
Hi, I'm Serial on standard TX RX pin! --> 3
Hi, I'm Serial on standard TX RX pin! --> 4

Here the serial monitor on Serial2 (USB to TTL converter).

Hi, I'm Serial2 on GPIO17! --> 1
Hi, I'm Serial2 on GPIO17! --> 2
Hi, I'm Serial2 on GPIO17! --> 3
Hi, I'm Serial2 on GPIO17! --> 4

Core debug

You can activate in Tools --> Debug level the logging at build time. This flag can activate low level system file and library log, the level are

  • None
  • Error
  • Warn
  • Info
  • Debug
  • Verbose

I write they in hierarchical order, so if you select None, no logs are shows, if you select Verbose all logs are shows.

ArduinoIDE esp32 log levels
ArduinoIDE esp32 log levels

But set the core Debug Level is not sufficient you must enable log on your serial:

Serial2.setDebugOutput(true);

And you can use the core debug level for your project also, the commands are one for every log level:

	log_v("Verbose");
	log_d("Debug");
	log_i("Info");
	log_w("Warning");
	log_e("Error");

Than here a simple sketch to test they:

/*
 *  ESP32 DOIT DEV KIT v1 - ESP32 WeMos LOLIN32
 *  Debug on standard Serial and Serial2 on GPIO17 pin
 *  And logging on Serial2
 *  You must set log level on option of your IDE
 *  by Mischianti Renzo <https://mischianti.org>
 *
 *  https://mischianti.org/
 *
 */
#include "Arduino.h"
#include <WiFi.h>

#define STA_SSID "<YOUR-SSID>"
#define STA_PASS "<YOUR-PASSWD>"

#include "esp32-hal-log.h"

void setup() {
	Serial.begin(9600);
	Serial2.begin(9600);
	Serial2.setDebugOutput(true);

	Serial2.println("START WIFI");
	WiFi.begin(STA_SSID, STA_PASS);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial2.print(".");
    }

    Serial2.println("");
    Serial2.println("WiFi connected");
    Serial2.println("IP address: ");
    Serial2.println(WiFi.localIP());
    delay(1000);
}

int i = 0;
void loop() {
	i++;
	Serial.print("Hi, I'm Serial on standard TX RX pin! --> ");
	Serial.println(i);
	delay(100);
	Serial2.print("Hi, I'm Serial2 on GPIO17! --> ");
	Serial2.println(i);
	delay(1000);

	log_v("Verbose");
	log_d("Debug");
	log_i("Info");
	log_w("Warning");
	log_e("Error");
	delay(5000);
}

Than if you compile and upload the sketch with log level set to Info you obtain this result on Serial and Serial2

START WIFI
.
WiFi connected
IP address:  
192.168.1.168
Hi, I'm Serial on standard TX RX pin! --> 1 
Hi, I'm Serial2 on GPIO17! --> 1
[I][esp32_serial_serial2_log_test.ino:50] loop(): Info
[W][esp32_serial_serial2_log_test.ino:51] loop(): Warning
[E][esp32_serial_serial2_log_test.ino:52] loop(): Error
Hi, I'm Serial on standard TX RX pin! --> 2 
Hi, I'm Serial2 on GPIO17! --> 2
[I][esp32_serial_serial2_log_test.ino:50] loop(): Info
[W][esp32_serial_serial2_log_test.ino:51] loop(): Warning
[E][esp32_serial_serial2_log_test.ino:52] loop(): Error

But if you upload sketch with Verbose:

START WIFI
[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 2 - STA_START
[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 4 - STA_CONNECTED
[D][WiFiGeneric.cpp:337] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:381] _eventCallback(): STA IP: 192.168.1.168, MASK: 255.255.255.0, GW: 192.168.1.1
.
WiFi connected
IP address: 
192.168.1.168
Hi, I'm Serial on standard TX RX pin! --> 1 
Hi, I'm Serial2 on GPIO17! --> 1
[V][esp32_serial_serial2_log_test.ino:48] loop(): Verbose
[D][esp32_serial_serial2_log_test.ino:49] loop(): Debug
[I][esp32_serial_serial2_log_test.ino:50] loop(): Info
[W][esp32_serial_serial2_log_test.ino:51] loop(): Warning
[E][esp32_serial_serial2_log_test.ino:52] loop(): Error
Hi, I'm Serial on standard TX RX pin! --> 2 
Hi, I'm Serial2 on GPIO17! --> 2
[V][esp32_serial_serial2_log_test.ino:48] loop(): Verbose
[D][esp32_serial_serial2_log_test.ino:49] loop(): Debug
[I][esp32_serial_serial2_log_test.ino:50] loop(): Info
[W][esp32_serial_serial2_log_test.ino:51] loop(): Warning
[E][esp32_serial_serial2_log_test.ino:52] loop(): Error

You can see that there are WiFi low level debug [D] log also, this because are set on WiFi library.

Thanks

  1. ESP32: pinout, specs and Arduino IDE configuration
  2. ESP32: integrated SPIFFS Filesystem
  3. ESP32: manage multiple Serial and logging
  4. ESP32 practical power saving
    1. ESP32 practical power saving: manage WiFi and CPU
    2. ESP32 practical power saving: modem and light sleep
    3. ESP32 practical power saving: deep sleep and hibernation
    4. ESP32 practical power saving: preserve data, timer and touch wake up
    5. ESP32 practical power saving: external and ULP wake up
    6. ESP32 practical power saving: UART and GPIO wake up
  5. ESP32: integrated LittleFS FileSystem
  6. ESP32: integrated FFat (Fat/exFAT) FileSystem
  7. ESP32-wroom-32
    1. ESP32-wroom-32: flash, pinout, specs and IDE configuration
  8. ESP32-CAM
    1. ESP32-CAM: pinout, specs and Arduino IDE configuration
    2. ESP32-CAM: upgrade CamerWebServer with flash features
  9. ESP32: use ethernet w5500 with plain (HTTP) and SSL (HTTPS)
  10. ESP32: use ethernet enc28j60 with plain (HTTP) and SSL (HTTPS)
  11. How to use SD card with esp32
  12. esp32 and esp8266: FAT filesystem on external SPI flash memory
  1. Firmware and OTA update management
    1. Firmware management
      1. ESP32: flash compiled firmware (.bin)
      2. ESP32: flash compiled firmware and filesystem (.bin) with GUI tools
    2. OTA update with Arduino IDE
      1. ESP32 OTA update with Arduino IDE: filesystem, firmware, and password
    3. OTA update with Web Browser
      1. ESP32 OTA update with Web Browser: firmware, filesystem, and authentication
      2. ESP32 OTA update with Web Browser: upload in HTTPS (SSL/TLS) with self-signed certificate
      3. ESP32 OTA update with Web Browser: custom web interface
    4. Self OTA uptate from HTTP server
      1. ESP32 self OTA update firmware from the server
      2. ESP32 self OTA update firmware from the server with version check
      3. ESP32 self-OTA update in HTTPS (SSL/TLS) with trusted self-signed certificate
    5. Non-standard Firmware update
      1. ESP32 firmware and filesystem update from SD card
      2. ESP32 firmware and filesystem update with FTP client
  1. Integrating LAN8720 with ESP32 for Ethernet Connectivity with plain (HTTP) and SSL (HTTPS)
  2. Connecting the EByte E70 to ESP32 c3/s3 devices and a simple sketch example
  3. ESP32-C3: pinout, specs and Arduino IDE configuration
  4. Integrating W5500 with ESP32 Using Core 3: Native Ethernet Protocol Support with SSL and Other Features
  5. Integrating LAN8720 with ESP32 Using Core 3: Native Ethernet Protocol Support with SSL and Other Features
  6. Dallas ds18b20:
    • Dallas ds18b20 with esp32 and esp8266: introduction and parasite mode
    • Dallas ds18b20 with esp32 and esp8266: pull-up P-MOSFET gate and alarms
    • Dallas ds18b20 with esp32 and esp8266: all OneWire topologies, long stubs and more devices

Spread the love

2 Responses

  1. Jithesh Srinivas says:

    In the first code block example inside the loop() function, serial1 is being referenced instead of serial 2.

Leave a Reply

Your email address will not be published. Required fields are marked *