From 7b84f0713fdeeda1c78196dee0e4160a48439992 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 16:17:54 +0700 Subject: [PATCH 1/9] feat: arduino uno r wifi r 4 support --- examples/renesas_uno/uno_r_wifi_4.ino | 126 ++++++++++++++++++++++++++ library.json | 4 +- src/WebSockets.h | 17 ++++ 3 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 examples/renesas_uno/uno_r_wifi_4.ino diff --git a/examples/renesas_uno/uno_r_wifi_4.ino b/examples/renesas_uno/uno_r_wifi_4.ino new file mode 100644 index 0000000..4a2f47c --- /dev/null +++ b/examples/renesas_uno/uno_r_wifi_4.ino @@ -0,0 +1,126 @@ +#include + +#include "WiFiS3.h" +#include + +#define WIFI_SSID "" +#define WIFI_PASS "" + +WebSocketsClient webSocket; + +#define USE_SERIAL Serial + +void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { + const uint8_t* src = (const uint8_t*) mem; + USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); + for(uint32_t i = 0; i < len; i++) { + if(i % cols == 0) { + USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); + } + USE_SERIAL.printf("%02X ", *src); + src++; + } + USE_SERIAL.printf("\n"); +} + +void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { + + switch(type) { + case WStype_DISCONNECTED: + USE_SERIAL.printf("[WSc] Disconnected!\n"); + break; + case WStype_CONNECTED: + USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); + + // send message to server when Connected + webSocket.sendTXT("Connected"); + break; + case WStype_TEXT: + USE_SERIAL.printf("[WSc] get text: %s\n", payload); + + // send message to server + // webSocket.sendTXT("message here"); + break; + case WStype_BIN: + USE_SERIAL.printf("[WSc] get binary length: %u\n", length); + hexdump(payload, length); + + // 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_FRAGMENT_FIN: + break; + } + +} + +void setup() { + // USE_SERIAL.begin(921600); + USE_SERIAL.begin(115200); + + while (!USE_SERIAL) { + ; // wait for serial port to connect. Needed for native USB port only + } + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for(uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_MODULE) { + USE_SERIAL.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + String fv = WiFi.firmwareVersion(); + if (fv < WIFI_FIRMWARE_LATEST_VERSION) { + USE_SERIAL.println("Please upgrade the firmware"); + } + + USE_SERIAL.println("[Wifi]: Connecting"); + + int status = WL_IDLE_STATUS; + + // attempt to connect to WiFi network: + while (status != WL_CONNECTED) { + USE_SERIAL.print("[Wifi]: Attempting to connect to SSID: "); + + USE_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); + + delay(1000); + } + + USE_SERIAL.println("Connected!"); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + USE_SERIAL.print("IP Address: "); + USE_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 3647d91..6370335 100644 --- a/library.json +++ b/library.json @@ -16,10 +16,10 @@ "keywords": "wifi, http, web, server, client, websocket", "license": "LGPL-2.1", "name": "WebSockets", - "platforms": "atmelavr, espressif8266, espressif32, raspberrypi", + "platforms": "atmelavr, espressif8266, espressif32, raspberrypi, renesas_uno", "repository": { "type": "git", "url": "https://github.com/Links2004/arduinoWebSockets.git" }, - "version": "2.4.1" + "version": "2.5.1" } \ No newline at end of file diff --git a/src/WebSockets.h b/src/WebSockets.h index db6c0e8..56b4988 100644 --- a/src/WebSockets.h +++ b/src/WebSockets.h @@ -93,6 +93,12 @@ #define WEBSOCKETS_YIELD() yield() #define WEBSOCKETS_YIELD_MORE() delay(1) +#elif defined(ARDUINO_UNOWIFIR4) + +#define WEBSOCKETS_MAX_DATA_SIZE (15 * 1024) +#define WEBSOCKETS_YIELD() yield() +#define WEBSOCKETS_YIELD_MORE() delay(1) + #else // atmega328p has only 2KB ram! @@ -114,6 +120,7 @@ #define NETWORK_ESP32 (4) #define NETWORK_ESP32_ETH (5) #define NETWORK_RP2040 (6) +#define NETWORK_UNOWIFIR4 (7) // max size of the WS Message Header #define WEBSOCKETS_MAX_HEADER_SIZE (14) @@ -132,6 +139,9 @@ #elif defined(ARDUINO_ARCH_RP2040) #define WEBSOCKETS_NETWORK_TYPE NETWORK_RP2040 +#elif defined(ARDUINO_UNOWIFIR4) +#define WEBSOCKETS_NETWORK_TYPE NETWORK_UNOWIFIR4 + #else #define WEBSOCKETS_NETWORK_TYPE NETWORK_W5100 @@ -224,6 +234,13 @@ #define WEBSOCKETS_NETWORK_SSL_CLASS WiFiClientSecure #define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer +#elif(WEBSOCKETS_NETWORK_TYPE == NETWORK_UNOWIFIR4) + +#include + +#define WEBSOCKETS_NETWORK_CLASS WiFiClient +#define WEBSOCKETS_NETWORK_SERVER_CLASS WiFiServer + #else #error "no network type selected!" #endif From 6c69b58065cc9128b082d538e1d7fb81d3d9c8a0 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 16:19:23 +0700 Subject: [PATCH 2/9] Update library.json --- library.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/library.json b/library.json index 3a8ee27..d51149f 100644 --- a/library.json +++ b/library.json @@ -21,9 +21,5 @@ "type": "git", "url": "https://github.com/Links2004/arduinoWebSockets.git" }, -<<<<<<< HEAD "version": "2.5.1" -======= - "version": "2.4.2" ->>>>>>> e364e6688455814a47f95c8ca79b27a753c0c630 -} \ No newline at end of file +} From fa6435aa48ecafa07b1d5daedb7123cfd8fdb108 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 16:20:16 +0700 Subject: [PATCH 3/9] Update README.md --- README.md | 111 +----------------------------------------------------- 1 file changed, 1 insertion(+), 110 deletions(-) diff --git a/README.md b/README.md index fdc4e4c..cc1bc2f 100644 --- a/README.md +++ b/README.md @@ -1,113 +1,4 @@ WebSocket Server and Client for Arduino [![Build Status](https://github.com/Links2004/arduinoWebSockets/workflows/CI/badge.svg?branch=master)](https://github.com/Links2004/arduinoWebSockets/actions?query=workflow%3ACI+branch%3Amaster) =========================================== -a WebSocket Server and Client for Arduino based on RFC6455. - - -##### Supported features of RFC6455 ##### - - text frame - - binary frame - - connection close - - ping - - pong - - continuation frame - -##### Limitations ##### - - max input length is limited to the ram size and the ```WEBSOCKETS_MAX_DATA_SIZE``` define - - max output length has no limit (the hardware is the limit) - - Client send big frames with mask 0x00000000 (on AVR all frames) - - continuation frame reassembly need to be handled in the application code - - ##### Limitations for Async ##### - - Functions called from within the context of the websocket event might not honor `yield()` and/or `delay()`. See [this issue](https://github.com/Links2004/arduinoWebSockets/issues/58#issuecomment-192376395) for more info and a potential workaround. - - wss / SSL is not possible. - -##### Supported Hardware ##### - - ESP8266 [Arduino for ESP8266](https://github.com/esp8266/Arduino/) - - ESP32 [Arduino for ESP32](https://github.com/espressif/arduino-esp32) - - ESP31B - - Raspberry Pi Pico W [Arduino for Pico](https://github.com/earlephilhower/arduino-pico) - - Particle with STM32 ARM Cortex M3 - - ATmega328 with Ethernet Shield (ATmega branch) - - ATmega328 with enc28j60 (ATmega branch) - - ATmega2560 with Ethernet Shield (ATmega branch) - - ATmega2560 with enc28j60 (ATmega branch) - -###### Note: ###### - - version 2.0.0 and up is not compatible with AVR/ATmega, check ATmega branch. - - version 2.3.0 has API changes for the ESP8266 BareSSL (may brakes existing code) - - Arduino for AVR not supports std namespace of c++. - -### wss / SSL ### - supported for: - - wss client on the ESP8266 - - wss / SSL is not natively supported in WebSocketsServer however it is possible to achieve secure websockets - by running the device behind an SSL proxy. See [Nginx](examples/Nginx/esp8266.ssl.reverse.proxy.conf) for a - sample Nginx server configuration file to enable this. - -### Root CA Cert Bundles for SSL/TLS connections ### - -Secure connections require the certificate of the server to be verified. One option is to provide a single certificate in the chain of trust. However, for flexibility and robustness, a certificate bundle is recommended. If a server changes the root CA from which it derives its certificates, this will not be a problem. With a single CA cert it will not connect. - - - For [technical details](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/esp_crt_bundle.html) - - For a [PlatformIO setup](https://github.com/Duckle29/esp32-certBundle/) - - For an [example](examples/esp32/WebSocketClientSSLBundle/) - -Including a bundle with all CA certs will use 77.2 kB but this list can be reduced to 16.5 kB for the 41 most common. This results in 90% absolute usage coverage and 99% market share coverage according to [W3Techs](https://w3techs.com/technologies/overview/ssl_certificate). The bundle is inserted into the compiled firmware. The bundle is not loaded into RAM, only its index. - -### ESP Async TCP ### - -This libary can run in Async TCP mode on the ESP. - -The mode can be activated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE define). - -[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required. - - -### High Level Client API ### - - - `begin` : Initiate connection sequence to the websocket host. -```c++ -void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino"); -void begin(String host, uint16_t port, String url = "/", String protocol = "arduino"); - ``` - - `onEvent`: Callback to handle for websocket events - - ```c++ - void onEvent(WebSocketClientEvent cbEvent); - ``` - - - `WebSocketClientEvent`: Handler for websocket events - ```c++ - void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length) - ``` -Where `WStype_t type` is defined as: - ```c++ - typedef enum { - WStype_ERROR, - WStype_DISCONNECTED, - WStype_CONNECTED, - WStype_TEXT, - WStype_BIN, - WStype_FRAGMENT_TEXT_START, - WStype_FRAGMENT_BIN_START, - WStype_FRAGMENT, - WStype_FRAGMENT_FIN, - WStype_PING, - WStype_PONG, - } WStype_t; - ``` - -### Issues ### -Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues - -[![Join the chat at https://gitter.im/Links2004/arduinoWebSockets](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Links2004/arduinoWebSockets?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) - -### License and credits ### - -The library is licensed under [LGPLv2.1](https://github.com/Links2004/arduinoWebSockets/blob/master/LICENSE) - -[libb64](http://libb64.sourceforge.net/) written by Chris Venter. It is distributed under Public Domain see [LICENSE](https://github.com/Links2004/arduinoWebSockets/blob/master/src/libb64/LICENSE). +https://github.com/Links2004/arduinoWebSockets + Arduino Uno WiFI R4 support added. From 59289b110dae655d50392a2493ba77ebdae37e85 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 20:53:17 +0700 Subject: [PATCH 4/9] feat: read me updated. example fixed. --- README.md | 112 +++++++++++++++++- .../arduino_uno_r4_wifi.ino} | 28 +++-- 2 files changed, 130 insertions(+), 10 deletions(-) rename examples/{renesas_uno/uno_r_wifi_4.ino => arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino} (80%) diff --git a/README.md b/README.md index cc1bc2f..08e3d87 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,114 @@ WebSocket Server and Client for Arduino [![Build Status](https://github.com/Links2004/arduinoWebSockets/workflows/CI/badge.svg?branch=master)](https://github.com/Links2004/arduinoWebSockets/actions?query=workflow%3ACI+branch%3Amaster) =========================================== -https://github.com/Links2004/arduinoWebSockets + Arduino Uno WiFI R4 support added. +a WebSocket Server and Client for Arduino based on RFC6455. + + +##### Supported features of RFC6455 ##### + - text frame + - binary frame + - connection close + - ping + - pong + - continuation frame + +##### Limitations ##### + - max input length is limited to the ram size and the ```WEBSOCKETS_MAX_DATA_SIZE``` define + - max output length has no limit (the hardware is the limit) + - Client send big frames with mask 0x00000000 (on AVR all frames) + - continuation frame reassembly need to be handled in the application code + + ##### Limitations for Async ##### + - Functions called from within the context of the websocket event might not honor `yield()` and/or `delay()`. See [this issue](https://github.com/Links2004/arduinoWebSockets/issues/58#issuecomment-192376395) for more info and a potential workaround. + - wss / SSL is not possible. + +##### Supported Hardware ##### + - ESP8266 [Arduino for ESP8266](https://github.com/esp8266/Arduino/) + - ESP32 [Arduino for ESP32](https://github.com/espressif/arduino-esp32) + - ESP31B + - Raspberry Pi Pico W [Arduino for Pico](https://github.com/earlephilhower/arduino-pico) + - Particle with STM32 ARM Cortex M3 + - ATmega328 with Ethernet Shield (ATmega branch) + - ATmega328 with enc28j60 (ATmega branch) + - ATmega2560 with Ethernet Shield (ATmega branch) + - ATmega2560 with enc28j60 (ATmega branch) + - Arduino UNO [R4 WiFi] (https://github.com/arduino/ArduinoCore-renesas) + +###### Note: ###### + + version 2.0.0 and up is not compatible with AVR/ATmega, check ATmega branch. + + version 2.3.0 has API changes for the ESP8266 BareSSL (may brakes existing code) + + Arduino for AVR not supports std namespace of c++. + +### wss / SSL ### + supported for: + - wss client on the ESP8266 + - wss / SSL is not natively supported in WebSocketsServer however it is possible to achieve secure websockets + by running the device behind an SSL proxy. See [Nginx](examples/Nginx/esp8266.ssl.reverse.proxy.conf) for a + sample Nginx server configuration file to enable this. + +### Root CA Cert Bundles for SSL/TLS connections ### + +Secure connections require the certificate of the server to be verified. One option is to provide a single certificate in the chain of trust. However, for flexibility and robustness, a certificate bundle is recommended. If a server changes the root CA from which it derives its certificates, this will not be a problem. With a single CA cert it will not connect. + + - For [technical details](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/esp_crt_bundle.html) + - For a [PlatformIO setup](https://github.com/Duckle29/esp32-certBundle/) + - For an [example](examples/esp32/WebSocketClientSSLBundle/) + +Including a bundle with all CA certs will use 77.2 kB but this list can be reduced to 16.5 kB for the 41 most common. This results in 90% absolute usage coverage and 99% market share coverage according to [W3Techs](https://w3techs.com/technologies/overview/ssl_certificate). The bundle is inserted into the compiled firmware. The bundle is not loaded into RAM, only its index. + +### ESP Async TCP ### + +This libary can run in Async TCP mode on the ESP. + +The mode can be activated in the ```WebSockets.h``` (see WEBSOCKETS_NETWORK_TYPE define). + +[ESPAsyncTCP](https://github.com/me-no-dev/ESPAsyncTCP) libary is required. + + +### High Level Client API ### + + - `begin` : Initiate connection sequence to the websocket host. +```c++ +void begin(const char *host, uint16_t port, const char * url = "/", const char * protocol = "arduino"); +void begin(String host, uint16_t port, String url = "/", String protocol = "arduino"); + ``` + - `onEvent`: Callback to handle for websocket events + + ```c++ + void onEvent(WebSocketClientEvent cbEvent); + ``` + + - `WebSocketClientEvent`: Handler for websocket events + ```c++ + void (*WebSocketClientEvent)(WStype_t type, uint8_t * payload, size_t length) + ``` +Where `WStype_t type` is defined as: + ```c++ + typedef enum { + WStype_ERROR, + WStype_DISCONNECTED, + WStype_CONNECTED, + WStype_TEXT, + WStype_BIN, + WStype_FRAGMENT_TEXT_START, + WStype_FRAGMENT_BIN_START, + WStype_FRAGMENT, + WStype_FRAGMENT_FIN, + WStype_PING, + WStype_PONG, + } WStype_t; + ``` + +### Issues ### +Submit issues to: https://github.com/Links2004/arduinoWebSockets/issues + +[![Join the chat at https://gitter.im/Links2004/arduinoWebSockets](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/Links2004/arduinoWebSockets?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +### License and credits ### + +The library is licensed under [LGPLv2.1](https://github.com/Links2004/arduinoWebSockets/blob/master/LICENSE) + +[libb64](http://libb64.sourceforge.net/) written by Chris Venter. It is distributed under Public Domain see [LICENSE](https://github.com/Links2004/arduinoWebSockets/blob/master/src/libb64/LICENSE). diff --git a/examples/renesas_uno/uno_r_wifi_4.ino b/examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino similarity index 80% rename from examples/renesas_uno/uno_r_wifi_4.ino rename to examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino index 4a2f47c..f0da504 100644 --- a/examples/renesas_uno/uno_r_wifi_4.ino +++ b/examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino @@ -1,4 +1,6 @@ #include +#include +#include #include "WiFiS3.h" #include @@ -8,41 +10,49 @@ WebSocketsClient webSocket; +size_t _printf(const char *format, ...) { + va_list arg; + va_start(arg, format); + size_t ret = vprintf(format, arg); + va_end(arg); + return ret; +} + #define USE_SERIAL Serial void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { const uint8_t* src = (const uint8_t*) mem; - USE_SERIAL.printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); + _printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); for(uint32_t i = 0; i < len; i++) { if(i % cols == 0) { - USE_SERIAL.printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); + _printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); } - USE_SERIAL.printf("%02X ", *src); + _printf("%02X ", *src); src++; } - USE_SERIAL.printf("\n"); + _printf("\n"); } void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { switch(type) { case WStype_DISCONNECTED: - USE_SERIAL.printf("[WSc] Disconnected!\n"); + _printf("[WSc] Disconnected!\n"); break; case WStype_CONNECTED: - USE_SERIAL.printf("[WSc] Connected to url: %s\n", payload); + _printf("[WSc] Connected to url: %s\n", payload); // send message to server when Connected webSocket.sendTXT("Connected"); break; case WStype_TEXT: - USE_SERIAL.printf("[WSc] get text: %s\n", payload); + _printf("[WSc] get text: %s\n", payload); // send message to server // webSocket.sendTXT("message here"); break; case WStype_BIN: - USE_SERIAL.printf("[WSc] get binary length: %u\n", length); + _printf("[WSc] get binary length: %u\n", length); hexdump(payload, length); // send data to server @@ -71,7 +81,7 @@ void setup() { USE_SERIAL.println(); for(uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t); + _printf("[SETUP] BOOT WAIT %d...\n", t); USE_SERIAL.flush(); delay(1000); } From 633fb98689474a4c5001c4ddb923776c2cfbb181 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 20:55:18 +0700 Subject: [PATCH 5/9] fix: broken link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 08e3d87..b5930eb 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ a WebSocket Server and Client for Arduino based on RFC6455. - ATmega328 with enc28j60 (ATmega branch) - ATmega2560 with Ethernet Shield (ATmega branch) - ATmega2560 with enc28j60 (ATmega branch) - - Arduino UNO [R4 WiFi] (https://github.com/arduino/ArduinoCore-renesas) + - Arduino UNO [R4 WiFi](https://github.com/arduino/ArduinoCore-renesas) ###### Note: ###### From cbba76e573b1ef8111f8ff50d8470820b788a3cb Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 21:02:05 +0700 Subject: [PATCH 6/9] fix: logging --- .../arduino_uno_r4_wifi.ino | 157 ++++++++---------- 1 file changed, 65 insertions(+), 92 deletions(-) diff --git a/examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino b/examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino index f0da504..2168a62 100644 --- a/examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino +++ b/examples/arduino_renesas/arduino_uno_r4_wifi/arduino_uno_r4_wifi.ino @@ -5,132 +5,105 @@ #include "WiFiS3.h" #include -#define WIFI_SSID "" -#define WIFI_PASS "" +#define WIFI_SSID "" +#define WIFI_PASS "" WebSocketsClient webSocket; - -size_t _printf(const char *format, ...) { - va_list arg; - va_start(arg, format); - size_t ret = vprintf(format, arg); - va_end(arg); - return ret; -} - -#define USE_SERIAL Serial - -void hexdump(const void *mem, uint32_t len, uint8_t cols = 16) { - const uint8_t* src = (const uint8_t*) mem; - _printf("\n[HEXDUMP] Address: 0x%08X len: 0x%X (%d)", (ptrdiff_t)src, len, len); - for(uint32_t i = 0; i < len; i++) { - if(i % cols == 0) { - _printf("\n[0x%08X] 0x%08X: ", (ptrdiff_t)src, i); - } - _printf("%02X ", *src); - src++; - } - _printf("\n"); -} - -void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { - - switch(type) { - case WStype_DISCONNECTED: - _printf("[WSc] Disconnected!\n"); - break; - case WStype_CONNECTED: - _printf("[WSc] Connected to url: %s\n", payload); - - // send message to server when Connected - webSocket.sendTXT("Connected"); - break; - case WStype_TEXT: - _printf("[WSc] get text: %s\n", payload); - - // send message to server - // webSocket.sendTXT("message here"); - break; - case WStype_BIN: - _printf("[WSc] get binary length: %u\n", length); - hexdump(payload, length); - - // 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_FRAGMENT_FIN: - break; - } - + +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_FRAGMENT_FIN: + break; + } } void setup() { - // USE_SERIAL.begin(921600); - USE_SERIAL.begin(115200); + Serial.begin(115200); - while (!USE_SERIAL) { - ; // wait for serial port to connect. Needed for native USB port only - } - - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } - for(uint8_t t = 4; t > 0; t--) { - _printf("[SETUP] BOOT WAIT %d...\n", t); - USE_SERIAL.flush(); - delay(1000); - } + Serial.println(); + Serial.println(); + Serial.println(); - // check for the WiFi module: + for (uint8_t t = 4; t > 0; t--) { + Serial.println("[SETUP] BOOT WAIT ..."); + Serial.flush(); + delay(1000); + } + + // check for the WiFi module: if (WiFi.status() == WL_NO_MODULE) { - USE_SERIAL.println("Communication with WiFi module failed!"); + Serial.println("Communication with WiFi module failed!"); // don't continue - while (true); + while (true) + ; } String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { - USE_SERIAL.println("Please upgrade the firmware"); + Serial.println("Please upgrade the firmware"); } - USE_SERIAL.println("[Wifi]: Connecting"); + Serial.println("[Wifi]: Connecting"); int status = WL_IDLE_STATUS; // attempt to connect to WiFi network: while (status != WL_CONNECTED) { - USE_SERIAL.print("[Wifi]: Attempting to connect to SSID: "); + Serial.print("[Wifi]: Attempting to connect to SSID: "); + Serial.println(WIFI_SSID); - USE_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); - + delay(1000); } - USE_SERIAL.println("Connected!"); + Serial.println("Connected!"); // print your board's IP address: IPAddress ip = WiFi.localIP(); - USE_SERIAL.print("IP Address: "); - USE_SERIAL.println(ip); - - // server address, port and URL - webSocket.begin("192.168.0.123", 8011); + Serial.print("IP Address: "); + Serial.println(ip); - // event handler - webSocket.onEvent(webSocketEvent); + // server address, port and URL + webSocket.begin("192.168.0.123", 8011); - // try ever 5000 again if connection has failed - webSocket.setReconnectInterval(5000); + // event handler + webSocket.onEvent(webSocketEvent); + // try ever 5000 again if connection has failed + webSocket.setReconnectInterval(5000); } void loop() { - webSocket.loop(); + webSocket.loop(); } \ No newline at end of file From 01387c97cd29d649ae9cc59197a527487b977019 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Sun, 28 Jul 2024 23:56:43 +0700 Subject: [PATCH 7/9] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 487596c..462efc5 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=WebSockets -version=2.4.2 +version=2.5.1 author=Markus Sattler maintainer=Markus Sattler sentence=WebSockets for Arduino (Server + Client) From 07c3b94b3b2b8605de19274c0b31faea894ba502 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Mon, 29 Jul 2024 00:55:17 +0700 Subject: [PATCH 8/9] Update WebSocketsVersion.h --- src/WebSocketsVersion.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/WebSocketsVersion.h b/src/WebSocketsVersion.h index 11fc8b1..8d6bff5 100644 --- a/src/WebSocketsVersion.h +++ b/src/WebSocketsVersion.h @@ -25,11 +25,11 @@ #ifndef WEBSOCKETSVERSION_H_ #define WEBSOCKETSVERSION_H_ -#define WEBSOCKETS_VERSION "2.4.2" +#define WEBSOCKETS_VERSION "2.5.1" #define WEBSOCKETS_VERSION_MAJOR 2 -#define WEBSOCKETS_VERSION_MINOR 4 -#define WEBSOCKETS_VERSION_PATCH 2 +#define WEBSOCKETS_VERSION_MINOR 5 +#define WEBSOCKETS_VERSION_PATCH 1 #define WEBSOCKETS_VERSION_INT 2004002 From 3f7795c0ccad14fda00ddf9b9af1aada833876b3 Mon Sep 17 00:00:00 2001 From: Aruna Tennakoon Date: Mon, 29 Jul 2024 00:55:58 +0700 Subject: [PATCH 9/9] Update WebSocketsVersion.h --- src/WebSocketsVersion.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WebSocketsVersion.h b/src/WebSocketsVersion.h index 8d6bff5..9c69078 100644 --- a/src/WebSocketsVersion.h +++ b/src/WebSocketsVersion.h @@ -31,6 +31,6 @@ #define WEBSOCKETS_VERSION_MINOR 5 #define WEBSOCKETS_VERSION_PATCH 1 -#define WEBSOCKETS_VERSION_INT 2004002 +#define WEBSOCKETS_VERSION_INT 2005001 #endif /* WEBSOCKETSVERSION_H_ */