Skip to content

Commit

Permalink
start/stop webserver
Browse files Browse the repository at this point in the history
start/stop webserver

start/stop webserver
  • Loading branch information
Chris McKeever committed Aug 3, 2020
1 parent e281084 commit 2a8b49d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 110 deletions.
83 changes: 30 additions & 53 deletions examples/custom-http/custom-http.ino
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ struct Metadata {
#include "ConfigManager.h"
ConfigManager configManager;

//
// Lets serve something with a different Library!!!
// This library is only for ESP32
// This library has naming conflicts with <Webserver.h>
//
/**********
***********
Lets serve something with a different Library!!!
This library is only for ESP32
This library has naming conflicts with <Webserver.h>
***********
**********/

#if defined(ARDUINO_ARCH_ESP32)
namespace HTTP {
#include "esp_http_server.h"
Expand All @@ -32,43 +37,11 @@ HTTP::httpd_handle_t h_server = NULL;

void customHTTP(int port=80);

void customWifi() {
Serial.println("Setting up custom Wifi");
WiFi.mode(WIFI_AP);

const char* ssid = config.deviceName == "" ? "CustomHTTPDemo" : config.deviceName;
WiFi.softAP(ssid);

// Need to wait to get IP
delay(500);

IPAddress ip(192, 168, 10, 1);
IPAddress NMask(255, 255, 255, 0);
WiFi.softAPConfig(ip, ip, NMask);
Serial.print("SSID: ");
Serial.println(ssid);
}

void setup() {
Serial.begin(115200);
DEBUG_MODE = true;
DebugPrintln(F(""));

/**********
***********
To toggle using ConfigManagers built in
WiFi Setup or HTTP Server, toggle these values.
When using HTTP and no WIFI, it will default to
API Mode, and serve all pages defined.
***********
**********/
bool cmWIFI = true;
bool cmHTTP = true;

if (!cmWIFI) configManager.disableWIFI();
if (!cmHTTP) configManager.disableHTTP();

meta.version = 3;

// Settings
Expand All @@ -80,33 +53,37 @@ void setup() {
// Meta Settings
configManager.addParameter("version", &meta.version, get);

if(cmWIFI) {
Serial.println("Setting up ConfigManager Wifi");
configManager.setAPName("CM-Demo");
} else customWifi();
configManager.begin(config);

if (cmHTTP) {
Serial.println("Setting up ConfigManager configApi");
configManager.setAPFilename("/index.html");
configManager.setAPCallback(APCallback);
configManager.setAPICallback(APICallback);
} else customHTTP();
/**********
***********
configManager.begin(config);
Allow for the stop of ConfigMangers built in
HTTP server, and start your own.
This example allows HTTP in AP mode, but provides
a custom HTTP server when in wifi station mode.
***********
**********/

bool stopHTTP = false;
if (configManager.getMode() == station && stopHTTP) {
configManager.stopWebserver();
customHTTP();
}

/**********
Updating settings variables
Updating settings variables .. and now you know
**********/
config.someNumber = 62;
configManager.save();
}

void loop() {
configManager.loop();



// configManager.updateParameter("randomNumber", random(300));
DEBUG_MODE = config.debugEnabled;

// Add your loop code here
Expand Down
2 changes: 2 additions & 0 deletions examples/save_config_demo/save_config_demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ void setup() {

void loop() {
configManager.loop();

// Add your loop code here
}
96 changes: 45 additions & 51 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,46 @@ bool DEBUG_MODE = false;
// Setup and Loop
//
void ConfigManager::setup() {
this->mode = basic;

if (!this->disabledWIFI) {
char magic[MAGIC_LENGTH];
char ssid[SSID_LENGTH];
char password[PASSWORD_LENGTH];
char magic[MAGIC_LENGTH];
char ssid[SSID_LENGTH];
char password[PASSWORD_LENGTH];

DebugPrint(F("MAC: "));
DebugPrintln(WiFi.macAddress());
DebugPrint(F("MAC: "));
DebugPrintln(WiFi.macAddress());

DebugPrintln(F("Reading saved configuration"));
EEPROM.get(0, magic);
DebugPrintln(F("Reading saved configuration"));
EEPROM.get(0, magic);

if (memcmp(magic, magicBytes, MAGIC_LENGTH) == 0) {
EEPROM.get(MAGIC_LENGTH, ssid);
DebugPrint(F("SSID: \""));
DebugPrint(ssid);
DebugPrintln(F("\""));
if (memcmp(magic, magicBytes, MAGIC_LENGTH) == 0) {
EEPROM.get(MAGIC_LENGTH, ssid);
DebugPrint(F("SSID: \""));
DebugPrint(ssid);
DebugPrintln(F("\""));

EEPROM.get(MAGIC_LENGTH + SSID_LENGTH, password);
readConfig();
EEPROM.get(MAGIC_LENGTH + SSID_LENGTH, password);
readConfig();

WiFi.begin(ssid, password[0] == '\0' ? NULL : password);
WiFi.begin(ssid, password[0] == '\0' ? NULL : password);

if (wifiConnected()) {
DebugPrint(F("Connected to "));
DebugPrint(ssid);
DebugPrint(F(" with "));
DebugPrintln(WiFi.localIP());
if (wifiConnected()) {
DebugPrint(F("Connected to "));
DebugPrint(ssid);
DebugPrint(F(" with "));
DebugPrintln(WiFi.localIP());

WiFi.mode(WIFI_STA);
}
} else {
// We are at a cold start, don't bother timing out.
apTimeout = 0;
DebugPrintln(F("MagicBytes mismatch"));
}
WiFi.mode(WIFI_STA);

if (this->getMode() != wifi) {
startAP();
startApi();
}
} else {
// We are at a cold start, don't bother timing out.
apTimeout = 0;
DebugPrintln(F("MagicBytes mismatch"));
}

if (!this->disabledHTTP) {
(this->getMode() == ap) ? startConfigApi() : startApi();
if (this->getMode() != station) {
startAP();
startAPApi();
}

DebugPrint("enum MODE: ");
Expand All @@ -69,7 +64,7 @@ void ConfigManager::setup() {

void ConfigManager::loop() {

if (!this->disabledWIFI && this->getMode() == ap) {
if (this->getMode() == ap) {
if (apTimeout > 0 &&
((millis() - apStart) / 1000) > (uint16_t)apTimeout) {
ESP.restart();
Expand All @@ -80,7 +75,7 @@ void ConfigManager::loop() {
}
}

if (!this->disabledHTTP && server) {
if (server && this->webserverRunning) {
server->handleClient();
}
}
Expand Down Expand Up @@ -136,22 +131,19 @@ void ConfigManager::startAP() {
apStart = millis();
}

void ConfigManager::startConfigApi() {
this->mode = configApi;
DebugPrintln(F("ConfigApi Mode"));
void ConfigManager::startAPApi() {
DebugPrintln(F("AP Api Mode"));
createBaseWebServer();


if (apCallback) {
apCallback(server.get());
}

server->begin();
this->startWebserver();
}

void ConfigManager::startApi() {
this->mode = api;
DebugPrintln(F("API Mode"));
DebugPrintln(F("Station Mode"));
createBaseWebServer();

server->on("/settings", HTTPMethod::HTTP_GET,
Expand All @@ -163,7 +155,7 @@ void ConfigManager::startApi() {
apiCallback(server.get());
}

server->begin();
this->startWebserver();
}

void ConfigManager::setAPCallback(std::function<void(WebServer*)> callback) {
Expand All @@ -177,10 +169,6 @@ void ConfigManager::setAPICallback(std::function<void(WebServer*)> callback) {
//
// ConfigManager Wifi Utilitiees
//
void ConfigManager::disableWIFI() {
this->disabledWIFI = true;
}

void ConfigManager::setWifiConnectRetries(const int retries) {
this->wifiConnectRetries = retries;
}
Expand All @@ -196,7 +184,7 @@ bool ConfigManager::wifiConnected() {
while (i < wifiConnectRetries) {
if (WiFi.status() == WL_CONNECTED) {
DebugPrintln("");
this->mode = wifi;
this->mode = station;
return true;
}

Expand Down Expand Up @@ -382,8 +370,14 @@ void ConfigManager::clearSettings(bool reboot) {
//
// ConfigManager HTTP Utilities
//
void ConfigManager::disableHTTP() {
this->disabledHTTP = true;
void ConfigManager::startWebserver() {
this->server->begin();
this->webserverRunning = true;
}

void ConfigManager::stopWebserver() {
this->server->stop();
this->webserverRunning = false;
}

void ConfigManager::setWebPort(const int port) {
Expand Down
11 changes: 5 additions & 6 deletions src/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern const char mimePlain[];
extern const char mimeCSS[];
extern const char mimeJS[];

enum Mode { basic, ap, wifi, configApi, api };
enum Mode { ap, station };
enum ParameterMode { get, set, both };

/**
Expand Down Expand Up @@ -172,8 +172,8 @@ class ConfigManager {
void updateFromJson(JsonObject obj);
void setAPCallback(std::function<void(WebServer*)> callback);
void setAPICallback(std::function<void(WebServer*)> callback);
void disableHTTP();
void disableWIFI();
void startWebserver();
void stopWebserver();
void save();

template <typename T>
Expand Down Expand Up @@ -209,8 +209,7 @@ class ConfigManager {
void* config;
size_t configSize;

bool disabledWIFI = false;
bool disabledHTTP = false;
bool webserverRunning = false;

char* apName = (char*)"ConfigManager-Thing";
char* apPassword = NULL;
Expand Down Expand Up @@ -240,7 +239,7 @@ class ConfigManager {
bool wifiConnected();
void setup();
void startAP();
void startConfigApi();
void startAPApi();
void startApi();
void createBaseWebServer();

Expand Down

0 comments on commit 2a8b49d

Please sign in to comment.