2017-03-23 18:45:29 +00:00
|
|
|
/*
|
|
|
|
* Stairs lighting
|
|
|
|
* Copyright 2017 (c) Mark van Renswoude
|
2018-01-01 18:56:07 +00:00
|
|
|
*
|
|
|
|
* https://git.x2software.net/pub/Stairs
|
2017-03-23 18:45:29 +00:00
|
|
|
*/
|
2018-01-01 18:56:07 +00:00
|
|
|
#include <Arduino.h>
|
2017-03-23 18:45:29 +00:00
|
|
|
#include <ESP8266WiFi.h>
|
2018-01-16 21:07:20 +00:00
|
|
|
//#include <ESPAsyncTCP.h>
|
|
|
|
#include <WiFiUDP.h>
|
2018-01-01 18:56:07 +00:00
|
|
|
#include <ESPAsyncWebServer.h>
|
2018-01-18 22:20:31 +00:00
|
|
|
#include <ESPAsyncTCP.h>
|
2018-01-05 20:15:04 +00:00
|
|
|
#include <TimeLib.h>
|
2018-01-18 22:20:31 +00:00
|
|
|
#include <ArduinoJson.h>
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include <user_interface.h>
|
|
|
|
}
|
2017-03-23 18:45:29 +00:00
|
|
|
|
2018-01-14 20:31:37 +00:00
|
|
|
#include "./config.h"
|
|
|
|
#include "./debug.h"
|
|
|
|
#include "./global.h"
|
|
|
|
#include "./components/PCA9685.h"
|
|
|
|
#include "./settings/connection.h"
|
|
|
|
#include "./server/static.h"
|
|
|
|
#include "./server/settings.h"
|
|
|
|
#include "./server/firmware.h"
|
|
|
|
#include "./server/api.h"
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-16 21:07:20 +00:00
|
|
|
#include "./main.wifi.h"
|
|
|
|
#include "./main.debug.h"
|
|
|
|
#include "./main.led.h"
|
|
|
|
#include "./main.triggers.h"
|
|
|
|
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
ADC_MODE(ADC_VCC);
|
|
|
|
|
2018-01-11 20:03:46 +00:00
|
|
|
|
2018-01-16 21:07:20 +00:00
|
|
|
// Forward declarations
|
2018-01-01 18:56:07 +00:00
|
|
|
void handleNotFound(AsyncWebServerRequest* request);
|
|
|
|
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
2018-01-04 21:53:16 +00:00
|
|
|
PCA9685* pwmDriver;
|
|
|
|
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
_dinit();
|
2018-01-03 20:44:34 +00:00
|
|
|
|
2018-01-01 18:56:07 +00:00
|
|
|
currentTime = millis();
|
|
|
|
|
|
|
|
if (!SPIFFS.begin())
|
|
|
|
_dln("Setup :: failed to mount file system");
|
|
|
|
|
|
|
|
connectionSettings->read();
|
2018-01-08 20:58:22 +00:00
|
|
|
systemSettings->read();
|
2018-01-04 21:53:16 +00:00
|
|
|
stepsSettings->read();
|
2018-01-10 20:04:55 +00:00
|
|
|
timeTriggerSettings->read();
|
2018-01-14 20:31:37 +00:00
|
|
|
motionTriggerSettings->read();
|
2018-01-04 21:53:16 +00:00
|
|
|
|
2018-01-08 20:58:22 +00:00
|
|
|
pinMode(systemSettings->pinAPButton(), INPUT_PULLUP);
|
|
|
|
pinMode(systemSettings->pinLEDAP(), OUTPUT);
|
|
|
|
pinMode(systemSettings->pinLEDSTA(), OUTPUT);
|
2018-01-14 20:31:37 +00:00
|
|
|
initMotionPins();
|
2018-01-07 22:12:42 +00:00
|
|
|
|
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
_dln("Setup :: initializing PCA9685");
|
|
|
|
pwmDriver = new PCA9685();
|
2018-01-08 20:58:22 +00:00
|
|
|
pwmDriver->setAddress(systemSettings->pwmDriverAddress(), systemSettings->pinPWMDriverSDA(), systemSettings->pinPWMDriverSCL());
|
|
|
|
pwmDriver->setPWMFrequency(systemSettings->pwmDriverFrequency());
|
2018-01-05 19:02:30 +00:00
|
|
|
pwmDriver->setAll(0);
|
2018-01-04 21:53:16 +00:00
|
|
|
|
|
|
|
_dln("Setup :: initializing Stairs");
|
|
|
|
stairs = new Stairs();
|
|
|
|
stairs->init(pwmDriver);
|
|
|
|
|
|
|
|
|
|
|
|
_dln("Setup :: starting initialization sequence");
|
|
|
|
stairs->set(0, 255);
|
|
|
|
delay(300);
|
|
|
|
|
|
|
|
uint8_t stepCount = stepsSettings->count();
|
|
|
|
for (int step = 1; step < stepCount; step++)
|
|
|
|
{
|
|
|
|
stairs->set(step - 1, 0);
|
|
|
|
stairs->set(step, 255);
|
|
|
|
delay(300);
|
|
|
|
}
|
|
|
|
|
|
|
|
stairs->set(stepCount - 1, 0);
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
_dln("Setup :: initializing WiFi");
|
|
|
|
WiFi.persistent(false);
|
|
|
|
WiFi.mode(WIFI_OFF);
|
|
|
|
|
2018-01-16 21:07:20 +00:00
|
|
|
initDebug();
|
2018-01-01 18:56:07 +00:00
|
|
|
initWiFi();
|
|
|
|
|
|
|
|
_dln("Setup :: registering routes");
|
|
|
|
registerStaticRoutes(&server);
|
2018-01-04 21:53:16 +00:00
|
|
|
registerSettingsRoutes(&server);
|
2018-01-03 20:44:34 +00:00
|
|
|
registerFirmwareRoutes(&server);
|
2018-01-04 21:53:16 +00:00
|
|
|
registerAPIRoutes(&server);
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
_dln("Setup :: starting HTTP server");
|
|
|
|
server.onNotFound(handleNotFound);
|
|
|
|
server.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
2018-01-08 20:58:22 +00:00
|
|
|
if (shouldReboot || systemSettingsChanged)
|
2018-01-03 20:44:34 +00:00
|
|
|
{
|
2018-01-04 21:53:16 +00:00
|
|
|
_dln("Loop :: reboot requested, so long and thanks for all the fish!");
|
2018-01-03 20:44:34 +00:00
|
|
|
delay(100);
|
|
|
|
ESP.restart();
|
|
|
|
}
|
|
|
|
|
2018-01-14 20:31:37 +00:00
|
|
|
if (motionTriggerSettingsChanged)
|
|
|
|
{
|
|
|
|
initMotionPins();
|
|
|
|
motionTriggerSettingsChanged = false;
|
|
|
|
}
|
|
|
|
|
2018-01-01 18:56:07 +00:00
|
|
|
|
2018-01-16 21:07:20 +00:00
|
|
|
currentTime = millis();
|
2018-01-05 20:15:04 +00:00
|
|
|
updateDebugStatus();
|
2018-01-03 20:44:34 +00:00
|
|
|
|
|
|
|
|
2018-01-01 18:56:07 +00:00
|
|
|
if (connectionSettingsChanged)
|
|
|
|
{
|
|
|
|
_dln("Loop :: connection settings changed");
|
|
|
|
initWiFi();
|
|
|
|
connectionSettingsChanged = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-16 21:07:20 +00:00
|
|
|
updateWiFi();
|
2018-01-01 18:56:07 +00:00
|
|
|
updateLED();
|
2018-01-05 20:15:04 +00:00
|
|
|
updateNTPClient();
|
2018-01-14 20:31:37 +00:00
|
|
|
checkTriggers();
|
2018-01-07 22:12:42 +00:00
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
stairs->tick();
|
2018-01-01 18:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-04 21:53:16 +00:00
|
|
|
void handleNotFound(AsyncWebServerRequest *request)
|
2017-03-23 18:45:29 +00:00
|
|
|
{
|
2018-01-04 21:53:16 +00:00
|
|
|
_d("HTTP :: not found: "); _dln(request->url());
|
|
|
|
request->send(404);
|
|
|
|
}
|