-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from jnthas/improv-wifi
Improv wifi & Settings page
- Loading branch information
Showing
12 changed files
with
1,067 additions
and
693 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"files.associations": { | ||
"array": "cpp", | ||
"atomic": "cpp", | ||
"*.tcc": "cpp", | ||
"cctype": "cpp", | ||
"clocale": "cpp", | ||
"cmath": "cpp", | ||
"cstdarg": "cpp", | ||
"cstddef": "cpp", | ||
"cstdint": "cpp", | ||
"cstdio": "cpp", | ||
"cstdlib": "cpp", | ||
"cstring": "cpp", | ||
"ctime": "cpp", | ||
"cwchar": "cpp", | ||
"cwctype": "cpp", | ||
"deque": "cpp", | ||
"unordered_map": "cpp", | ||
"unordered_set": "cpp", | ||
"vector": "cpp", | ||
"exception": "cpp", | ||
"algorithm": "cpp", | ||
"functional": "cpp", | ||
"iterator": "cpp", | ||
"map": "cpp", | ||
"memory": "cpp", | ||
"memory_resource": "cpp", | ||
"numeric": "cpp", | ||
"optional": "cpp", | ||
"random": "cpp", | ||
"string": "cpp", | ||
"string_view": "cpp", | ||
"system_error": "cpp", | ||
"tuple": "cpp", | ||
"type_traits": "cpp", | ||
"utility": "cpp", | ||
"fstream": "cpp", | ||
"initializer_list": "cpp", | ||
"iomanip": "cpp", | ||
"iosfwd": "cpp", | ||
"istream": "cpp", | ||
"limits": "cpp", | ||
"new": "cpp", | ||
"ostream": "cpp", | ||
"sstream": "cpp", | ||
"stdexcept": "cpp", | ||
"streambuf": "cpp", | ||
"cinttypes": "cpp", | ||
"typeinfo": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#pragma once | ||
|
||
#include <Preferences.h> | ||
|
||
Preferences preferences; | ||
|
||
struct ClockwiseParams | ||
{ | ||
const char* const PREF_SWAP_BLUE_GREEN = "swapBlueGreen"; | ||
const char* const PREF_USE_24H_FORMAT = "use24hFormat"; | ||
const char* const PREF_DISPLAY_BRIGHT = "displayBright"; | ||
const char* const PREF_TIME_ZONE = "timeZone"; | ||
const char* const PREF_WIFI_SSID = "wifiSsid"; | ||
const char* const PREF_WIFI_PASSWORD = "wifiPwd"; | ||
|
||
|
||
bool swapBlueGreen; | ||
bool use24hFormat; | ||
uint8_t displayBright; | ||
String timeZone; | ||
String wifiSsid; | ||
String wifiPwd; | ||
|
||
|
||
ClockwiseParams() { | ||
preferences.begin("clockwise", false); | ||
//preferences.clear(); | ||
} | ||
|
||
static ClockwiseParams* getInstance() { | ||
static ClockwiseParams base; | ||
return &base; | ||
} | ||
|
||
|
||
void save() | ||
{ | ||
preferences.putBool(PREF_SWAP_BLUE_GREEN, swapBlueGreen); | ||
preferences.putBool(PREF_USE_24H_FORMAT, use24hFormat); | ||
preferences.putUInt(PREF_DISPLAY_BRIGHT, displayBright); | ||
preferences.putString(PREF_TIME_ZONE, timeZone); | ||
preferences.putString(PREF_WIFI_SSID, wifiSsid); | ||
preferences.putString(PREF_WIFI_PASSWORD, wifiPwd); | ||
} | ||
|
||
void load() | ||
{ | ||
swapBlueGreen = preferences.getBool(PREF_SWAP_BLUE_GREEN, false); | ||
use24hFormat = preferences.getBool(PREF_USE_24H_FORMAT, true); | ||
displayBright = preferences.getUInt(PREF_DISPLAY_BRIGHT, 32); | ||
timeZone = preferences.getString(PREF_TIME_ZONE, "America/Sao_Paulo"); | ||
wifiSsid = preferences.getString(PREF_WIFI_SSID, ""); | ||
wifiPwd = preferences.getString(PREF_WIFI_PASSWORD, ""); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#pragma once | ||
|
||
#include <WiFi.h> | ||
#include <CWPreferences.h> | ||
#include "StatusController.h" | ||
#include "SettingsWebPage.h" | ||
|
||
WiFiServer server(80); | ||
|
||
struct ClockwiseWebServer | ||
{ | ||
String httpBuffer; | ||
|
||
static ClockwiseWebServer *getInstance() | ||
{ | ||
static ClockwiseWebServer base; | ||
return &base; | ||
} | ||
|
||
void startWebServer() | ||
{ | ||
server.begin(); | ||
StatusController::getInstance()->blink_led(100, 3); | ||
} | ||
|
||
void stopWebServer() | ||
{ | ||
server.stop(); | ||
} | ||
|
||
void handleHttpRequest() | ||
{ | ||
WiFiClient client = server.available(); | ||
if (client) | ||
{ | ||
StatusController::getInstance()->blink_led(100, 1); | ||
|
||
while (client.connected()) | ||
{ | ||
if (client.available()) | ||
{ | ||
char c = client.read(); | ||
httpBuffer.concat(c); | ||
|
||
if (c == '\n') | ||
{ | ||
uint8_t method_pos = httpBuffer.indexOf(' '); | ||
uint8_t path_pos = httpBuffer.indexOf(' ', method_pos + 1); | ||
|
||
String method = httpBuffer.substring(0, method_pos); | ||
String path = httpBuffer.substring(method_pos + 1, path_pos); | ||
String key = ""; | ||
String value = ""; | ||
|
||
if (method == "POST" && path.indexOf('?') > 0) | ||
{ | ||
key = path.substring(path.indexOf('?') + 1, path.indexOf('=')); | ||
value = path.substring(path.indexOf('=') + 1, ' '); | ||
path = path.substring(0, path.indexOf('?')); | ||
} | ||
|
||
processRequest(client, method, path, key, value); | ||
httpBuffer = ""; | ||
break; | ||
} | ||
} | ||
} | ||
delay(1); | ||
client.stop(); | ||
} | ||
} | ||
|
||
void processRequest(WiFiClient client, String method, String path, String key, String value) | ||
{ | ||
if (method == "GET" && path == "/") { | ||
client.println("HTTP/1.0 200 OK"); | ||
client.println("Content-Type: text/html"); | ||
client.println(); | ||
client.println(SETTINGS_PAGE); | ||
} else if (method == "GET" && path == "/get") { | ||
getCurrentSettings(client); | ||
} else if (method == "POST" && path == "/restart") { | ||
client.println("HTTP/1.0 204 No Content"); | ||
delay(1000); | ||
ESP.restart(); | ||
} else if (method == "POST" && path == "/set") { | ||
ClockwiseParams::getInstance()->load(); | ||
//a baby seal has died due this ifs | ||
if (key == ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT) { | ||
ClockwiseParams::getInstance()->displayBright = value.toInt(); | ||
} else if (key == ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN) { | ||
ClockwiseParams::getInstance()->swapBlueGreen = (value == "1"); | ||
} else if (key == ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT) { | ||
ClockwiseParams::getInstance()->use24hFormat = (value == "1"); | ||
} else if (key == ClockwiseParams::getInstance()->PREF_TIME_ZONE) { | ||
ClockwiseParams::getInstance()->timeZone = value; | ||
} | ||
ClockwiseParams::getInstance()->save(); | ||
client.println("HTTP/1.0 204 No Content"); | ||
} | ||
} | ||
|
||
|
||
void getCurrentSettings(WiFiClient client) { | ||
ClockwiseParams::getInstance()->load(); | ||
|
||
char response[256]; | ||
snprintf(response, sizeof(response), "{\"%s\":%d,\"%s\":%d,\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\"}", \ | ||
ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT, | ||
ClockwiseParams::getInstance()->displayBright, | ||
ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN, | ||
ClockwiseParams::getInstance()->swapBlueGreen, | ||
ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT, | ||
ClockwiseParams::getInstance()->use24hFormat, | ||
ClockwiseParams::getInstance()->PREF_TIME_ZONE, | ||
ClockwiseParams::getInstance()->timeZone.c_str(), | ||
ClockwiseParams::getInstance()->PREF_WIFI_SSID, | ||
ClockwiseParams::getInstance()->wifiSsid.c_str()); | ||
|
||
client.println("HTTP/1.0 200 OK"); | ||
client.println("Content-Type: application/json"); | ||
client.println(); | ||
client.println(response); | ||
} | ||
|
||
}; |
Oops, something went wrong.