Hello,
I'm trying to convert a ASCII String containing HEX color values to something useful like integers.
The code passes a HEX colorcode as String UrlColor = server.arg ("Color") from a web page.
First the %23 ("#"sign) is removed to only get the hex color in a String object.
Now i would like to change this String Object to set the color of RGB leds.
This is where i get stuck, how to get the 3 blocks of true (HEX,DEC,BIN) Integer from a string?
The Code So Far
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
//WiFi Settings
const char* ssid = "SIDD";
const char* password = "PASS";
//MDNS Settings
const char* MSDNhostname = "esp8266";
// Url variables
String UrlColor = "ff0000";
String Range = "50";
// Initialize Webserver And MDNS
ESP8266WebServer server(80);
MDNSResponder mdns;
void setup ()
{
//Setup Serial interface
Serial.begin(115200);
//Setup WiFi
WiFi.begin(ssid, password);
Serial.print("Connecting To WiFi Network: ");
Serial.println(ssid);
// Wait For WiFi Connection
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println("");
// Show Connection info
Serial.print("Connected To Wifi Network: ");
Serial.println(ssid);
//Start WebServer
server.begin();
Serial.print("HTTP Server: ");
Serial.print ("http://");
Serial.println (WiFi.localIP());
//Start MDNSserver
mdns.begin(MSDNhostname, WiFi.localIP());
Serial.print("MDNS Server: ");
Serial.print ("http://");
Serial.print (MSDNhostname);
Serial.println (".local");
//WebServer Calls
server.on("/", handleRoot);
server.onNotFound(handleNotFound);
}
void loop(void)
{
//Allow ESP to MultiTask and prevent code from Blocking WiFi functions etc.
yield();
//Handle WebServerClients
server.handleClient();
}
void handleRoot() {
if (server.hasArg ("Color"))
{
//Store server.arg("Color") to String UrlColor
UrlColor = server.arg ("Color");
// Remove first 3 places of UrlColor ("#" = %23) to get a HexColor String
UrlColor.remove(0, 3);
}
//Show Received UrlColor String containing HEX values on Serial
Serial.print("Color Received From Browser: ");
Serial.println(UrlColor);
if (server.hasArg ("Range"))
{
//Store server.arg("Range") to String Range
Range = server.arg ("Range");
}
//Show Received Range String as integer on Serial
Serial.print("Range Received From Browser: ");
Serial.println(Range.toInt());
String HtmlDocument = "<!DOCTYPE html>";
HtmlDocument += "<html>";
HtmlDocument += "<body>";
HtmlDocument += "<form action='/' method='post'>";
HtmlDocument += "<input type='Range' onchange='this.form.submit()' name='Range' min='0' max='255'value='"+Range+"' style='width: 98%;'>";
HtmlDocument += "
";
HtmlDocument += "<input type='color' onchange='this.form.submit()' name='Color' value='#"+UrlColor+"' style='width: 98%;'>";
HtmlDocument += "</form>";
HtmlDocument += "</body>";
HtmlDocument += "</html>";
server.send(200, "text/html", HtmlDocument);
}
void handleNotFound(){
String HtmlDocument = "<!DOCTYPE html>";
HtmlDocument += "<html>";
HtmlDocument += "<body>";
HtmlDocument += "<h1>File Not Found !</h1>";
HtmlDocument += "<p>URI: "+server.uri()+"</p>";
HtmlDocument += "</body>";
HtmlDocument += "</html>";
server.send(404, "text/html", HtmlDocument);
}
Thanks in Advance