|
| 1 | +/* |
| 2 | + WiFiAccessPoint.ino creates a WiFi access point and provides a web server on it. |
| 3 | +
|
| 4 | + Steps: |
| 5 | + 1. Connect to the access point "yourAp" |
| 6 | + 2. Point your web browser to http://192.168.4.1/H to turn the LED on or http://192.168.4.1/L to turn it off |
| 7 | + OR |
| 8 | + Run raw TCP "GET /H" and "GET /L" on PuTTY terminal with 192.168.4.1 as IP address and 80 as port |
| 9 | +
|
| 10 | + Created for arduino-esp32 on 04 July, 2018 |
| 11 | + by Elochukwu Ifediora (fedy0) |
| 12 | +*/ |
| 13 | + |
| 14 | +#include <WiFi.h> |
| 15 | +#include <WiFiClient.h> |
| 16 | +#include <WiFiAP.h> |
| 17 | + |
| 18 | +#define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED |
| 19 | + |
| 20 | +// Set these to your desired credentials. |
| 21 | +const char *ssid = "yourAP"; |
| 22 | +const char *password = "yourPassword"; |
| 23 | + |
| 24 | +WiFiServer server(80); |
| 25 | + |
| 26 | + |
| 27 | +void setup() { |
| 28 | + pinMode(LED_BUILTIN, OUTPUT); |
| 29 | + |
| 30 | + Serial.begin(115200); |
| 31 | + Serial.println(); |
| 32 | + Serial.println("Configuring access point..."); |
| 33 | + |
| 34 | + // You can remove the password parameter if you want the AP to be open. |
| 35 | + WiFi.softAP(ssid, password); |
| 36 | + IPAddress myIP = WiFi.softAPIP(); |
| 37 | + Serial.print("AP IP address: "); |
| 38 | + Serial.println(myIP); |
| 39 | + server.begin(); |
| 40 | + |
| 41 | + Serial.println("Server started"); |
| 42 | +} |
| 43 | + |
| 44 | +void loop() { |
| 45 | + WiFiClient client = server.available(); // listen for incoming clients |
| 46 | + |
| 47 | + if (client) { // if you get a client, |
| 48 | + Serial.println("New Client."); // print a message out the serial port |
| 49 | + String currentLine = ""; // make a String to hold incoming data from the client |
| 50 | + while (client.connected()) { // loop while the client's connected |
| 51 | + if (client.available()) { // if there's bytes to read from the client, |
| 52 | + char c = client.read(); // read a byte, then |
| 53 | + Serial.write(c); // print it out the serial monitor |
| 54 | + if (c == '\n') { // if the byte is a newline character |
| 55 | + |
| 56 | + // if the current line is blank, you got two newline characters in a row. |
| 57 | + // that's the end of the client HTTP request, so send a response: |
| 58 | + if (currentLine.length() == 0) { |
| 59 | + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) |
| 60 | + // and a content-type so the client knows what's coming, then a blank line: |
| 61 | + client.println("HTTP/1.1 200 OK"); |
| 62 | + client.println("Content-type:text/html"); |
| 63 | + client.println(); |
| 64 | + |
| 65 | + // the content of the HTTP response follows the header: |
| 66 | + client.print("Click <a href=\"/H\">here</a> to turn ON the LED.<br>"); |
| 67 | + client.print("Click <a href=\"/L\">here</a> to turn OFF the LED.<br>"); |
| 68 | + |
| 69 | + // The HTTP response ends with another blank line: |
| 70 | + client.println(); |
| 71 | + // break out of the while loop: |
| 72 | + break; |
| 73 | + } else { // if you got a newline, then clear currentLine: |
| 74 | + currentLine = ""; |
| 75 | + } |
| 76 | + } else if (c != '\r') { // if you got anything else but a carriage return character, |
| 77 | + currentLine += c; // add it to the end of the currentLine |
| 78 | + } |
| 79 | + |
| 80 | + // Check to see if the client request was "GET /H" or "GET /L": |
| 81 | + if (currentLine.endsWith("GET /H")) { |
| 82 | + digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on |
| 83 | + } |
| 84 | + if (currentLine.endsWith("GET /L")) { |
| 85 | + digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + // close the connection: |
| 90 | + client.stop(); |
| 91 | + Serial.println("Client Disconnected."); |
| 92 | + } |
| 93 | +} |
0 commit comments