#include #define RED_PIN 9 #define BLUE_PIN 20 // #define GREEN_PIN 8 // #define BUTTON 2 // Replace with your network credentials const char* ssid = "phone"; const char* password = "fastasfuckboy"; WiFiServer server(80); // Variable to store the HTTP req uest String header; // Decode HTTP GET value int pos1 = 0; int pos2 = 0; int pos3 = 0; int pos4 = 0; // set on status int ON_STATUS = 0; // Current time unsigned long currentTime = millis(); // Previous time unsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s) const long timeoutTime = 2000; void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(BUTTON, INPUT_PULLUP); pinMode(RED_PIN, OUTPUT); pinMode(BLUE_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); digitalWrite(RED_PIN, HIGH); digitalWrite(BLUE_PIN, HIGH); digitalWrite(GREEN_PIN, HIGH); // Connect to Wi-Fi network with SSID and password Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } // Print local IP address and start web server Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); // start server server.begin(); } void loop() { // read pin state int button_read = digitalRead(BUTTON); // Take a reading if(button_read == 0){ while (digitalRead(BUTTON) == 0){ // delay so that a hold down is read just once delay(100); } if (ON_STATUS == 0) { analogWrite(RED_PIN, 0); analogWrite(GREEN_PIN, 235); analogWrite(BLUE_PIN, 108); ON_STATUS = 1; } else { // turn them off and reset ON_STATUS analogWrite(RED_PIN, 255); analogWrite(GREEN_PIN, 255); analogWrite(BLUE_PIN, 255); ON_STATUS = 0; } } delay(200); WiFiClient client = server.available(); // Listen for incoming clients if (client) { // If a new client connects, currentTime = millis(); previousTime = currentTime; Serial.println("New Client."); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); if (client.available()) { // if there's bytes to read from the client, char c = client.read(); // read a byte, then Serial.write(c); // print it out the serial monitor header += c; if (c == '\n') { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // Display the HTML web page client.println(""); client.println(""); client.println(""); client.println(""); client.println(""); client.println(" send to lamp "); client.println(""); client.println(""); // The HTTP response ends with another blank line client.println(); // Request sample: /?r201g32b255& // Red = 201 | Green = 32 | Blue = 255 if(header.indexOf("GET /?r") >= 0) { pos1 = header.indexOf('r'); pos2 = header.indexOf('g'); pos3 = header.indexOf('b'); pos4 = header.indexOf('&'); int redValue = header.substring(pos1+1, pos2).toInt(); int greenValue = header.substring(pos2+1, pos3).toInt(); int blueValue = header.substring(pos3+1, pos4).toInt(); // adjust for anode led redValue = 255 - redValue * .9; // adjust red led because they all have the same resistor blueValue = 255 - blueValue; greenValue = 255 - greenValue; Serial.print("red: "); Serial.println(redValue); Serial.print("green: "); Serial.println(greenValue); Serial.print("blue: "); Serial.println(blueValue); analogWrite(RED_PIN, redValue); analogWrite(BLUE_PIN, blueValue); analogWrite(GREEN_PIN, greenValue); } // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }