2016-12-31 00:38:51 +01:00
/*
* Setup code
*/
2016-11-19 19:39:17 +01:00
void wledInit ( )
{
Serial . begin ( 115200 ) ;
2017-05-08 21:00:06 +02:00
# ifdef USEFS
2016-11-19 19:39:17 +01:00
SPIFFS . begin ( ) ;
{
Dir dir = SPIFFS . openDir ( " / " ) ;
while ( dir . next ( ) ) {
String fileName = dir . fileName ( ) ;
size_t fileSize = dir . fileSize ( ) ;
2017-01-15 00:24:28 +01:00
# ifdef DEBUG
2016-11-19 19:39:17 +01:00
Serial . printf ( " FS File: %s, size: %s \n " , fileName . c_str ( ) , formatBytes ( fileSize ) . c_str ( ) ) ;
2017-01-15 00:24:28 +01:00
# endif
2016-11-19 19:39:17 +01:00
}
2017-01-15 00:24:28 +01:00
DEBUG_PRINTF ( " \n " ) ;
2016-11-19 19:39:17 +01:00
}
2017-05-08 21:00:06 +02:00
# endif
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " Init EEPROM " ) ;
2016-11-20 01:47:15 +01:00
EEPROM . begin ( 1024 ) ;
2017-12-11 23:59:12 +01:00
loadSettingsFromEEPROM ( true ) ;
2017-01-15 00:24:28 +01:00
DEBUG_PRINT ( " CC: SSID: " ) ;
DEBUG_PRINT ( clientssid ) ;
2016-11-19 19:39:17 +01:00
WiFi . disconnect ( ) ; //close old connections
if ( staticip [ 0 ] ! = 0 )
{
WiFi . config ( staticip , staticgateway , staticsubnet ) ;
} else
{
WiFi . config ( 0U , 0U , 0U ) ;
}
if ( apssid . length ( ) > 0 )
{
2017-01-15 00:24:28 +01:00
DEBUG_PRINT ( " USING AP " ) ;
DEBUG_PRINTLN ( apssid . length ( ) ) ;
2016-11-19 19:39:17 +01:00
initAP ( ) ;
} else
{
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " NO AP " ) ;
2016-11-19 19:39:17 +01:00
WiFi . softAPdisconnect ( true ) ;
}
initCon ( ) ;
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " " ) ;
DEBUG_PRINT ( " Connected! IP address: " ) ;
DEBUG_PRINTLN ( WiFi . localIP ( ) ) ;
2016-11-19 19:39:17 +01:00
// Set up mDNS responder:
2017-11-20 00:07:37 +01:00
if ( cmdns ! = NULL & & ! onlyAP & & ! MDNS . begin ( cmdns . c_str ( ) ) ) {
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " Error setting up MDNS responder! " ) ;
2016-11-19 19:39:17 +01:00
down ( ) ;
}
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " mDNS responder started " ) ;
2016-11-27 22:37:51 +01:00
2017-03-20 19:56:07 +01:00
if ( udpPort > 0 & & udpPort ! = ntpLocalPort & & WiFi . status ( ) = = WL_CONNECTED )
2016-12-11 20:11:14 +01:00
{
udpConnected = notifierUdp . begin ( udpPort ) ;
}
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
//SERVER INIT
//settings page
server . on ( " /settings " , HTTP_GET , [ ] ( ) {
2017-11-20 00:07:37 +01:00
String settingsBuffer = getSettings ( ) ;
server . setContentLength ( strlen_P ( PAGE_settings0 ) + strlen_P ( PAGE_settings1 ) + settingsBuffer . length ( ) ) ;
server . send ( 200 , " text/html " , " " ) ;
server . sendContent_P ( PAGE_settings0 ) ;
server . sendContent ( settingsBuffer ) ;
server . sendContent_P ( PAGE_settings1 ) ;
2016-11-19 19:39:17 +01:00
} ) ;
server . on ( " /favicon.ico " , HTTP_GET , [ ] ( ) {
2017-05-08 20:20:48 +02:00
if ( ! handleFileRead ( " /favicon.ico " ) ) server . send ( 200 , " image/x-icon " , favicon ) ;
2016-11-19 19:39:17 +01:00
} ) ;
server . on ( " / " , HTTP_GET , [ ] ( ) {
2017-12-09 01:29:09 +01:00
if ( ! handleFileRead ( " /index.htm " ) ) {
server . setContentLength ( strlen_P ( PAGE_index0 ) + strlen_P ( PAGE_index1 ) + strlen_P ( PAGE_index2 ) ) ;
server . send ( 200 , " text/html " , " " ) ;
server . sendContent_P ( PAGE_index0 ) ;
server . sendContent_P ( PAGE_index1 ) ;
server . sendContent_P ( PAGE_index2 ) ;
}
2016-11-19 19:39:17 +01:00
} ) ;
2016-11-20 01:47:15 +01:00
server . on ( " /reset " , HTTP_GET , [ ] ( ) {
2017-11-20 00:07:37 +01:00
server . send ( 200 , " text/plain " , " Rebooting... " ) ;
2016-11-20 01:47:15 +01:00
reset ( ) ;
} ) ;
2016-11-19 19:39:17 +01:00
server . on ( " /set-settings " , HTTP_POST , [ ] ( ) {
handleSettingsSet ( ) ;
2017-02-01 21:25:09 +01:00
if ( ! handleFileRead ( " /settingssaved.htm " ) ) server . send ( 200 , " text/html " , PAGE_settingssaved ) ;
2016-11-19 19:39:17 +01:00
} ) ;
2017-04-26 14:04:53 +02:00
server . on ( " /version " , HTTP_GET , [ ] ( ) {
server . send ( 200 , " text/plain " , ( String ) VERSION ) ;
} ) ;
2017-10-12 22:20:37 +02:00
server . on ( " /uptime " , HTTP_GET , [ ] ( ) {
server . send ( 200 , " text/plain " , ( String ) millis ( ) ) ;
} ) ;
server . on ( " /freeheap " , HTTP_GET , [ ] ( ) {
server . send ( 200 , " text/plain " , ( String ) ESP . getFreeHeap ( ) ) ;
} ) ;
2017-12-14 00:12:02 +01:00
server . on ( " /power " , HTTP_GET , [ ] ( ) {
String val = ( String ) ( int ) strip . getPowerEstimate ( ledcount , strip . getColor ( ) , strip . getBrightness ( ) ) ;
val + = " mA currently \n Notice: This is just an estimate which does not take into account several factors (like effects and wire resistance). It is NOT an accurate measurement! " ;
server . send ( 200 , " text/plain " , val ) ;
} ) ;
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
server . on ( " /down " , HTTP_GET , down ) ;
server . on ( " /cleareeprom " , HTTP_GET , clearEEPROM ) ;
//init ota page
httpUpdater . setup ( & server ) ;
2016-11-20 01:47:15 +01:00
} else
{
server . on ( " /edit " , HTTP_GET , [ ] ( ) {
server . send ( 500 , " text/plain " , " OTA lock active " ) ;
} ) ;
server . on ( " /down " , HTTP_GET , [ ] ( ) {
server . send ( 500 , " text/plain " , " OTA lock active " ) ;
} ) ;
server . on ( " /cleareeprom " , HTTP_GET , [ ] ( ) {
server . send ( 500 , " text/plain " , " OTA lock active " ) ;
} ) ;
server . on ( " /update " , HTTP_GET , [ ] ( ) {
server . send ( 500 , " text/plain " , " OTA lock active " ) ;
} ) ;
2016-11-26 19:34:05 +01:00
server . on ( " /list " , HTTP_GET , [ ] ( ) {
server . send ( 500 , " text/plain " , " OTA lock active " ) ;
} ) ;
2016-11-19 19:39:17 +01:00
}
//called when the url is not defined here, ajax-in; get-settings
server . onNotFound ( [ ] ( ) {
if ( ! handleSet ( server . uri ( ) ) ) {
server . send ( 404 , " text/plain " , " FileNotFound " ) ;
}
} ) ;
server . begin ( ) ;
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " HTTP server started " ) ;
2016-11-19 19:39:17 +01:00
// Add service to MDNS
MDNS . addService ( " http " , " tcp " , 80 ) ;
2017-02-21 23:59:47 +01:00
//Init alexa service
alexaInit ( ) ;
2017-02-24 23:21:48 +01:00
2016-11-19 19:39:17 +01:00
// Initialize NeoPixel Strip
2016-12-14 23:40:47 +01:00
strip . init ( ) ;
2017-02-24 23:21:48 +01:00
strip . setLedCount ( ledcount ) ;
2016-12-29 00:03:58 +01:00
strip . setMode ( effectCurrent ) ;
2016-12-14 23:40:47 +01:00
strip . setColor ( 0 ) ;
strip . setSpeed ( effectSpeed ) ;
strip . setBrightness ( 255 ) ;
strip . start ( ) ;
2017-12-14 00:12:02 +01:00
# ifdef CRONIXIE
strip . driverModeCronixie ( true ) ;
# endif
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 ) ;
pinMode ( buttonPin , INPUT_PULLUP ) ;
}
void initAP ( ) {
WiFi . softAP ( apssid . c_str ( ) , appass . c_str ( ) , apchannel , aphide ) ;
}
void initCon ( )
{
int fail_count = 0 ;
if ( clientssid . length ( ) < 1 ) fail_count = 33 ;
WiFi . begin ( clientssid . c_str ( ) , clientpass . c_str ( ) ) ;
while ( WiFi . status ( ) ! = WL_CONNECTED ) {
delay ( 500 ) ;
2017-01-15 00:24:28 +01:00
DEBUG_PRINTLN ( " C_NC " ) ;
2016-11-19 19:39:17 +01:00
fail_count + + ;
2017-11-20 00:07:37 +01:00
if ( ! recoveryAPDisabled & & fail_count > apWaitTimeSecs * 2 )
2016-11-19 19:39:17 +01:00
{
WiFi . disconnect ( ) ;
2017-11-20 00:07:37 +01:00
DEBUG_PRINTLN ( " Can't connect. Opening AP... " ) ;
2016-11-19 19:39:17 +01:00
String save = apssid ;
2017-11-20 00:07:37 +01:00
onlyAP = true ;
2017-12-14 00:12:02 +01:00
# ifdef CRONIXIE
if ( apssid . length ( ) < 1 ) apssid = " CRONIXIE-AP " ;
# else
if ( apssid . length ( ) < 1 ) apssid = " WLED-AP " ;
# endif
2016-11-19 19:39:17 +01:00
initAP ( ) ;
apssid = save ;
return ;
}
}
}