WebSocketServer.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * WebSocketServer.ino
  3. *
  4. * Created on: 22.05.2015
  5. *
  6. */
  7. #include <Arduino.h>
  8. #include <ESP8266WiFi.h>
  9. #include <ESP8266WiFiMulti.h>
  10. #include <WebSocketsServer.h>
  11. #include <Hash.h>
  12. ESP8266WiFiMulti WiFiMulti;
  13. WebSocketsServer webSocket = WebSocketsServer(81);
  14. #define USE_SERIAL Serial1
  15. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  16. switch(type) {
  17. case WStype_DISCONNECTED:
  18. USE_SERIAL.printf("[%u] Disconnected!\n", num);
  19. break;
  20. case WStype_CONNECTED:
  21. {
  22. IPAddress ip = webSocket.remoteIP(num);
  23. USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  24. // send message to client
  25. webSocket.sendTXT(num, "Connected");
  26. }
  27. break;
  28. case WStype_TEXT:
  29. USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
  30. // send message to client
  31. // webSocket.sendTXT(num, "message here");
  32. // send data to all connected clients
  33. // webSocket.broadcastTXT("message here");
  34. break;
  35. case WStype_BIN:
  36. USE_SERIAL.printf("[%u] get binary length: %u\n", num, length);
  37. hexdump(payload, length);
  38. // send message to client
  39. // webSocket.sendBIN(num, payload, length);
  40. break;
  41. }
  42. }
  43. void setup() {
  44. // USE_SERIAL.begin(921600);
  45. USE_SERIAL.begin(115200);
  46. //Serial.setDebugOutput(true);
  47. USE_SERIAL.setDebugOutput(true);
  48. USE_SERIAL.println();
  49. USE_SERIAL.println();
  50. USE_SERIAL.println();
  51. for(uint8_t t = 4; t > 0; t--) {
  52. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  53. USE_SERIAL.flush();
  54. delay(1000);
  55. }
  56. WiFiMulti.addAP("SSID", "passpasspass");
  57. while(WiFiMulti.run() != WL_CONNECTED) {
  58. delay(100);
  59. }
  60. webSocket.begin();
  61. webSocket.onEvent(webSocketEvent);
  62. }
  63. void loop() {
  64. webSocket.loop();
  65. }