Callbacks
WebSerial comes with a single callback called onMessage. It’s responsible for notifying your firmware whenever it has received a command/message from user via webserial terminal.
If the callback is not registered, then the command received from terminal is simply discarded.
onMessage Callback
The onMessage callback is triggered whenever a command/message is received from the user via the webserial terminal. It’s a convenient way to handle incoming messages and execute specific actions based on the content of the message.
💡
You can either register a callback to handle incoming messages as String or as data streams (byte array). If you register both types, the last registered callback will take precedence.
Handle Incoming Messages as String
// If you want to handle incoming messages as strings
WebSerial.onMessage([](const String& msg) {
Serial.println(msg);
});Handle Incoming Messages as Data Streams
// If you want to handle incoming messages as data streams
WebSerial.onMessage([&](uint8_t *data, size_t len) {
Serial.printf("Received %lu bytes from WebSerial: ", len);
Serial.write(data, len);
Serial.println();
WebSerial.println("Received Data...");
String d = "";
for(size_t i=0; i < len; i++){
d += char(data[i]);
}
WebSerial.println(d);
});Last updated on