Skip to content

Commit

Permalink
feat: Support Arduino Nano 33 IoT, MKR WIFI 1010 (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakopappa committed Aug 11, 2024
1 parent dc6fd04 commit dcfb0df
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 9 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/arduino-lint.yaml
Original file line number Diff line number Diff line change
@@ -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
54 changes: 54 additions & 0 deletions .github/workflows/compile-arduino_wifinina-examples.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
45 changes: 45 additions & 0 deletions .github/workflows/compile-unor4wifi-examples.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: ######

Expand Down
100 changes: 100 additions & 0 deletions examples/arduino_wifinina/arduino_wifinina.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <WebSocketsClient.h>

#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();
}
4 changes: 2 additions & 2 deletions library.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"type": "git",
"url": "https://github.com/Links2004/arduinoWebSockets.git"
},
"version": "2.5.2"
}
"version": "2.5.3"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
23 changes: 23 additions & 0 deletions src/WebSockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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(<WiFiNINA.h>)
#include <WiFiNINA.h>
#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
Expand Down
6 changes: 5 additions & 1 deletion src/WebSocketsClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 13 additions & 2 deletions src/WebSocketsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/WebSocketsVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */

0 comments on commit dcfb0df

Please sign in to comment.