Skip to content

Commit c1e3410

Browse files
authored
Merge pull request #15 from jnthas/improv-wifi
Improv wifi & Settings page
2 parents efc8bff + d69abef commit c1e3410

12 files changed

+1067
-693
lines changed

firmware/.vscode/launch.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"name": "PIO Debug",
1515
"executable": "/home/jonathas/projects/clockwise/firmware/.pio/build/esp32dev/firmware.elf",
1616
"projectEnvName": "esp32dev",
17-
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa32/bin",
17+
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa-esp32/bin",
1818
"internalConsoleOptions": "openOnSessionStart",
1919
"preLaunchTask": {
2020
"type": "PlatformIO",
@@ -27,7 +27,7 @@
2727
"name": "PIO Debug (skip Pre-Debug)",
2828
"executable": "/home/jonathas/projects/clockwise/firmware/.pio/build/esp32dev/firmware.elf",
2929
"projectEnvName": "esp32dev",
30-
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa32/bin",
30+
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa-esp32/bin",
3131
"internalConsoleOptions": "openOnSessionStart"
3232
},
3333
{
@@ -36,7 +36,7 @@
3636
"name": "PIO Debug (without uploading)",
3737
"executable": "/home/jonathas/projects/clockwise/firmware/.pio/build/esp32dev/firmware.elf",
3838
"projectEnvName": "esp32dev",
39-
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa32/bin",
39+
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa-esp32/bin",
4040
"internalConsoleOptions": "openOnSessionStart",
4141
"loadMode": "manual"
4242
}

firmware/.vscode/settings.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"*.tcc": "cpp",
6+
"cctype": "cpp",
7+
"clocale": "cpp",
8+
"cmath": "cpp",
9+
"cstdarg": "cpp",
10+
"cstddef": "cpp",
11+
"cstdint": "cpp",
12+
"cstdio": "cpp",
13+
"cstdlib": "cpp",
14+
"cstring": "cpp",
15+
"ctime": "cpp",
16+
"cwchar": "cpp",
17+
"cwctype": "cpp",
18+
"deque": "cpp",
19+
"unordered_map": "cpp",
20+
"unordered_set": "cpp",
21+
"vector": "cpp",
22+
"exception": "cpp",
23+
"algorithm": "cpp",
24+
"functional": "cpp",
25+
"iterator": "cpp",
26+
"map": "cpp",
27+
"memory": "cpp",
28+
"memory_resource": "cpp",
29+
"numeric": "cpp",
30+
"optional": "cpp",
31+
"random": "cpp",
32+
"string": "cpp",
33+
"string_view": "cpp",
34+
"system_error": "cpp",
35+
"tuple": "cpp",
36+
"type_traits": "cpp",
37+
"utility": "cpp",
38+
"fstream": "cpp",
39+
"initializer_list": "cpp",
40+
"iomanip": "cpp",
41+
"iosfwd": "cpp",
42+
"istream": "cpp",
43+
"limits": "cpp",
44+
"new": "cpp",
45+
"ostream": "cpp",
46+
"sstream": "cpp",
47+
"stdexcept": "cpp",
48+
"streambuf": "cpp",
49+
"cinttypes": "cpp",
50+
"typeinfo": "cpp"
51+
}
52+
}

firmware/lib/cw-commons/CWDateTime.cpp

+4-20
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
#include "CWDateTime.h"
22

3-
void CWDateTime::begin()
3+
void CWDateTime::begin(const char *timeZone, bool use24format)
44
{
5-
myTZ.setCache(0);
6-
waitForSync();
7-
}
8-
9-
void CWDateTime::setTimezone(const char *timeZone)
10-
{
11-
myTZ.setCache(0);
125
myTZ.setLocation(timeZone);
6+
this->use24hFormat = use24format;
137
waitForSync();
148
}
159

16-
String CWDateTime::getTimezone()
17-
{
18-
return myTZ.getTimezoneName(0);
19-
}
20-
21-
22-
void CWDateTime::update()
23-
{
24-
}
25-
2610
String CWDateTime::getFormattedTime()
2711
{
2812
return myTZ.dateTime();
@@ -31,7 +15,7 @@ String CWDateTime::getFormattedTime()
3115
char *CWDateTime::getHour(const char *format)
3216
{
3317
static char buffer[3] = {'\0'};
34-
strncpy(buffer, myTZ.dateTime("H").c_str(), sizeof(buffer));
18+
strncpy(buffer, myTZ.dateTime((use24hFormat ? "H" : "h")).c_str(), sizeof(buffer));
3519
return buffer;
3620
}
3721

@@ -44,7 +28,7 @@ char *CWDateTime::getMinute(const char *format)
4428

4529
int CWDateTime::getHour()
4630
{
47-
return myTZ.dateTime("H").toInt();
31+
return myTZ.dateTime((use24hFormat ? "H" : "h")).toInt();
4832
}
4933

5034
int CWDateTime::getMinute()

firmware/lib/cw-commons/CWDateTime.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ class CWDateTime
88
{
99
private:
1010
Timezone myTZ;
11+
bool use24hFormat = true;
1112

1213
public:
13-
void begin();
14-
void setTimezone(const char *timeZone);
15-
String getTimezone();
16-
void update();
14+
void begin(const char *timeZone, bool use24format);
1715
String getFormattedTime();
1816

1917
char *getHour(const char *format);
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <Preferences.h>
4+
5+
Preferences preferences;
6+
7+
struct ClockwiseParams
8+
{
9+
const char* const PREF_SWAP_BLUE_GREEN = "swapBlueGreen";
10+
const char* const PREF_USE_24H_FORMAT = "use24hFormat";
11+
const char* const PREF_DISPLAY_BRIGHT = "displayBright";
12+
const char* const PREF_TIME_ZONE = "timeZone";
13+
const char* const PREF_WIFI_SSID = "wifiSsid";
14+
const char* const PREF_WIFI_PASSWORD = "wifiPwd";
15+
16+
17+
bool swapBlueGreen;
18+
bool use24hFormat;
19+
uint8_t displayBright;
20+
String timeZone;
21+
String wifiSsid;
22+
String wifiPwd;
23+
24+
25+
ClockwiseParams() {
26+
preferences.begin("clockwise", false);
27+
//preferences.clear();
28+
}
29+
30+
static ClockwiseParams* getInstance() {
31+
static ClockwiseParams base;
32+
return &base;
33+
}
34+
35+
36+
void save()
37+
{
38+
preferences.putBool(PREF_SWAP_BLUE_GREEN, swapBlueGreen);
39+
preferences.putBool(PREF_USE_24H_FORMAT, use24hFormat);
40+
preferences.putUInt(PREF_DISPLAY_BRIGHT, displayBright);
41+
preferences.putString(PREF_TIME_ZONE, timeZone);
42+
preferences.putString(PREF_WIFI_SSID, wifiSsid);
43+
preferences.putString(PREF_WIFI_PASSWORD, wifiPwd);
44+
}
45+
46+
void load()
47+
{
48+
swapBlueGreen = preferences.getBool(PREF_SWAP_BLUE_GREEN, false);
49+
use24hFormat = preferences.getBool(PREF_USE_24H_FORMAT, true);
50+
displayBright = preferences.getUInt(PREF_DISPLAY_BRIGHT, 32);
51+
timeZone = preferences.getString(PREF_TIME_ZONE, "America/Sao_Paulo");
52+
wifiSsid = preferences.getString(PREF_WIFI_SSID, "");
53+
wifiPwd = preferences.getString(PREF_WIFI_PASSWORD, "");
54+
}
55+
56+
};

firmware/lib/cw-commons/CWWebServer.h

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#pragma once
2+
3+
#include <WiFi.h>
4+
#include <CWPreferences.h>
5+
#include "StatusController.h"
6+
#include "SettingsWebPage.h"
7+
8+
WiFiServer server(80);
9+
10+
struct ClockwiseWebServer
11+
{
12+
String httpBuffer;
13+
14+
static ClockwiseWebServer *getInstance()
15+
{
16+
static ClockwiseWebServer base;
17+
return &base;
18+
}
19+
20+
void startWebServer()
21+
{
22+
server.begin();
23+
StatusController::getInstance()->blink_led(100, 3);
24+
}
25+
26+
void stopWebServer()
27+
{
28+
server.stop();
29+
}
30+
31+
void handleHttpRequest()
32+
{
33+
WiFiClient client = server.available();
34+
if (client)
35+
{
36+
StatusController::getInstance()->blink_led(100, 1);
37+
38+
while (client.connected())
39+
{
40+
if (client.available())
41+
{
42+
char c = client.read();
43+
httpBuffer.concat(c);
44+
45+
if (c == '\n')
46+
{
47+
uint8_t method_pos = httpBuffer.indexOf(' ');
48+
uint8_t path_pos = httpBuffer.indexOf(' ', method_pos + 1);
49+
50+
String method = httpBuffer.substring(0, method_pos);
51+
String path = httpBuffer.substring(method_pos + 1, path_pos);
52+
String key = "";
53+
String value = "";
54+
55+
if (method == "POST" && path.indexOf('?') > 0)
56+
{
57+
key = path.substring(path.indexOf('?') + 1, path.indexOf('='));
58+
value = path.substring(path.indexOf('=') + 1, ' ');
59+
path = path.substring(0, path.indexOf('?'));
60+
}
61+
62+
processRequest(client, method, path, key, value);
63+
httpBuffer = "";
64+
break;
65+
}
66+
}
67+
}
68+
delay(1);
69+
client.stop();
70+
}
71+
}
72+
73+
void processRequest(WiFiClient client, String method, String path, String key, String value)
74+
{
75+
if (method == "GET" && path == "/") {
76+
client.println("HTTP/1.0 200 OK");
77+
client.println("Content-Type: text/html");
78+
client.println();
79+
client.println(SETTINGS_PAGE);
80+
} else if (method == "GET" && path == "/get") {
81+
getCurrentSettings(client);
82+
} else if (method == "POST" && path == "/restart") {
83+
client.println("HTTP/1.0 204 No Content");
84+
delay(1000);
85+
ESP.restart();
86+
} else if (method == "POST" && path == "/set") {
87+
ClockwiseParams::getInstance()->load();
88+
//a baby seal has died due this ifs
89+
if (key == ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT) {
90+
ClockwiseParams::getInstance()->displayBright = value.toInt();
91+
} else if (key == ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN) {
92+
ClockwiseParams::getInstance()->swapBlueGreen = (value == "1");
93+
} else if (key == ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT) {
94+
ClockwiseParams::getInstance()->use24hFormat = (value == "1");
95+
} else if (key == ClockwiseParams::getInstance()->PREF_TIME_ZONE) {
96+
ClockwiseParams::getInstance()->timeZone = value;
97+
}
98+
ClockwiseParams::getInstance()->save();
99+
client.println("HTTP/1.0 204 No Content");
100+
}
101+
}
102+
103+
104+
void getCurrentSettings(WiFiClient client) {
105+
ClockwiseParams::getInstance()->load();
106+
107+
char response[256];
108+
snprintf(response, sizeof(response), "{\"%s\":%d,\"%s\":%d,\"%s\":%d,\"%s\":\"%s\",\"%s\":\"%s\"}", \
109+
ClockwiseParams::getInstance()->PREF_DISPLAY_BRIGHT,
110+
ClockwiseParams::getInstance()->displayBright,
111+
ClockwiseParams::getInstance()->PREF_SWAP_BLUE_GREEN,
112+
ClockwiseParams::getInstance()->swapBlueGreen,
113+
ClockwiseParams::getInstance()->PREF_USE_24H_FORMAT,
114+
ClockwiseParams::getInstance()->use24hFormat,
115+
ClockwiseParams::getInstance()->PREF_TIME_ZONE,
116+
ClockwiseParams::getInstance()->timeZone.c_str(),
117+
ClockwiseParams::getInstance()->PREF_WIFI_SSID,
118+
ClockwiseParams::getInstance()->wifiSsid.c_str());
119+
120+
client.println("HTTP/1.0 200 OK");
121+
client.println("Content-Type: application/json");
122+
client.println();
123+
client.println(response);
124+
}
125+
126+
};

0 commit comments

Comments
 (0)