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,96 +84,175 @@ 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
} Serial.println("Client connected");
while(!client.available()){
// Wait until the client sends some data delay(1);
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(); // Read the first line of the request
int green = req.substring(er+1,eg).toInt(); String req = client.readStringUntil('\r');
int blue = req.substring(eg+1,eb).toInt(); Serial.println(req);
client.flush();
// Sanitize values // Check if new value should be set or values should be read
if (red < 0) { red = 0; } if (req.indexOf("/set/") != -1) {
if (green < 0) { green = 0; } // Request of the form "/set/mode/red/green/blue/"
if (blue < 0) { blue = 0; } // Delimiters for values
if (red > 1023) { red = 1023; } int indexMode, indexRed, indexGreen, indexBlue, endIndexBlue;
if (green > 1023) { green = 1023; } indexMode = req.indexOf("/set/") + 5;
if (blue > 1023) { blue = 1023; } 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 // Update values
setColourRgb(red, green, blue); modeValue = mode;
} else { if (mode == 1) {
Serial.println("invalid request"); decColor = 0;
client.stop(); redValue = 255;
return; 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 redValue = rgbColor[0];
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"; greenValue = rgbColor[1];
blueValue = rgbColor[2];
// Send the response to the client if (rgbColor[incColor] == 255) {
client.print(s); decColor = incColor;
delay(1); }
}
Serial.println("Disconnecting client"); setLight(redValue, greenValue, blueValue);
// The client will actually be disconnected delay(15);
// when the function returns and 'client' object is detroyed
} }
/* 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();
}