Skip to content

Commit

Permalink
Merge pull request #15 from jnthas/improv-wifi
Browse files Browse the repository at this point in the history
Improv wifi & Settings page
  • Loading branch information
jnthas authored Apr 2, 2023
2 parents efc8bff + d69abef commit c1e3410
Show file tree
Hide file tree
Showing 12 changed files with 1,067 additions and 693 deletions.
6 changes: 3 additions & 3 deletions firmware/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"name": "PIO Debug",
"executable": "/home/jonathas/projects/clockwise/firmware/.pio/build/esp32dev/firmware.elf",
"projectEnvName": "esp32dev",
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa32/bin",
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa-esp32/bin",
"internalConsoleOptions": "openOnSessionStart",
"preLaunchTask": {
"type": "PlatformIO",
Expand All @@ -27,7 +27,7 @@
"name": "PIO Debug (skip Pre-Debug)",
"executable": "/home/jonathas/projects/clockwise/firmware/.pio/build/esp32dev/firmware.elf",
"projectEnvName": "esp32dev",
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa32/bin",
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa-esp32/bin",
"internalConsoleOptions": "openOnSessionStart"
},
{
Expand All @@ -36,7 +36,7 @@
"name": "PIO Debug (without uploading)",
"executable": "/home/jonathas/projects/clockwise/firmware/.pio/build/esp32dev/firmware.elf",
"projectEnvName": "esp32dev",
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa32/bin",
"toolchainBinDir": "/home/jonathas/.platformio/packages/toolchain-xtensa-esp32/bin",
"internalConsoleOptions": "openOnSessionStart",
"loadMode": "manual"
}
Expand Down
52 changes: 52 additions & 0 deletions firmware/.vscode/settings.json
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"
}
}
24 changes: 4 additions & 20 deletions firmware/lib/cw-commons/CWDateTime.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
#include "CWDateTime.h"

void CWDateTime::begin()
void CWDateTime::begin(const char *timeZone, bool use24format)
{
myTZ.setCache(0);
waitForSync();
}

void CWDateTime::setTimezone(const char *timeZone)
{
myTZ.setCache(0);
myTZ.setLocation(timeZone);
this->use24hFormat = use24format;
waitForSync();
}

String CWDateTime::getTimezone()
{
return myTZ.getTimezoneName(0);
}


void CWDateTime::update()
{
}

String CWDateTime::getFormattedTime()
{
return myTZ.dateTime();
Expand All @@ -31,7 +15,7 @@ String CWDateTime::getFormattedTime()
char *CWDateTime::getHour(const char *format)
{
static char buffer[3] = {'\0'};
strncpy(buffer, myTZ.dateTime("H").c_str(), sizeof(buffer));
strncpy(buffer, myTZ.dateTime((use24hFormat ? "H" : "h")).c_str(), sizeof(buffer));
return buffer;
}

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

int CWDateTime::getHour()
{
return myTZ.dateTime("H").toInt();
return myTZ.dateTime((use24hFormat ? "H" : "h")).toInt();
}

int CWDateTime::getMinute()
Expand Down
6 changes: 2 additions & 4 deletions firmware/lib/cw-commons/CWDateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ class CWDateTime
{
private:
Timezone myTZ;
bool use24hFormat = true;

public:
void begin();
void setTimezone(const char *timeZone);
String getTimezone();
void update();
void begin(const char *timeZone, bool use24format);
String getFormattedTime();

char *getHour(const char *format);
Expand Down
56 changes: 56 additions & 0 deletions firmware/lib/cw-commons/CWPreferences.h
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, "");
}

};
126 changes: 126 additions & 0 deletions firmware/lib/cw-commons/CWWebServer.h
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);
}

};
Loading

0 comments on commit c1e3410

Please sign in to comment.