diff --git a/examples/Generic/Teensy41_NativeEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino b/examples/Generic/Teensy41_NativeEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino index 1a8db44c..8fd66179 100644 --- a/examples/Generic/Teensy41_NativeEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino +++ b/examples/Generic/Teensy41_NativeEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino @@ -64,12 +64,24 @@ byte mac[6]; // Enter websockets server port. const uint16_t port = 81; -// Define how many clients we accpet simultaneously. +// Define how many clients we accept simultaneously. const byte maxClients = 4; WebsocketsClient clients[maxClients]; WebsocketsServer server; +//#define USING_DHCP true +#define USING_DHCP false + +#if !USING_DHCP + // Set the static IP address to use if the DHCP fails to assign + IPAddress myIP(192, 168, 2, 222); + IPAddress myNetmask(255, 255, 255, 0); + IPAddress myGW(192, 168, 2, 1); + //IPAddress mydnsServer(192, 168, 2, 1); + IPAddress mydnsServer(8, 8, 8, 8); +#endif + void setup() { // Set the MAC address. @@ -83,15 +95,32 @@ void setup() Serial.println(WEBSOCKETS2_GENERIC_VERSION); // Connect to ethernet. - if (Ethernet.begin(mac)) +#if USING_DHCP + if (Ethernet.begin(mac)) { - Serial.println("Ethernet connected"); + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); } - else + else { Serial.println("Ethernet failed"); } + // give the Ethernet shield minimum 1 sec for DHCP + delay(1000); +#else + // Use Static IP + Ethernet.begin(mac, myIP, mydnsServer); + + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); + + // give the Ethernet shield minimum 2 secs for staticP to initialize: + delay(2000); +#endif + // Start websockets server. server.listen(port); diff --git a/examples/Generic/Teensy41_NativeEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino b/examples/Generic/Teensy41_NativeEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino index 1916aa11..b623b4ed 100644 --- a/examples/Generic/Teensy41_NativeEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino +++ b/examples/Generic/Teensy41_NativeEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino @@ -75,6 +75,18 @@ WebsocketsServer socketServer; //EthernetServer httpServer; EthernetServer httpServer(WEBSERVER_PORT); +//#define USING_DHCP true +#define USING_DHCP false + +#if !USING_DHCP + // Set the static IP address to use if the DHCP fails to assign + IPAddress myIP(192, 168, 2, 222); + IPAddress myNetmask(255, 255, 255, 0); + IPAddress myGW(192, 168, 2, 1); + //IPAddress mydnsServer(192, 168, 2, 1); + IPAddress mydnsServer(8, 8, 8, 8); +#endif + void setup() { // Set the MAC address. @@ -88,15 +100,32 @@ void setup() Serial.println(WEBSOCKETS2_GENERIC_VERSION); // Connect to ethernet. - if (Ethernet.begin(mac)) +#if USING_DHCP + if (Ethernet.begin(mac)) { - Serial.println("Ethernet connected"); + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); } - else + else { Serial.println("Ethernet failed"); } + // give the Ethernet shield minimum 1 sec for DHCP + delay(1000); +#else + // Use Static IP + Ethernet.begin(mac, myIP, mydnsServer); + + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); + + // give the Ethernet shield minimum 2 secs for staticP to initialize: + delay(2000); +#endif + // Start websockets server. socketServer.listen(websocketsPort); diff --git a/examples/Generic/Teensy41_NativeEthernet/Teensy41_Client/Teensy41_Client.ino b/examples/Generic/Teensy41_NativeEthernet/Teensy41_Client/Teensy41_Client.ino index 72dd3acb..9e2cf041 100644 --- a/examples/Generic/Teensy41_NativeEthernet/Teensy41_Client/Teensy41_Client.ino +++ b/examples/Generic/Teensy41_NativeEthernet/Teensy41_Client/Teensy41_Client.ino @@ -61,7 +61,20 @@ byte mac[6]; //const char* url = "ws://echo.websocket.org"; const char* url = "ws://192.168.2.30:8080"; -void setup() { +//#define USING_DHCP true +#define USING_DHCP false + +#if !USING_DHCP + // Set the static IP address to use if the DHCP fails to assign + IPAddress myIP(192, 168, 2, 222); + IPAddress myNetmask(255, 255, 255, 0); + IPAddress myGW(192, 168, 2, 1); + //IPAddress mydnsServer(192, 168, 2, 1); + IPAddress mydnsServer(8, 8, 8, 8); +#endif + +void setup() +{ // Set the MAC address. teensyMAC(mac); @@ -73,17 +86,32 @@ void setup() { Serial.println(WEBSOCKETS2_GENERIC_VERSION); // Connect to ethernet. +#if USING_DHCP if (Ethernet.begin(mac)) { Serial.print("Ethernet connected ("); Serial.print(Ethernet.localIP()); Serial.println(")"); - } + } else { Serial.println("Ethernet failed"); } + // give the Ethernet shield minimum 1 sec for DHCP + delay(1000); +#else + // Use Static IP + Ethernet.begin(mac, myIP, mydnsServer); + + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); + + // give the Ethernet shield minimum 2 secs for staticP to initialize: + delay(2000); +#endif + // Connect to websocket server. if (client.connect(url)) { diff --git a/examples/Generic/Teensy41_NativeEthernet/Teensy41_Server/Teensy41_Server.ino b/examples/Generic/Teensy41_NativeEthernet/Teensy41_Server/Teensy41_Server.ino index 366cfccb..62e22d32 100644 --- a/examples/Generic/Teensy41_NativeEthernet/Teensy41_Server/Teensy41_Server.ino +++ b/examples/Generic/Teensy41_NativeEthernet/Teensy41_Server/Teensy41_Server.ino @@ -59,6 +59,18 @@ byte mac[6]; // Enter websockets server port. const uint16_t port = 80; +//#define USING_DHCP true +#define USING_DHCP false + +#if !USING_DHCP + // Set the static IP address to use if the DHCP fails to assign + IPAddress myIP(192, 168, 2, 222); + IPAddress myNetmask(255, 255, 255, 0); + IPAddress myGW(192, 168, 2, 1); + //IPAddress mydnsServer(192, 168, 2, 1); + IPAddress mydnsServer(8, 8, 8, 8); +#endif + void setup() { // Set the MAC address. @@ -72,13 +84,32 @@ void setup() Serial.println(WEBSOCKETS2_GENERIC_VERSION); // Connect to ethernet. - if (Ethernet.begin(mac)) +#if USING_DHCP + if (Ethernet.begin(mac)) + { + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); + } + else { - Serial.println("Ethernet connected"); - } else { Serial.println("Ethernet failed"); } + // give the Ethernet shield minimum 1 sec for DHCP + delay(1000); +#else + // Use Static IP + Ethernet.begin(mac, myIP, mydnsServer); + + Serial.print("Ethernet connected ("); + Serial.print(Ethernet.localIP()); + Serial.println(")"); + + // give the Ethernet shield minimum 2 secs for staticP to initialize: + delay(2000); +#endif + // Start websockets server. server.listen(port); diff --git a/examples/Generic/Teensy41_QNEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino b/examples/Generic/Teensy41_QNEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino index 22f6bc65..e90d0c26 100644 --- a/examples/Generic/Teensy41_QNEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino +++ b/examples/Generic/Teensy41_QNEthernet/MultipleClients_Teensy41_Server/MultipleClients_Teensy41_Server.ino @@ -175,6 +175,12 @@ void setup() #endif +// give the Ethernet shield minimum 1 sec for DHCP and 2 secs for staticP to initialize: +#if USING_DHCP + delay(1000); +#else + delay(2000); +#endif // Start websockets server. server.listen(port); diff --git a/examples/Generic/Teensy41_QNEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino b/examples/Generic/Teensy41_QNEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino index d39865ce..673b1e06 100644 --- a/examples/Generic/Teensy41_QNEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino +++ b/examples/Generic/Teensy41_QNEthernet/SocketAndHttp_Teensy41_Server/SocketAndHttp_Teensy41_Server.ino @@ -183,6 +183,13 @@ void setup() #endif +// give the Ethernet shield minimum 1 sec for DHCP and 2 secs for staticP to initialize: +#if USING_DHCP + delay(1000); +#else + delay(2000); +#endif + // Start websockets server. socketServer.listen(websocketsPort); diff --git a/examples/Generic/Teensy41_QNEthernet/Teensy41_Client/Teensy41_Client.ino b/examples/Generic/Teensy41_QNEthernet/Teensy41_Client/Teensy41_Client.ino index cd9072d8..b6d9f897 100644 --- a/examples/Generic/Teensy41_QNEthernet/Teensy41_Client/Teensy41_Client.ino +++ b/examples/Generic/Teensy41_QNEthernet/Teensy41_Client/Teensy41_Client.ino @@ -6,7 +6,7 @@ to support STM32F/L/H/G/WB/MP1, nRF52, SAMD21/SAMD51, SAM DUE, Teensy boards besides ESP8266 and ESP32 The library provides simple and easy interface for websockets (Client and Server). - + Built by Khoi Hoang https://github.com/khoih-prog/Websockets2_Generic Licensed under MIT license *****************************************************************************************************************************/ @@ -35,20 +35,24 @@ Written by https://github.com/arnoson */ -#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) - // For Teensy 4.1 - #define BOARD_TYPE "TEENSY 4.1" - // Use true for NativeEthernet Library, false if using other Ethernet libraries - #define USE_NATIVE_ETHERNET false - #define USE_QN_ETHERNET true +#if ( defined(CORE_TEENSY) && defined(__IMXRT1062__) && defined(ARDUINO_TEENSY41) ) +// For Teensy 4.1 +#define BOARD_TYPE "TEENSY 4.1" +// Use true for NativeEthernet Library, false if using other Ethernet libraries +#define USE_NATIVE_ETHERNET false +#define USE_QN_ETHERNET true #else - #error Only Teensy 4.1 supported +#error Only Teensy 4.1 supported #endif #ifndef BOARD_NAME - #define BOARD_NAME BOARD_TYPE +#define BOARD_NAME BOARD_TYPE #endif +#define DEBUG_WEBSOCKETS_PORT Serial +// Debug Level from 0 to 4 +#define _WEBSOCKETS_LOGLEVEL_ 4 + #define WEBSOCKETS_USE_ETHERNET true #include @@ -65,31 +69,31 @@ IPAddress myIP(192, 168, 2, 222); #if USE_NATIVE_ETHERNET - #include "NativeEthernet.h" - #warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "using NativeEthernet" +#include "NativeEthernet.h" +#warning Using NativeEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error +#define SHIELD_TYPE "using NativeEthernet" - // We will set the MAC address at the beginning of `setup()` using TeensyID's - // `teensyMac` helper. - byte mac[6]; +// We will set the MAC address at the beginning of `setup()` using TeensyID's +// `teensyMac` helper. +byte mac[6]; #else - #include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet - using namespace qindesign::network; - #warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error - #define SHIELD_TYPE "using QNEthernet" - - // Static IP not working yet. Don't use now until fixed. - #define USING_DHCP true - - #if !USING_DHCP - - IPAddress myNetmask(255, 255, 255, 0); - IPAddress myGW(192, 168, 2, 1); - //IPAddress mydnsServer(192, 168, 2, 1); - IPAddress mydnsServer(8, 8, 8, 8); - #endif +#include "QNEthernet.h" // https://github.com/ssilverman/QNEthernet +using namespace qindesign::network; +#warning Using QNEthernet lib for Teensy 4.1. Must also use Teensy Packages Patch or error +#define SHIELD_TYPE "using QNEthernet" + +//#define USING_DHCP true +#define USING_DHCP false + +#if !USING_DHCP + +IPAddress myNetmask(255, 255, 255, 0); +IPAddress myGW(192, 168, 2, 1); +//IPAddress mydnsServer(192, 168, 2, 1); +IPAddress mydnsServer(8, 8, 8, 8); +#endif #endif @@ -99,9 +103,9 @@ IPAddress myIP(192, 168, 2, 222); //const char* url = "ws://echo.websocket.org"; const char* url = "ws://192.168.2.30:8080"; -void setup() +void setup() { -#if USE_NATIVE_ETHERNET +#if USE_NATIVE_ETHERNET // Set the MAC address. teensyMAC(mac); #endif @@ -109,8 +113,8 @@ void setup() // Start Serial and wait until it is ready. Serial.begin(115200); while (!Serial); - - Serial.print("\nStarting Teensy41_Client on "); Serial.print(BOARD_NAME); + + Serial.print("\nStarting Teensy41_Client on "); Serial.print(BOARD_NAME); Serial.print(" "); Serial.println(SHIELD_TYPE); Serial.println(WEBSOCKETS2_GENERIC_VERSION); @@ -123,7 +127,7 @@ void setup() #if USE_NATIVE_ETHERNET // start the ethernet connection and the server: - + // Use Static IP //Ethernet.begin(mac, myIP); // Use DHCP dynamic IP @@ -134,16 +138,16 @@ void setup() #else - #if USING_DHCP - // Start the Ethernet connection, using DHCP - Serial.print("Initialize Ethernet using DHCP => "); - Ethernet.begin(); - #else - // Start the Ethernet connection, using static IP - Serial.print("Initialize Ethernet using static IP => "); - Ethernet.begin(myIP, myNetmask, myGW); - Ethernet.setDNSServerIP(mydnsServer); - #endif +#if USING_DHCP + // Start the Ethernet connection, using DHCP + Serial.print("Initialize Ethernet using DHCP => "); + Ethernet.begin(); +#else + // Start the Ethernet connection, using static IP + Serial.print("Initialize Ethernet using static IP => "); + Ethernet.begin(myIP, myNetmask, myGW); + Ethernet.setDNSServerIP(mydnsServer); +#endif if (!Ethernet.waitForLocalIP(5000)) { @@ -167,30 +171,37 @@ void setup() #endif +// give the Ethernet shield minimum 1 sec for DHCP and 2 secs for staticP to initialize: +#if USING_DHCP + delay(1000); +#else + delay(2000); +#endif + // Connect to websocket server. - if (client.connect(url)) + if (client.connect(url)) { Serial.print("Connected to server : "); Serial.println(url); // Send welcome message. client.send("Hello Server"); - } - else + } + else { Serial.print("Couldn't connect to server : "); Serial.println(url); } // Run callback when messages are received. - client.onMessage([&](WebsocketsMessage message) + client.onMessage([&](WebsocketsMessage message) { Serial.print("Got Message: "); Serial.println(message.data()); }); } -void loop() +void loop() { // Check for incoming messages. - if (client.available()) + if (client.available()) { client.poll(); } diff --git a/examples/Generic/Teensy41_QNEthernet/Teensy41_Server/Teensy41_Server.ino b/examples/Generic/Teensy41_QNEthernet/Teensy41_Server/Teensy41_Server.ino index 8771fc6c..d19b4184 100644 --- a/examples/Generic/Teensy41_QNEthernet/Teensy41_Server/Teensy41_Server.ino +++ b/examples/Generic/Teensy41_QNEthernet/Teensy41_Server/Teensy41_Server.ino @@ -167,6 +167,13 @@ void setup() #endif + +// give the Ethernet shield minimum 1 sec for DHCP and 2 secs for staticP to initialize: +#if USING_DHCP + delay(1000); +#else + delay(2000); +#endif // Start websockets server. server.listen(port);