Remote Serial Monitor for an ESP32 with the Arduino IDE
Read an Arduino sketch's serial output in your browser and type commands back to the ESP32, from anywhere, by printing to ClavdaSerial instead of Serial. In C++ or C.
Updated 2 min read
The Arduino Serial Monitor needs a USB cable. With the ClavdaOTA library, anything the sketch prints to ClavdaSerial shows up live in the Clavda console instead, wherever the board is, and lines you type there arrive back in the sketch.
What you need
- An ESP32 already connected to Clavda with the Arduino IDE.
Add the Serial Monitor to your sketch
Start the Serial Monitor
In C++, call ClavdaSerial.begin(&Serial) in setup(). Passing &Serial copies everything to the USB port too, so a cable still works when you have one. In C, clavda_serial_begin(115200, 0) does the same, and starts Serial at 115200 baud as well.
Print to it
Print with ClavdaSerial.print, println and printf wherever you would use Serial. In C, use clavda_serial_printf() and clavda_serial_println().
Handle typed lines
A line typed into the console's Serial Monitor arrives in the callback you give ClavdaSerial.onMessage(), or clavda_serial_on_message() in C.
This program does all three. It prints its uptime every five seconds, and answers status with its free memory:
#include <Arduino.h>
#include <WiFi.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"
const char *ssid = "your-wifi";
const char *password = "your-wifi-password";
unsigned long lastReport = 0;
void setup() {
Serial.begin(115200);
// Start the Serial Monitor; &Serial copies everything to USB too.
ClavdaSerial.begin(&Serial);
// A line typed in the console's Serial Monitor arrives here.
ClavdaSerial.onMessage([](const String &msg) {
if (msg == "status") {
ClavdaSerial.printf("Free heap: %u\n", (unsigned)ESP.getFreeHeap());
} else {
ClavdaSerial.printf("Unknown: %s (try \"status\")\n", msg.c_str());
}
});
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
ClavdaOTA.begin(CLAVDA_DEVICE);
}
void loop() {
ClavdaOTA.loop();
// Print to ClavdaSerial wherever you would print to Serial.
if (millis() - lastReport >= 5000) {
lastReport = millis();
ClavdaSerial.printf("Uptime: %lu s\n", millis() / 1000);
}
}
The .ino hands setup() and loop() over to the C file, as in Write the sketch:
extern "C" void app_setup(void);
extern "C" void app_loop(void);
void setup() {
app_setup();
}
void loop() {
app_loop();
}
#include <Arduino.h>
#include <string.h>
#include "esp_heap_caps.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";
static unsigned long lastReport = 0;
// 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_setup(void) {
// Start the Serial Monitor. It starts Serial at 115200 baud too, and
// copies everything to USB.
clavda_serial_begin(115200, 0);
clavda_serial_on_message(onMessage, NULL);
clavda_wifi_begin(ssid, password);
clavda_begin(&CLAVDA_DEVICE);
}
void app_loop(void) {
clavda_loop();
// Print with clavda_serial_printf() wherever you would print to Serial.
if (millis() - lastReport >= 5000) {
lastReport = millis();
clavda_serial_printf("Uptime: %lu s\n", millis() / 1000);
}
}
Flash it, over USB or over the air. The library's examples include this as cpp/SerialMonitor and c/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.
- 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.