Improved ESP8266 WifiServer for RGB ribbon. Added option for color cycling and persistent storage in eeprom. Added command to retreive current values.

This commit is contained in:
Nis Boerge Wechselberg 2015-11-19 02:21:51 +01:00
parent bece994f1b
commit 587a7ed304

View file

@ -26,20 +26,39 @@
* ESP8266-RGB-WiFiServer
* Sets the color of an RGB LED Ribbon through an HTTP Server
********************************************************************************/
#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include "ESP8266-RGB-WiFiServer.h"
// Pin configuration
const int redPin = 12;
const int greenPin = 13;
const int bluePin = 14;
const short redPin = 12;
const short greenPin = 13;
const short bluePin = 14;
// 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
float globalScale = 3.0;
float redScale = 1.0;
float greenScale = 1.0;
float blueScale = 0.7;
// HTTP configuration
const unsigned char port = 80;
// Create an instance of the server
WiFiServer server(port);
void setup() {
// Initialize Serial connection
Serial.begin(115200);
@ -55,9 +74,7 @@ void setup() {
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
@ -67,96 +84,175 @@ void setup() {
// Start the server
server.begin();
Serial.println("Server started");
Serial.print("Server started at IP address ");
// Print the IP address
Serial.println(WiFi.localIP());
// Read stored mode and previous color values from EEPROM
EEPROM.begin(16);
readFromEEPROM();
// Test the LED color channels
setColourRgb(0,0,0);
setLight(0,0,0);
delay(500);
setColourRgb(200,0,0);
setLight(100,0,0);
delay(500);
setColourRgb(0,200,0);
setLight(0,100,0);
delay(500);
setColourRgb(0,0,200);
setLight(0,0,100);
delay(500);
setColourRgb(200,200,200);
setLight(100,100,100);
delay(500);
setColourRgb(0,0,0);
updateLight();
}
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;
if (client) {
// Wait until the client sends some data
Serial.println("Client connected");
while(!client.available()){
delay(1);
}
// 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();
// Read the first line of the request
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// 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; }
// 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 > 1) { mode = 1; }
if (red > 255) { red = 255; }
if (green > 255) { green = 255; }
if (blue > 255) { blue = 255; }
// Apply colors
setColourRgb(red, green, blue);
} else {
Serial.println("invalid request");
client.stop();
return;
// Update values
modeValue = mode;
if (mode == 1) {
decColor = 0;
redValue = 255;
greenValue = 0;
blueValue = 0;
} else {
redValue = red;
greenValue = green;
blueValue = blue;
}
persistToEEPROM();
}
} else if (req.indexOf("/get/") != -1) {
} else {
Serial.println("invalid request");
client.stop();
}
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);
client.flush();
Serial.println("Disconnecting client");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
updateLight();
}
client.flush();
void updateLight() {
if (modeValue == 0) {
// Normal static color mode
} else if (modeValue == 1) {
// 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;
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nOK</html>\n";
redValue = rgbColor[0];
greenValue = rgbColor[1];
blueValue = rgbColor[2];
// 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
if (rgbColor[incColor] == 255) {
decColor = incColor;
}
}
setLight(redValue, greenValue, blueValue);
delay(15);
}
/* 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);
void setLight(unsigned char red, unsigned char green, unsigned char blue) {
analogWrite(redPin, red * globalScale * redScale);
analogWrite(greenPin, green * globalScale * greenScale);
analogWrite(bluePin, blue * globalScale * blueScale);
}
void readFromEEPROM() {
unsigned char newMode = EEPROM.read(modeAddress);
if (newMode > 1) {
// 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();
}