Arduino/ESP8266-RGB-WiFiServer/ESP8266-RGB-WiFiServer.ino

286 lines
8.1 KiB
Arduino
Raw Normal View History

/********************************************************************************
* 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 <EEPROM.h>
#include <ESP8266WiFi.h>
#include "ESP8266-RGB-WiFiServer.h"
// Pin configuration
const short redPin = 14;
const short greenPin = 16;
const short bluePin = 12;
// 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
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;
// HTTP configuration
const unsigned char port = 80;
WiFiServer server(port);
void setup() {
2016-01-15 20:42:10 +01:00
// Set wifi mode to client
WiFi.mode(WIFI_STA);
// 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();
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
setLight(0,0,0);
delay(500);
setLight(100,0,0);
delay(500);
setLight(0,100,0);
delay(500);
setLight(0,0,100);
delay(500);
setLight(100,100,100);
delay(500);
updateLight();
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
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(".");
}
2017-02-17 23:31:10 +01:00
Serial.println("");
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();
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();
}
2017-02-17 23:31:10 +01:00
} else if (req.indexOf("/get/") != -1) {
} else {
Serial.println("invalid request");
client.stop();
}
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);
}
client.flush();
Serial.println("Disconnecting client");
// The client will actually be disconnected
// when the function returns and 'client' object is detroyed
}
updateLight();
}
void updateLight() {
if (modeValue % 2 == 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;
}
}
if (modeValue > 1) {
// Breath mode
breathScale += breathAdjustment;
if (breathScale <= 0.001 || breathScale >= 0.999) {
breathAdjustment = 0 - breathAdjustment;
}
}
setLight(redValue, greenValue, blueValue);
delay(30);
}
/* Helper function to set the colors of the led strip */
void setLight(unsigned char red, unsigned char green, unsigned char blue) {
analogWrite(redPin, red * globalScale * breathScale * redScale);
analogWrite(greenPin, green * globalScale * breathScale * greenScale);
analogWrite(bluePin, blue * globalScale * breathScale * blueScale);
}
void readFromEEPROM() {
unsigned char newMode = EEPROM.read(modeAddress);
if (newMode > 3) {
// 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();
}