Remote Serial Monitor for an ESP32 with ESP-IDF
Read an ESP-IDF firmware's output and ESP_LOGx logs in your browser and type commands back to the ESP32, from anywhere, with the ClavdaOTA component. In C or C++.
Updated 2 min read
idf.py monitor needs a USB cable. With the ClavdaOTA component, 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
- An ESP32 already connected to Clavda with ESP-IDF.
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 IDF console too, so idf.py 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().
Print to it
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:
#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 project's source file in main/ as it is, with no change to main/CMakeLists.txt.
The file must end in .cpp, and be the one main/CMakeLists.txt builds, as in Write the code:
idf_component_register(SRCS "main.cpp"
INCLUDE_DIRS ".")
#include <string>
#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";
extern "C" 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.
ClavdaSerial.begin();
ClavdaSerial.captureLogs();
// A line typed in the console's Serial Monitor arrives here.
ClavdaSerial.onMessage([](const std::string &msg) {
if (msg == "status") {
ClavdaSerial.printf("Free heap: %u\n",
(unsigned)heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
} else {
ClavdaSerial.printf("Unknown: %s (try \"status\")\n", msg.c_str());
}
});
clavda_wifi_begin(ssid, password);
ClavdaOTA.begin(CLAVDA_DEVICE);
TickType_t lastReport = 0;
for (;;) {
ClavdaOTA.loop();
// Print to ClavdaSerial wherever you would use printf().
const TickType_t now = xTaskGetTickCount();
if (now - lastReport >= pdMS_TO_TICKS(5000)) {
lastReport = now;
ClavdaSerial.printf("Uptime: %lu s\n", (unsigned long)(now / configTICK_RATE_HZ));
}
vTaskDelay(pdMS_TO_TICKS(10));
}
}
Flash it, over USB or over the air. The library's complete ESP-IDF project is examples/idf/SerialMonitor.
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, andESP_LOGxonce 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.