Skip to content

Commit 7ad5e5c

Browse files
committed
WiFiC3 example WiFiPagerServer
to show how server.available() and server.print() work
1 parent 9a838ba commit 7ad5e5c

File tree

2 files changed

+75
-0
lines changed

2 files changed

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