Demo_AP.ino 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. --------------
  3. WebSerial Demo
  4. --------------
  5. Skill Level: Beginner
  6. This example provides with a bare minimal app with WebSerial functionality using softAP mode.
  7. Github: https://github.com/ayushsharma82/WebSerial
  8. Wiki: https://docs.webserial.pro
  9. Works with following hardware:
  10. - ESP8266
  11. - ESP32
  12. WebSerial terminal will be accessible at your microcontroller's <IPAddress>/webserial URL.
  13. Checkout WebSerial Pro: https://webserial.pro
  14. */
  15. #include <Arduino.h>
  16. #if defined(ESP8266)
  17. #include <ESP8266WiFi.h>
  18. #include <ESPAsyncTCP.h>
  19. #elif defined(ESP32)
  20. #include <WiFi.h>
  21. #include <AsyncTCP.h>
  22. #endif
  23. #include <ESPAsyncWebServer.h>
  24. #include <WebSerial.h>
  25. AsyncWebServer server(80);
  26. const char* ssid = "WSLDemo"; // WiFi AP SSID
  27. const char* password = ""; // WiFi AP Password
  28. unsigned long last_print_time = millis();
  29. void setup() {
  30. Serial.begin(115200);
  31. WiFi.softAP(ssid, password);
  32. // Once connected, print IP
  33. Serial.print("IP Address: ");
  34. Serial.println(WiFi.softAPIP());
  35. server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
  36. request->send(200, "text/plain", "Hi! This is WebSerial demo. You can access webserial interface at http://" + WiFi.softAPIP().toString() + "/webserial");
  37. });
  38. // WebSerial is accessible at "<IP Address>/webserial" in browser
  39. WebSerial.begin(&server);
  40. /* Attach Message Callback */
  41. WebSerial.onMessage([&](uint8_t *data, size_t len) {
  42. Serial.printf("Received %u bytes from WebSerial: ", len);
  43. Serial.write(data, len);
  44. Serial.println();
  45. WebSerial.println("Received Data...");
  46. String d = "";
  47. for(size_t i=0; i < len; i++){
  48. d += char(data[i]);
  49. }
  50. WebSerial.println(d);
  51. });
  52. // Start server
  53. server.begin();
  54. }
  55. void loop() {
  56. // Print every 2 seconds (non-blocking)
  57. if ((unsigned long)(millis() - last_print_time) > 2000) {
  58. WebSerial.print(F("IP address: "));
  59. WebSerial.println(WiFi.softAPIP());
  60. WebSerial.printf("Uptime: %lums\n", millis());
  61. WebSerial.printf("Free heap: %" PRIu32 "\n", ESP.getFreeHeap());
  62. last_print_time = millis();
  63. }
  64. WebSerial.loop();
  65. }