1
0

HighPerf.ino 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <AsyncTCP.h>
  25. #include <WiFi.h>
  26. #endif
  27. #include <DNSServer.h>
  28. #include <ESPAsyncWebServer.h>
  29. #include <WString.h>
  30. #include <WebSerial.h>
  31. AsyncWebServer server(80);
  32. static const char* dict = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890";
  33. static uint32_t last = millis();
  34. static uint32_t count = 0;
  35. void setup() {
  36. Serial.begin(115200);
  37. WiFi.softAP("WSLDemo");
  38. Serial.print("IP Address: ");
  39. Serial.println(WiFi.softAPIP().toString());
  40. WebSerial.onMessage([](const String& msg) { Serial.println(msg); });
  41. WebSerial.begin(&server);
  42. server.onNotFound([](AsyncWebServerRequest* request) { request->redirect("/webserial"); });
  43. server.begin();
  44. }
  45. void loop() {
  46. if (millis() - last > 50) {
  47. count++;
  48. long r = random(10, 250) + 15;
  49. String buffer;
  50. buffer.reserve(r);
  51. buffer += count;
  52. while (buffer.length() < 10) {
  53. buffer += " ";
  54. }
  55. buffer += "";
  56. for (int i = 0; i < r; i++) {
  57. buffer += dict[random(0, 62)];
  58. }
  59. WebSerial.print(buffer);
  60. last = millis();
  61. }
  62. }