Logging.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * This example shows how to use WebSerial variant to send logging data to the browser.
  3. *
  4. * Before using this example, make sure to look at the WebSerial example before and its description.\
  5. *
  6. * You might want to control these flags to control the async library performance:
  7. * -D CONFIG_ASYNC_TCP_QUEUE_SIZE=128
  8. * -D CONFIG_ASYNC_TCP_RUNNING_CORE=1
  9. * -D WS_MAX_QUEUED_MESSAGES=128
  10. */
  11. #include <Arduino.h>
  12. #if defined(ESP8266)
  13. #include <ESP8266WiFi.h>
  14. #include <ESPAsyncTCP.h>
  15. #elif defined(ESP32)
  16. #include <WiFi.h>
  17. #include <AsyncTCP.h>
  18. #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
  19. #include <WiFi.h>
  20. #include <RPAsyncTCP.h>
  21. #endif
  22. #include <DNSServer.h>
  23. #include <ESPAsyncWebServer.h>
  24. #include <WString.h>
  25. #include <WebSerial.h>
  26. AsyncWebServer server(80);
  27. static uint32_t last = millis();
  28. static uint32_t count = 0;
  29. void setup() {
  30. Serial.begin(115200);
  31. WiFi.softAP("WSLDemo");
  32. Serial.print("IP Address: ");
  33. Serial.println(WiFi.softAPIP().toString());
  34. WebSerial.onMessage([](const String& msg) { Serial.println(msg); });
  35. WebSerial.begin(&server);
  36. WebSerial.setBuffer(100);
  37. server.onNotFound([](AsyncWebServerRequest* request) { request->redirect("/webserial"); });
  38. server.begin();
  39. }
  40. void loop() {
  41. if (millis() - last > 1000) {
  42. count++;
  43. WebSerial.print(F("IP address: "));
  44. WebSerial.println(WiFi.softAPIP());
  45. WebSerial.printf("Uptime: %lums\n", millis());
  46. #if defined(ESP8266)
  47. WebSerial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap());
  48. #elif defined(ESP32)
  49. WebSerial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap());
  50. #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
  51. WebSerial.printf("Free heap: %" PRIu32 "\n", rp2040.getFreeHeap());
  52. #endif
  53. last = millis();
  54. }
  55. }