-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
814 additions
and
595 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
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,104 @@ | ||
#pragma once | ||
|
||
#include "ImprovWiFiLibrary.h" | ||
#include <WiFi.h> | ||
#include <CWPreferences.h> | ||
#include "IOManager.h" | ||
|
||
|
||
WiFiServer server(80); | ||
ImprovWiFi improvSerial(&Serial); | ||
|
||
struct ClockwiseWebServer | ||
{ | ||
|
||
char linebuf[80]; | ||
int charcount = 0; | ||
|
||
|
||
static void onImprovWiFiErrorCb(improv::Error err) | ||
{ | ||
server.stop(); | ||
IOManager::blink_led(2000, 3); | ||
} | ||
|
||
static void onImprovWiFiConnectedCb(std::string ssid, std::string password) | ||
{ | ||
ClockwiseParams::getInstance()->load(); | ||
ClockwiseParams::getInstance()->wifiSsid = String(ssid.c_str()); | ||
ClockwiseParams::getInstance()->wifiPwd = String(password.c_str()); | ||
ClockwiseParams::getInstance()->save(); | ||
|
||
startCWWebServer(); | ||
} | ||
|
||
static void startCWWebServer() { | ||
server.begin(); | ||
IOManager::blink_led(100, 3); | ||
} | ||
|
||
void begin() | ||
{ | ||
WiFi.mode(WIFI_STA); | ||
WiFi.disconnect(); | ||
|
||
improvSerial.setDeviceInfo(improv::ChipFamily::CF_ESP32, "CW-20230324", "1.1.0", "Clockwise"); | ||
improvSerial.onImprovWiFiError(onImprovWiFiErrorCb); | ||
improvSerial.onImprovWiFiConnected(onImprovWiFiConnectedCb); | ||
|
||
ClockwiseParams::getInstance()->load(); | ||
if (improvSerial.tryConnectToWifi(ClockwiseParams::getInstance()->wifiSsid.c_str(), ClockwiseParams::getInstance()->wifiPwd.c_str())) { | ||
startCWWebServer(); | ||
} | ||
|
||
} | ||
|
||
void handleHttpRequest() | ||
{ | ||
|
||
improvSerial.handleSerial(); | ||
|
||
if (!improvSerial.isConnected()) | ||
return; | ||
|
||
WiFiClient client = server.available(); | ||
|
||
if (client) | ||
{ | ||
IOManager::blink_led(100, 1); | ||
memset(linebuf, 0, sizeof(linebuf)); | ||
charcount = 0; | ||
// an http request ends with a blank line | ||
boolean currentLineIsBlank = true; | ||
while (client.connected()) | ||
{ | ||
if (client.available()) | ||
{ | ||
char c = client.read(); | ||
// read char by char HTTP request | ||
linebuf[charcount] = c; | ||
if (charcount < sizeof(linebuf) - 1) | ||
charcount++; | ||
|
||
if (c == '\n' && currentLineIsBlank) | ||
{ | ||
// send a standard http response header | ||
client.println("HTTP/1.1 200 OK"); | ||
client.println("Content-Type: text/html"); | ||
client.println("Connection: close"); // the connection will be closed after completion of the response | ||
client.println(); | ||
client.println("<!DOCTYPE HTML><html><body>"); | ||
client.println("<h1 id=\"welcome\">Welcome!</h1>"); | ||
client.println("<p>This is a simple webpage served by your ESP32</p>"); | ||
client.println("</body></html>"); | ||
break; | ||
} | ||
} | ||
} | ||
delay(1); | ||
client.stop(); | ||
} | ||
} | ||
|
||
|
||
}; |
Oops, something went wrong.