WebSocketServerAllFunctionsDemo.ino 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * WebSocketServerAllFunctionsDemo.ino
  3. *
  4. * Created on: 10.05.2018
  5. *
  6. */
  7. #include <Arduino.h>
  8. #include <ESP8266WiFi.h>
  9. #include <ESP8266WiFiMulti.h>
  10. #include <WebSocketsServer.h>
  11. #include <ESP8266WebServer.h>
  12. #include <ESP8266mDNS.h>
  13. #include <Hash.h>
  14. #define LED_RED 15
  15. #define LED_GREEN 12
  16. #define LED_BLUE 13
  17. #define USE_SERIAL Serial
  18. ESP8266WiFiMulti WiFiMulti;
  19. ESP8266WebServer server(80);
  20. WebSocketsServer webSocket = WebSocketsServer(81);
  21. void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
  22. switch(type) {
  23. case WStype_DISCONNECTED:
  24. USE_SERIAL.printf("[%u] Disconnected!\n", num);
  25. break;
  26. case WStype_CONNECTED: {
  27. IPAddress ip = webSocket.remoteIP(num);
  28. USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);
  29. // send message to client
  30. webSocket.sendTXT(num, "Connected");
  31. }
  32. break;
  33. case WStype_TEXT:
  34. USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
  35. if(payload[0] == '#') {
  36. // we get RGB data
  37. // decode rgb data
  38. uint32_t rgb = (uint32_t) strtol((const char *) &payload[1], NULL, 16);
  39. analogWrite(LED_RED, ((rgb >> 16) & 0xFF));
  40. analogWrite(LED_GREEN, ((rgb >> 8) & 0xFF));
  41. analogWrite(LED_BLUE, ((rgb >> 0) & 0xFF));
  42. }
  43. break;
  44. }
  45. }
  46. void setup() {
  47. //USE_SERIAL.begin(921600);
  48. USE_SERIAL.begin(115200);
  49. //USE_SERIAL.setDebugOutput(true);
  50. USE_SERIAL.println();
  51. USE_SERIAL.println();
  52. USE_SERIAL.println();
  53. for(uint8_t t = 4; t > 0; t--) {
  54. USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
  55. USE_SERIAL.flush();
  56. delay(1000);
  57. }
  58. pinMode(LED_RED, OUTPUT);
  59. pinMode(LED_GREEN, OUTPUT);
  60. pinMode(LED_BLUE, OUTPUT);
  61. digitalWrite(LED_RED, 1);
  62. digitalWrite(LED_GREEN, 1);
  63. digitalWrite(LED_BLUE, 1);
  64. WiFiMulti.addAP("SSID", "passpasspass");
  65. while(WiFiMulti.run() != WL_CONNECTED) {
  66. delay(100);
  67. }
  68. // start webSocket server
  69. webSocket.begin();
  70. webSocket.onEvent(webSocketEvent);
  71. if(MDNS.begin("esp8266")) {
  72. USE_SERIAL.println("MDNS responder started");
  73. }
  74. // handle index
  75. server.on("/", []() {
  76. // send index.html
  77. server.send(200, "text/html", "<html><head><script>var connection = new WebSocket('ws://'+location.hostname+':81/', ['arduino']);connection.onopen = function () { connection.send('Connect ' + new Date()); }; connection.onerror = function (error) { console.log('WebSocket Error ', error);};connection.onmessage = function (e) { console.log('Server: ', e.data);};function sendRGB() { var r = parseInt(document.getElementById('r').value).toString(16); var g = parseInt(document.getElementById('g').value).toString(16); var b = parseInt(document.getElementById('b').value).toString(16); if(r.length < 2) { r = '0' + r; } if(g.length < 2) { g = '0' + g; } if(b.length < 2) { b = '0' + b; } var rgb = '#'+r+g+b; console.log('RGB: ' + rgb); connection.send(rgb); }</script></head><body>LED Control:<br/><br/>R: <input id=\"r\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>G: <input id=\"g\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/>B: <input id=\"b\" type=\"range\" min=\"0\" max=\"255\" step=\"1\" oninput=\"sendRGB();\" /><br/></body></html>");
  78. });
  79. server.begin();
  80. // Add service to MDNS
  81. MDNS.addService("http", "tcp", 80);
  82. MDNS.addService("ws", "tcp", 81);
  83. digitalWrite(LED_RED, 0);
  84. digitalWrite(LED_GREEN, 0);
  85. digitalWrite(LED_BLUE, 0);
  86. }
  87. unsigned long last_10sec = 0;
  88. unsigned int counter = 0;
  89. void loop() {
  90. unsigned long t = millis();
  91. webSocket.loop();
  92. server.handleClient();
  93. if((t - last_10sec) > 10 * 1000) {
  94. counter++;
  95. bool ping = (counter % 2);
  96. int i = webSocket.connectedClients(ping);
  97. USE_SERIAL.printf("%d Connected websocket clients ping: %d\n", i, ping);
  98. last_10sec = millis();
  99. }
  100. }