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 * ESP8266-RGB-WiFiServer
* Sets the color of an RGB LED Ribbon through an HTTP Server * Sets the color of an RGB LED Ribbon through an HTTP Server
********************************************************************************/ ********************************************************************************/
#include <EEPROM.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "ESP8266-RGB-WiFiServer.h" #include "ESP8266-RGB-WiFiServer.h"
// Pin configuration // Pin configuration
const int redPin = 12; const short redPin = 12;
const int greenPin = 13; const short greenPin = 13;
const int bluePin = 14; 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 // HTTP configuration
const unsigned char port = 80; const unsigned char port = 80;
// Create an instance of the server
WiFiServer server(port); WiFiServer server(port);
void setup() { void setup() {
// Initialize Serial connection // Initialize Serial connection
Serial.begin(115200); Serial.begin(115200);
@ -55,9 +74,7 @@ void setup() {
Serial.println(); Serial.println();
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.println(ssid); Serial.println(ssid);
WiFi.begin(ssid, password); WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(500); delay(500);
Serial.print("."); Serial.print(".");
@ -67,34 +84,34 @@ void setup() {
// Start the server // Start the server
server.begin(); server.begin();
Serial.println("Server started"); Serial.print("Server started at IP address ");
// Print the IP address // Print the IP address
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
// Read stored mode and previous color values from EEPROM
EEPROM.begin(16);
readFromEEPROM();
// Test the LED color channels // Test the LED color channels
setColourRgb(0,0,0); setLight(0,0,0);
delay(500); delay(500);
setColourRgb(200,0,0); setLight(100,0,0);
delay(500); delay(500);
setColourRgb(0,200,0); setLight(0,100,0);
delay(500); delay(500);
setColourRgb(0,0,200); setLight(0,0,100);
delay(500); delay(500);
setColourRgb(200,200,200); setLight(100,100,100);
delay(500); delay(500);
setColourRgb(0,0,0); updateLight();
} }
void loop() { void loop() {
// Check if a client has connected // Check if a client has connected
WiFiClient client = server.available(); WiFiClient client = server.available();
if (!client) { if (client) {
return;
}
// Wait until the client sends some data // Wait until the client sends some data
Serial.println("new client"); Serial.println("Client connected");
while(!client.available()){ while(!client.available()){
delay(1); delay(1);
} }
@ -104,59 +121,138 @@ void loop() {
Serial.println(req); Serial.println(req);
client.flush(); client.flush();
// Match the request // Check if new value should be set or values should be read
if (req.indexOf("/gpio/rgb/") != -1) { if (req.indexOf("/set/") != -1) {
// Request of the form "/gpio/rgb/red/green/blue/" // Request of the form "/set/mode/red/green/blue/"
// Delimiters for pwm values // Delimiters for values
int br, er, eg, eb; int indexMode, indexRed, indexGreen, indexBlue, endIndexBlue;
br = req.indexOf("/gpio/rgb/") + 10; indexMode = req.indexOf("/set/") + 5;
er = req.indexOf("/",br+1); indexRed = req.indexOf("/",indexMode + 1);
eg = req.indexOf("/",er+1); indexGreen = req.indexOf("/",indexRed + 1);
eb = req.indexOf("/",eg+1); indexBlue = req.indexOf("/",indexGreen + 1);
endIndexBlue = req.indexOf("/",indexBlue + 1);
// Check if all values were filled in the request // Check if all values were filled in the request
if (br == -1 || er == -1 || eg == -1 || eb == -1) { if (indexMode == -1 || indexRed == -1 || indexGreen == -1
|| indexBlue == -1 || endIndexBlue == -1) {
// Invalid request, abort
Serial.println("Invalid request"); Serial.println("Invalid request");
client.stop(); client.stop();
return; } else {
}
// Parse values in request // Parse values in request
int red = req.substring(br,er).toInt(); int mode = req.substring(indexMode, indexRed).toInt();
int green = req.substring(er+1,eg).toInt(); int red = req.substring(indexRed + 1, indexGreen).toInt();
int blue = req.substring(eg+1,eb).toInt(); int green = req.substring(indexGreen + 1, indexBlue).toInt();
int blue = req.substring(indexBlue + 1, endIndexBlue).toInt();
// Sanitize values // Sanitize values
if (mode < 0) { mode = 0; }
if (red < 0) { red = 0; } if (red < 0) { red = 0; }
if (green < 0) { green = 0; } if (green < 0) { green = 0; }
if (blue < 0) { blue = 0; } if (blue < 0) { blue = 0; }
if (red > 1023) { red = 1023; } if (mode > 1) { mode = 1; }
if (green > 1023) { green = 1023; } if (red > 255) { red = 255; }
if (blue > 1023) { blue = 1023; } if (green > 255) { green = 255; }
if (blue > 255) { blue = 255; }
// 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) {
// Apply colors
setColourRgb(red, green, blue);
} else { } else {
Serial.println("invalid request"); Serial.println("invalid request");
client.stop(); client.stop();
return;
} }
client.flush(); client.flush();
// Prepare the response // 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"; 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 // Send the response to the client
client.print(s); client.print(response);
delay(1); delay(1);
client.flush();
Serial.println("Disconnecting client"); Serial.println("Disconnecting client");
// The client will actually be disconnected // The client will actually be disconnected
// when the function returns and 'client' object is detroyed // when the function returns and 'client' object is detroyed
}
updateLight();
}
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;
redValue = rgbColor[0];
greenValue = rgbColor[1];
blueValue = rgbColor[2];
if (rgbColor[incColor] == 255) {
decColor = incColor;
}
}
setLight(redValue, greenValue, blueValue);
delay(15);
} }
/* Helper function to set the colors of the led strip */ /* Helper function to set the colors of the led strip */
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { void setLight(unsigned char red, unsigned char green, unsigned char blue) {
analogWrite(redPin, red); analogWrite(redPin, red * globalScale * redScale);
analogWrite(greenPin, green); analogWrite(greenPin, green * globalScale * greenScale);
analogWrite(bluePin, blue); 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();
}