2015-10-16 11:08:49 +02:00
|
|
|
/********************************************************************************
|
|
|
|
* 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
|
|
|
|
********************************************************************************/
|
2015-11-19 02:21:51 +01:00
|
|
|
#include <EEPROM.h>
|
2015-10-16 11:08:49 +02:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include "ESP8266-RGB-WiFiServer.h"
|
|
|
|
|
|
|
|
// Pin configuration
|
2016-07-02 18:12:14 +02:00
|
|
|
const short redPin = 14;
|
|
|
|
const short greenPin = 16;
|
|
|
|
const short bluePin = 12;
|
2015-11-19 02:21:51 +01:00
|
|
|
|
|
|
|
// EEPROM Adress config
|
|
|
|
const unsigned char modeAddress = 0;
|
|
|
|
const unsigned char redAddress = 1;
|
|
|
|
const unsigned char greenAddress = 2;
|
|
|
|
const unsigned char blueAddress = 3;
|
|
|
|
|
|
|
|
// Color values
|
|
|
|
unsigned char modeValue;
|
|
|
|
unsigned char redValue;
|
|
|
|
unsigned char greenValue;
|
|
|
|
unsigned char blueValue;
|
|
|
|
unsigned char decColor;
|
|
|
|
|
|
|
|
// Scale factors
|
2015-11-20 21:38:45 +01:00
|
|
|
const float globalScale = 3.0;
|
|
|
|
const float redScale = 1.0;
|
|
|
|
const float greenScale = 1.0;
|
|
|
|
const float blueScale = 0.7;
|
|
|
|
float breathScale = 0.999;
|
|
|
|
float breathAdjustment = -0.001;
|
|
|
|
|
2015-10-16 11:08:49 +02:00
|
|
|
|
|
|
|
// HTTP configuration
|
|
|
|
const unsigned char port = 80;
|
|
|
|
WiFiServer server(port);
|
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
|
2015-10-16 11:08:49 +02:00
|
|
|
void setup() {
|
2016-01-15 20:42:10 +01:00
|
|
|
// Set wifi mode to client
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
|
2015-10-16 11:08:49 +02:00
|
|
|
// 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.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();
|
2015-11-19 02:21:51 +01:00
|
|
|
Serial.print("Server started at IP address ");
|
2015-10-16 11:08:49 +02:00
|
|
|
// Print the IP address
|
|
|
|
Serial.println(WiFi.localIP());
|
2015-11-19 02:21:51 +01:00
|
|
|
|
|
|
|
// Read stored mode and previous color values from EEPROM
|
|
|
|
EEPROM.begin(16);
|
|
|
|
readFromEEPROM();
|
2015-10-16 11:08:49 +02:00
|
|
|
|
|
|
|
// Test the LED color channels
|
2015-11-19 02:21:51 +01:00
|
|
|
setLight(0,0,0);
|
2015-10-16 11:08:49 +02:00
|
|
|
delay(500);
|
2015-11-19 02:21:51 +01:00
|
|
|
setLight(100,0,0);
|
2015-10-16 11:08:49 +02:00
|
|
|
delay(500);
|
2015-11-19 02:21:51 +01:00
|
|
|
setLight(0,100,0);
|
2015-10-16 11:08:49 +02:00
|
|
|
delay(500);
|
2015-11-19 02:21:51 +01:00
|
|
|
setLight(0,0,100);
|
2015-10-16 11:08:49 +02:00
|
|
|
delay(500);
|
2015-11-19 02:21:51 +01:00
|
|
|
setLight(100,100,100);
|
2015-10-16 11:08:49 +02:00
|
|
|
delay(500);
|
2015-11-19 02:21:51 +01:00
|
|
|
updateLight();
|
2015-10-16 11:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// Check if a client has connected
|
|
|
|
WiFiClient client = server.available();
|
2015-11-19 02:21:51 +01:00
|
|
|
if (client) {
|
|
|
|
// Wait until the client sends some data
|
2017-02-17 23:31:10 +01:00
|
|
|
Serial.print("Client connected");
|
|
|
|
unsigned char timeouts = 10;
|
|
|
|
while(!client.available() && timeouts > 0){
|
|
|
|
delay(10);
|
|
|
|
timeouts -= 1;
|
|
|
|
Serial.print(".");
|
2015-11-19 02:21:51 +01:00
|
|
|
}
|
2017-02-17 23:31:10 +01:00
|
|
|
Serial.println("");
|
2015-11-19 02:21:51 +01:00
|
|
|
|
2017-02-17 23:31:10 +01:00
|
|
|
if (timeouts > 0) {
|
|
|
|
// Read the first line of the request
|
|
|
|
String req = client.readStringUntil('\r');
|
|
|
|
Serial.println(req);
|
|
|
|
client.flush();
|
2015-11-19 02:21:51 +01:00
|
|
|
|
2017-02-17 23:31:10 +01:00
|
|
|
// Check if new value should be set or values should be read
|
|
|
|
if (req.indexOf("/set/") != -1) {
|
|
|
|
// Request of the form "/set/mode/red/green/blue/"
|
|
|
|
// Delimiters for values
|
|
|
|
int indexMode, indexRed, indexGreen, indexBlue, endIndexBlue;
|
|
|
|
indexMode = req.indexOf("/set/") + 5;
|
|
|
|
indexRed = req.indexOf("/",indexMode + 1);
|
|
|
|
indexGreen = req.indexOf("/",indexRed + 1);
|
|
|
|
indexBlue = req.indexOf("/",indexGreen + 1);
|
|
|
|
endIndexBlue = req.indexOf("/",indexBlue + 1);
|
|
|
|
// Check if all values were filled in the request
|
|
|
|
if (indexMode == -1 || indexRed == -1 || indexGreen == -1
|
|
|
|
|| indexBlue == -1 || endIndexBlue == -1) {
|
|
|
|
// Invalid request, abort
|
|
|
|
Serial.println("Invalid request");
|
|
|
|
client.stop();
|
|
|
|
} else {
|
|
|
|
// Parse values in request
|
|
|
|
int mode = req.substring(indexMode, indexRed).toInt();
|
|
|
|
int red = req.substring(indexRed + 1, indexGreen).toInt();
|
|
|
|
int green = req.substring(indexGreen + 1, indexBlue).toInt();
|
|
|
|
int blue = req.substring(indexBlue + 1, endIndexBlue).toInt();
|
|
|
|
|
|
|
|
// Sanitize values
|
|
|
|
if (mode < 0) { mode = 0; }
|
|
|
|
if (red < 0) { red = 0; }
|
|
|
|
if (green < 0) { green = 0; }
|
|
|
|
if (blue < 0) { blue = 0; }
|
|
|
|
if (mode > 3) { mode = 3; }
|
|
|
|
if (red > 255) { red = 255; }
|
|
|
|
if (green > 255) { green = 255; }
|
|
|
|
if (blue > 255) { blue = 255; }
|
|
|
|
|
|
|
|
// Update values
|
|
|
|
modeValue = mode;
|
|
|
|
|
|
|
|
|
|
|
|
breathScale = 0.999;
|
|
|
|
breathAdjustment = -0.001;
|
|
|
|
decColor = 0;
|
|
|
|
|
|
|
|
if (modeValue % 2 == 0) {
|
|
|
|
redValue = red;
|
|
|
|
greenValue = green;
|
|
|
|
blueValue = blue;
|
|
|
|
}
|
|
|
|
if (modeValue % 2 != 0) {
|
|
|
|
redValue = 255;
|
|
|
|
greenValue = 0;
|
|
|
|
blueValue = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
persistToEEPROM();
|
2015-11-20 21:38:45 +01:00
|
|
|
}
|
2017-02-17 23:31:10 +01:00
|
|
|
} else if (req.indexOf("/get/") != -1) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Serial.println("invalid request");
|
|
|
|
client.stop();
|
2015-11-19 02:21:51 +01:00
|
|
|
}
|
2015-10-16 11:08:49 +02:00
|
|
|
|
2017-02-17 23:31:10 +01:00
|
|
|
client.flush();
|
|
|
|
|
|
|
|
// Prepare the response
|
|
|
|
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
|
|
|
|
response += modeValue;
|
|
|
|
response += "\n";
|
|
|
|
response += redValue;
|
|
|
|
response += "\n";
|
|
|
|
response += greenValue;
|
|
|
|
response += "\n";
|
|
|
|
response += blueValue;
|
|
|
|
response += "\n";
|
|
|
|
|
|
|
|
// Send the response to the client
|
|
|
|
client.print(response);
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
client.flush();
|
|
|
|
Serial.println("Disconnecting client");
|
|
|
|
// The client will actually be disconnected
|
|
|
|
// when the function returns and 'client' object is detroyed
|
2015-10-16 11:08:49 +02:00
|
|
|
}
|
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
updateLight();
|
2015-10-16 11:08:49 +02:00
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
}
|
2015-10-16 11:08:49 +02:00
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
void updateLight() {
|
2015-11-20 21:38:45 +01:00
|
|
|
if (modeValue % 2 == 1) {
|
2015-11-19 02:21:51 +01:00
|
|
|
// Color cylce mode
|
|
|
|
unsigned short rgbColor[3];
|
|
|
|
rgbColor[0] = redValue;
|
|
|
|
rgbColor[1] = greenValue;
|
|
|
|
rgbColor[2] = blueValue;
|
|
|
|
|
|
|
|
unsigned short incColor = (decColor + 1) % 3;
|
|
|
|
|
|
|
|
rgbColor[decColor] -= 1;
|
|
|
|
rgbColor[incColor] += 1;
|
2015-10-16 11:08:49 +02:00
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
redValue = rgbColor[0];
|
|
|
|
greenValue = rgbColor[1];
|
|
|
|
blueValue = rgbColor[2];
|
2015-10-16 11:08:49 +02:00
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
if (rgbColor[incColor] == 255) {
|
|
|
|
decColor = incColor;
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 21:38:45 +01:00
|
|
|
|
|
|
|
if (modeValue > 1) {
|
|
|
|
// Breath mode
|
|
|
|
breathScale += breathAdjustment;
|
|
|
|
|
|
|
|
if (breathScale <= 0.001 || breathScale >= 0.999) {
|
|
|
|
breathAdjustment = 0 - breathAdjustment;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-19 02:21:51 +01:00
|
|
|
setLight(redValue, greenValue, blueValue);
|
2015-11-20 21:38:45 +01:00
|
|
|
delay(30);
|
2015-10-16 11:08:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Helper function to set the colors of the led strip */
|
2015-11-19 02:21:51 +01:00
|
|
|
void setLight(unsigned char red, unsigned char green, unsigned char blue) {
|
2015-11-20 21:38:45 +01:00
|
|
|
analogWrite(redPin, red * globalScale * breathScale * redScale);
|
|
|
|
analogWrite(greenPin, green * globalScale * breathScale * greenScale);
|
|
|
|
analogWrite(bluePin, blue * globalScale * breathScale * blueScale);
|
2015-11-19 02:21:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void readFromEEPROM() {
|
|
|
|
unsigned char newMode = EEPROM.read(modeAddress);
|
2015-11-20 21:38:45 +01:00
|
|
|
if (newMode > 3) {
|
2015-11-19 02:21:51 +01:00
|
|
|
// Uninitialized cells return 255, so reset that
|
|
|
|
modeValue = 0;
|
|
|
|
} else {
|
|
|
|
modeValue = newMode;
|
|
|
|
}
|
|
|
|
redValue = EEPROM.read(redAddress);
|
|
|
|
greenValue = EEPROM.read(greenAddress);
|
|
|
|
blueValue = EEPROM.read(blueAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void persistToEEPROM() {
|
|
|
|
EEPROM.write(modeAddress, modeValue);
|
|
|
|
EEPROM.write(redAddress, redValue);
|
|
|
|
EEPROM.write(greenAddress, greenValue);
|
|
|
|
EEPROM.write(blueAddress, blueValue);
|
|
|
|
EEPROM.commit();
|
2015-10-16 11:08:49 +02:00
|
|
|
}
|
2015-11-19 02:21:51 +01:00
|
|
|
|