HighPerf.ino 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * This example shows how to use WebSerial variant to send data to the browser when timing, speed and latency are important.
  3. * WebSerial focuses on reducing latency and increasing speed by enqueueing messages and sending them in a single packet.
  4. *
  5. * The responsibility is left to the caller to ensure that the messages sent are not too large or not too small and frequent.
  6. * For example, use of printf(), write(c), print(c), etc are not recommended.
  7. *
  8. * This variant can allow WebSerial to support a high speed of more than 20 messages per second like in this example.
  9. *
  10. * It can be used to log data, debug, or send data to the browser in real-time without any delay.
  11. *
  12. * You might want to look at the Logging variant to see how to better use WebSerial for streaming logging.
  13. *
  14. * You might want to control these flags to control the async library performance:
  15. * -D CONFIG_ASYNC_TCP_QUEUE_SIZE=128
  16. * -D CONFIG_ASYNC_TCP_RUNNING_CORE=1
  17. * -D WS_MAX_QUEUED_MESSAGES=128
  18. */
  19. #include <Arduino.h>
  20. #if defined(ESP8266)
  21. #include <ESP8266WiFi.h>
  22. #include <ESPAsyncTCP.h>
  23. #elif defined(ESP32)
  24. #include <WiFi.h>
  25. #include <AsyncTCP.h>
  26. #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
  27. #include <WiFi.h>
  28. #include <RPAsyncTCP.h>
  29. #endif
  30. #include <DNSServer.h>
  31. #include <ESPAsyncWebServer.h>
  32. #include <WString.h>
  33. #include <WebSerial.h>
  34. AsyncWebServer server(80);
  35. static const char* dict = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890";
  36. static uint32_t last = millis();
  37. static uint32_t count = 0;
  38. void setup() {
  39. Serial.begin(115200);
  40. WiFi.softAP("WSLDemo");
  41. Serial.print("IP Address: ");
  42. Serial.println(WiFi.softAPIP().toString());
  43. WebSerial.onMessage([](const String& msg) { Serial.println(msg); });
  44. WebSerial.begin(&server);
  45. server.onNotFound([](AsyncWebServerRequest* request) { request->redirect("/webserial"); });
  46. server.begin();
  47. }
  48. void loop() {
  49. #ifdef ESP8266
  50. if (millis() - last > 500) {
  51. #else
  52. if (millis() - last > 50) {
  53. #endif
  54. count++;
  55. long r = random(10, 250) + 15;
  56. String buffer;
  57. buffer.reserve(r);
  58. buffer += count;
  59. while (buffer.length() < 10) {
  60. buffer += " ";
  61. }
  62. buffer += "";
  63. for (int i = 0; i < r; i++) {
  64. buffer += dict[random(0, 62)];
  65. }
  66. #ifdef WSL_HIGH_PERF
  67. // Using internal websocket buffer to improve memory consumption and avoid another internal copy when enqueueing the message
  68. AsyncWebSocketMessageBuffer* wsBuffer = WebSerial.makeBuffer(buffer.length());
  69. memmove(wsBuffer->get(), buffer.c_str(), buffer.length());
  70. WebSerial.send(wsBuffer);
  71. #else
  72. WebSerial.print(buffer);
  73. #endif
  74. last = millis();
  75. }
  76. }