ClavdaClavda

Remote Serial Monitor for an ESP32 with PlatformIO

Read a PlatformIO ESP-IDF firmware's output and ESP_LOGx logs in your browser and type commands back to the ESP32, from anywhere, with the ClavdaOTA library. In C or C++.

Updated 2 min read

PlatformIO's Serial Monitor needs a USB cable. With the ClavdaOTA library, what the firmware prints through the library, and its ESP_LOGx logs if you like, shows up live in the Clavda console instead, wherever the board is, and lines you type there arrive back in the firmware.

What you need

Add the Serial Monitor to your firmware

Start the Serial Monitor

In C, call clavda_serial_begin(115200, 0). Everything printed through it goes to the USB console too, so PlatformIO's Serial Monitor still works when you have a cable; the baud rate is ignored, since ESP-IDF set the console up already. clavda_serial_capture_logs(true) also sends what ESP_LOGx prints, the system's messages included. In C++, the same calls are ClavdaSerial.begin() and ClavdaSerial.captureLogs().

Use clavda_serial_printf() and clavda_serial_println() where you would use printf(). In C++, ClavdaSerial.printf(), print() and println().

Handle typed lines

A line typed into the console's Serial Monitor arrives in the function you give clavda_serial_on_message(), or ClavdaSerial.onMessage() in C++.

This program does all three. It prints its uptime every five seconds, and answers status with its free memory:

src/main.c
#include <string.h>
#include "esp_heap_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

// Your firmware version. Change it for every build you upload: the
// console counts an update done once the board reports it.
#define CLAVDA_FIRMWARE_VERSION "1.0.0"

// The library, then this board's credentials from the console.
#include <ClavdaOTA.h>
#include "clavda_device.h"

static const char *ssid = "your-wifi";
static const char *password = "your-wifi-password";

// A line typed in the console's Serial Monitor arrives here.
static void onMessage(const char *line, size_t len, void *user) {
  if (strcmp(line, "status") == 0) {
    clavda_serial_printf("Free heap: %u\n",
                         (unsigned)heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
  } else {
    clavda_serial_printf("Unknown: %s (try \"status\")\n", line);
  }
}

void app_main(void) {
  // Start the Serial Monitor; it prints to the IDF console too. Also send
  // ESP_LOGx output there, the system's own messages included.
  clavda_serial_begin(115200, 0);
  clavda_serial_capture_logs(true);
  clavda_serial_on_message(onMessage, NULL);

  clavda_wifi_begin(ssid, password);
  clavda_begin(&CLAVDA_DEVICE);

  TickType_t lastReport = 0;
  for (;;) {
    clavda_loop();

    // Print with clavda_serial_printf() wherever you would use printf().
    const TickType_t now = xTaskGetTickCount();
    if (now - lastReport >= pdMS_TO_TICKS(5000)) {
      lastReport = now;
      clavda_serial_printf("Uptime: %lu s\n", (unsigned long)(now / configTICK_RATE_HZ));
    }
    vTaskDelay(pdMS_TO_TICKS(10));
  }
}

It replaces the contents of src/main.c as it is.

Flash it, over USB with Upload or over the air.

Open the Serial Monitor

In Clavda, open the device's Connect sheet and press Serial Monitor. Output appears as the board prints it. Type status and press Enter to see the reply.

Good to know

  • Boot messages are kept. The last 2 KB of output stays on the board, so a monitor you open late still starts with what the board printed when it booted.
  • Plain printf() is not sent. Only what goes through the library, and ESP_LOGx once logs are captured, reaches the console's Serial Monitor.
  • It is end-to-end encrypted. Output and typed lines are encrypted between the board and your browser.
  • Permissions. Anyone who can see the device can watch the output. Typing into it needs permission to edit devices.

Next steps