MultipleClients-Teensy41-Server.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Teensy41 Websockets Server and Http Server (using NativeEthernet).
  3. Combining the Teensy41-Server example with the NativeEthernet WebServer
  4. example (https://github.com/vjmuzik/NativeEthernet/blob/master/examples/WebServer/WebServer.ino).
  5. This sketch:
  6. 1. Connects to a ethernet network
  7. 2. Starts a websocket server on port 80
  8. 3. Waits for connections
  9. 4. As soon as a client wants to establish a connection, it checks whether a
  10. free slot is available and accepts it accordingly
  11. 5. If the client is accepted it sends a welcome message and echoes any
  12. messages from the client
  13. 6. Goes back to step 3
  14. Note:
  15. Make sure you share your computer's internet connection with the Teensy
  16. via ethernet.
  17. Libraries:
  18. To use this sketch install
  19. * TeensyID library (https://github.com/sstaub/TeensyID)
  20. * NativeEthernet (https://github.com/vjmuzik/NativeEthernet)
  21. Hardware:
  22. For this sketch you need a Teensy 4.1 board and the Teensy 4.1 Ethernet Kit
  23. (https://www.pjrc.com/store/ethernet_kit.html).
  24. */
  25. #include <NativeEthernet.h>
  26. #include <ArduinoWebsockets.h>
  27. #include <TeensyID.h>
  28. using namespace websockets;
  29. // We will set the MAC address at the beginning of `setup()` using TeensyID's
  30. // `teensyMac` helper.
  31. byte mac[6];
  32. // Enter websockets server port.
  33. const uint16_t port = 80;
  34. // Define how many clients we accpet simultaneously.
  35. const byte maxClients = 4;
  36. WebsocketsClient clients[maxClients];
  37. WebsocketsServer server;
  38. void setup() {
  39. // Set the MAC address.
  40. teensyMAC(mac);
  41. // Connect to ethernet.
  42. if (Ethernet.begin(mac)) {
  43. Serial.println("Ethernet connected");
  44. } else {
  45. Serial.println("Ethernet failed");
  46. }
  47. // Start websockets server.
  48. server.listen(port);
  49. if (server.available()) {
  50. Serial.print("Server available at ws://");
  51. Serial.print(Ethernet.localIP());
  52. // Also log any non default port.
  53. if (port != 80) Serial.printf(":%d", port);
  54. Serial.println();
  55. } else {
  56. Serial.println("Server not available!");
  57. }
  58. }
  59. void handleMessage(WebsocketsClient &client, WebsocketsMessage message) {
  60. auto data = message.data();
  61. // Log message
  62. Serial.print("Got Message: ");
  63. Serial.println(data);
  64. // Echo message
  65. client.send("Echo: " + data);
  66. }
  67. void handleEvent(WebsocketsClient &client, WebsocketsEvent event, String data) {
  68. if (event == WebsocketsEvent::ConnectionClosed) {
  69. Serial.println("Connection closed");
  70. }
  71. }
  72. int8_t getFreeClientIndex() {
  73. // If a client in our list is not available, it's connection is closed and we
  74. // can use it for a new client.
  75. for (byte i = 0; i < maxClients; i++) {
  76. if (!clients[i].available()) return i;
  77. }
  78. return -1;
  79. }
  80. void listenForClients() {
  81. if (server.poll()) {
  82. int8_t freeIndex = getFreeClientIndex();
  83. if (freeIndex >= 0) {
  84. WebsocketsClient newClient = server.accept();
  85. Serial.printf("Accepted new websockets client at index %d\n", freeIndex);
  86. newClient.onMessage(handleMessage);
  87. newClient.onEvent(handleEvent);
  88. newClient.send("Hello from Teensy");
  89. clients[freeIndex] = newClient;
  90. }
  91. }
  92. }
  93. void pollClients() {
  94. for (byte i = 0; i < maxClients; i++) {
  95. clients[i].poll();
  96. }
  97. }
  98. void loop() {
  99. listenForClients();
  100. pollClients();
  101. }