2020-09-19 17:24:34 +00:00
|
|
|
/*
|
|
|
|
* ESP8266 RGBW controller
|
|
|
|
* Copyright 2020 (c) Mark van Renswoude
|
|
|
|
*
|
|
|
|
* https://git.x2software.net/pub/RGBWifi
|
|
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <ESP8266WiFi.h>
|
2020-09-20 09:08:20 +00:00
|
|
|
#include <ESP8266mDNS.h>
|
2020-09-19 17:24:34 +00:00
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
#include <LittleFS.h>
|
|
|
|
|
|
|
|
#include "./config.h"
|
|
|
|
#include "./debug.h"
|
|
|
|
#include "./global.h"
|
|
|
|
|
|
|
|
#include "./main.wifi.h"
|
|
|
|
#include "./main.led.h"
|
|
|
|
#include "./main.debug.h"
|
|
|
|
|
|
|
|
#include "./server/static.h"
|
|
|
|
#include "./server/settings.h"
|
|
|
|
#include "./server/firmware.h"
|
|
|
|
#include "./server/api.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
void handleNotFound(AsyncWebServerRequest* request);
|
|
|
|
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
_dinit();
|
|
|
|
currentTime = millis();
|
|
|
|
|
2020-09-20 09:08:20 +00:00
|
|
|
_dln("Starting LittleFS");
|
2020-09-19 17:24:34 +00:00
|
|
|
if (!LittleFS.begin())
|
|
|
|
_dln("Setup :: failed to mount file system");
|
|
|
|
|
2020-09-20 09:08:20 +00:00
|
|
|
_dln("Reading settings");
|
2020-09-19 17:24:34 +00:00
|
|
|
connectionSettings->read();
|
|
|
|
systemSettings->read();
|
|
|
|
|
2020-09-20 09:08:20 +00:00
|
|
|
_dln("Setting pin modes");
|
2020-09-19 17:24:34 +00:00
|
|
|
pinMode(systemSettings->pinAPButton(), INPUT_PULLUP);
|
|
|
|
pinMode(systemSettings->pinLEDAP(), OUTPUT);
|
|
|
|
pinMode(systemSettings->pinLEDSTA(), OUTPUT);
|
|
|
|
|
|
|
|
|
2020-09-20 10:58:27 +00:00
|
|
|
_dln("Initializing LED strip");
|
|
|
|
strip = new Strip();
|
|
|
|
strip->init(systemSettings->ledCount());
|
|
|
|
|
|
|
|
|
2020-09-19 17:24:34 +00:00
|
|
|
_dln("Setup :: starting initialization sequence");
|
2020-09-20 09:08:20 +00:00
|
|
|
for (uint8_t blink = 0; blink <= 1; blink++)
|
|
|
|
{
|
|
|
|
digitalWrite(systemSettings->pinLEDAP(), HIGH);
|
|
|
|
delay(100);
|
|
|
|
digitalWrite(systemSettings->pinLEDAP(), LOW);
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t blink = 0; blink <= 1; blink++)
|
|
|
|
{
|
|
|
|
digitalWrite(systemSettings->pinLEDSTA(), HIGH);
|
|
|
|
delay(100);
|
|
|
|
digitalWrite(systemSettings->pinLEDSTA(), LOW);
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
|
2020-09-19 17:24:34 +00:00
|
|
|
/*
|
|
|
|
uint8_t bottomStep = stepsSettings->count() - 1;
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < InitialisationBlinkCount; i++)
|
|
|
|
{
|
|
|
|
stairs->set(bottomStep, InitialisationBrightness, InitialisationFadeTime);
|
|
|
|
waitForTransition();
|
|
|
|
|
|
|
|
stairs->set(bottomStep, 0, InitialisationFadeTime);
|
|
|
|
waitForTransition();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
_dln("Setup :: initializing WiFi");
|
|
|
|
WiFi.persistent(false);
|
|
|
|
WiFi.mode(WIFI_OFF);
|
|
|
|
|
|
|
|
initDebug();
|
|
|
|
initWiFi();
|
|
|
|
|
|
|
|
_dln("Setup :: registering routes");
|
|
|
|
registerStaticRoutes(&server);
|
|
|
|
registerAPIRoutes(&server);
|
|
|
|
registerSettingsRoutes(&server);
|
|
|
|
registerFirmwareRoutes(&server);
|
|
|
|
|
|
|
|
_dln("Setup :: starting HTTP server");
|
|
|
|
server.onNotFound(handleNotFound);
|
|
|
|
server.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
if (shouldReboot || systemSettingsChanged)
|
|
|
|
{
|
|
|
|
_dln("Loop :: reboot requested, so long and thanks for all the fish!");
|
|
|
|
delay(100);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTime = millis();
|
|
|
|
updateDebugStatus();
|
|
|
|
|
|
|
|
|
|
|
|
if (connectionSettingsChanged)
|
|
|
|
{
|
|
|
|
_dln("Loop :: connection settings changed");
|
|
|
|
initWiFi();
|
|
|
|
connectionSettingsChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
updateWiFi();
|
|
|
|
updateLED();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void handleNotFound(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
_d("HTTP :: not found: "); _dln(request->url());
|
|
|
|
request->send(404);
|
2020-09-20 10:48:57 +00:00
|
|
|
}
|