Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #define LED_PIN 16
- const char* ssid = "*****";
- const char* password = "******";
- int state = 0;
- WiFiServer server(80); //Initialize the server on Port 80
- void setup()
- {
- Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window
- delay(10);
- pinMode(LED_PIN, OUTPUT); //GPIO16 is an OUTPUT pin;
- digitalWrite(LED_PIN, LOW); //Initial state is ON
- WiFi.begin(ssid, password); // Provide the (SSID, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- server.begin(); // Start the HTTP Server
- //Looking under the hood
- Serial.print("Server IP is: "); // Print the IP to the monitor window
- Serial.println(WiFi.localIP());
- }
- void loop()
- {
- WiFiClient client = server.available();
- if (!client)
- {
- return;
- }
- //Looking under the hood
- Serial.println("Somebody has connected :)");
- //Read what the browser has sent into a String class and print the request to the monitor
- String request = client.readStringUntil('\r');
- //Looking under the hood
- Serial.println(request);
- // Handle the Request
- if (request.indexOf("/OFF") != -1)
- {
- digitalWrite(LED_PIN, HIGH);
- Serial.println(request.indexOf("/ON"));
- }
- else if (request.indexOf("/ON") != -1)
- {
- digitalWrite(LED_PIN, LOW);
- Serial.println(request.indexOf("/OFF"));
- }
- if (request.indexOf("/Green_BT") == 4)
- {
- if(state !=0)
- {
- state = 0;
- Serial.println("Turn OFF");
- }
- else
- {
- state = 1;
- Serial.println("Turn ON");
- }
- Serial.println("Green is pressed");
- Serial.println(request.indexOf("/Green_BT"));
- }
- Serial.println(request.indexOf("/name_field"));
- //Prepare the HTML document to respond and add buttons:
- String s = "HTTP/1.1 200 OK\r\n";
- s += "Content-Type: text/html\r\n\r\n";
- s += "<!DOCTYPE HTML>\r\n<html>\r\n";
- s += "<style> .button { background-color: #4CAF50; border: none; color: white; padding: 16px 32px; text-align: center; text-decoration: none; display: inline-block;mfont-size: 16px; margin: 4px 2px; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; cursor: pointer;}";
- s += " .button1 { background-color: #4CAF50; color: white; border: 2px solid #4CAF50;}";
- s += " .button1:hover { background-color: white; color: black;}";
- s += "</style>";
- s += "<br><input type=\"button\" name=\"b1\" value=\"Turn LED ON\" onclick=\"location.href='/ON'\">";
- s += "<br><br><br>";
- s += "<input type=\"button\" name=\"b1\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
- s += "<br><br><br>";
- s += "<button class=\"button button1\" onclick=\"location.href='/Green_BT'\">Green</button>";
- s += "<br><br><br>";
- s += "<form action=\"/team_name_url/\" method=\"post\">";
- s += "<label for=\"team_name\">Enter name: </label>";
- s += "<input id=\"team_name\" type=\"text\" name=\"name_field\" placeholder=\"Default name for team.\">";
- s += "<input type=\"submit\" value=\"OK\">";
- s += "</form>";
- s += "</html>\n";
- //Serve the HTML document to the browser.
- client.flush(); //clear previous info in the stream
- client.print(s); // Send the response to the client
- delay(1);
- Serial.println("Client disonnected"); //Looking under the hood
- }
Advertisement
Add Comment
Please, Sign In to add comment