/******************************************************************************** * The MIT License (MIT) * * Copyright (c) 2015 Nis Wechselberg * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. ********************************************************************************/ /******************************************************************************** * ESP8266-RGB-WiFiServer * Sets the color of an RGB LED Ribbon through an HTTP Server ********************************************************************************/ #include #include "ESP8266-RGB-WiFiServer.h" // Pin configuration const int redPin = 12; const int greenPin = 13; const int bluePin = 14; // HTTP configuration const unsigned char port = 80; // Create an instance of the server WiFiServer server(port); void setup() { // Initialize Serial connection Serial.begin(115200); delay(10); // Prepare color pins pinMode(redPin, OUTPUT); pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); // 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()); // Test the LED color channels setColourRgb(0,0,0); delay(500); setColourRgb(200,0,0); delay(500); setColourRgb(0,200,0); delay(500); setColourRgb(0,0,200); delay(500); setColourRgb(200,200,200); delay(500); setColourRgb(0,0,0); } 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 req = client.readStringUntil('\r'); Serial.println(req); client.flush(); // Match the request if (req.indexOf("/gpio/rgb/") != -1) { // Request of the form "/gpio/rgb/red/green/blue/" // Delimiters for pwm values int br, er, eg, eb; br = req.indexOf("/gpio/rgb/") + 10; er = req.indexOf("/",br+1); eg = req.indexOf("/",er+1); eb = req.indexOf("/",eg+1); // Check if all values were filled in the request if (br == -1 || er == -1 || eg == -1 || eb == -1) { Serial.println("Invalid request"); client.stop(); return; } // Parse values in request int red = req.substring(br,er).toInt(); int green = req.substring(er+1,eg).toInt(); int blue = req.substring(eg+1,eb).toInt(); // Sanitize values if (red < 0) { red = 0; } if (green < 0) { green = 0; } if (blue < 0) { blue = 0; } if (red > 1023) { red = 1023; } if (green > 1023) { green = 1023; } if (blue > 1023) { blue = 1023; } // Apply colors setColourRgb(red, green, blue); } else { Serial.println("invalid request"); client.stop(); return; } client.flush(); // Prepare the response String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nOK\n"; // Send the response to the client client.print(s); delay(1); Serial.println("Disconnecting client"); // The client will actually be disconnected // when the function returns and 'client' object is detroyed } /* Helper function to set the colors of the led strip */ void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); }