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-12-04 00:58:06 +01:00
|
|
|
ledCount = EEPROM.read(229) + ((EEPROM.read(398) << 8) & 0xFF00);
|
|
|
|
if (ledCount > 1200 || ledCount == 0) ledCount = 30;
|
2019-02-20 23:44:34 +01:00
|
|
|
#ifndef ARDUINO_ARCH_ESP32
|
|
|
|
#if LEDPIN == 3
|
|
|
|
if (ledCount > 300) ledCount = 300; //DMA method uses too much ram
|
|
|
|
#endif
|
2018-04-15 15:27:54 +02:00
|
|
|
#endif
|
2016-11-19 19:39:17 +01:00
|
|
|
Serial.begin(115200);
|
2018-04-11 23:50:35 +02:00
|
|
|
Serial.setTimeout(50);
|
2019-03-07 16:35:02 +01:00
|
|
|
DEBUG_PRINTLN();
|
|
|
|
DEBUG_PRINT("---WLED "); DEBUG_PRINT(versionString); DEBUG_PRINT(" "); DEBUG_PRINT(VERSION); DEBUG_PRINTLN(" INIT---");
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
DEBUG_PRINT("esp32 "); DEBUG_PRINTLN(ESP.getSdkVersion());
|
|
|
|
#else
|
|
|
|
DEBUG_PRINT("esp8266 "); DEBUG_PRINTLN(ESP.getCoreVersion());
|
|
|
|
#endif
|
|
|
|
int heapPreAlloc = ESP.getFreeHeap();
|
|
|
|
DEBUG_PRINT("heap ");
|
|
|
|
DEBUG_PRINTLN(ESP.getFreeHeap());
|
2018-03-15 12:04:14 +01:00
|
|
|
|
2019-02-11 23:49:04 +01:00
|
|
|
strip.init(EEPROM.read(372),ledCount,EEPROM.read(2204)); //init LEDs quickly
|
2019-03-07 16:35:02 +01:00
|
|
|
|
|
|
|
DEBUG_PRINT("LEDs inited. heap usage ~");
|
|
|
|
DEBUG_PRINTLN(heapPreAlloc - ESP.getFreeHeap());
|
|
|
|
|
2019-03-16 02:09:37 +01:00
|
|
|
#ifndef WLED_DISABLE_FILESYSTEM
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
SPIFFS.begin(true);
|
|
|
|
#endif
|
|
|
|
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);
|
2019-02-11 23:49:04 +01:00
|
|
|
beginStrip();
|
2018-04-11 23:50:35 +02:00
|
|
|
DEBUG_PRINT("CSSID: ");
|
2018-03-14 13:16:28 +01:00
|
|
|
DEBUG_PRINT(clientSSID);
|
2018-02-25 14:46:11 +01:00
|
|
|
userBeginPreConnection();
|
2018-09-08 20:26:04 +02:00
|
|
|
if (strcmp(clientSSID,"Your_Network") == 0) showWelcomePage = true;
|
2019-01-31 23:42:48 +01:00
|
|
|
WiFi.persistent(false);
|
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-10-04 18:17:01 +02:00
|
|
|
//start captive portal if AP active
|
|
|
|
if (onlyAP || strlen(apSSID) > 0)
|
2018-03-14 11:41:24 +01:00
|
|
|
{
|
2019-02-16 00:21:22 +01:00
|
|
|
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);
|
|
|
|
dnsServer.start(53, "wled.me", WiFi.softAPIP());
|
2018-03-14 11:41:24 +01:00
|
|
|
dnsActive = true;
|
|
|
|
}
|
2018-10-04 18:17:01 +02:00
|
|
|
|
2018-09-28 23:53:51 +02:00
|
|
|
prepareIds(); //UUID from MAC (for Alexa and MQTT)
|
2019-02-05 19:40:24 +01:00
|
|
|
if (strcmp(cmDNS,"x") == 0) //fill in unique mdns default
|
|
|
|
{
|
|
|
|
strcpy(cmDNS, "wled-");
|
|
|
|
strcat(cmDNS, escapedMac.c_str());
|
|
|
|
}
|
2018-10-04 22:38:39 +02:00
|
|
|
if (mqttDeviceTopic[0] == 0)
|
|
|
|
{
|
|
|
|
strcpy(mqttDeviceTopic, "wled/");
|
|
|
|
strcat(mqttDeviceTopic, escapedMac.c_str());
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:17:01 +02:00
|
|
|
//smartInit, we only init some resources when connected
|
|
|
|
if (!onlyAP && WiFi.status() == WL_CONNECTED)
|
|
|
|
{
|
2019-02-17 19:21:09 +01:00
|
|
|
mqtt = new AsyncMqttClient();
|
|
|
|
initMqtt();
|
2018-10-04 18:17:01 +02:00
|
|
|
}
|
|
|
|
|
2019-02-11 23:49:04 +01:00
|
|
|
strip.service();
|
2018-02-20 22:29:48 +01:00
|
|
|
|
2018-09-30 20:24:57 +02:00
|
|
|
//HTTP server page init
|
|
|
|
initServer();
|
2018-05-22 21:11:19 +02:00
|
|
|
|
2019-02-11 23:49:04 +01:00
|
|
|
strip.service();
|
2018-01-09 14:21:37 +01:00
|
|
|
//init Alexa hue emulation
|
2018-10-04 18:17:01 +02:00
|
|
|
if (alexaEnabled && !onlyAP) alexaInit();
|
2016-11-19 19:39:17 +01:00
|
|
|
|
2019-02-16 00:21:22 +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
|
2018-10-04 18:17:01 +02:00
|
|
|
if (!onlyAP) {
|
2018-11-01 15:36:13 +01:00
|
|
|
#ifndef WLED_DISABLE_OTA
|
2018-10-04 18:17:01 +02:00
|
|
|
if (aOtaEnabled)
|
|
|
|
{
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
|
|
#ifndef ARDUINO_ARCH_ESP32
|
|
|
|
wifi_set_sleep_type(NONE_SLEEP_T);
|
|
|
|
#endif
|
|
|
|
DEBUG_PRINTLN("Start ArduinoOTA");
|
|
|
|
});
|
|
|
|
if (strlen(cmDNS) > 0) ArduinoOTA.setHostname(cmDNS);
|
|
|
|
ArduinoOTA.begin();
|
|
|
|
}
|
2018-11-01 15:36:13 +01:00
|
|
|
#endif
|
2018-10-04 18:17:01 +02:00
|
|
|
|
2019-02-11 23:49:04 +01:00
|
|
|
strip.service();
|
2018-10-04 18:17:01 +02:00
|
|
|
// Set up mDNS responder:
|
|
|
|
if (strlen(cmDNS) > 0 && !onlyAP)
|
|
|
|
{
|
|
|
|
MDNS.begin(cmDNS);
|
|
|
|
DEBUG_PRINTLN("mDNS responder started");
|
|
|
|
// Add service to MDNS
|
|
|
|
MDNS.addService("http", "tcp", 80);
|
2019-02-05 19:40:24 +01:00
|
|
|
MDNS.addService("wled", "tcp", 80);
|
2018-10-04 18:17:01 +02:00
|
|
|
}
|
2019-02-11 23:49:04 +01:00
|
|
|
strip.service();
|
2018-10-04 18:17:01 +02:00
|
|
|
|
|
|
|
initBlynk(blynkApiKey);
|
|
|
|
initE131();
|
2019-02-18 22:34:21 +01:00
|
|
|
reconnectHue();
|
2018-10-04 18:17:01 +02:00
|
|
|
} else {
|
|
|
|
e131Enabled = false;
|
2018-09-15 17:29:01 +02:00
|
|
|
}
|
2018-05-10 19:55:58 +02:00
|
|
|
|
2018-02-25 14:46:11 +01:00
|
|
|
userBegin();
|
2018-11-18 00:31:45 +01:00
|
|
|
|
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-11-24 11:52:23 +01:00
|
|
|
|
2019-02-11 23:49:04 +01:00
|
|
|
void beginStrip()
|
2018-03-15 12:04:14 +01:00
|
|
|
{
|
|
|
|
// Initialize NeoPixel Strip and button
|
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);
|
2018-01-14 00:53:16 +01:00
|
|
|
|
2019-05-01 03:09:08 +02:00
|
|
|
#ifdef BTNPIN
|
2018-11-18 00:31:45 +01:00
|
|
|
pinMode(BTNPIN, INPUT_PULLUP);
|
2019-05-01 03:09:08 +02:00
|
|
|
#endif
|
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-11-09 17:00:36 +01:00
|
|
|
|
2019-03-13 11:13:03 +01:00
|
|
|
//init relay pin
|
|
|
|
#if RLYPIN >= 0
|
|
|
|
pinMode(RLYPIN, OUTPUT);
|
|
|
|
#if RLYMDE
|
|
|
|
digitalWrite(RLYPIN, bri);
|
|
|
|
#else
|
|
|
|
digitalWrite(RLYPIN, !bri);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 17:00:36 +01:00
|
|
|
//disable button if it is "pressed" unintentionally
|
2019-05-01 03:09:08 +02:00
|
|
|
#ifdef BTNPIN
|
2018-11-18 00:31:45 +01:00
|
|
|
if(digitalRead(BTNPIN) == LOW) buttonEnabled = false;
|
2019-05-01 03:09:08 +02:00
|
|
|
#else
|
|
|
|
buttonEnabled = false;
|
|
|
|
#endif
|
2016-11-19 19:39:17 +01:00
|
|
|
}
|
|
|
|
|
2018-11-24 11:52:23 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-24 11:52:23 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2019-03-09 21:41:23 +01:00
|
|
|
WiFi.config(staticIP, staticGateway, staticSubnet, IPAddress(8,8,8,8));
|
2018-04-01 00:08:50 +02:00
|
|
|
} 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
|
|
|
{
|
2018-10-04 18:17:01 +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
|
|
|
|
{
|
2018-10-04 18:17:01 +02:00
|
|
|
DEBUG_PRINTLN(" NO AP");
|
2018-04-01 00:08:50 +02:00
|
|
|
WiFi.softAPdisconnect(true);
|
|
|
|
}
|
2016-11-19 19:39:17 +01:00
|
|
|
int fail_count = 0;
|
2018-11-09 17:00:36 +01: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
|
2018-11-09 17:00:36 +01:00
|
|
|
WiFi.hostname(serverDescription);
|
2018-06-24 01:20:15 +02:00
|
|
|
#endif
|
2018-11-09 17:00:36 +01:00
|
|
|
WiFi.begin(clientSSID, clientPass);
|
2018-06-24 01:20:15 +02:00
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
2018-11-09 17:00:36 +01: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();
|
2019-02-11 23:49:04 +01:00
|
|
|
handleTransitions();
|
|
|
|
handleButton();
|
|
|
|
handleOverlays();
|
|
|
|
if (briT) strip.service();
|
2018-03-15 12:04:14 +01:00
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|