Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- const char* ssid = "************"; // Update according to the router name you are connecting to
- const char* password = "***********"; // Update according to the password of the router
- const char* host = "***.***.***.***"; //it will tell you the IP once it starts up
- //just write it here afterwards and upload
- int ledPin = 4;
- int buz = 14;
- int pb = 12;
- int pbV = 0;
- WiFiServer server(4023); //just pick any port number you like
- void setup() {
- Serial.begin(115200);
- delay(10);
- // prepare GPIOs
- pinMode(ledPin, OUTPUT);
- pinMode(buz, OUTPUT);
- pinMode(pb, INPUT);
- digitalWrite(ledPin , LOW);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Start the server
- server.begin();
- Serial.println("Server started");
- // Print the IP address
- Serial.println(WiFi.localIP());
- }
- void loop() {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- while (!client.available()) {
- delay(1);
- }
- // Read the first line of the request
- String req = client.readStringUntil('\r');
- client.flush();
- // Match the request
- if (req.indexOf("") != -10) { //checks if you're on the main page
- if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF
- digitalWrite(ledPin, LOW);
- Serial.println("You clicked OFF");
- }
- if (req.indexOf("/ON") != -1) { //checks if you clicked ON
- digitalWrite(ledPin, HIGH);
- Serial.println("You clicked ON");
- }
- if (req.indexOf("/BON") != -1) { //checks if you clicked ON
- digitalWrite(buz, HIGH);
- Serial.println("You clicked Buzz ON");
- }
- if (req.indexOf("/BOFF") != -1) { //checks if you clicked ON
- digitalWrite(buz, LOW);
- Serial.println("You clicked Buzz OFF");
- }
- }
- else {
- Serial.println("invalid request");
- client.stop();
- return;
- }
- // Prepare the response
- 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 += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED ON \" onclick=\"location.href='/ON'\">";
- s += "<br><br><br>";
- s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
- s += "<br><br><br>";
- s += "<br><input type=\"button\" name=\"bl\" value=\"Buzz On\" onclick=\"location.href='/BON'\">";
- s += "<br><br><br>";
- s += "<br><input type=\"button\" name=\"bl\" value=\"Buzz Off\" onclick=\"location.href='/BOFF'\">";
- s += "<br><br><br>";
- pbV = digitalRead(pb);
- if (pbV == HIGH) {
- s += "<br>Button is pressed";
- }
- else {
- s += "<br>Button is released";
- }
- s += "</html>\n";
- client.flush();
- // Send the response to the client
- client.print(s);
- delay(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement