|
| 1 | +#include <WiFi.h> |
| 2 | +#include "arduino_secrets.h" |
| 3 | + |
| 4 | +///////please enter your sensitive data in the Secret tab/arduino_secrets.h |
| 5 | +char ssid[] = SECRET_SSID; // your network SSID (name) |
| 6 | +char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP) |
| 7 | +int keyIndex = 0; // your network key Index number (needed only for WEP) |
| 8 | + |
| 9 | +int status = WL_IDLE_STATUS; |
| 10 | + |
| 11 | +WiFiServer server(80); |
| 12 | + |
| 13 | +void setup() { |
| 14 | + // put your setup code here, to run once: |
| 15 | + Serial.begin(9600); |
| 16 | + while (!Serial) { |
| 17 | + ; // wait for serial port to connect. Needed for native USB port only |
| 18 | + } |
| 19 | + |
| 20 | + Serial.println("Access Point Web Server"); |
| 21 | + |
| 22 | + pinMode(LEDR, OUTPUT); |
| 23 | + pinMode(LEDG, OUTPUT); |
| 24 | + pinMode(LEDB, OUTPUT); |
| 25 | + |
| 26 | + // by default the local IP address of will be 192.168.3.1 |
| 27 | + // you can override it with the following: |
| 28 | + // WiFi.config(IPAddress(10, 0, 0, 1)); |
| 29 | + |
| 30 | + // print the network name (SSID); |
| 31 | + Serial.print("Creating access point named: "); |
| 32 | + Serial.println(ssid); |
| 33 | + |
| 34 | + //Create the Access point |
| 35 | + status = WiFi.beginAP(ssid, pass); |
| 36 | + if (status != WL_AP_LISTENING) { |
| 37 | + Serial.println("Creating access point failed"); |
| 38 | + // don't continue |
| 39 | + while (true); |
| 40 | + } |
| 41 | + |
| 42 | + // wait 10 seconds for connection: |
| 43 | + delay(10000); |
| 44 | + |
| 45 | + // start the web server on port 80 |
| 46 | + server.begin(); |
| 47 | + |
| 48 | + // you're connected now, so print out the status |
| 49 | + printWiFiStatus(); |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + |
| 55 | + // compare the previous status to the current status |
| 56 | + if (status != WiFi.status()) { |
| 57 | + // it has changed update the variable |
| 58 | + status = WiFi.status(); |
| 59 | + |
| 60 | + if (status == WL_AP_CONNECTED) { |
| 61 | + // a device has connected to the AP |
| 62 | + Serial.println("Device connected to AP"); |
| 63 | + } else { |
| 64 | + // a device has disconnected from the AP, and we are back in listening mode |
| 65 | + Serial.println("Device disconnected from AP"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + WiFiClient client = server.available(); // listen for incoming clients |
| 70 | + |
| 71 | + if (client) { // if you get a client, |
| 72 | + Serial.println("new client"); // print a message out the serial port |
| 73 | + String currentLine = ""; // make a String to hold incoming data from the client |
| 74 | + |
| 75 | + while (client.connected()) { // loop while the client's connected |
| 76 | + |
| 77 | + if (client.available()) { // if there's bytes to read from the client, |
| 78 | + char c = client.read(); // read a byte, then |
| 79 | + Serial.write(c); // print it out the serial monitor |
| 80 | + if (c == '\n') { // if the byte is a newline character |
| 81 | + |
| 82 | + // if the current line is blank, you got two newline characters in a row. |
| 83 | + // that's the end of the client HTTP request, so send a response: |
| 84 | + if (currentLine.length() == 0) { |
| 85 | + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) |
| 86 | + // and a content-type so the client knows what's coming, then a blank line: |
| 87 | + client.println("HTTP/1.1 200 OK"); |
| 88 | + client.println("Content-type:text/html"); |
| 89 | + client.println(); |
| 90 | + |
| 91 | + // the content of the HTTP response follows the header: |
| 92 | + client.print("<html><head>"); |
| 93 | + client.print("<style>"); |
| 94 | + client.print("* { font-family: sans-serif;}"); |
| 95 | + client.print("body { padding: 2em; font-size: 2em; text-align: center;}"); |
| 96 | + client.print("a { -webkit-appearance: button;-moz-appearance: button;appearance: button;text-decoration: none;color: initial; padding: 25px;} #red{color:red;} #green{color:green;} #blue{color:blue;}"); |
| 97 | + client.print("</style></head>"); |
| 98 | + client.print("<body><h1> LED CONTROLS </h1>"); |
| 99 | + client.print("<h2><span id=\"red\">RED </span> LED </h2>"); |
| 100 | + client.print("<a href=\"/Hr\">ON</a> <a href=\"/Lr\">OFF</a>"); |
| 101 | + client.print("<h2> <span id=\"green\">GREEN</span> LED </h2>"); |
| 102 | + client.print("<a href=\"/Hg\">ON</a> <a href=\"/Lg\">OFF</a>"); |
| 103 | + client.print("<h2> <span id=\"blue\">BLUE</span> LED </h2>"); |
| 104 | + client.print("<a href=\"/Hb\">ON</a> <a href=\"/Lb\">OFF</a>"); |
| 105 | + client.print("</body></html>"); |
| 106 | + |
| 107 | + // The HTTP response ends with another blank line: |
| 108 | + client.println(); |
| 109 | + // break out of the while loop: |
| 110 | + break; |
| 111 | + } else { // if you got a newline, then clear currentLine: |
| 112 | + currentLine = ""; |
| 113 | + } |
| 114 | + } else if (c != '\r') { // if you got anything else but a carriage return character, |
| 115 | + currentLine += c; // add it to the end of the currentLine |
| 116 | + } |
| 117 | + |
| 118 | + // Check to see if the client request was "GET /H" or "GET /L": |
| 119 | + if (currentLine.endsWith("GET /Hr")) { |
| 120 | + digitalWrite(LEDR, LOW); // GET /Hr turns the Red LED on |
| 121 | + } |
| 122 | + if (currentLine.endsWith("GET /Lr")) { |
| 123 | + digitalWrite(LEDR, HIGH); // GET /Lr turns the Red LED off |
| 124 | + } |
| 125 | + if (currentLine.endsWith("GET /Hg")) { |
| 126 | + digitalWrite(LEDG, LOW); // GET /Hg turns the Green LED on |
| 127 | + } |
| 128 | + if (currentLine.endsWith("GET /Lg")) { |
| 129 | + digitalWrite(LEDG, HIGH); // GET /Hg turns the Green LED on |
| 130 | + } |
| 131 | + if (currentLine.endsWith("GET /Hb")) { |
| 132 | + digitalWrite(LEDB, LOW); // GET /Hg turns the Green LED on |
| 133 | + } |
| 134 | + if (currentLine.endsWith("GET /Lb")) { |
| 135 | + digitalWrite(LEDB, HIGH); // GET /Hg turns the Green LED on |
| 136 | + } |
| 137 | + |
| 138 | + } |
| 139 | + } |
| 140 | + // close the connection: |
| 141 | + client.stop(); |
| 142 | + Serial.println("client disconnected"); |
| 143 | + } |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | +void printWiFiStatus() { |
| 148 | + // print the SSID of the network you're attached to: |
| 149 | + Serial.print("SSID: "); |
| 150 | + Serial.println(WiFi.SSID()); |
| 151 | + |
| 152 | + // print your WiFi shield's IP address: |
| 153 | + IPAddress ip = WiFi.localIP(); |
| 154 | + Serial.print("IP Address: "); |
| 155 | + Serial.println(ip); |
| 156 | + |
| 157 | + // print where to go in a browser: |
| 158 | + Serial.print("To see this page in action, open a browser to http://"); |
| 159 | + Serial.println(ip); |
| 160 | +} |
0 commit comments