2016-12-31 00:38:51 +01:00
/*
* Setup code
*/
2016-11-19 19:39:17 +01:00
void wledInit ( )
2018-04-15 15:27:54 +02:00
{
EEPROM . begin ( EEPSIZE ) ;
2018-08-11 22:57:13 +02:00
showWelcomePage = ( EEPROM . read ( 233 ) ! = 233 ) ;
2018-05-10 19:55:58 +02:00
ledCount = ( ( EEPROM . read ( 229 ) < < 0 ) & 0xFF ) + ( ( EEPROM . read ( 398 ) < < 8 ) & 0xFF00 ) ; if ( ledCount > 1200 | | ledCount = = 0 ) ledCount = 10 ;
2018-04-15 15:27:54 +02:00
//RMT eats up too much RAM
# ifdef ARDUINO_ARCH_ESP32
if ( ledCount > 600 ) ledCount = 600 ;
# endif
2018-06-24 01:20:15 +02:00
if ( ! EEPROM . read ( 397 ) ) strip . init ( EEPROM . read ( 372 ) , ledCount , PIN , EEPROM . read ( 2204 ) ) ; //quick init
2018-04-15 15:27:54 +02:00
2016-11-19 19:39:17 +01:00
Serial . begin ( 115200 ) ;
2018-04-11 23:50:35 +02:00
Serial . setTimeout ( 50 ) ;
2018-03-15 12:04:14 +01:00
2017-05-08 21:00:06 +02:00
# ifdef USEFS
2016-11-19 19:39:17 +01:00
SPIFFS . begin ( ) ;
2017-05-08 21:00:06 +02:00
# endif
2018-03-15 12:04:14 +01:00
DEBUG_PRINTLN ( " Load EEPROM " ) ;
2017-12-11 23:59:12 +01:00
loadSettingsFromEEPROM ( true ) ;
2018-03-15 12:04:14 +01:00
if ( ! initLedsLast ) initStrip ( ) ;
2018-04-11 23:50:35 +02:00
DEBUG_PRINT ( " CSSID: " ) ;
2018-03-14 13:16:28 +01:00
DEBUG_PRINT ( clientSSID ) ;
2018-02-20 22:29:48 +01:00
buildCssColorString ( ) ;
2018-02-25 14:46:11 +01:00
userBeginPreConnection ( ) ;
2016-11-19 19:39:17 +01:00
initCon ( ) ;
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " " ) ;
DEBUG_PRINT ( " Connected! IP address: " ) ;
DEBUG_PRINTLN ( WiFi . localIP ( ) ) ;
2018-02-28 00:27:10 +01:00
if ( hueIP [ 0 ] = = 0 )
{
hueIP [ 0 ] = WiFi . localIP ( ) [ 0 ] ;
hueIP [ 1 ] = WiFi . localIP ( ) [ 1 ] ;
hueIP [ 2 ] = WiFi . localIP ( ) [ 2 ] ;
}
2016-11-27 22:37:51 +01:00
2018-08-11 22:57:13 +02:00
if ( udpPort > 0 & & udpPort ! = ntpLocalPort )
2016-12-11 20:11:14 +01:00
{
udpConnected = notifierUdp . begin ( udpPort ) ;
2018-05-18 23:24:47 +02:00
if ( udpConnected & & udpRgbPort ! = udpPort ) udpRgbConnected = rgbUdp . begin ( udpRgbPort ) ;
2016-12-11 20:11:14 +01:00
}
2017-03-20 19:56:07 +01:00
if ( ntpEnabled & & WiFi . status ( ) = = WL_CONNECTED )
2017-02-07 16:02:27 +01:00
ntpConnected = ntpUdp . begin ( ntpLocalPort ) ;
2016-11-19 19:39:17 +01:00
2018-03-14 11:41:24 +01:00
//start captive portal
2018-07-21 23:21:07 +02:00
if ( onlyAP | | strlen ( apSSID ) > 0 )
2018-03-14 11:41:24 +01:00
{
dnsServer . setErrorReplyCode ( DNSReplyCode : : NoError ) ;
dnsServer . start ( 53 , " * " , WiFi . softAPIP ( ) ) ;
dnsActive = true ;
}
2018-04-11 23:50:35 +02:00
if ( ! initLedsLast ) strip . service ( ) ;
2016-11-19 19:39:17 +01:00
//SERVER INIT
//settings page
server . on ( " /settings " , HTTP_GET , [ ] ( ) {
2018-02-20 22:29:48 +01:00
serveSettings ( 0 ) ;
} ) ;
server . on ( " /settings/wifi " , HTTP_GET , [ ] ( ) {
2018-02-23 01:33:32 +01:00
if ( ! ( wifiLock & & otaLock ) )
{
serveSettings ( 1 ) ;
} else {
serveMessage ( 500 , " Access Denied " , txd , 254 ) ;
}
2018-02-20 22:29:48 +01:00
} ) ;
server . on ( " /settings/leds " , HTTP_GET , [ ] ( ) {
serveSettings ( 2 ) ;
} ) ;
server . on ( " /settings/ui " , HTTP_GET , [ ] ( ) {
serveSettings ( 3 ) ;
} ) ;
server . on ( " /settings/sync " , HTTP_GET , [ ] ( ) {
serveSettings ( 4 ) ;
2016-11-19 19:39:17 +01:00
} ) ;
2018-02-20 22:29:48 +01:00
server . on ( " /settings/time " , HTTP_GET , [ ] ( ) {
serveSettings ( 5 ) ;
} ) ;
server . on ( " /settings/sec " , HTTP_GET , [ ] ( ) {
serveSettings ( 6 ) ;
} ) ;
2016-11-19 19:39:17 +01:00
server . on ( " /favicon.ico " , HTTP_GET , [ ] ( ) {
2018-02-23 01:33:32 +01:00
if ( ! handleFileRead ( " /favicon.ico " ) )
{
server . send_P ( 200 , " image/x-icon " , favicon , 156 ) ;
}
2016-11-19 19:39:17 +01:00
} ) ;
2018-02-20 22:29:48 +01:00
2016-11-19 19:39:17 +01:00
server . on ( " / " , HTTP_GET , [ ] ( ) {
2018-03-14 11:41:24 +01:00
serveIndexOrWelcome ( ) ;
} ) ;
server . on ( " /generate_204 " , HTTP_GET , [ ] ( ) {
serveIndexOrWelcome ( ) ;
} ) ;
server . on ( " /fwlink " , HTTP_GET , [ ] ( ) {
serveIndexOrWelcome ( ) ;
2016-11-19 19:39:17 +01:00
} ) ;
2018-02-20 22:29:48 +01:00
server . on ( " /sliders " , HTTP_GET , serveIndex ) ;
server . on ( " /welcome " , HTTP_GET , [ ] ( ) {
serveSettings ( 255 ) ;
} ) ;
2016-11-20 01:47:15 +01:00
server . on ( " /reset " , HTTP_GET , [ ] ( ) {
2018-03-15 12:04:14 +01:00
serveMessage ( 200 , " Rebooting now... " , " (takes ~20 seconds, wait for auto-redirect) " , 79 ) ;
2016-11-20 01:47:15 +01:00
reset ( ) ;
} ) ;
2018-02-20 22:29:48 +01:00
server . on ( " /settings/wifi " , HTTP_POST , [ ] ( ) {
2018-02-23 01:33:32 +01:00
if ( ! ( wifiLock & & otaLock ) ) handleSettingsSet ( 1 ) ;
serveMessage ( 200 , " WiFi settings saved. " , " Rebooting now... (takes ~20 seconds, wait for auto-redirect) " , 139 ) ;
2018-02-20 22:29:48 +01:00
reset ( ) ;
} ) ;
server . on ( " /settings/leds " , HTTP_POST , [ ] ( ) {
handleSettingsSet ( 2 ) ;
2018-02-23 01:33:32 +01:00
serveMessage ( 200 , " LED settings saved. " , " Redirecting... " , 1 ) ;
2018-02-20 22:29:48 +01:00
} ) ;
server . on ( " /settings/ui " , HTTP_POST , [ ] ( ) {
handleSettingsSet ( 3 ) ;
2018-02-23 01:33:32 +01:00
serveMessage ( 200 , " UI settings saved. " , " Reloading to apply theme... " , 122 ) ;
2018-02-20 22:29:48 +01:00
} ) ;
server . on ( " /settings/sync " , HTTP_POST , [ ] ( ) {
handleSettingsSet ( 4 ) ;
2018-02-28 00:27:10 +01:00
if ( hueAttempt )
{
serveMessage ( 200 , " Hue setup result " , hueError , 253 ) ;
} else {
serveMessage ( 200 , " Sync settings saved. " , " Redirecting... " , 1 ) ;
}
hueAttempt = false ;
2016-11-19 19:39:17 +01:00
} ) ;
2018-02-20 22:29:48 +01:00
server . on ( " /settings/time " , HTTP_POST , [ ] ( ) {
handleSettingsSet ( 5 ) ;
2018-02-23 01:33:32 +01:00
serveMessage ( 200 , " Time settings saved. " , " Redirecting... " , 1 ) ;
2018-02-20 22:29:48 +01:00
} ) ;
server . on ( " /settings/sec " , HTTP_POST , [ ] ( ) {
handleSettingsSet ( 6 ) ;
2018-02-23 01:33:32 +01:00
serveMessage ( 200 , " Security settings saved. " , " Rebooting now... (takes ~20 seconds, wait for auto-redirect) " , 139 ) ;
2018-02-20 22:29:48 +01:00
reset ( ) ;
} ) ;
2017-04-26 14:04:53 +02:00
server . on ( " /version " , HTTP_GET , [ ] ( ) {
server . send ( 200 , " text/plain " , ( String ) VERSION ) ;
} ) ;
2018-02-20 22:29:48 +01:00
2017-10-12 22:20:37 +02:00
server . on ( " /uptime " , HTTP_GET , [ ] ( ) {
server . send ( 200 , " text/plain " , ( String ) millis ( ) ) ;
} ) ;
2018-02-20 22:29:48 +01:00
2017-10-12 22:20:37 +02:00
server . on ( " /freeheap " , HTTP_GET , [ ] ( ) {
server . send ( 200 , " text/plain " , ( String ) ESP . getFreeHeap ( ) ) ;
} ) ;
2018-02-20 22:29:48 +01:00
2017-12-14 00:12:02 +01:00
server . on ( " /power " , HTTP_GET , [ ] ( ) {
2018-03-14 13:16:28 +01:00
String val = ( String ) ( int ) strip . getPowerEstimate ( ledCount , strip . getColor ( ) , strip . getBrightness ( ) ) ;
2018-02-23 01:33:32 +01:00
val + = " mA currently " ;
serveMessage ( 200 , val , " This is just an estimate (does not take into account several factors like effects and wire resistance). It is NOT an accurate measurement! " , 254 ) ;
2017-12-14 00:12:02 +01:00
} ) ;
2018-04-24 12:03:25 +02:00
server . on ( " /u " , HTTP_GET , [ ] ( ) {
2018-05-10 19:55:58 +02:00
server . setContentLength ( strlen_P ( PAGE_usermod ) ) ;
server . send ( 200 , " text/html " , " " ) ;
server . sendContent_P ( PAGE_usermod ) ;
2018-04-24 12:03:25 +02:00
} ) ;
2018-02-20 22:29:48 +01:00
2018-01-15 00:20:23 +01:00
server . on ( " /teapot " , HTTP_GET , [ ] ( ) {
2018-02-23 01:33:32 +01:00
serveMessage ( 418 , " 418. I'm a teapot. " , " (Tangible Embedded Advanced Project Of Twinkling) " , 254 ) ;
2018-01-15 00:20:23 +01:00
} ) ;
2018-02-20 22:29:48 +01:00
2018-01-10 00:37:45 +01:00
server . on ( " /build " , HTTP_GET , [ ] ( ) {
2018-07-20 19:35:31 +02:00
getBuildInfo ( ) ;
server . send ( 200 , " text/plain " , obuf ) ;
2018-01-10 00:37:45 +01:00
} ) ;
2018-01-15 00:20:23 +01:00
//if OTA is allowed
2017-11-20 00:07:37 +01:00
if ( ! otaLock ) {
2016-11-19 19:39:17 +01:00
server . on ( " /edit " , HTTP_GET , [ ] ( ) {
2017-02-01 21:25:09 +01:00
if ( ! handleFileRead ( " /edit.htm " ) ) server . send ( 200 , " text/html " , PAGE_edit ) ;
2016-11-19 19:39:17 +01:00
} ) ;
2017-05-08 21:46:04 +02:00
# ifdef USEFS
2016-11-19 19:39:17 +01:00
server . on ( " /edit " , HTTP_PUT , handleFileCreate ) ;
server . on ( " /edit " , HTTP_DELETE , handleFileDelete ) ;
server . on ( " /edit " , HTTP_POST , [ ] ( ) { server . send ( 200 , " text/plain " , " " ) ; } , handleFileUpload ) ;
2017-05-08 21:46:04 +02:00
server . on ( " /list " , HTTP_GET , handleFileList ) ;
# endif
2016-11-19 19:39:17 +01:00
//init ota page
2018-01-16 14:48:02 +01:00
httpUpdater . setup ( & server ) ;
2016-11-20 01:47:15 +01:00
} else
{
server . on ( " /edit " , HTTP_GET , [ ] ( ) {
2018-02-23 01:33:32 +01:00
serveMessage ( 500 , " Access Denied " , txd , 254 ) ;
2016-11-20 01:47:15 +01:00
} ) ;
server . on ( " /update " , HTTP_GET , [ ] ( ) {
2018-02-23 01:33:32 +01:00
serveMessage ( 500 , " Access Denied " , txd , 254 ) ;
2016-11-20 01:47:15 +01:00
} ) ;
2016-11-26 19:34:05 +01:00
server . on ( " /list " , HTTP_GET , [ ] ( ) {
2018-02-23 01:33:32 +01:00
serveMessage ( 500 , " Access Denied " , txd , 254 ) ;
2016-11-26 19:34:05 +01:00
} ) ;
2016-11-19 19:39:17 +01:00
}
//called when the url is not defined here, ajax-in; get-settings
server . onNotFound ( [ ] ( ) {
2018-01-09 11:55:07 +01:00
DEBUG_PRINTLN ( " Not-Found HTTP call: " ) ;
DEBUG_PRINTLN ( " URI: " + server . uri ( ) ) ;
DEBUG_PRINTLN ( " Body: " + server . arg ( 0 ) ) ;
2016-11-19 19:39:17 +01:00
if ( ! handleSet ( server . uri ( ) ) ) {
2018-01-09 11:55:07 +01:00
if ( ! handleAlexaApiCall ( server . uri ( ) , server . arg ( 0 ) ) )
2018-02-20 22:29:48 +01:00
server . send ( 404 , " text/plain " , " Not Found " ) ;
2016-11-19 19:39:17 +01:00
}
} ) ;
2018-06-24 01:20:15 +02:00
# ifndef ARDUINO_ARCH_ESP32
2018-05-22 21:11:19 +02:00
const char * headerkeys [ ] = { " User-Agent " } ;
2018-08-05 19:22:48 +02:00
server . collectHeaders ( headerkeys , sizeof ( headerkeys ) / sizeof ( char * ) ) ;
2018-06-24 01:20:15 +02:00
# else
String ua = " User-Agent " ;
server . collectHeaders ( ua ) ;
# endif
2018-05-22 21:11:19 +02:00
2018-04-11 23:50:35 +02:00
if ( ! initLedsLast ) strip . service ( ) ;
2018-01-09 14:21:37 +01:00
//init Alexa hue emulation
if ( alexaEnabled ) alexaInit ( ) ;
2016-11-19 19:39:17 +01:00
server . begin ( ) ;
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " HTTP server started " ) ;
2018-01-14 00:53:16 +01:00
2018-02-20 22:29:48 +01:00
//init ArduinoOTA
if ( aOtaEnabled )
{
ArduinoOTA . onStart ( [ ] ( ) {
# ifndef ARDUINO_ARCH_ESP32
wifi_set_sleep_type ( NONE_SLEEP_T ) ;
# endif
DEBUG_PRINTLN ( " Start ArduinoOTA " ) ;
} ) ;
2018-07-21 23:21:07 +02:00
if ( strlen ( cmDNS ) > 0 ) ArduinoOTA . setHostname ( cmDNS ) ;
2018-02-20 22:29:48 +01:00
ArduinoOTA . begin ( ) ;
}
2018-05-10 19:55:58 +02:00
if ( ! initLedsLast ) strip . service ( ) ;
// Set up mDNS responder:
2018-07-21 23:21:07 +02:00
if ( strlen ( cmDNS ) > 0 & & ! onlyAP )
2018-05-10 19:55:58 +02:00
{
2018-07-21 23:21:07 +02:00
MDNS . begin ( cmDNS ) ;
2018-05-10 19:55:58 +02:00
DEBUG_PRINTLN ( " mDNS responder started " ) ;
// Add service to MDNS
MDNS . addService ( " http " , " tcp " , 80 ) ;
2018-07-16 11:50:09 +02:00
}
2018-07-21 23:21:07 +02:00
initBlynk ( blynkApiKey ) ;
2018-08-11 22:57:13 +02:00
initE131 ( ) ;
2018-05-10 19:55:58 +02:00
2018-03-15 12:04:14 +01:00
if ( initLedsLast ) initStrip ( ) ;
2018-02-25 14:46:11 +01:00
userBegin ( ) ;
2018-03-15 12:04:14 +01:00
if ( macroBoot > 0 ) applyMacro ( macroBoot ) ;
2018-04-11 23:50:35 +02:00
Serial . println ( " Ada " ) ;
2018-03-15 12:04:14 +01:00
}
2018-02-25 14:46:11 +01:00
2018-03-15 12:04:14 +01:00
void initStrip ( )
{
// Initialize NeoPixel Strip and button
2018-06-24 01:20:15 +02:00
if ( initLedsLast ) strip . init ( useRGBW , ledCount , PIN , skipFirstLed ) ;
2018-03-06 23:47:08 +01:00
strip . setReverseMode ( reverseMode ) ;
2016-12-14 23:40:47 +01:00
strip . setColor ( 0 ) ;
strip . setBrightness ( 255 ) ;
strip . start ( ) ;
2018-01-14 00:53:16 +01:00
2018-01-15 00:20:23 +01:00
pinMode ( buttonPin , INPUT_PULLUP ) ;
2018-05-10 19:55:58 +02:00
pinMode ( 4 , OUTPUT ) ; //this is only needed in special cases
digitalWrite ( 4 , LOW ) ;
2018-03-06 23:47:08 +01:00
2017-12-12 14:52:28 +01:00
if ( bootPreset > 0 ) applyPreset ( bootPreset , turnOnAtBoot , true , true ) ;
2016-11-19 19:39:17 +01:00
colorUpdated ( 0 ) ;
2018-01-15 00:20:23 +01:00
if ( digitalRead ( buttonPin ) = = LOW ) buttonEnabled = false ; //disable button if it is "pressed" unintentionally
2016-11-19 19:39:17 +01:00
}
void initAP ( ) {
2018-07-21 23:21:07 +02:00
bool set = apSSID [ 0 ] ;
if ( ! set ) strcpy ( apSSID , " WLED-AP " ) ;
WiFi . softAP ( apSSID , apPass , apChannel , apHide ) ;
if ( ! set ) apSSID [ 0 ] = 0 ;
2016-11-19 19:39:17 +01:00
}
void initCon ( )
{
2018-04-01 00:08:50 +02:00
WiFi . disconnect ( ) ; //close old connections
if ( staticIP [ 0 ] ! = 0 )
{
WiFi . config ( staticIP , staticGateway , staticSubnet , staticDNS ) ;
} else
{
WiFi . config ( 0U , 0U , 0U ) ;
}
2018-07-21 23:21:07 +02:00
if ( strlen ( apSSID ) > 0 )
2018-04-01 00:08:50 +02:00
{
DEBUG_PRINT ( " USING AP " ) ;
2018-07-21 23:21:07 +02:00
DEBUG_PRINTLN ( strlen ( apSSID ) ) ;
2018-04-01 00:08:50 +02:00
initAP ( ) ;
} else
{
DEBUG_PRINTLN ( " NO AP " ) ;
WiFi . softAPdisconnect ( true ) ;
}
2016-11-19 19:39:17 +01:00
int fail_count = 0 ;
2018-07-21 23:21:07 +02:00
if ( strlen ( clientSSID ) < 1 | | strcmp ( clientSSID , " Your_Network " ) = = 0 ) fail_count = apWaitTimeSecs * 2 ; //instantly go to ap mode
2018-06-24 01:20:15 +02:00
# ifndef ARDUINO_ARCH_ESP32
WiFi . hostname ( serverDescription ) ;
# endif
2018-07-21 23:21:07 +02:00
WiFi . begin ( clientSSID , clientPass ) ;
2018-06-24 01:20:15 +02:00
# ifdef ARDUINO_ARCH_ESP32
2018-07-21 23:21:07 +02:00
WiFi . setHostname ( serverDescription ) ;
2018-06-24 01:20:15 +02:00
# endif
2018-03-15 12:04:14 +01:00
unsigned long lastTry = 0 ;
bool con = false ;
while ( ! con )
{
yield ( ) ;
if ( ! initLedsLast )
2016-11-19 19:39:17 +01:00
{
2018-03-15 12:04:14 +01:00
handleTransitions ( ) ;
handleButton ( ) ;
handleOverlays ( ) ;
if ( briT ) strip . service ( ) ;
}
if ( millis ( ) - lastTry > 499 ) {
con = ( WiFi . status ( ) = = WL_CONNECTED ) ;
lastTry = millis ( ) ;
DEBUG_PRINTLN ( " C_NC " ) ;
if ( ! recoveryAPDisabled & & fail_count > apWaitTimeSecs * 2 )
{
WiFi . disconnect ( ) ;
DEBUG_PRINTLN ( " Can't connect. Opening AP... " ) ;
onlyAP = true ;
initAP ( ) ;
return ;
}
fail_count + + ;
2016-11-19 19:39:17 +01:00
}
}
}
2018-01-27 23:28:20 +01:00
void buildCssColorString ( )
{
2018-02-23 01:33:32 +01:00
String cs [ ] = { " " , " " , " " , " " , " " , " " } ;
2018-02-20 22:29:48 +01:00
switch ( currentTheme )
{
2018-02-23 01:33:32 +01:00
default : cs [ 0 ] = " D9B310 " ; cs [ 1 ] = " 0B3C5D " ; cs [ 2 ] = " 1D2731 " ; cs [ 3 ] = " 328CC1 " ; cs [ 4 ] = " 000 " ; cs [ 5 ] = " 328CC1 " ; break ; //night
case 1 : cs [ 0 ] = " eee " ; cs [ 1 ] = " ddd " ; cs [ 2 ] = " b9b9b9 " ; cs [ 3 ] = " 049 " ; cs [ 4 ] = " 777 " ; cs [ 5 ] = " 049 " ; break ; //modern
case 2 : cs [ 0 ] = " abc " ; cs [ 1 ] = " fff " ; cs [ 2 ] = " ddd " ; cs [ 3 ] = " 000 " ; cs [ 4 ] = " 0004 " ; cs [ 5 ] = " 000 " ; break ; //bright
case 3 : cs [ 0 ] = " c09f80 " ; cs [ 1 ] = " d7cec7 " ; cs [ 2 ] = " 76323f " ; cs [ 3 ] = " 888 " ; cs [ 4 ] = " 3334 " ; cs [ 5 ] = " 888 " ; break ; //wine
case 4 : cs [ 0 ] = " 3cc47c " ; cs [ 1 ] = " 828081 " ; cs [ 2 ] = " d9a803 " ; cs [ 3 ] = " 1e392a " ; cs [ 4 ] = " 000a " ; cs [ 5 ] = " 1e392a " ; break ; //electric
case 5 : cs [ 0 ] = " 57bc90 " ; cs [ 1 ] = " a5a5af " ; cs [ 2 ] = " 015249 " ; cs [ 3 ] = " 88c9d4 " ; cs [ 4 ] = " 0004 " ; cs [ 5 ] = " 88c9d4 " ; break ; //mint
case 6 : cs [ 0 ] = " f7c331 " ; cs [ 1 ] = " dcc7aa " ; cs [ 2 ] = " 6b7a8f " ; cs [ 3 ] = " f7882f " ; cs [ 4 ] = " 0007 " ; cs [ 5 ] = " f7882f " ; break ; //amber
case 7 : cs [ 0 ] = " fc3 " ; cs [ 1 ] = " 124 " ; cs [ 2 ] = " 334 " ; cs [ 3 ] = " f1d " ; cs [ 4 ] = " f00 " ; cs [ 5 ] = " f1d " ; break ; //club
case 8 : cs [ 0 ] = " 0ac " ; cs [ 1 ] = " 124 " ; cs [ 2 ] = " 224 " ; cs [ 3 ] = " 003eff " ; cs [ 4 ] = " 003eff " ; cs [ 5 ] = " 003eff " ; break ; //air
case 9 : cs [ 0 ] = " f70 " ; cs [ 1 ] = " 421 " ; cs [ 2 ] = " 221 " ; cs [ 3 ] = " a50 " ; cs [ 4 ] = " f70 " ; cs [ 5 ] = " f70 " ; break ; //nixie
case 10 : cs [ 0 ] = " 2d2 " ; cs [ 1 ] = " 010 " ; cs [ 2 ] = " 121 " ; cs [ 3 ] = " 060 " ; cs [ 4 ] = " 040 " ; cs [ 5 ] = " 3f3 " ; break ; //terminal
2018-03-06 23:47:08 +01:00
case 11 : cs [ 0 ] = " 867ADE " ; cs [ 1 ] = " 4033A3 " ; cs [ 2 ] = " 483AAA " ; cs [ 3 ] = " 483AAA " ; cs [ 4 ] = " " ; cs [ 5 ] = " 867ADE " ; break ; //c64
2018-04-01 00:08:50 +02:00
case 12 : cs [ 0 ] = " fbe8a6 " ; cs [ 1 ] = " d2fdff " ; cs [ 2 ] = " b4dfe5 " ; cs [ 3 ] = " f4976c " ; cs [ 4 ] = " " ; cs [ 5 ] = " 303c6c " ; break ; //c64
2018-02-23 01:33:32 +01:00
case 14 : cs [ 0 ] = " fc7 " ; cs [ 1 ] = " 49274a " ; cs [ 2 ] = " 94618e " ; cs [ 3 ] = " f4decb " ; cs [ 4 ] = " 0008 " ; cs [ 5 ] = " f4decb " ; break ; //end
case 15 : for ( int i = 0 ; i < 6 ; i + + ) cs [ i ] = cssCol [ i ] ; //custom
2018-02-20 22:29:48 +01:00
}
cssColorString = " <style>:root{--aCol:# " ;
2018-02-23 01:33:32 +01:00
cssColorString + = cs [ 0 ] ;
2018-02-20 22:29:48 +01:00
cssColorString + = " ;--bCol:# " ;
2018-02-23 01:33:32 +01:00
cssColorString + = cs [ 1 ] ;
2018-02-20 22:29:48 +01:00
cssColorString + = " ;--cCol:# " ;
2018-02-23 01:33:32 +01:00
cssColorString + = cs [ 2 ] ;
2018-02-20 22:29:48 +01:00
cssColorString + = " ;--dCol:# " ;
2018-02-23 01:33:32 +01:00
cssColorString + = cs [ 3 ] ;
2018-02-20 22:29:48 +01:00
cssColorString + = " ;--sCol:# " ;
2018-02-23 01:33:32 +01:00
cssColorString + = cs [ 4 ] ;
2018-02-20 22:29:48 +01:00
cssColorString + = " ;--tCol:# " ;
2018-02-23 01:33:32 +01:00
cssColorString + = cs [ 5 ] ;
cssColorString + = " ;--cFn: " ;
cssColorString + = cssFont ;
2018-01-27 23:28:20 +01:00
cssColorString + = " ;} " ;
}
2018-03-14 11:41:24 +01:00
void serveIndexOrWelcome ( )
{
if ( ! showWelcomePage ) {
if ( ! handleFileRead ( " /index.htm " ) ) {
serveIndex ( ) ;
}
} else {
if ( ! handleFileRead ( " /welcome.htm " ) ) {
serveSettings ( 255 ) ;
}
2018-08-11 22:57:13 +02:00
showWelcomePage = false ;
2018-03-14 11:41:24 +01:00
}
}
2018-06-24 01:20:15 +02:00
void serveRealtimeError ( bool settings )
{
String mesg = " The " ;
mesg + = ( settings ) ? " settings " : " WLED " ;
2018-08-11 22:57:13 +02:00
mesg + = " UI is not available while receiving real-time data ( " ;
if ( realtimeIP [ 0 ] = = 0 )
2018-06-24 01:20:15 +02:00
{
2018-08-11 22:57:13 +02:00
mesg + = " E1.31 " ;
} else {
mesg + = " UDP from " ;
mesg + = realtimeIP [ 0 ] ;
for ( int i = 1 ; i < 4 ; i + + )
{
mesg + = " . " ;
mesg + = realtimeIP [ i ] ;
}
2018-06-24 01:20:15 +02:00
}
mesg + = " ). " ;
server . send ( 200 , " text/plain " , mesg ) ;
}
2018-01-27 23:28:20 +01:00
void serveIndex ( )
{
2018-05-22 21:11:19 +02:00
bool serveMobile = false ;
if ( uiConfiguration = = 0 ) serveMobile = checkClientIsMobile ( server . header ( " User-Agent " ) ) ;
else if ( uiConfiguration = = 2 ) serveMobile = true ;
2018-05-10 19:55:58 +02:00
if ( ! arlsTimeout | | enableRealtimeUI ) //do not serve while receiving realtime
2018-01-27 23:28:20 +01:00
{
2018-05-22 21:11:19 +02:00
if ( serveMobile )
{
server . setContentLength ( strlen_P ( PAGE_indexM ) ) ;
server . send ( 200 , " text/html " , " " ) ;
server . sendContent_P ( PAGE_indexM ) ;
} else
{
server . setContentLength ( strlen_P ( PAGE_index0 ) + cssColorString . length ( ) + strlen_P ( PAGE_index1 ) + strlen_P ( PAGE_index2 ) + strlen_P ( PAGE_index3 ) ) ;
server . send ( 200 , " text/html " , " " ) ;
server . sendContent_P ( PAGE_index0 ) ;
server . sendContent ( cssColorString ) ;
server . sendContent_P ( PAGE_index1 ) ;
server . sendContent_P ( PAGE_index2 ) ;
server . sendContent_P ( PAGE_index3 ) ;
}
2018-01-27 23:28:20 +01:00
} else {
2018-06-24 01:20:15 +02:00
serveRealtimeError ( false ) ;
2018-01-27 23:28:20 +01:00
}
}
2018-02-23 01:33:32 +01:00
void serveMessage ( int code , String headl , String subl = " " , int optionType )
2018-02-20 22:29:48 +01:00
{
String messageBody = " <h2> " ;
messageBody + = headl ;
messageBody + = " </h2> " ;
messageBody + = subl ;
2018-02-23 01:33:32 +01:00
switch ( optionType )
{
case 255 : break ; //simple message
case 254 : messageBody + = " <br><br><button type= \" button \" onclick= \" B() \" >Back</button> " ; break ; //back button
case 253 : messageBody + = " <br><br><form action=/settings><button type=submit>Back</button></form> " ; //button to settings
}
if ( optionType < 60 ) //redirect to settings after optionType seconds
{
messageBody + = " <script>setTimeout(RS, " + String ( optionType * 1000 ) + " )</script> " ;
} else if ( optionType < 120 ) //redirect back after optionType-60 seconds
2018-02-20 22:29:48 +01:00
{
2018-02-23 01:33:32 +01:00
messageBody + = " <script>setTimeout(B, " + String ( ( optionType - 60 ) * 1000 ) + " )</script> " ;
} else if ( optionType < 180 ) //reload parent after optionType-120 seconds
2018-02-20 22:29:48 +01:00
{
2018-02-23 01:33:32 +01:00
messageBody + = " <script>setTimeout(RP, " + String ( ( optionType - 120 ) * 1000 ) + " )</script> " ;
2018-02-20 22:29:48 +01:00
}
messageBody + = " </body></html> " ;
server . setContentLength ( strlen_P ( PAGE_msg0 ) + cssColorString . length ( ) + strlen_P ( PAGE_msg1 ) + messageBody . length ( ) ) ;
server . send ( code , " text/html " , " " ) ;
server . sendContent_P ( PAGE_msg0 ) ;
server . sendContent ( cssColorString ) ;
server . sendContent_P ( PAGE_msg1 ) ;
server . sendContent ( messageBody ) ;
}
2018-03-14 13:16:28 +01:00
void serveSettings ( byte subPage )
2018-01-27 23:28:20 +01:00
{
2018-02-20 22:29:48 +01:00
//0: menu 1: wifi 2: leds 3: ui 4: sync 5: time 6: sec 255: welcomepage
2018-05-10 19:55:58 +02:00
if ( ! arlsTimeout | | enableRealtimeUI ) //do not serve while receiving realtime
2018-02-20 22:29:48 +01:00
{
int pl0 , pl1 ;
switch ( subPage )
{
case 1 : pl0 = strlen_P ( PAGE_settings_wifi0 ) ; pl1 = strlen_P ( PAGE_settings_wifi1 ) ; break ;
case 2 : pl0 = strlen_P ( PAGE_settings_leds0 ) ; pl1 = strlen_P ( PAGE_settings_leds1 ) ; break ;
case 3 : pl0 = strlen_P ( PAGE_settings_ui0 ) ; pl1 = strlen_P ( PAGE_settings_ui1 ) ; break ;
case 4 : pl0 = strlen_P ( PAGE_settings_sync0 ) ; pl1 = strlen_P ( PAGE_settings_sync1 ) ; break ;
case 5 : pl0 = strlen_P ( PAGE_settings_time0 ) ; pl1 = strlen_P ( PAGE_settings_time1 ) ; break ;
case 6 : pl0 = strlen_P ( PAGE_settings_sec0 ) ; pl1 = strlen_P ( PAGE_settings_sec1 ) ; break ;
case 255 : pl0 = strlen_P ( PAGE_welcome0 ) ; pl1 = strlen_P ( PAGE_welcome1 ) ; break ;
default : pl0 = strlen_P ( PAGE_settings0 ) ; pl1 = strlen_P ( PAGE_settings1 ) ;
}
2018-07-20 19:35:31 +02:00
getSettingsJS ( subPage ) ;
2018-02-20 22:29:48 +01:00
int sCssLength = ( subPage > 0 & & subPage < 7 ) ? strlen_P ( PAGE_settingsCss ) : 0 ;
2018-07-20 19:35:31 +02:00
server . setContentLength ( pl0 + cssColorString . length ( ) + olen + sCssLength + pl1 ) ;
2018-02-20 22:29:48 +01:00
server . send ( 200 , " text/html " , " " ) ;
switch ( subPage )
{
case 1 : server . sendContent_P ( PAGE_settings_wifi0 ) ; break ;
case 2 : server . sendContent_P ( PAGE_settings_leds0 ) ; break ;
case 3 : server . sendContent_P ( PAGE_settings_ui0 ) ; break ;
case 4 : server . sendContent_P ( PAGE_settings_sync0 ) ; break ;
case 5 : server . sendContent_P ( PAGE_settings_time0 ) ; break ;
case 6 : server . sendContent_P ( PAGE_settings_sec0 ) ; break ;
case 255 : server . sendContent_P ( PAGE_welcome0 ) ; break ;
default : server . sendContent_P ( PAGE_settings0 ) ;
}
2018-07-20 19:35:31 +02:00
server . sendContent ( obuf ) ;
2018-02-20 22:29:48 +01:00
server . sendContent ( cssColorString ) ;
if ( subPage > 0 & & subPage < 7 ) server . sendContent_P ( PAGE_settingsCss ) ;
switch ( subPage )
{
case 1 : server . sendContent_P ( PAGE_settings_wifi1 ) ; break ;
case 2 : server . sendContent_P ( PAGE_settings_leds1 ) ; break ;
case 3 : server . sendContent_P ( PAGE_settings_ui1 ) ; break ;
case 4 : server . sendContent_P ( PAGE_settings_sync1 ) ; break ;
case 5 : server . sendContent_P ( PAGE_settings_time1 ) ; break ;
case 6 : server . sendContent_P ( PAGE_settings_sec1 ) ; break ;
case 255 : server . sendContent_P ( PAGE_welcome1 ) ; break ;
default : server . sendContent_P ( PAGE_settings1 ) ;
}
} else {
2018-06-24 01:20:15 +02:00
serveRealtimeError ( true ) ;
2018-02-20 22:29:48 +01:00
}
2018-01-27 23:28:20 +01:00
}
2018-07-20 19:35:31 +02:00
void getBuildInfo ( )
2018-04-11 23:50:35 +02:00
{
2018-07-20 19:35:31 +02:00
//fill string buffer with build info
olen = 0 ;
oappend ( " hard-coded build info: \r \n \n " ) ;
2018-04-11 23:50:35 +02:00
# ifdef ARDUINO_ARCH_ESP32
2018-07-20 19:35:31 +02:00
oappend ( " platform: esp32 " ) ;
2018-04-11 23:50:35 +02:00
# else
2018-07-20 19:35:31 +02:00
oappend ( " platform: esp8266 " ) ;
2018-04-11 23:50:35 +02:00
# endif
2018-07-20 19:35:31 +02:00
oappend ( " \r \n version: " ) ;
oappend ( versionString ) ;
oappend ( " \r \n build: " ) ;
oappendi ( VERSION ) ;
oappend ( " \r \n eepver: " ) ;
oappendi ( EEPVER ) ;
2018-04-11 23:50:35 +02:00
# ifdef USEFS
2018-07-20 19:35:31 +02:00
oappend ( " \r \n spiffs: true \r \n " ) ;
2018-04-11 23:50:35 +02:00
# else
2018-07-20 19:35:31 +02:00
oappend ( " \r \n spiffs: false \r \n " ) ;
2018-04-11 23:50:35 +02:00
# endif
# ifdef DEBUG
2018-07-20 19:35:31 +02:00
oappend ( " debug: true \r \n " ) ;
2018-04-11 23:50:35 +02:00
# else
2018-07-20 19:35:31 +02:00
oappend ( " debug: false \r \n " ) ;
2018-04-11 23:50:35 +02:00
# endif
2018-07-20 19:35:31 +02:00
oappend ( " button-pin: gpio " ) ;
oappendi ( buttonPin ) ;
oappend ( " \r \n " ) ;
2018-04-11 23:50:35 +02:00
# ifdef ARDUINO_ARCH_ESP32
2018-07-20 19:35:31 +02:00
oappend ( " strip-pin: gpio " ) ;
oappendi ( PIN ) ;
2018-04-11 23:50:35 +02:00
# else
2018-07-20 19:35:31 +02:00
oappend ( " strip-pin: gpio2 " ) ;
2018-04-11 23:50:35 +02:00
# endif
2018-08-11 22:57:13 +02:00
oappend ( " \r \n build-type: src \r \n " ) ;
2018-04-11 23:50:35 +02:00
}
2018-01-27 23:28:20 +01:00
2018-05-22 21:11:19 +02:00
bool checkClientIsMobile ( String useragent )
{
//to save complexity this function is not comprehensive
if ( useragent . indexOf ( " Android " ) > = 0 ) return true ;
if ( useragent . indexOf ( " iPhone " ) > = 0 ) return true ;
if ( useragent . indexOf ( " iPod " ) > = 0 ) return true ;
return false ;
}
2018-01-27 23:28:20 +01:00