|
| 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 Shield (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 5. |
| 8 | +
|
| 9 | + If the IP address of your shield 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 | + * WiFi shield attached |
| 18 | + * LED attached to pin 5 |
| 19 | +
|
| 20 | + created for arduino 25 Nov 2012 |
| 21 | + by Tom Igoe |
| 22 | +
|
| 23 | +ported for sparkfun esp32 |
| 24 | +31.01.2017 by Jan Hendrik Berlin |
| 25 | + |
| 26 | + */ |
| 27 | + |
| 28 | +#include <WiFi.h> |
| 29 | + |
| 30 | +const char* ssid = "yourssid"; |
| 31 | +const char* password = "yourpasswd"; |
| 32 | + |
| 33 | +WiFiServer server(80); |
| 34 | + |
| 35 | +void setup() |
| 36 | +{ |
| 37 | + Serial.begin(115200); |
| 38 | + pinMode(5, OUTPUT); // set the LED pin mode |
| 39 | + |
| 40 | + delay(10); |
| 41 | + |
| 42 | + // We start by connecting to a WiFi network |
| 43 | + |
| 44 | + Serial.println(); |
| 45 | + Serial.println(); |
| 46 | + Serial.print("Connecting to "); |
| 47 | + Serial.println(ssid); |
| 48 | + |
| 49 | + WiFi.begin(ssid, password); |
| 50 | + |
| 51 | + while (WiFi.status() != WL_CONNECTED) { |
| 52 | + delay(500); |
| 53 | + Serial.print("."); |
| 54 | + } |
| 55 | + |
| 56 | + Serial.println(""); |
| 57 | + Serial.println("WiFi connected"); |
| 58 | + Serial.println("IP address: "); |
| 59 | + Serial.println(WiFi.localIP()); |
| 60 | + |
| 61 | + server.begin(); |
| 62 | + |
| 63 | +} |
| 64 | + |
| 65 | +int value = 0; |
| 66 | + |
| 67 | +void loop(){ |
| 68 | + WiFiClient client = server.available(); // listen for incoming clients |
| 69 | + |
| 70 | + if (client) { // if you get a client, |
| 71 | + Serial.println("new client"); // print a message out the serial port |
| 72 | + String currentLine = ""; // make a String to hold incoming data from the client |
| 73 | + while (client.connected()) { // loop while the client's connected |
| 74 | + if (client.available()) { // if there's bytes to read from the client, |
| 75 | + char c = client.read(); // read a byte, then |
| 76 | + Serial.write(c); // print it out the serial monitor |
| 77 | + if (c == '\n') { // if the byte is a newline character |
| 78 | + |
| 79 | + // if the current line is blank, you got two newline characters in a row. |
| 80 | + // that's the end of the client HTTP request, so send a response: |
| 81 | + if (currentLine.length() == 0) { |
| 82 | + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) |
| 83 | + // and a content-type so the client knows what's coming, then a blank line: |
| 84 | + client.println("HTTP/1.1 200 OK"); |
| 85 | + client.println("Content-type:text/html"); |
| 86 | + client.println(); |
| 87 | + |
| 88 | + // the content of the HTTP response follows the header: |
| 89 | + client.print("Click <a href=\"/H\">here</a> turn the LED on pin 5 on<br>"); |
| 90 | + client.print("Click <a href=\"/L\">here</a> turn the LED on pin 5 off<br>"); |
| 91 | + |
| 92 | + // The HTTP response ends with another blank line: |
| 93 | + client.println(); |
| 94 | + // break out of the while loop: |
| 95 | + break; |
| 96 | + } else { // if you got a newline, then clear currentLine: |
| 97 | + currentLine = ""; |
| 98 | + } |
| 99 | + } else if (c != '\r') { // if you got anything else but a carriage return character, |
| 100 | + currentLine += c; // add it to the end of the currentLine |
| 101 | + } |
| 102 | + |
| 103 | + // Check to see if the client request was "GET /H" or "GET /L": |
| 104 | + if (currentLine.endsWith("GET /H")) { |
| 105 | + digitalWrite(5, HIGH); // GET /H turns the LED on |
| 106 | + } |
| 107 | + if (currentLine.endsWith("GET /L")) { |
| 108 | + digitalWrite(5, LOW); // GET /L turns the LED off |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + // close the connection: |
| 113 | + client.stop(); |
| 114 | + Serial.println("client disonnected"); |
| 115 | + } |
| 116 | +} |
0 commit comments