Anniversary Sale · up to 80% off all librariesEnds Nov 9Shop now
SOFTTDocs

Printing

Send log output to the WebSerial terminal with print, println, printf and write, and understand how WebSerial buffers output.

The WebSerial object derives from the Arduino Print class, so it provides the same printing methods as Serial. Output is sent over a WebSocket to every browser that has the terminal open.

Keep messages small

Do not send large amounts of data in a single call (more than about 1 KB). WebSockets on microcontrollers have limited throughput. WebSerial buffers output to reduce the load, but you should still keep individual writes small for stable operation. In the default mode, a single write must not exceed WSL_PRINT_BUFFER_SIZE (1024 bytes by default).

Methods

print()

Prints data to the terminal as human-readable text.

WebSerial.print("Temperature: ");
WebSerial.print(23.5);

println()

Prints data to the terminal, followed by a carriage return and a newline.

WebSerial.println(WiFi.localIP());

printf()

Prints formatted data to the terminal.

WebSerial.printf("Uptime: %lums\n", millis());

write()

Writes raw bytes to the terminal.

WebSerial.write('A');
WebSerial.write(buffer, size);

WebSerial implements both write() overloads of Print:

SignatureDescription
size_t write(uint8_t)Writes a single byte. Not supported in High Performance Mode unless a line buffer is enabled.
size_t write(const uint8_t* buffer, size_t size)Writes size bytes from buffer. Returns 0 if begin() has not been called or size is 0.

Output buffering

In the default mode, WebSerial does not send each write as a separate WebSocket message:

  1. Writes are collected in a print buffer of WSL_PRINT_BUFFER_SIZE bytes (default 1024).
  2. When no data has been written for WSL_PRINT_FLUSH_TIME_US microseconds (default 100), or when the print buffer is full, its content is moved into a global buffer of WSL_BUFFER_SIZE bytes (default 2048) as one row. Rows longer than WSL_MAX_ROW_PACKET_PAYLOAD_SIZE bytes (default 512) are split into several packets.
  3. The global buffer is sent to all clients when it is full, or WSL_GLOBAL_FLUSH_TIME_MS milliseconds (default 100) after the last flush.

These steps run in WebSerial.loop() and at the start of every write. Call WebSerial.loop() on every iteration of loop() so that output is delivered even when nothing else is printed. The buffer sizes and intervals can be changed with build flags; see API Reference.

To send each message immediately instead, use High Performance Mode.

On this page