|
| 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 | +} |
0 commit comments