Stairs/src/main.cpp

291 lines
6.3 KiB
C++
Raw Normal View History

/*
* Stairs lighting
* Copyright 2017 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/Stairs
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
extern "C" {
#include <user_interface.h>
}
#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"
ADC_MODE(ADC_VCC);
// Forward declarations
void initWiFi();
#ifdef SerialDebug
void wifiEvent(WiFiEvent_t event);
#endif
void updateLED();
void startServer();
void stopServer();
void handleNotFound(AsyncWebServerRequest* request);
AsyncWebServer server(80);
PCA9685* pwmDriver;
bool accessPoint = false;
bool stationMode = false;
bool forceAccessPoint = false;
uint32_t stationModeStart = 0;
#ifdef SerialDebug
uint32_t memoryStatusTime = 0;
#endif
void setup()
{
_dinit();
currentTime = millis();
if (!SPIFFS.begin())
_dln("Setup :: failed to mount file system");
connectionSettings->read();
stepsSettings->read();
_dln("Setup :: initializing PCA9685");
pwmDriver = new PCA9685();
pwmDriver->setAddress(PWMDriverAddress, PinSDA, PinSCL);
pwmDriver->setPWMFrequency(PWMDriverPWMFrequency);
_dln("Setup :: initializing Stairs");
stairs = new Stairs();
stairs->init(pwmDriver);
_dln("Setup :: starting initialization sequence");
stairs->setAll(0);
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);
_dln("Setup :: initializing WiFi");
WiFi.persistent(false);
WiFi.mode(WIFI_OFF);
#ifdef SerialDebug
// onEvent is already deprecated, but since I'm only using it
// for debug purposes we'll see how long it lasts...
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
WiFi.onEvent(wifiEvent);
_d("WiFi :: MAC address: ");
_dln(WiFi.macAddress());
#pragma GCC diagnostic pop
#endif
initWiFi();
_dln("Setup :: registering routes");
registerStaticRoutes(&server);
registerSettingsRoutes(&server);
registerFirmwareRoutes(&server);
registerAPIRoutes(&server);
_dln("Setup :: starting HTTP server");
server.onNotFound(handleNotFound);
server.begin();
}
void loop()
{
if (shouldReboot)
{
_dln("Loop :: reboot requested, so long and thanks for all the fish!");
delay(100);
ESP.restart();
}
currentTime = millis();
#ifdef SerialDebug
if (currentTime - memoryStatusTime >= 5000)
{
_d("Memory :: available: ");
_dln(ESP.getFreeHeap());
memoryStatusTime = currentTime;
}
#endif
if (connectionSettingsChanged)
{
_dln("Loop :: connection settings changed");
initWiFi();
connectionSettingsChanged = false;
}
if (stationModeStart > 0)
{
bool isConnected = WiFi.status() == WL_CONNECTED;
if (isConnected)
{
_d("WiFi :: connected, IP address: ");
_dln(WiFi.localIP());
stationModeStart = 0;
}
else if (stationMode && accessPoint &&
currentTime - stationModeStart >= StationModeTimeout)
{
_dln("WiFi :: unable to connect, switching off station mode, status:");
_dln(WiFi.status());
#ifdef SerialDebug
WiFi.printDiag(Serial);
#endif
// Connecting to access point is taking too long and is blocking
// the access point mode, stop trying
stationMode = false;
WiFi.disconnect();
WiFi.mode(WIFI_AP);
}
}
updateLED();
stairs->tick();
}
void initWiFi()
{
WiFi.disconnect();
WiFi.softAPdisconnect();
accessPoint = connectionSettings->flag(AccessPoint) || forceAccessPoint;
stationMode = connectionSettings->flag(StationMode) && connectionSettings->ssid() != NULL;
WiFi.mode(accessPoint && stationMode ? WIFI_AP_STA :
accessPoint ? WIFI_AP :
stationMode ? WIFI_STA :
WIFI_OFF);
if (accessPoint)
{
_dln("WiFi :: starting access point");
String ssidString = DefaultAPSSIDPrefix + String(ESP.getChipId(), HEX);
if (WiFi.softAP((const char *)ssidString.c_str()))
{
_d("WiFi :: IP address: ");
_dln(WiFi.softAPIP());
}
else
_d("WiFi :: failed to start soft access point");
}
if (stationMode)
{
_d("WiFi :: starting station mode to: ");
_dln(connectionSettings->ssid());
stationModeStart = currentTime;
if (WiFi.begin(connectionSettings->ssid(), connectionSettings->password()))
{
if (connectionSettings->flag(DHCP))
// I've had the same issue as described here with config(0, 0, 0):
// https://stackoverflow.com/questions/40069654/how-to-clear-static-ip-configuration-and-start-dhcp
wifi_station_dhcpc_start();
else
WiFi.config(connectionSettings->ip(), connectionSettings->gateway(), connectionSettings->subnetMask());
}
else
_d("WiFi :: failed to start station mode");
}
}
#ifdef SerialDebug
void wifiEvent(WiFiEvent_t event)
{
switch (event)
{
case WIFI_EVENT_STAMODE_CONNECTED:
_dln("WiFi:: station mode: connected"); break;
case WIFI_EVENT_STAMODE_DISCONNECTED:
_dln("WiFi:: station mode: disconnected"); break;
case WIFI_EVENT_STAMODE_AUTHMODE_CHANGE:
_dln("WiFi:: station mode: authmode change"); break;
case WIFI_EVENT_STAMODE_GOT_IP:
_dln("WiFi:: station mode: got IP");
_dln(WiFi.localIP());
break;
case WIFI_EVENT_STAMODE_DHCP_TIMEOUT:
_dln("WiFi:: station mode: DHCP timeout"); break;
case WIFI_EVENT_SOFTAPMODE_STACONNECTED:
_dln("WiFi:: soft AP mode: station connected"); break;
case WIFI_EVENT_SOFTAPMODE_STADISCONNECTED:
_dln("WiFi:: soft AP mode: station disconnected"); break;
}
}
#endif
void updateLED()
{
/*
// Pulsate the bottom step while WiFi is connecting
uint16_t brightness = 0;
uint16_t speed = 16;
while (WiFi.status() != WL_CONNECTED)
{
brightness += speed;
if (brightness <= 0 || brightness >= 1024)
speed = -speed;
stairs->set(0, brightness);
delay(16);
}
*/
}
void handleNotFound(AsyncWebServerRequest *request)
{
_d("HTTP :: not found: "); _dln(request->url());
request->send(404);
}