Skip to content

Commit 2c40ba8

Browse files
authored
Merge pull request #982 from JAndrassy/wifi_examples
WiFi examples
2 parents afdf359 + f3e524a commit 2c40ba8

File tree

12 files changed

+773
-0
lines changed

12 files changed

+773
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
WiFi Web Server LED Blink
3+
4+
A simple web server that lets you blink an LED via the web.
5+
This sketch will create a new access point (with no password).
6+
It will then launch a new server and print out the IP address
7+
to the Serial Monitor. From there, you can open that address in a web browser
8+
to turn on and off the LED on pin 13.
9+
10+
If the IP address of your board is yourAddress:
11+
http://yourAddress/H turns the LED on
12+
http://yourAddress/L turns it off
13+
14+
created 25 Nov 2012
15+
by Tom Igoe
16+
adapted to WiFi AP by Adafruit
17+
*/
18+
19+
#include <WiFi.h>
20+
#include "arduino_secrets.h"
21+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
22+
char ssid[] = SECRET_SSID; // your network SSID (name)
23+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
24+
int keyIndex = 0; // your network key index number (needed only for WEP)
25+
26+
int led = LED_BUILTIN;
27+
int status = WL_IDLE_STATUS;
28+
WiFiServer server(80);
29+
30+
void setup() {
31+
//Initialize serial and wait for port to open:
32+
Serial.begin(9600);
33+
while (!Serial) {
34+
; // wait for serial port to connect. Needed for native USB port only
35+
}
36+
37+
Serial.println("Access Point Web Server");
38+
39+
pinMode(led, OUTPUT); // set the LED pin mode
40+
41+
// check for the WiFi module:
42+
if (WiFi.status() == WL_NO_MODULE) {
43+
Serial.println("Communication with WiFi module failed!");
44+
// don't continue
45+
while (true);
46+
}
47+
48+
WiFi.config(IPAddress(10, 0, 0, 1));
49+
50+
// print the network name (SSID);
51+
Serial.print("Creating access point named: ");
52+
Serial.println(ssid);
53+
54+
// Create open network. Change this line if you want to create an open network:
55+
status = WiFi.beginAP(ssid, pass);
56+
if (status != WL_AP_LISTENING) {
57+
Serial.println("Creating access point failed");
58+
// don't continue
59+
while (true);
60+
}
61+
62+
// start the web server on port 80
63+
server.begin();
64+
65+
// you're connected now, so print out the status
66+
printWiFiStatus();
67+
}
68+
69+
70+
void loop() {
71+
// compare the previous status to the current status
72+
if (status != WiFi.status()) {
73+
// it has changed update the variable
74+
status = WiFi.status();
75+
76+
if (status == WL_AP_CONNECTED) {
77+
// a device has connected to the AP
78+
Serial.println("Device connected to AP");
79+
} else {
80+
// a device has disconnected from the AP, and we are back in listening mode
81+
Serial.println("Device disconnected from AP");
82+
}
83+
}
84+
85+
WiFiClient client = server.accept(); // listen for incoming clients
86+
87+
if (client) { // if you get a client,
88+
Serial.println("new client"); // print a message out the serial port
89+
String currentLine = ""; // make a String to hold incoming data from the client
90+
while (client.connected()) { // loop while the client's connected
91+
delayMicroseconds(10); // This is required for the Arduino Nano RP2040 Connect - otherwise it will loop so fast that SPI will never be served.
92+
if (client.available()) { // if there's bytes to read from the client,
93+
char c = client.read(); // read a byte, then
94+
Serial.write(c); // print it out to the serial monitor
95+
if (c == '\n') { // if the byte is a newline character
96+
97+
// if the current line is blank, you got two newline characters in a row.
98+
// that's the end of the client HTTP request, so send a response:
99+
if (currentLine.length() == 0) {
100+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
101+
// and a content-type so the client knows what's coming, then a blank line:
102+
client.println("HTTP/1.1 200 OK");
103+
client.println("Content-type:text/html");
104+
client.println();
105+
106+
// the content of the HTTP response follows the header:
107+
client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
108+
client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");
109+
110+
// The HTTP response ends with another blank line:
111+
client.println();
112+
// break out of the while loop:
113+
break;
114+
}
115+
else { // if you got a newline, then clear currentLine:
116+
currentLine = "";
117+
}
118+
}
119+
else if (c != '\r') { // if you got anything else but a carriage return character,
120+
currentLine += c; // add it to the end of the currentLine
121+
}
122+
123+
// Check to see if the client request was "GET /H" or "GET /L":
124+
if (currentLine.endsWith("GET /H")) {
125+
digitalWrite(led, HIGH); // GET /H turns the LED on
126+
}
127+
if (currentLine.endsWith("GET /L")) {
128+
digitalWrite(led, LOW); // GET /L turns the LED off
129+
}
130+
}
131+
}
132+
// close the connection:
133+
client.stop();
134+
Serial.println("client disconnected");
135+
}
136+
}
137+
138+
void printWiFiStatus() {
139+
// print the SSID of the network you're attached to:
140+
Serial.print("SSID: ");
141+
Serial.println(WiFi.SSID());
142+
143+
// print your WiFi shield's IP address:
144+
IPAddress ip = WiFi.localIP();
145+
Serial.print("IP Address: ");
146+
Serial.println(ip);
147+
148+
// print where to go in a browser:
149+
Serial.print("To see this page in action, open a browser to http://");
150+
Serial.println(ip);
151+
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Both SSID and password must be 8 characters or longer
2+
#define SECRET_SSID ""
3+
#define SECRET_PASS ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
WiFi Web Server LED Blink
3+
4+
A simple web server that lets you blink an LED via the web.
5+
This sketch will print the IP address of your WiFi module (once connected)
6+
to the Serial Monitor. From there, you can open that address in a web browser
7+
to turn on and off the LED on pin 9.
8+
9+
If the IP address of your board is yourAddress:
10+
http://yourAddress/H turns the LED on
11+
http://yourAddress/L turns it off
12+
13+
This example is written for a network using WPA encryption. For
14+
WEP or WPA, change the WiFi.begin() call accordingly.
15+
16+
Circuit:
17+
* Board with WiFi
18+
* LED attached to pin 9
19+
20+
created 25 Nov 2012
21+
by Tom Igoe
22+
*/
23+
24+
#include <WiFi.h>
25+
26+
#include "arduino_secrets.h"
27+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
28+
char ssid[] = SECRET_SSID; // your network SSID (name)
29+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
30+
int keyIndex = 0; // your network key index number (needed only for WEP)
31+
32+
int status = WL_IDLE_STATUS;
33+
WiFiServer server(80);
34+
35+
void setup() {
36+
Serial.begin(9600); // initialize serial communication
37+
pinMode(LED_BUILTIN, OUTPUT); // set the LED pin mode
38+
39+
// check for the WiFi module:
40+
if (WiFi.status() == WL_NO_MODULE) {
41+
Serial.println("Communication with WiFi module failed!");
42+
// don't continue
43+
while (true);
44+
}
45+
46+
// attempt to connect to WiFi network:
47+
while (status != WL_CONNECTED) {
48+
Serial.print("Attempting to connect to Network named: ");
49+
Serial.println(ssid); // print the network name (SSID);
50+
51+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
52+
status = WiFi.begin(ssid, pass);
53+
// wait 3 seconds for connection:
54+
delay(3000);
55+
}
56+
server.begin(); // start the web server on port 80
57+
printWifiStatus(); // you're connected now, so print out the status
58+
}
59+
60+
61+
void loop() {
62+
WiFiClient client = server.accept(); // listen for incoming clients
63+
64+
if (client) { // if you get a client,
65+
Serial.println("new client"); // print a message out the serial port
66+
String currentLine = ""; // make a String to hold incoming data from the client
67+
while (client.connected()) { // loop while the client's connected
68+
if (client.available()) { // if there's bytes to read from the client,
69+
char c = client.read(); // read a byte, then
70+
Serial.write(c); // print it out to the serial monitor
71+
if (c == '\n') { // if the byte is a newline character
72+
73+
// if the current line is blank, you got two newline characters in a row.
74+
// that's the end of the client HTTP request, so send a response:
75+
if (currentLine.length() == 0) {
76+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
77+
// and a content-type so the client knows what's coming, then a blank line:
78+
client.println("HTTP/1.1 200 OK");
79+
client.println("Content-type:text/html");
80+
client.println();
81+
82+
// the content of the HTTP response follows the header:
83+
client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
84+
client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");
85+
86+
// The HTTP response ends with another blank line:
87+
client.println();
88+
// break out of the while loop:
89+
break;
90+
} else { // if you got a newline, then clear currentLine:
91+
currentLine = "";
92+
}
93+
} else if (c != '\r') { // if you got anything else but a carriage return character,
94+
currentLine += c; // add it to the end of the currentLine
95+
}
96+
97+
// Check to see if the client request was "GET /H" or "GET /L":
98+
if (currentLine.endsWith("GET /H")) {
99+
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
100+
}
101+
if (currentLine.endsWith("GET /L")) {
102+
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
103+
}
104+
}
105+
}
106+
// close the connection:
107+
client.stop();
108+
Serial.println("client disconnected");
109+
}
110+
}
111+
112+
void printWifiStatus() {
113+
// print the SSID of the network you're attached to:
114+
Serial.print("SSID: ");
115+
Serial.println(WiFi.SSID());
116+
117+
// print your board's IP address:
118+
IPAddress ip = WiFi.localIP();
119+
Serial.print("IP Address: ");
120+
Serial.println(ip);
121+
122+
// print the received signal strength:
123+
long rssi = WiFi.RSSI();
124+
Serial.print("signal strength (RSSI):");
125+
Serial.print(rssi);
126+
Serial.println(" dBm");
127+
// print where to go in a browser:
128+
Serial.print("To see this page in action, open a browser to http://");
129+
Serial.println(ip);
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Advanced WiFi Chat Server
3+
4+
A more advanced server that distributes any incoming messages
5+
to all connected clients but the client the message comes from.
6+
To use, telnet to your device's IP address and type.
7+
8+
Circuit:
9+
* Board with WiFi
10+
11+
*/
12+
13+
#include <WiFi.h>
14+
15+
#include "arduino_secrets.h"
16+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
17+
char ssid[] = SECRET_SSID; // your network SSID (name)
18+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
19+
20+
int status = WL_IDLE_STATUS;
21+
22+
// telnet defaults to port 23
23+
WiFiServer server(23);
24+
25+
WiFiClient clients[8];
26+
27+
void setup() {
28+
//Initialize serial and wait for port to open:
29+
Serial.begin(9600);
30+
while (!Serial) {
31+
; // wait for serial port to connect. Needed for native USB port only
32+
}
33+
34+
// check for the WiFi module:
35+
if (WiFi.status() == WL_NO_MODULE) {
36+
Serial.println("Communication with WiFi module failed!");
37+
// don't continue
38+
while (true);
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 network:
46+
status = WiFi.begin(ssid, pass);
47+
48+
// wait 3 seconds for connection:
49+
delay(3000);
50+
}
51+
52+
// start the server:
53+
server.begin();
54+
55+
Serial.print("Chat server address:");
56+
Serial.println(WiFi.localIP());
57+
}
58+
59+
void loop() {
60+
// check for any new client connecting, and say hello (before any incoming data)
61+
WiFiClient newClient = server.accept();
62+
if (newClient) {
63+
for (byte i=0; i < 8; i++) {
64+
if (!clients[i]) {
65+
Serial.print("We have a new client #");
66+
Serial.println(i);
67+
newClient.print("Hello, client number: ");
68+
newClient.println(i);
69+
// Once we "accept", the client is no longer tracked by WiFiServer
70+
// so we must store it into our list of clients
71+
clients[i] = newClient;
72+
break;
73+
}
74+
}
75+
}
76+
77+
// check for incoming data from all clients
78+
for (byte i=0; i < 8; i++) {
79+
if (clients[i] && clients[i].available() > 0) {
80+
// read bytes from a client
81+
byte buffer[80];
82+
int count = clients[i].read(buffer, 80);
83+
// write the bytes to all other connected clients
84+
for (byte j=0; j < 8; j++) {
85+
if (j != i && clients[j].connected()) {
86+
clients[j].write(buffer, count);
87+
}
88+
}
89+
}
90+
}
91+
92+
// stop any clients which disconnect
93+
for (byte i=0; i < 8; i++) {
94+
if (clients[i] && !clients[i].connected()) {
95+
Serial.print("disconnect client #");
96+
Serial.println(i);
97+
clients[i].stop();
98+
}
99+
}
100+
101+
}
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)