Skip to content

Commit 999f4aa

Browse files
committed
added WiFiPagerServer example
1 parent 8816d8a commit 999f4aa

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
WiFi Pager Server
3+
4+
The example is a simple server that echoes any incoming
5+
messages to all connected clients. Connect two or more
6+
telnet sessions to see how server.available() and
7+
server.print() work.
8+
9+
Circuit:
10+
* Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)
11+
12+
*/
13+
14+
#include <WiFiNINA.h>
15+
16+
#include "arduino_secrets.h"
17+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
18+
char ssid[] = SECRET_SSID; // your network SSID (name)
19+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
20+
21+
int status = WL_IDLE_STATUS;
22+
23+
WiFiServer server(23);
24+
25+
void setup() {
26+
27+
//Initialize serial and wait for port to open:
28+
Serial.begin(9600);
29+
while (!Serial) {
30+
; // wait for serial port to connect. Needed for native USB port only
31+
}
32+
33+
// check for the WiFi module:
34+
if (WiFi.status() == WL_NO_MODULE) {
35+
Serial.println("Communication with WiFi module failed!");
36+
// don't continue
37+
while (true);
38+
}
39+
40+
String fv = WiFi.firmwareVersion();
41+
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
42+
Serial.println("Please upgrade the firmware");
43+
}
44+
45+
// attempt to connect to WiFi network:
46+
while (status != WL_CONNECTED) {
47+
Serial.print("Attempting to connect to SSID: ");
48+
Serial.println(ssid);
49+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
50+
status = WiFi.begin(ssid, pass);
51+
52+
// wait 10 seconds for connection:
53+
delay(10000);
54+
}
55+
56+
server.begin();
57+
58+
IPAddress ip = WiFi.localIP();
59+
Serial.println();
60+
Serial.println("Connected to WiFi network.");
61+
Serial.print("To access the server, connect with Telnet client to ");
62+
Serial.print(ip);
63+
Serial.println(" 23");
64+
}
65+
66+
void loop() {
67+
68+
WiFiClient client = server.available(); // returns first client which has data to read or a 'false' client
69+
if (client) { // client is true only if it is connected and has data to read
70+
String s = client.readStringUntil('\n'); // read the message incoming from one of the clients
71+
s.trim(); // trim eventual \r
72+
Serial.println(s); // print the message to Serial Monitor
73+
client.print("echo: "); // this is only for the sending client
74+
server.println(s); // send the message to all connected clients
75+
server.flush(); // flush the buffers
76+
}
77+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""

0 commit comments

Comments
 (0)