From dcfb0df665840a43f24e7fb9aa73a6db2874fe8b Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 11 Aug 2024 14:09:47 +0700 Subject: [PATCH] feat: Support Arduino Nano 33 IoT, MKR WIFI 1010 (#898) --- .github/workflows/arduino-lint.yaml | 10 ++ .../compile-arduino_wifinina-examples.yaml | 54 ++++++++++ .../workflows/compile-unor4wifi-examples.yaml | 45 ++++++++ README.md | 1 + .../arduino_wifinina/arduino_wifinina.ino | 100 ++++++++++++++++++ library.json | 4 +- library.properties | 2 +- src/WebSockets.h | 23 ++++ src/WebSocketsClient.cpp | 6 +- src/WebSocketsServer.cpp | 15 ++- src/WebSocketsVersion.h | 6 +- 11 files changed, 257 insertions(+), 9 deletions(-) create mode 100644 .github/workflows/arduino-lint.yaml create mode 100644 .github/workflows/compile-arduino_wifinina-examples.yaml create mode 100644 .github/workflows/compile-unor4wifi-examples.yaml create mode 100644 examples/arduino_wifinina/arduino_wifinina.ino diff --git a/.github/workflows/arduino-lint.yaml b/.github/workflows/arduino-lint.yaml new file mode 100644 index 0000000..7c15bc9 --- /dev/null +++ b/.github/workflows/arduino-lint.yaml @@ -0,0 +1,10 @@ +name: Arduino library compliance (Lint) +on: [push, pull_request] +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: arduino/arduino-lint-action@v1 + with: + library-manager: update diff --git a/.github/workflows/compile-arduino_wifinina-examples.yaml b/.github/workflows/compile-arduino_wifinina-examples.yaml new file mode 100644 index 0000000..166847f --- /dev/null +++ b/.github/workflows/compile-arduino_wifinina-examples.yaml @@ -0,0 +1,54 @@ +name: Compile Arduino WiFiNINA Examples + +# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows +on: [push, pull_request] + +jobs: + build: + name: ${{ matrix.board.fqbn }} + runs-on: ubuntu-latest + + env: + SKETCHES_REPORTS_PATH: sketches-reports + + strategy: + fail-fast: false + + matrix: + board: + - fqbn: arduino:samd:mkrwifi1010 + platforms: | + - name: arduino:samd + artifact-name-suffix: arduino-samd-mkrwifi1010 + libraries: | + - name: WiFiNINA + - fqbn: arduino:samd:nano_33_iot + platforms: | + - name: arduino:samd + artifact-name-suffix: arduino-samd-nano_33_iot + libraries: | + - name: WiFiNINA + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Compile examples + uses: arduino/compile-sketches@v1 + with: + fqbn: ${{ matrix.board.fqbn }} + platforms: ${{ matrix.board.platforms }} + libraries: | + # Install the library from the local path. + - source-path: ./ + ${{ matrix.board.libraries }} + sketch-paths: | + - examples/arduino_wifinina/arduino_wifinina.ino + enable-deltas-report: true + sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }} + + - name: Save sketches report as workflow artifact + uses: actions/upload-artifact@v4 + with: + if-no-files-found: error + path: ${{ env.SKETCHES_REPORTS_PATH }} + name: sketches-report-${{ matrix.board.artifact-name-suffix }} diff --git a/.github/workflows/compile-unor4wifi-examples.yaml b/.github/workflows/compile-unor4wifi-examples.yaml new file mode 100644 index 0000000..9be9878 --- /dev/null +++ b/.github/workflows/compile-unor4wifi-examples.yaml @@ -0,0 +1,45 @@ +name: Compile Arduino UNO R4 WiFi Examples + +# See: https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows +on: [push, pull_request] + +jobs: + build: + name: ${{ matrix.board.fqbn }} + runs-on: ubuntu-latest + + env: + SKETCHES_REPORTS_PATH: sketches-reports + + strategy: + fail-fast: false + + matrix: + board: + - fqbn: arduino:renesas_uno:unor4wifi + platforms: | + - name: arduino:renesas_uno + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Compile examples + uses: arduino/compile-sketches@v1 + with: + fqbn: ${{ matrix.board.fqbn }} + platforms: ${{ matrix.board.platforms }} + libraries: | + # Install the library from the local path. + - source-path: ./ + sketch-paths: | + - examples/arduino_renesas/arduino_uno_r4_wifi + enable-deltas-report: true + sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }} + + - name: Save sketches report as workflow artifact + uses: actions/upload-artifact@v4 + with: + if-no-files-found: error + path: ${{ env.SKETCHES_REPORTS_PATH }} + name: sketches-report-${{ matrix.board.artifact-name-suffix }} diff --git a/README.md b/README.md index f0b8ea9..2dcec63 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ a WebSocket Server and Client for Arduino based on RFC6455. - ATmega2560 with Ethernet Shield (ATmega branch) - ATmega2560 with enc28j60 (ATmega branch) - Arduino UNO [R4 WiFi](https://github.com/arduino/ArduinoCore-renesas) + - Arduino Nano 33 IoT, MKR WIFI 1010 ###### Note: ###### diff --git a/examples/arduino_wifinina/arduino_wifinina.ino b/examples/arduino_wifinina/arduino_wifinina.ino new file mode 100644 index 0000000..d8f6e3a --- /dev/null +++ b/examples/arduino_wifinina/arduino_wifinina.ino @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +#define WIFI_SSID "" +#define WIFI_PASS "" + +int status = WL_IDLE_STATUS; +WiFiClient client; +WebSocketsClient webSocket; + +void webSocketEvent(WStype_t type, uint8_t *payload, size_t length) { + + switch (type) { + case WStype_DISCONNECTED: + Serial.println("[WSc] Disconnected!"); + break; + case WStype_CONNECTED: + Serial.println("[WSc] Connected!"); + + // send message to server when Connected + webSocket.sendTXT("Connected"); + break; + case WStype_TEXT: + Serial.print("[WSc] get text:"); + Serial.println((char *)payload); + + // send message to server + // webSocket.sendTXT("message here"); + break; + case WStype_BIN: + // send data to server + // webSocket.sendBIN(payload, length); + break; + case WStype_ERROR: + case WStype_FRAGMENT_TEXT_START: + case WStype_FRAGMENT_BIN_START: + case WStype_FRAGMENT: + case WStype_PING: + case WStype_PONG: + case WStype_FRAGMENT_FIN: + break; + } +} + +void setup() { + Serial.begin(115200); + + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + Serial.println(); + Serial.println(); + Serial.println(); + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + String fv = WiFi.firmwareVersion(); + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { + Serial.println("Please upgrade the firmware"); + } + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(WIFI_SSID); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(WIFI_SSID, WIFI_PASS); + + // wait 10 seconds for connection: + delay(10000); + } + + Serial.println("Connected to WiFi"); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // server address, port and URL + webSocket.begin("192.168.0.123", 8011); + + // event handler + webSocket.onEvent(webSocketEvent); + + // try ever 5000 again if connection has failed + webSocket.setReconnectInterval(5000); +} + +void loop() { + webSocket.loop(); +} \ No newline at end of file diff --git a/library.json b/library.json index 6869b8c..56ae925 100644 --- a/library.json +++ b/library.json @@ -21,5 +21,5 @@ "type": "git", "url": "https://github.com/Links2004/arduinoWebSockets.git" }, - "version": "2.5.2" -} \ No newline at end of file + "version": "2.5.3" +} diff --git a/library.properties b/library.properties index ceb4a3f..18c7748 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WebSockets -version=2.5.2 +version=2.5.3 author=Markus Sattler maintainer=Markus Sattler sentence=WebSockets for Arduino (Server + Client) diff --git a/src/WebSockets.h b/src/WebSockets.h index 56b4988..f0a2bf9 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -99,6 +99,12 @@ #define WEBSOCKETS_YIELD() yield() #define WEBSOCKETS_YIELD_MORE() delay(1) +#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) + +#define WEBSOCKETS_MAX_DATA_SIZE (15 * 1024) +#define WEBSOCKETS_YIELD() yield() +#define WEBSOCKETS_YIELD_MORE() delay(1) + #else // atmega328p has only 2KB ram! @@ -121,6 +127,8 @@ #define NETWORK_ESP32_ETH (5) #define NETWORK_RP2040 (6) #define NETWORK_UNOWIFIR4 (7) +#define NETWORK_WIFI_NINA (8) + // max size of the WS Message Header #define WEBSOCKETS_MAX_HEADER_SIZE (14) @@ -142,6 +150,9 @@ #elif defined(ARDUINO_UNOWIFIR4) #define WEBSOCKETS_NETWORK_TYPE NETWORK_UNOWIFIR4 +#elif defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) +#define WEBSOCKETS_NETWORK_TYPE NETWORK_WIFI_NINA + #else #define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100 @@ -241,6 +252,18 @@ #define WEBSOCKETS_NETWORK_CLASS WiFiClient #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer +#define WEBSOCKETS_NETWORK_CLASS WiFiClient +#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer + +#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_WIFI_NINA) +#if __has_include() + #include +#else + #error "Please install WiFiNINA library!" +#endif + +#define WEBSOCKETS_NETWORK_CLASS WiFiClient +#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer #else #error "no network type selected!" #endif diff --git a/src/WebSocketsClient.cpp b/src/WebSocketsClient.cpp index 78512e3..1b8f7a3 100644 --- a/src/WebSocketsClient.cpp +++ b/src/WebSocketsClient.cpp @@ -534,7 +534,11 @@ void WebSocketsClient::clientDisconnect(WSclient_t * client) { #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) client->status = WSC_NOT_CONNECTED; #else - delete client->tcp; + #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_WIFI_NINA) + // does not support delete (no destructor) + #else + delete client->tcp; + #endif #endif client->tcp = NULL; } diff --git a/src/WebSocketsServer.cpp b/src/WebSocketsServer.cpp index e11e1cf..697c1d2 100644 --- a/src/WebSocketsServer.cpp +++ b/src/WebSocketsServer.cpp @@ -65,7 +65,11 @@ WebSocketsServerCore::~WebSocketsServerCore() { } WebSocketsServer::~WebSocketsServer() { - delete _server; + #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_WIFI_NINA) + // does not support delete (no destructor) + #else + delete _server; + #endif } /** @@ -539,6 +543,8 @@ void WebSocketsServerCore::dropNativeClient(WSclient_t * client) { } #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC) client->status = WSC_NOT_CONNECTED; +#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_WIFI_NINA) + // does not support delete (no destructor) #else delete client->tcp; #endif @@ -655,7 +661,12 @@ void WebSocketsServer::handleNewClients(void) { #endif // store new connection - WEBSOCKETS_NETWORK_CLASS * tcpClient = new WEBSOCKETS_NETWORK_CLASS(_server->accept()); + #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_WIFI_NINA) + WEBSOCKETS_NETWORK_CLASS * tcpClient = new WEBSOCKETS_NETWORK_CLASS(_server->available()); + #else + WEBSOCKETS_NETWORK_CLASS * tcpClient = new WEBSOCKETS_NETWORK_CLASS(_server->accept()); + #endif + if(!tcpClient) { DEBUG_WEBSOCKETS("[WS-Client] creating Network class failed!"); return; diff --git a/src/WebSocketsVersion.h b/src/WebSocketsVersion.h index 76bacef..8662206 100644 --- a/src/WebSocketsVersion.h +++ b/src/WebSocketsVersion.h @@ -25,12 +25,12 @@ #ifndef WEBSOCKETSVERSION_H_ #define WEBSOCKETSVERSION_H_ -#define WEBSOCKETS_VERSION "2.5.2" +#define WEBSOCKETS_VERSION "2.5.3" #define WEBSOCKETS_VERSION_MAJOR 2 #define WEBSOCKETS_VERSION_MINOR 5 -#define WEBSOCKETS_VERSION_PATCH 2 +#define WEBSOCKETS_VERSION_PATCH 3 -#define WEBSOCKETS_VERSION_INT 2005002 +#define WEBSOCKETS_VERSION_INT 2005003 #endif /* WEBSOCKETSVERSION_H_ */