Skip to content

Commit a4b9773

Browse files
committed
Port WiFi101 examples over
1 parent 9676cfe commit a4b9773

File tree

7 files changed

+580
-0
lines changed

7 files changed

+580
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 shield 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 <SPI.h>
20+
#include <WiFi1010.h>
21+
#include "arduino_secrets.h"
22+
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
23+
char ssid[] = SECRET_SSID; // your network SSID (name)
24+
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
25+
int keyIndex = 0; // your network key Index number (needed only for WEP)
26+
27+
int led = LED_BUILTIN;
28+
int status = WL_IDLE_STATUS;
29+
WiFiServer server(80);
30+
31+
void setup() {
32+
//Initialize serial and wait for port to open:
33+
Serial.begin(9600);
34+
while (!Serial) {
35+
; // wait for serial port to connect. Needed for native USB port only
36+
}
37+
38+
Serial.println("Access Point Web Server");
39+
40+
pinMode(led, OUTPUT); // set the LED pin mode
41+
42+
// check for the WiFi module:
43+
if (WiFi.status() == WL_NO_MODULE) {
44+
Serial.println("Communication with WiFi module failed!");
45+
// don't continue
46+
while (true);
47+
}
48+
49+
String fv = WiFi.firmwareVersion();
50+
if (fv != "1.0.0") {
51+
Serial.println("Please upgrade the firmware");
52+
}
53+
54+
// by default the local IP address of will be 192.168.4.1
55+
// you can override it with the following:
56+
// WiFi.config(IPAddress(10, 0, 0, 1));
57+
58+
// print the network name (SSID);
59+
Serial.print("Creating access point named: ");
60+
Serial.println(ssid);
61+
62+
// Create open network. Change this line if you want to create an WEP network:
63+
status = WiFi.beginAP(ssid, pass);
64+
if (status != WL_AP_LISTENING) {
65+
Serial.println("Creating access point failed");
66+
// don't continue
67+
while (true);
68+
}
69+
70+
// wait 10 seconds for connection:
71+
delay(10000);
72+
73+
// start the web server on port 80
74+
server.begin();
75+
76+
// you're connected now, so print out the status
77+
printWiFiStatus();
78+
}
79+
80+
81+
void loop() {
82+
// compare the previous status to the current status
83+
if (status != WiFi.status()) {
84+
// it has changed update the variable
85+
status = WiFi.status();
86+
87+
if (status == WL_AP_CONNECTED) {
88+
// a device has connected to the AP
89+
Serial.println("Device connected to AP");
90+
} else {
91+
// a device has disconnected from the AP, and we are back in listening mode
92+
Serial.println("Device disconnected from AP");
93+
}
94+
}
95+
96+
WiFiClient client = server.available(); // listen for incoming clients
97+
98+
if (client) { // if you get a client,
99+
Serial.println("new client"); // print a message out the serial port
100+
String currentLine = ""; // make a String to hold incoming data from the client
101+
while (client.connected()) { // loop while the client's connected
102+
if (client.available()) { // if there's bytes to read from the client,
103+
char c = client.read(); // read a byte, then
104+
Serial.write(c); // print it out the serial monitor
105+
if (c == '\n') { // if the byte is a newline character
106+
107+
// if the current line is blank, you got two newline characters in a row.
108+
// that's the end of the client HTTP request, so send a response:
109+
if (currentLine.length() == 0) {
110+
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
111+
// and a content-type so the client knows what's coming, then a blank line:
112+
client.println("HTTP/1.1 200 OK");
113+
client.println("Content-type:text/html");
114+
client.println();
115+
116+
// the content of the HTTP response follows the header:
117+
client.print("Click <a href=\"/H\">here</a> turn the LED on<br>");
118+
client.print("Click <a href=\"/L\">here</a> turn the LED off<br>");
119+
120+
// The HTTP response ends with another blank line:
121+
client.println();
122+
// break out of the while loop:
123+
break;
124+
}
125+
else { // if you got a newline, then clear currentLine:
126+
currentLine = "";
127+
}
128+
}
129+
else if (c != '\r') { // if you got anything else but a carriage return character,
130+
currentLine += c; // add it to the end of the currentLine
131+
}
132+
133+
// Check to see if the client request was "GET /H" or "GET /L":
134+
if (currentLine.endsWith("GET /H")) {
135+
digitalWrite(led, HIGH); // GET /H turns the LED on
136+
}
137+
if (currentLine.endsWith("GET /L")) {
138+
digitalWrite(led, LOW); // GET /L turns the LED off
139+
}
140+
}
141+
}
142+
// close the connection:
143+
client.stop();
144+
Serial.println("client disconnected");
145+
}
146+
}
147+
148+
void printWiFiStatus() {
149+
// print the SSID of the network you're attached to:
150+
Serial.print("SSID: ");
151+
Serial.println(WiFi.SSID());
152+
153+
// print your WiFi shield's IP address:
154+
IPAddress ip = WiFi.localIP();
155+
Serial.print("IP Address: ");
156+
Serial.println(ip);
157+
158+
// print where to go in a browser:
159+
Serial.print("To see this page in action, open a browser to http://");
160+
Serial.println(ip);
161+
162+
}
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 ""
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
3+
This example prints the WiFi 101 shield or MKR1000 MAC address, and
4+
scans for available WiFi networks using the WiFi 101 shield or MKR1000 board.
5+
Every ten seconds, it scans again. It doesn't actually
6+
connect to any network, so no encryption scheme is specified.
7+
BSSID and WiFi channel are printed
8+
9+
This example is based on ScanNetworks
10+
11+
created 1 Mar 2017
12+
by Arturo Guadalupi
13+
*/
14+
15+
16+
#include <SPI.h>
17+
#include <WiFi1010.h>
18+
19+
void setup() {
20+
//Initialize serial and wait for port to open:
21+
Serial.begin(9600);
22+
while (!Serial) {
23+
; // wait for serial port to connect. Needed for native USB port only
24+
}
25+
26+
// check for the WiFi module:
27+
if (WiFi.status() == WL_NO_MODULE) {
28+
Serial.println("Communication with WiFi module failed!");
29+
// don't continue
30+
while (true);
31+
}
32+
33+
String fv = WiFi.firmwareVersion();
34+
if (fv != "1.0.0") {
35+
Serial.println("Please upgrade the firmware");
36+
}
37+
38+
// Print WiFi MAC address:
39+
printMacAddress();
40+
41+
// scan for existing networks:
42+
Serial.println();
43+
Serial.println("Scanning available networks...");
44+
listNetworks();
45+
}
46+
47+
void loop() {
48+
delay(10000);
49+
// scan for existing networks:
50+
Serial.println("Scanning available networks...");
51+
listNetworks();
52+
}
53+
54+
void printMacAddress() {
55+
// the MAC address of your WiFi shield
56+
byte mac[6];
57+
58+
// print your MAC address:
59+
WiFi.macAddress(mac);
60+
Serial.print("MAC: ");
61+
print2Digits(mac[5]);
62+
Serial.print(":");
63+
print2Digits(mac[4]);
64+
Serial.print(":");
65+
print2Digits(mac[3]);
66+
Serial.print(":");
67+
print2Digits(mac[2]);
68+
Serial.print(":");
69+
print2Digits(mac[1]);
70+
Serial.print(":");
71+
print2Digits(mac[0]);
72+
}
73+
74+
void listNetworks() {
75+
// scan for nearby networks:
76+
Serial.println("** Scan Networks **");
77+
int numSsid = WiFi.scanNetworks();
78+
if (numSsid == -1)
79+
{
80+
Serial.println("Couldn't get a WiFi connection");
81+
while (true);
82+
}
83+
84+
// print the list of networks seen:
85+
Serial.print("number of available networks: ");
86+
Serial.println(numSsid);
87+
88+
// print the network number and name for each network found:
89+
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
90+
Serial.print(thisNet + 1);
91+
Serial.print(") ");
92+
Serial.print("Signal: ");
93+
Serial.print(WiFi.RSSI(thisNet));
94+
Serial.print(" dBm");
95+
Serial.print("\tChannel: ");
96+
Serial.print(WiFi.channel(thisNet));
97+
byte bssid[6];
98+
Serial.print("\t\tBSSID: ");
99+
printBSSID(WiFi.BSSID(thisNet, bssid));
100+
Serial.print("\tEncryption: ");
101+
printEncryptionType(WiFi.encryptionType(thisNet));
102+
Serial.print("\t\tSSID: ");
103+
Serial.println(WiFi.SSID(thisNet));
104+
Serial.flush();
105+
}
106+
Serial.println();
107+
}
108+
109+
void printBSSID(byte bssid[]) {
110+
print2Digits(bssid[5]);
111+
Serial.print(":");
112+
print2Digits(bssid[4]);
113+
Serial.print(":");
114+
print2Digits(bssid[3]);
115+
Serial.print(":");
116+
print2Digits(bssid[2]);
117+
Serial.print(":");
118+
print2Digits(bssid[1]);
119+
Serial.print(":");
120+
print2Digits(bssid[0]);
121+
}
122+
123+
void printEncryptionType(int thisType) {
124+
// read the encryption type and print out the name:
125+
switch (thisType) {
126+
case ENC_TYPE_WEP:
127+
Serial.print("WEP");
128+
break;
129+
case ENC_TYPE_TKIP:
130+
Serial.print("WPA");
131+
break;
132+
case ENC_TYPE_CCMP:
133+
Serial.print("WPA2");
134+
break;
135+
case ENC_TYPE_NONE:
136+
Serial.print("None");
137+
break;
138+
case ENC_TYPE_AUTO:
139+
Serial.print("Auto");
140+
break;
141+
case ENC_TYPE_UNKNOWN:
142+
Serial.print("Unknown");
143+
break;
144+
}
145+
}
146+
147+
void print2Digits(byte thisByte) {
148+
if (thisByte < 0xF) {
149+
Serial.print("0");
150+
}
151+
Serial.print(thisByte, HEX);
152+
}
153+
154+

0 commit comments

Comments
 (0)