Last active
April 1, 2018 16:17
-
-
Save yhdesai/c0b9ed02619a4bd4ca89b4122f13df50 to your computer and use it in GitHub Desktop.
temp code delete me
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <ESP8266WiFi.h> | |
| const char* ssid = "HT"; | |
| const char* password = "123456789"; | |
| int tb = 13; // GPIO13 | |
| int fan = 12; // GPIO12 | |
| int led = 14; // GPIO14 | |
| int tube = 5; //GPIO5 | |
| int ll = 4; //GPIO4 | |
| WiFiServer server(80); | |
| void setup() { | |
| Serial.begin(115200); | |
| delay(10); | |
| pinMode(led, OUTPUT); | |
| digitalWrite(led, 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.print("Use this URL to connect: "); | |
| Serial.print("http://"); | |
| Serial.print(WiFi.localIP()); | |
| Serial.println("/"); | |
| } | |
| void loop() { | |
| // Check if a client has connected | |
| WiFiClient client = server.available(); | |
| if (!client) { | |
| return; | |
| } | |
| // Wait until the client sends some data | |
| Serial.println("new client"); | |
| while (!client.available()) { | |
| delay(1); | |
| } | |
| // Read the first line of the request | |
| String request = client.readStringUntil('\r'); | |
| Serial.println(request); | |
| client.flush(); | |
| // Match the request | |
| // Initializing var | |
| int value1 = LOW; | |
| int value2 = LOW; | |
| int value3 = LOW; | |
| int value4 = LOW; | |
| int value5 = LOW; | |
| // TubeLight | |
| if (request.indexOf("/tb=on") != -1) { | |
| digitalWrite(tb, HIGH); | |
| value1 = HIGH; | |
| } | |
| if (request.indexOf("/tb=off") != -1) { | |
| digitalWrite(tb, LOW); | |
| value1 = LOW; | |
| } | |
| // Fan | |
| if (request.indexOf("/fan=on") != -1) { | |
| digitalWrite(fan, HIGH); | |
| value2 = HIGH; | |
| } | |
| if (request.indexOf("/fan=off") != -1) { | |
| digitalWrite(fan, LOW); | |
| value2 = LOW; | |
| } | |
| // led | |
| if (request.indexOf("/led=on") != -1) { | |
| digitalWrite(led, HIGH); | |
| value3 = HIGH; | |
| } | |
| if (request.indexOf("/led=off") != -1) { | |
| digitalWrite(led, LOW); | |
| value3 = LOW; | |
| } | |
| // tube | |
| if (request.indexOf("/tube=on") != -1) { | |
| digitalWrite(tube, HIGH); | |
| value4 = HIGH; | |
| } | |
| if (request.indexOf("/tube=off") != -1) { | |
| digitalWrite(tube, LOW); | |
| value4 = LOW; | |
| } | |
| // ledLIGHT | |
| if (request.indexOf("/ll=on") != -1) { | |
| digitalWrite(ll, HIGH); | |
| value5 = HIGH; | |
| } | |
| if (request.indexOf("/ll=off") != -1) { | |
| digitalWrite(ll, LOW); | |
| value5 = LOW; | |
| } | |
| //gn | |
| if (request.indexOf("/gn") != -1) { | |
| digitalWrite(tb, LOW); | |
| digitalWrite(fan, LOW); | |
| digitalWrite(led, LOW); | |
| digitalWrite(tube, LOW); | |
| digitalWrite(ll, LOW); | |
| digitalWrite(tb, HIGH); | |
| digitalWrite(fan, HIGH); | |
| digitalWrite(led, HIGH); | |
| delay(1000); | |
| digitalWrite(tb, LOW); | |
| delay(200); | |
| digitalWrite(tb, HIGH); | |
| delay(1000); | |
| digitalWrite(tb, LOW); | |
| delay(200); | |
| digitalWrite(tb, HIGH); | |
| delay(300000); | |
| digitalWrite(tb, LOW); | |
| } | |
| //all on | |
| if (request.indexOf("/on") != -1) { | |
| digitalWrite(tb, HIGH); | |
| digitalWrite(fan, HIGH); | |
| digitalWrite(led, HIGH); | |
| digitalWrite(ll, HIGH); | |
| digitalWrite(tube, HIGH); | |
| } | |
| // all of | |
| if (request.indexOf("/off") != -1) { | |
| digitalWrite(tb, LOW); | |
| digitalWrite(fan, LOW); | |
| digitalWrite(led, LOW); | |
| digitalWrite(ll, LOW); | |
| digitalWrite(tube, LOW); | |
| } | |
| // Set led according to the request | |
| //digitalWrite(led, value1); | |
| // Return the response | |
| client.println("HTTP/1.1 200 OK"); | |
| client.println("Content-Type: text/html"); | |
| client.println(""); // do not forget this one | |
| client.println("<!DOCTYPE HTML>"); | |
| client.println("<html>"); | |
| // Device Buttons | |
| client.println("<br><br>"); | |
| client.println("<a href=\"/tb=on\"\"><button>Turn On TuebLight</button></a>"); | |
| client.println("<a href=\"/tb=off\"\"><button>Turn Off Tubelight</button></a><br />"); | |
| client.println("<a href=\"/fan=on\"\"><button>Turn On Fan</button></a>"); | |
| client.println("<a href=\"/fan=off\"\"><button>Turn Off Fan </button></a><br />"); | |
| client.println("<a href=\"/led=on\"\"><button>Turn On Led</button></a>"); | |
| client.println("<a href=\"/led=off\"\"><button>Turn Off Led</button></a><br />"); | |
| client.println("<a href=\"/tube=on\"\"><button>Turn On Tube</button></a>"); | |
| client.println("<a href=\"/tube=off\"\"><button>Turn Off Tube</button></a><br />"); | |
| client.println("<a href=\"/ll=on\"\"><button>Turn On Led Light</button></a>"); | |
| client.println("<a href=\"/ll=off\"\"><button>Turn Off Led Light</button></a><br />"); | |
| client.println("<a href=\"/on\"\"><button>Turn On All</button></a><br />"); | |
| client.println("<a href=\"/off\"\"><button>Turn Off All</button></a><br />"); | |
| // Device Status | |
| // Device 1 | |
| client.println("<br><br>"); | |
| client.print("TubeLight is now: "); | |
| if (value1 == HIGH) { | |
| client.print("On"); | |
| } else { | |
| client.print("Off"); | |
| } | |
| // Device 2 | |
| client.println("<br><br>"); | |
| client.print("Fan is now: "); | |
| if (value2 == HIGH) { | |
| client.print("On"); | |
| } else { | |
| client.print("Off"); | |
| } | |
| // Device 3 | |
| client.println("<br><br>"); | |
| client.print("Led is now: "); | |
| if (value3 == HIGH) { | |
| client.print("On"); | |
| } else { | |
| client.print("Off"); | |
| } | |
| // Device 4 | |
| client.println("<br><br>"); | |
| client.print("Tube is now: "); | |
| if (value4 == HIGH) { | |
| client.print("On"); | |
| } else { | |
| client.print("Off"); | |
| } | |
| // Device 5 | |
| client.println("<br><br>"); | |
| client.print("Led Light is now: "); | |
| if (value5 == HIGH) { | |
| client.print("On"); | |
| } else { | |
| client.print("Off"); | |
| } | |
| client.println("</html>"); | |
| delay(1); | |
| Serial.println("Client disonnected"); | |
| Serial.println(""); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment