From 6d5e2b961350bcf8c70d331f4827186f7b4b7424 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 20 Jun 2025 08:03:59 +0800 Subject: [PATCH 1/7] SerialUSB: fix missing overload after #123 This would have been spotted if we extend the CI to compile all the exaples (TestClient in particular). --- cores/arduino/SerialUSB.h | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/arduino/SerialUSB.h b/cores/arduino/SerialUSB.h index 442d28d0..be1377f8 100644 --- a/cores/arduino/SerialUSB.h +++ b/cores/arduino/SerialUSB.h @@ -19,6 +19,7 @@ class SerialUSB_ : public ZephyrSerial { operator bool() override; size_t write(const uint8_t *buffer, size_t size) override; + size_t write(const uint8_t data) override { return write(&data, 1); } void flush() override; protected: From fdc4356bb3dbcce48235fa60a37f0cc9a3dcaeed Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:37:47 +0200 Subject: [PATCH 2/7] WiFi add SSID() and RSSI() --- libraries/SocketWrapper/WiFi.h | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index c83846b2..c223a72b 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -75,14 +75,12 @@ class WiFiClass: public NetworkInterface } int status() { - struct wifi_iface_status status = { 0 }; - - if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &status, + if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, sizeof(struct wifi_iface_status))) { return WL_NO_SHIELD; } - if (status.state >= WIFI_STATE_ASSOCIATED) { + if (sta_state.state >= WIFI_STATE_ASSOCIATED) { return WL_CONNECTED; } else { return WL_DISCONNECTED; @@ -94,12 +92,28 @@ class WiFiClass: public NetworkInterface // TODO: borrow code from mbed core for scan results handling } + char* SSID() { + if (status() == WL_CONNECTED) { + return (char *)sta_state.ssid; + } + return nullptr; + } + + int32_t RSSI() { + if (status() == WL_CONNECTED) { + return sta_state.rssi; + } + return 0; + } + private: struct net_if *sta_iface = nullptr; struct net_if *ap_iface = nullptr; struct wifi_connect_req_params ap_config; struct wifi_connect_req_params sta_config; + + struct wifi_iface_status sta_state = { 0 }; }; extern WiFiClass WiFi; From 2b7243dbf3be78cf2ce49cf58d6755d698c0f2e4 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:38:23 +0200 Subject: [PATCH 3/7] WiFi allow status() to be called before begin() --- libraries/SocketWrapper/WiFi.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index c223a72b..43b3bc58 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -75,6 +75,8 @@ class WiFiClass: public NetworkInterface } int status() { + sta_iface = net_if_get_wifi_sta(); + netif = sta_iface; if (net_mgmt(NET_REQUEST_WIFI_IFACE_STATUS, netif, &sta_state, sizeof(struct wifi_iface_status))) { return WL_NO_SHIELD; From 6452f1e570fb09a0fed47b2cf67846fb25a6f4b5 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:40:27 +0200 Subject: [PATCH 4/7] WiFi wait for event NET_EVENT_WIFI_CONNECT_RESULT in blocking mode --- libraries/SocketWrapper/WiFi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index 43b3bc58..eb0f275b 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -37,7 +37,7 @@ class WiFiClass: public NetworkInterface NetworkInterface::begin(false, NET_EVENT_WIFI_MASK); if (blocking) { - net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_AP_STA_CONNECTED, NULL, NULL, NULL, K_FOREVER); + net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); } return true; From 067f686edd52bbacd05a2ee7259fa0c47258b2e6 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:41:15 +0200 Subject: [PATCH 5/7] WiFi make begin() blocking by default --- libraries/SocketWrapper/WiFi.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index eb0f275b..4af09ea4 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -16,7 +16,7 @@ class WiFiClass: public NetworkInterface WiFiClass() {} ~WiFiClass() {} - bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = false) { + bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { sta_iface = net_if_get_wifi_sta(); netif = sta_iface; sta_config.ssid = (const uint8_t *)ssid; From 29c603709541e46fc1a6b72bf260fa2fa858f86c Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:43:51 +0200 Subject: [PATCH 6/7] WiFi make begin() return status() --- libraries/SocketWrapper/WiFi.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/SocketWrapper/WiFi.h b/libraries/SocketWrapper/WiFi.h index 4af09ea4..641a5a70 100644 --- a/libraries/SocketWrapper/WiFi.h +++ b/libraries/SocketWrapper/WiFi.h @@ -16,7 +16,7 @@ class WiFiClass: public NetworkInterface WiFiClass() {} ~WiFiClass() {} - bool begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { + int begin(const char* ssid, const char* passphrase, wl_enc_type security = ENC_TYPE_UNKNOWN, bool blocking = true) { sta_iface = net_if_get_wifi_sta(); netif = sta_iface; sta_config.ssid = (const uint8_t *)ssid; @@ -40,7 +40,7 @@ class WiFiClass: public NetworkInterface net_mgmt_event_wait_on_iface(sta_iface, NET_EVENT_WIFI_CONNECT_RESULT, NULL, NULL, NULL, K_FOREVER); } - return true; + return status(); } bool beginAP(char* ssid, char* passphrase, int channel = WIFI_CHANNEL_ANY, bool blocking = false) { From 13d666dbba2d9b02d351c35d02f0a24efee72f81 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 24 Jun 2025 15:49:00 +0200 Subject: [PATCH 7/7] WiFi add WiFiWebclient example --- .../examples/WiFiWebClient/WiFiWebClient.ino | 106 ++++++++++++++++++ .../examples/WiFiWebClient/arduino_secrets.h | 2 + 2 files changed, 108 insertions(+) create mode 100644 libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino create mode 100644 libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h diff --git a/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino b/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino new file mode 100644 index 00000000..a5687041 --- /dev/null +++ b/libraries/SocketWrapper/examples/WiFiWebClient/WiFiWebClient.ino @@ -0,0 +1,106 @@ +/* + Web client + + This sketch connects to a website (http://example.com) using the WiFi module. + + This example is written for a network using WPA encryption. For + WEP or WPA, change the Wifi.begin() call accordingly. + + created 13 July 2010 + by dlf (Metodo2 srl) + modified 31 May 2012 + by Tom Igoe + */ + +#include +#include + +#include "arduino_secrets.h" +///////please enter your sensitive data in the Secret tab/arduino_secrets.h +char ssid[] = SECRET_SSID; // your network SSID (name) +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key Index number (needed only for WEP) + +int status = WL_IDLE_STATUS; +// if you don't want to use DNS (and reduce your sketch size) +// use the numeric IP instead of the name for the server: +// IPAddress server(93,184,216,34); // IP address for example.com (no DNS) +char server[] = "example.com"; // host name for example.com (using DNS) + +ZephyrClient client; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // check for the WiFi module: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true); + } + + // attempt to connect to Wifi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to SSID: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: + status = WiFi.begin(ssid, pass); + Serial.println(status); + // wait 3 seconds for connection: + delay(3000); + } + Serial.println("Connected to wifi"); + printWifiStatus(); + + Serial.println("\nStarting connection to server..."); + // if you get a connection, report back via serial: + if (client.connect(server, 80)) { + Serial.println("connected to server"); + // Make a HTTP request: + client.println("GET /index.html HTTP/1.1"); + client.print("Host: "); + client.println(server); + client.println("Connection: close"); + client.println(); + } +} + +void loop() { + // if there are incoming bytes available + // from the server, read them and print them: + while (client.available()) { + char c = client.read(); + Serial.write(c); + } + + // if the server's disconnected, stop the client: + if (!client.connected()) { + Serial.println(); + Serial.println("disconnecting from server."); + client.stop(); + + // do nothing forevermore: + while (true); + } +} + +void printWifiStatus() { + // print the SSID of the network you're attached to: + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.print(rssi); + Serial.println(" dBm"); +} diff --git a/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h b/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h new file mode 100644 index 00000000..0c9fdd55 --- /dev/null +++ b/libraries/SocketWrapper/examples/WiFiWebClient/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_PASS ""