Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFiServer operator bool #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2764,6 +2764,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
3 changes: 3 additions & 0 deletions src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ uint8_t WiFiServer::status() {
}
}

WiFiServer::operator bool() {
return (_sock != NO_SOCKET_AVAIL);
}

size_t WiFiServer::write(uint8_t b)
{
Expand Down
1 change: 1 addition & 0 deletions src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class WiFiServer : public Server {
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
uint8_t status();
explicit operator bool();

using Print::write;
};
Expand Down