### Example: Initialize WiFi and Start Server with Port Argument Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Connect to a WiFi network and start the WiFi server on a specific port. This example shows the usage of server.begin(port). ```arduino #include #include char ssid[] = "lamaison"; // your network SSID (name) char pass[] = "tenantaccess247"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { // initialize serial: Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); while(true); } else { server.begin(); Serial.print("Connected to WiFi. My address:"); IPAddress myAddress = WiFi.localIP(); Serial.println(myAddress); } } void loop() { } ``` -------------------------------- ### Initialize WiFi and Start Server Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Connect to a WiFi network and start the WiFi server. This example demonstrates the typical setup sequence including error handling for WiFi connection. ```arduino #include #include char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { // initialize serial: Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); while(true); } else { server.begin(); Serial.print("Connected to WiFi. My address:"); IPAddress myAddress = WiFi.localIP(); Serial.println(myAddress); } } void loop() { } ``` -------------------------------- ### WiFiUDP Multicast Setup Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Example demonstrating the initialization of a WiFi connection and starting a multicast UDP socket. ```cpp … WiFiUDP udp; void setup() { Serial.begin(9600); while (!Serial) { ; } if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); // wait 10 seconds for connection: delay(10000); } Serial.println("Connected to WiFi"); printWifiStatus(); Serial.println("\nStarting connection to server..."); udp.beginMulticast(IPAddress(226, 1, 1, 1), 4096); Serial.println("\n connected UDP multicast"); … ``` -------------------------------- ### Basic WiFi Server Setup and Client Handling Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This example demonstrates setting up a WiFi server, connecting to a network, and handling incoming client connections. It shows how to initialize the server, listen for clients, and close connections. ```arduino #include #include char ssid[] = "Network"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { // initialize serial: Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); while(true); } else { server.begin(); Serial.print("Connected to WiFi. My address:"); IPAddress myAddress = WiFi.localIP(); Serial.println(myAddress); } } void loop() { // listen for incoming clients WiFiClient client = server.available(); if (client) { if (client.connected()) { Serial.println("Connected to client"); } // close the connection: client.stop(); } } ``` -------------------------------- ### Server Initialization and Status Check Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Connect to WiFi, start the server, and check if it's listening. This example includes error handling for server startup failure. ```arduino #include 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 Write Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Connect to WiFi, start the server, and write incoming client data to all connected clients. This example demonstrates broadcasting received data. ```arduino #include #include char ssid[] = "yourNetwork"; char pass[] = "yourPassword"; int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { // initialize serial: Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); while(true); } else { server.begin(); } } void loop() { // listen for incoming clients WiFiClient client = server.available(); if (client == true) { // read bytes from the incoming client and write them back // to any clients connected to the server: server.write(client.read()); } } ``` -------------------------------- ### Accept New Clients with WiFiServer Source: https://context7.com/arduino-libraries/wifinina/llms.txt Use server.accept() to get new clients without waiting for data. Manually manage client objects in an array. This example sets up a basic chat server. ```cpp #include #include char ssid[] = "yourNetwork"; char pass[] = "secretPassword"; int status = WL_IDLE_STATUS; WiFiServer server(23); // Telnet server on port 23 WiFiClient clients[8]; // Track up to 8 clients void setup() { Serial.begin(9600); while (!Serial); if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } status = WiFi.begin(ssid, pass); if (status != WL_CONNECTED) { Serial.println("Couldn't connect to WiFi"); while (true); } server.begin(); Serial.print("Chat server at: "); Serial.print(WiFi.localIP()); Serial.println(":23"); } void loop() { // Check for new clients WiFiClient newClient = server.accept(); if (newClient) { for (byte i = 0; i < 8; i++) { if (!clients[i]) { Serial.print("New client #"); Serial.println(i); newClient.print("Welcome, client #"); newClient.println(i); 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) { byte buffer[80]; int count = clients[i].read(buffer, 80); // Broadcast to all other clients for (byte j = 0; j < 8; j++) { if (j != i && clients[j].connected()) { clients[j].write(buffer, count); } } } } // Clean up disconnected clients for (byte i = 0; i < 8; i++) { if (clients[i] && !clients[i].connected()) { Serial.print("Client #"); Serial.print(i); Serial.println(" disconnected"); clients[i].stop(); } } } ``` -------------------------------- ### Arduino WiFiNINA Chat Server Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This example demonstrates how to create a simple chat server using the WiFiNINA library. It handles multiple clients, accepts new connections, reads incoming data, and broadcasts it to other connected clients. Ensure your network SSID and password are correctly configured. ```cpp #include #include 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(); } } } ``` -------------------------------- ### Initialize WiFi connection Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Syntax examples for connecting to various types of WiFi networks. ```cpp WiFi.begin(ssid); WiFi.begin(ssid, pass); WiFi.begin(ssid, keyIndex, key); ``` -------------------------------- ### WiFiClient Connection Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Demonstrates connecting to a network and performing an HTTP request using WiFiClient. ```cpp #include #include char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google // Initialize the client library WiFiClient client; void setup() { Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); // don't do anything else: while(true); } else { Serial.println("Connected to WiFi"); Serial.println("\nStarting connection..."); // if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.0"); client.println(); } } } void loop() { } ``` -------------------------------- ### Initialize Access Point mode Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Syntax examples for configuring the module to act as a WiFi access point. ```cpp WiFi.beginAP(ssid); WiFi.beginAP(ssid, channel); WiFi.beginAP(ssid, passphrase); WiFi.beginAP(ssid, passphrase, channel); ``` -------------------------------- ### Arduino WiFiNINA server.peek() Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This example demonstrates the `server.peek()` function, which reads the next byte from the client's buffer without removing it. This is useful for inspecting incoming data before deciding whether to read it. Note that `peek()` returns -1 if no data is available. ```cpp #include #include char ssid[] = SECRET_SSID; // your network SSID (name) char pass[] = SECRET_PASS; // your network password int status = WL_IDLE_STATUS; WiFiServer server(80); void setup() { Serial.begin(9600); while (!Serial) { } if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); // don't continue while (true); } while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); } server.begin(); } void loop() { // listen for incoming clients WiFiClient client = server.available(); if (client) { Serial.println("new client"); // an http request ends with a blank line boolean currentLineIsBlank = true; int i = 0; while (client.connected()) { if (client.available()) { char c = client.peek(); Serial.print("peek: "); Serial.println(c); Serial.print("calling a second time peek, the char is the same: "); c = client.peek(); Serial.println(c); Serial.print("calling the read retry the char and erase from the buffer: "); c = client.read(); Serial.println(c); if (i == 2) { while (1); } i++; if (c == '\n') { // you're starting a new line currentLineIsBlank = true; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } } ``` -------------------------------- ### Connect to a WPA network Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md A complete example demonstrating how to connect to a WPA-secured network. ```cpp #include //SSID of your network char ssid[] = "yourNetwork"; //password of your WPA Network char pass[] = "secretPassword"; void setup() { WiFi.begin(ssid, pass); } void loop () {} ``` -------------------------------- ### Full WiFi Ping Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This example connects to a WiFi network and continuously pings a specified host. It requires network credentials to be set in 'arduino_secrets.h'. ```arduino /* This example connects to an encrypted WiFi network (WPA/WPA2). Then it prints the MAC address of the board, the IP address obtained, and other network details. Then it continuously pings given host specified by IP Address or name. Circuit: * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and Uno WiFi Rev.2) created 13 July 2010 by dlf (Metodo2 srl) modified 09 June 2016 by Petar Georgiev */ #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 status = WL_IDLE_STATUS; // the WiFi radio's status // Specify IP address or hostname String hostName = "www.google.com"; int pingResult; 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"); } // attempt to connect to WiFi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to WPA SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network: status = WiFi.begin(ssid, pass); // wait 5 seconds for connection: delay(5000); } // you're connected now, so print out the data: Serial.println("You're connected to the network"); printCurrentNet(); printWiFiData(); } void loop() { Serial.print("Pinging "); Serial.print(hostName); Serial.print(": "); pingResult = WiFi.ping(hostName); if (pingResult >= 0) { Serial.print("SUCCESS! RTT = "); Serial.print(pingResult); Serial.println(" ms"); } else { Serial.print("FAILED! Error code: "); Serial.println(pingResult); } delay(5000); } void printWiFiData() { // print your board's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP address : "); Serial.println(ip); Serial.print("Subnet mask: "); Serial.println((IPAddress)WiFi.subnetMask()); Serial.print("Gateway IP : "); Serial.println((IPAddress)WiFi.gatewayIP()); // print your MAC address: byte mac[6]; WiFi.macAddress(mac); Serial.print("MAC address: "); printMacAddress(mac); } void printCurrentNet() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // print the MAC address of the router you're attached to: byte bssid[6]; WiFi.BSSID(bssid); Serial.print("BSSID: "); printMacAddress(bssid); // print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI): "); Serial.println(rssi); // print the encryption type: byte encryption = WiFi.encryptionType(); Serial.print("Encryption Type: "); Serial.println(encryption, HEX); Serial.println(); } void printMacAddress(byte mac[]) { for (int i = 5; i >= 0; i--) { if (mac[i] < 16) { Serial.print("0"); } Serial.print(mac[i], HEX); if (i > 0) { Serial.print(":"); } } Serial.println(); } ``` -------------------------------- ### Start WiFiServer Listening Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Begin listening for incoming connections on the server. This function is called after successfully connecting to WiFi. ```arduino server.begin() ``` -------------------------------- ### Connect and Transfer Data with SSL Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This example demonstrates how to create a client object that connects and transfers data using SSL. It is compatible with methods for plain connections. Ensure your network credentials are in 'arduino_secrets.h'. ```cpp #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(74,125,232,128); // numeric IP for Google (no DNS) char server[] = "www.google.com"; // name address for Google (using DNS) // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): WiFiSSLClient 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_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(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); } 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, 443)) { Serial.println("connected to server"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: www.google.com"); 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"); } ``` -------------------------------- ### GET /H and GET /L Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Endpoints used to control the state of an LED via HTTP requests. ```APIDOC ## GET /H ### Description Turns the LED on. ### Method GET ### Endpoint /H ## GET /L ### Description Turns the LED off. ### Method GET ### Endpoint /L ``` -------------------------------- ### Start WiFiServer with Specific Port Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Begin listening for incoming connections on a specified port. This overload allows dynamic port selection. ```arduino server.begin(port) ``` -------------------------------- ### Initialize WiFiUDP Multicast Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Starts a WiFiUDP socket listening on a specific port and multicast IP address. ```cpp WiFiUDP.beginMulticast(IPAddress, port); ``` -------------------------------- ### WiFiUDP.beginPacket Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Starts a connection to write UDP data to the remote connection. ```APIDOC ## WiFiUDP.beginPacket ### Description Starts a connection to write UDP data to the remote connection. ### Parameters #### Path Parameters - **hostName** (string/IPAddress) - Required - The address of the remote host - **hostIp** (4 bytes) - Required - The IP address of the remote connection - **port** (int) - Required - The port of the remote connection ### Returns - 1: if successful - 0: if there was a problem with the supplied IP address or port ``` -------------------------------- ### Connect to WiFi and Server Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Connects to a WiFi network and then establishes a connection to a specified server to send an HTTP GET request. Requires network credentials and server details. ```cpp #include #include #include "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; char server[] = "www.google.com"; // name address for Google (using DNS) WiFiClient client; void setup() { //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; } if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); } Serial.println("Connected to WiFi"); printWifiStatus(); Serial.println("\nStarting connection to server..."); IPAddress result; int err = WiFi.hostByName(server, result) ; if (err == 1) { Serial.print("IP address: "); Serial.println(result); } else { Serial.print("Error code: "); Serial.println(err); } if (client.connect(result, 80)) { Serial.println("connected to server"); client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: www.google.com"); client.println("Connection: close"); client.println(); } } void loop() { int i = 0; while (client.available()) { char c = client.peek(); Serial.print("peek: "); Serial.println(c); Serial.print("calling a second time peek, the char is the same: "); c = client.peek(); Serial.println(c); Serial.print("calling the read retry the char and erase from the buffer: "); c = client.read(); Serial.println(c); if (i == 2) { while (1); } i++; } } ``` -------------------------------- ### Check if Server is Listening Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Check if the server has successfully started listening for clients. This is useful for verifying the success of server.begin(). ```arduino if (server) ``` -------------------------------- ### Connect to WiFi and Server Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Connects to a WiFi network and then to a server, sending an HTTP GET request. Requires network credentials and server details. ```arduino void setup() { Serial.begin(9600); while (!Serial) { ; } if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); } Serial.println("Connected to WiFi"); printWifiStatus(); Serial.println("\nStarting connection to server..."); IPAddress result; int err = WiFi.hostByName(server, result) ; if (err == 1) { Serial.print("IP address: "); Serial.println(result); } else { Serial.print("Error code: "); Serial.println(err); } if (client.connect(result, 80)) { Serial.println("connected to server"); client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: www.google.com"); client.println("Connection: close"); client.println(); } Serial.print("status: "); Serial.println(client.status()); } … ``` -------------------------------- ### Arduino WiFiNINA WiFiUDP Class Initialization Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This section describes the initialization of the WiFi UDP class. The `WiFiUDP` object is created to enable sending and receiving UDP messages. The `begin()` method is used to initialize the library and network settings, starting a UDP socket on a specified local port. ```cpp WiFiUDP ``` ```cpp WiFiUDP.begin(port) ``` -------------------------------- ### WiFiServer Source: https://context7.com/arduino-libraries/wifinina/llms.txt Creates a TCP server that listens for incoming client connections on a specified port. Use available() to get clients with data ready to read, or accept() for immediate notification of new connections. ```APIDOC ## WiFiServer ### Description Creates a TCP server that listens for incoming client connections on a specified port. Use `available()` to get clients with data ready to read, or `accept()` for immediate notification of new connections. ### Method `WiFiServer server(port)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```cpp #include #include char ssid[] = "yourNetwork"; char pass[] = "secretPassword"; int status = WL_IDLE_STATUS; WiFiServer server(80); // HTTP server on port 80 void setup() { Serial.begin(9600); while (!Serial); if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } while (status != WL_CONNECTED) { Serial.print("Connecting to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); } server.begin(); Serial.print("Server started at: http://"); Serial.println(WiFi.localIP()); } void loop() { WiFiClient client = server.available(); if (client) { Serial.println("New client connected"); String currentLine = ""; while (client.connected()) { if (client.available()) { char c = client.read(); if (c == '\n') { if (currentLine.length() == 0) { // End of HTTP headers, send response client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println(); client.println(""); client.println("

Arduino Web Server

"); client.print("

Uptime: "); client.print(millis() / 1000); client.println(" seconds

"); client.println(""); client.println(); break; } else { currentLine = ""; } } else if (c != '\r') { currentLine += c; } } } client.stop(); Serial.println("Client disconnected"); } } ``` ### Response #### Success Response (200) Indicates a new client connection has been established or data is available. #### Response Example `New client connected` ``` -------------------------------- ### Check if Server is NOT Listening Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Check if the server has failed to start listening for clients. Use this to handle server initialization errors. ```arduino if (!server) ``` -------------------------------- ### Full Network Scan Example Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Scans for networks every ten seconds and prints details including MAC address and encryption type. ```cpp /* This example prints the board's MAC address, and scans for available WiFi networks using the NINA module. Every ten seconds, it scans again. It doesn't actually connect to any network, so no encryption scheme is specified. Circuit: * Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and Uno WiFi Rev.2) created 13 July 2010 by dlf (Metodo2 srl) modified 21 Junn 2012 by Tom Igoe and Jaymes Dec */ #include #include 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"); } // print your MAC address: byte mac[6]; WiFi.macAddress(mac); Serial.print("MAC: "); printMacAddress(mac); } void loop() { // scan for existing networks: Serial.println("Scanning available networks..."); listNetworks(); delay(10000); } void listNetworks() { // scan for nearby networks: Serial.println("** Scan Networks **"); int numSsid = WiFi.scanNetworks(); if (numSsid == -1) { Serial.println("Couldn't get a WiFi connection"); while (true); } // print the list of networks seen: Serial.print("number of available networks:"); Serial.println(numSsid); // print the network number and name for each network found: for (int thisNet = 0; thisNet < numSsid; thisNet++) { Serial.print(thisNet); Serial.print(") "); Serial.print(WiFi.SSID(thisNet)); Serial.print("\tSignal: "); Serial.print(WiFi.RSSI(thisNet)); Serial.print(" dBm"); Serial.print("\tEncryption: "); printEncryptionType(WiFi.encryptionType(thisNet)); } } void printEncryptionType(int thisType) { // read the encryption type and print out the name: switch (thisType) { case ENC_TYPE_WEP: Serial.println("WEP"); break; case ENC_TYPE_TKIP: Serial.println("WPA"); break; case ENC_TYPE_CCMP: Serial.println("WPA2"); break; case ENC_TYPE_NONE: Serial.println("None"); break; case ENC_TYPE_AUTO: Serial.println("Auto"); break; case ENC_TYPE_UNKNOWN: default: Serial.println("Unknown"); break; } } void printMacAddress(byte mac[]) { for (int i = 5; i >= 0; i--) { if (mac[i] < 16) { Serial.print("0"); } Serial.print(mac[i], HEX); if (i > 0) { Serial.print(":"); } } Serial.println(); } ``` -------------------------------- ### Check WiFi Client Connection Status Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md This example shows how to check if a WiFi client is connected. A client is considered connected even if the connection is closed but unread data is available. Ensure your network credentials are correct. ```cpp #include #include char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; IPAddress server(74,125,115,105); // Google // Initialize the client library WiFiClient client; void setup() { Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); // don't do anything else: while(true); } else { Serial.println("Connected to WiFi"); Serial.println("\nStarting connection..."); // if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.0"); client.println(); } } } void loop() { if (client.available()) { char c = client.read(); Serial.print(c); } if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); for(;;) ; } } ``` -------------------------------- ### Read incoming data from server Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Reads incoming bytes from the server and prints them to the serial monitor. This example demonstrates reading data in a loop until the server disconnects. ```arduino #include #include char ssid[] = "myNetwork"; // your network SSID (name) char pass[] = "myPassword"; // your network password int status = WL_IDLE_STATUS; char servername[]="google.com"; // Google WiFiClient client; void setup() { Serial.begin(9600); Serial.println("Attempting to connect to WPA network..."); Serial.print("SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a WiFi connection"); // don't do anything else: while(true); } else { Serial.println("Connected to WiFi"); Serial.println("\nStarting connection..."); // if you get a connection, report back via serial: if (client.connect(servername, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.0"); client.println(); } } } void loop() { // if there are incoming bytes available // from the server, read them and print them: if (client.available()) { char c = client.read(); Serial.print(c); } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); // do nothing forevermore: for(;;) ; } } ``` -------------------------------- ### Send and Receive UDP Packets with WiFiUDP Source: https://context7.com/arduino-libraries/wifinina/llms.txt Utilize WiFiUDP for connectionless UDP communication. This example demonstrates sending NTP requests to get current time and parsing the response. ```cpp #include #include #include char ssid[] = "yourNetwork"; char pass[] = "secretPassword"; int status = WL_IDLE_STATUS; unsigned int localPort = 2390; IPAddress timeServer(162, 159, 200, 123); // NTP server const int NTP_PACKET_SIZE = 48; byte packetBuffer[NTP_PACKET_SIZE]; WiFiUDP Udp; void setup() { Serial.begin(9600); while (!Serial); if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } while (status != WL_CONNECTED) { Serial.print("Connecting to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); } Serial.println("Connected to WiFi"); Udp.begin(localPort); } void loop() { sendNTPpacket(timeServer); delay(1000); if (Udp.parsePacket()) { Serial.println("NTP packet received"); Udp.read(packetBuffer, NTP_PACKET_SIZE); unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long secsSince1900 = highWord << 16 | lowWord; const unsigned long seventyYears = 2208988800UL; unsigned long epoch = secsSince1900 - seventyYears; Serial.print("Unix time: "); Serial.println(epoch); Serial.print("UTC time: "); Serial.print((epoch % 86400L) / 3600); Serial.print(':'); if (((epoch % 3600) / 60) < 10) Serial.print('0'); Serial.print((epoch % 3600) / 60); Serial.print(':'); if ((epoch % 60) < 10) Serial.print('0'); Serial.println(epoch % 60); } delay(10000); } void sendNTPpacket(IPAddress& address) { memset(packetBuffer, 0, NTP_PACKET_SIZE); packetBuffer[0] = 0b11100011; // LI, Version, Mode packetBuffer[1] = 0; // Stratum packetBuffer[2] = 6; // Polling Interval packetBuffer[3] = 0xEC; // Peer Clock Precision packetBuffer[12] = 49; packetBuffer[13] = 0x4E; packetBuffer[14] = 49; packetBuffer[15] = 52; Udp.beginPacket(address, 123); Udp.write(packetBuffer, NTP_PACKET_SIZE); Udp.endPacket(); } ``` -------------------------------- ### WiFi.begin() Source: https://context7.com/arduino-libraries/wifinina/llms.txt Initializes the WiFiNINA library and connects to a specified WiFi network. ```APIDOC ## WiFi.begin() ### Description Initializes the WiFiNINA library and connects to a WiFi network. Supports open networks, WEP encryption, and WPA/WPA2 encryption. ### Parameters - **ssid** (char[]) - Required - The network name. - **pass** (char[]) - Optional - The network password (WPA/WPA2). ### Response - **status** (int) - Returns WL_CONNECTED on success or WL_IDLE_STATUS if not connected. ``` -------------------------------- ### WiFi.begin() Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Initializes the WiFiNINA library's network settings and provides the current status. Use this to connect to an existing WiFi network. ```APIDOC ## WiFi.begin() ### Description Initializes the WiFiNINA library's network settings and provides the current status. ### Method WiFi.begin(ssid) WiFi.begin(ssid, pass) WiFi.begin(ssid, keyIndex, key) ### Parameters - **ssid** (string) - Required - The SSID (Service Set Identifier) is the name of the WiFi network you want to connect to. - **pass** (string) - Required - WPA encrypted networks use a password in the form of a string for security. - **keyIndex** (integer) - Optional - WEP encrypted networks can hold up to 4 different keys. This identifies which key you are going to use. - **key** (string) - Optional - A hexadecimal string used as a security code for WEP encrypted networks. ### Returns - WL_CONNECTED: when connected to a network - WL_IDLE_STATUS: when not connected to a network, but powered on ### Request Example ```cpp #include //SSID of your network char ssid[] = "yourNetwork"; //password of your WPA Network char pass[] = "secretPassword"; void setup() { WiFi.begin(ssid, pass); } void loop () {} ``` ``` -------------------------------- ### Connect to a WiFi Network with WiFi.begin() Source: https://context7.com/arduino-libraries/wifinina/llms.txt Initializes the WiFi module and establishes a connection to a WPA/WPA2 network. Includes checks for module presence and firmware version compatibility. ```cpp #include #include char ssid[] = "yourNetwork"; // Network SSID (name) char pass[] = "secretPassword"; // Network password (WPA) int status = WL_IDLE_STATUS; void setup() { Serial.begin(9600); while (!Serial); // Wait for serial port // Check for WiFi module if (WiFi.status() == WL_NO_MODULE) { Serial.println("Communication with WiFi module failed!"); while (true); } // Check firmware version String fv = WiFi.firmwareVersion(); if (fv < WIFI_FIRMWARE_LATEST_VERSION) { Serial.println("Please upgrade the firmware"); } // Connect to WPA/WPA2 network while (status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); status = WiFi.begin(ssid, pass); delay(10000); // Wait 10 seconds for connection } Serial.println("Connected to WiFi"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } void loop() {} ``` -------------------------------- ### WiFiUDP.remoteIP Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Gets the IP address of the remote connection. ```APIDOC ## WiFiUDP.remoteIP ### Description Gets the IP address of the remote connection. Must be called after WiFiUDP.parsePacket(). ### Returns - 4 bytes: the IP address of the host who sent the current incoming packet ``` -------------------------------- ### WiFiUDP.available Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Gets the number of bytes available for reading from the buffer. ```APIDOC ## WiFiUDP.available ### Description Get the number of bytes available for reading from the buffer. This function can only be successfully called after WiFiUDP.parsePacket(). ### Returns - the number of bytes available in the current packet - 0: if WiFiUDP.parsePacket() hasn't been called yet ``` -------------------------------- ### server.begin() Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Tells the server to begin listening for incoming connections. ```APIDOC ## server.begin() ### Description Tells the server to begin listening for incoming connections. ### Parameters - **port** (int) - Optional - The port to listen on. ``` -------------------------------- ### WiFiUDP.parsePacket Source: https://github.com/arduino-libraries/wifinina/blob/master/docs/api.md Starts processing the next available incoming packet. ```APIDOC ## WiFiUDP.parsePacket ### Description It starts processing the next available incoming packet, checks for the presence of a UDP packet, and reports the size. Must be called before reading the buffer. ### Returns - the size of the packet in bytes - 0: if no packets are available ```