WebSocketServerHooked.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * WebSocketServerHooked.ino
  3. *
  4. * Created on: 22.05.2015
  5. * Hooked on: 28.10.2020
  6. *
  7. */
  8. #include <Arduino.h>
  9. #include <ESP8266WiFi.h>
  10. #include <ESP8266WiFiMulti.h>
  11. #include <WebSockets4WebServer.h>
  12. #include <Hash.h>
  13. #include <ESP8266mDNS.h>
  14. ESP8266WiFiMulti WiFiMulti;
  15. ESP8266WebServer server(80);
  16. WebSockets4WebServer webSocket;
  17. #define USE_SERIAL Serial
  18. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  19. switch(type) {
  20. case WStype_DISCONNECTED:
  21. USE_SERIAL.printf("[%u] Disconnected!\n", num);
  22. break;
  23. case WStype_CONNECTED:
  24. {
  25. IPAddress ip = webSocket.remoteIP(num);
  26. USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  27. // send message to client
  28. webSocket.sendTXT(num, "Connected");
  29. }
  30. break;
  31. case WStype_TEXT:
  32. USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
  33. // send message to client
  34. // webSocket.sendTXT(num, "message here");
  35. // send data to all connected clients
  36. // webSocket.broadcastTXT("message here");
  37. break;
  38. case WStype_BIN:
  39. USE_SERIAL.printf("[%u] get binary length: %u\n", num, length);
  40. hexdump(payload, length);
  41. // send message to client
  42. // webSocket.sendBIN(num, payload, length);
  43. break;
  44. }
  45. }
  46. void setup() {
  47. // USE_SERIAL.begin(921600);
  48. USE_SERIAL.begin(115200);
  49. //Serial.setDebugOutput(true);
  50. USE_SERIAL.setDebugOutput(true);
  51. USE_SERIAL.println();
  52. USE_SERIAL.println();
  53. USE_SERIAL.println();
  54. for(uint8_t t = 4; t > 0; t--) {
  55. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  56. USE_SERIAL.flush();
  57. delay(1000);
  58. }
  59. WiFiMulti.addAP("SSID", "passpasspass");
  60. while(WiFiMulti.run() != WL_CONNECTED) {
  61. delay(100);
  62. }
  63. server.on("/", []() {
  64. server.send(200, "text/plain", "I am a regular webserver on port 80!\r\n");
  65. server.send(200, "text/plain", "I am also a websocket server on '/ws' on the same port 80\r\n");
  66. });
  67. server.addHook(webSocket.hookForWebserver("/ws", webSocketEvent));
  68. server.begin();
  69. Serial.println("HTTP server started on port 80");
  70. Serial.println("WebSocket server started on the same port");
  71. Serial.printf("my network address is either 'arduinoWebsockets.local' (mDNS) or '%s'\n", WiFi.localIP().toString().c_str());
  72. if (!MDNS.begin("arduinoWebsockets")) {
  73. Serial.println("Error setting up MDNS responder!");
  74. }
  75. }
  76. void loop() {
  77. server.handleClient();
  78. webSocket.loop();
  79. MDNS.update();
  80. }