Skip to content

Commit

Permalink
Merge branch 'master' into mainline
Browse files Browse the repository at this point in the history
  • Loading branch information
gershnik committed Jan 21, 2025
2 parents 86fe161 + 36b71f5 commit dacc6ec
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 10 deletions.
163 changes: 163 additions & 0 deletions .docbuild/Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,63 @@ void loop() {
```

### `if(server)`

#### Description
Indicates whether the server is listening for new clients. You can use this to detect whether server.begin() was successful.


#### Syntax

```
if (server)
if (!server)
```

#### Parameters
none

#### Returns
- whether the server is listening for new clients (bool).

#### Example

```
#include <WiFiNINA.h>
char ssid[] = "Network"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
WiFiServer server(23);
void setup() {
Serial.begin(115200);
while (!Serial) {}
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while(true);
}
server.begin();
if (!server) {
Serial.println("Server failed to start.");
while(true);
}
}
void loop() {
WiFiClient client = server.available();
if (client) {
String s = client.readStringUntil('\n');
server.println(s);
}
}
```

### `server.status()`

#### Description
Expand Down Expand Up @@ -338,6 +395,112 @@ void loop() {
```

### `server.accept()`

#### Description

The traditional server.available() function would only tell you of a new client after it sent data, which makes some protocols like FTP impossible to properly implement.

The intention is programs will use either available() or accept(), but not both. With available(), the client connection continues to be managed by WiFiServer. You don’t need to keep a client object, since calling available() will give you whatever client has sent data. Simple servers can be written with very little code using available().

With accept(), WiFiServer gives you the client only once, regardless of whether it has sent any data. You must keep track of the connected clients. This requires more code, but you gain more control.


#### Syntax

```
server.accept()
```

#### Parameters
none

#### Returns
- a Client object. If no client has data available for reading, this object will evaluate to false in an if-statement. (WiFiClient).

#### Example

```
#include <SPI.h>
#include <WiFiNINA.h>
char ssid[] = "Network"; // your network SSID (name)
char pass[] = "myPassword"; // your network password
int status = WL_IDLE_STATUS;
// telnet defaults to port 23
WiFiServer server(23);
WiFiClient clients[8];
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
}
// attempt to connect to WiFi network:
status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while(true);
}
// start the server:
server.begin();
Serial.print("Chat server address:");
Serial.println(WiFi.localIP());
}
void loop() {
// check for any new client connecting, and say hello (before any incoming data)
WiFiClient newClient = server.accept();
if (newClient) {
for (byte i=0; i < 8; i++) {
if (!clients[i]) {
Serial.print("We have a new client #");
Serial.println(i);
newClient.print("Hello, client number: ");
newClient.println(i);
// Once we "accept", the client is no longer tracked by WiFiServer
// so we must store it into our list of clients
clients[i] = newClient;
break;
}
}
}
// check for incoming data from all clients
for (byte i=0; i < 8; i++) {
if (clients[i] && clients[i].available() > 0) {
// read bytes from a client
byte buffer[80];
int count = clients[i].read(buffer, 80);
// write the bytes to all other connected clients
for (byte j=0; j < 8; j++) {
if (j != i && clients[j].connected()) {
clients[j].write(buffer, count);
}
}
}
}
// stop any clients which disconnect
for (byte i=0; i < 8; i++) {
if (clients[i] && !clients[i].connected()) {
Serial.print("disconnect client #");
Serial.println(i);
clients[i].stop();
}
}
}
```

### `server.peek()`

#### Description
Expand Down
52 changes: 52 additions & 0 deletions .docbuild/WiFi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,58 @@ Serial.println(gateway);

```

### `WiFi.dnsIP()`
```{eval-rst}
.. index:: WiFi::dnsIP (C++ function)
```

#### Description

Returns the DNS server IP address for the device.


#### Syntax

```
WiFi.dnsIP()
WiFi.dnsIP(n)
```

#### Parameters
optional parameter n for the number of the DNS server to get the second DNS serverv

#### Returns
- the DNS server IP address for the device (IPAddress).

#### Example

```cpp

IPAddress emptyIP;

int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a WiFi connection");
while(true);
}

Serial.print("DHCP assigned DNS server: ");
IPAddress dns1 = WiFi.dnsIP();
if (dns1 == emptyIP) {
Serial.println("not set");
} else {
dns1.printTo(Serial);
Serial.println();
IPAddress dns2 = WiFi.dnsIP(1);
if (dns2 != emptyIP) {
Serial.print("DNS server2: ");
dns2.printTo(Serial);
Serial.println();
}
}
```

### `WiFi.getTime()`
```{eval-rst}
.. index:: WiFi::getTime (C++ function)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/check-arduino.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: actions/checkout@v4

- name: Arduino Lint
uses: arduino/arduino-lint-action@v1
uses: arduino/arduino-lint-action@v2
with:
compliance: specification
library-manager: update
Expand Down
114 changes: 114 additions & 0 deletions examples/WiFiAdvancedChatServer/WiFiAdvancedChatServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Advanced WiFi Chat Server
A more advanced server that distributes any incoming messages
to all connected clients but the client the message comes from.
To use, telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Circuit:
* Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)
*/

#include <SPI.h>
#include <WiFiNINA.h>

#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 status = WL_IDLE_STATUS;

// telnet defaults to port 23
WiFiServer server(23);

WiFiClient clients[8];

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_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");
}

if (fv < "1.6.0") {
Serial.println("Advanced WiFi Chat Server requires firmware version 1.6.0 or higher.");
// 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);

// wait 10 seconds for connection:
delay(10000);
}

// start the server:
server.begin();

Serial.print("Chat server address:");
Serial.println(WiFi.localIP());
}

void loop() {
// check for any new client connecting, and say hello (before any incoming data)
WiFiClient newClient = server.accept();
if (newClient) {
for (byte i=0; i < 8; i++) {
if (!clients[i]) {
Serial.print("We have a new client #");
Serial.println(i);
newClient.print("Hello, client number: ");
newClient.println(i);
// Once we "accept", the client is no longer tracked by WiFiServer
// so we must store it into our list of clients
clients[i] = newClient;
break;
}
}
}

// check for incoming data from all clients
for (byte i=0; i < 8; i++) {
if (clients[i] && clients[i].available() > 0) {
// read bytes from a client
byte buffer[80];
int count = clients[i].read(buffer, 80);
// write the bytes to all other connected clients
for (byte j=0; j < 8; j++) {
if (j != i && clients[j].connected()) {
clients[j].write(buffer, count);
}
}
}
}

// stop any clients which disconnect
for (byte i=0; i < 8; i++) {
if (clients[i] && !clients[i].connected()) {
Serial.print("disconnect client #");
Serial.println(i);
clients[i].stop();
}
}

}
2 changes: 2 additions & 0 deletions examples/WiFiAdvancedChatServer/arduino_secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define SECRET_SSID ""
#define SECRET_PASS ""
10 changes: 10 additions & 0 deletions src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ IPAddress WiFiClass::gatewayIP()
return ret;
}

IPAddress WiFiClass::dnsIP(int n)
{
if (n > 1)
return IPAddress(0,0,0,0);
IPAddress dnsip0;
IPAddress dnsip1;
WiFiDrv::getDNS(dnsip0, dnsip1);
return n ? dnsip1 : dnsip0;
}

const char* WiFiClass::SSID()
{
return WiFiDrv::getCurrentSSID();
Expand Down
Loading

0 comments on commit dacc6ec

Please sign in to comment.