HighPerf.ino 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifdef ESP8266
  47. if (millis() - last > 500) {
  48. #else
  49. if (millis() - last > 50) {
  50. #endif
  51. count++;
  52. long r = random(10, 250) + 15;
  53. String buffer;
  54. buffer.reserve(r);
  55. buffer += count;
  56. while (buffer.length() < 10) {
  57. buffer += " ";
  58. }
  59. buffer += "";
  60. for (int i = 0; i < r; i++) {
  61. buffer += dict[random(0, 62)];
  62. }
  63. WebSerial.print(buffer);
  64. last = millis();
  65. }
  66. }