diff --git a/src/data/indexvujs.html b/data/index.html similarity index 92% rename from src/data/indexvujs.html rename to data/index.html index 6992665..5bf0cd7 100644 --- a/src/data/indexvujs.html +++ b/data/index.html @@ -81,7 +81,9 @@ $(function () { - settings = [ { "kedy": 0 , "cas": "6:38", "datum": null, "edit": true}, { "kedy": 0 , "cas": "17:00", "datum": null, "edit":false} ]; + settings = %%SETTINGS%%; + settings = settings["data"]; + when = { 0 : "Pracovne dni", 1 : "Vikendy", 2 : "Zajtra", @@ -112,8 +114,16 @@ }, postChanges: function () { console.log("SAVE CHANGES TO ESP"); - axios.get('https://api.coindesk.com/v1/bpi/currentprice.json') - .then(response => (console.log(response))); + + axios.post('/settings', { + data: this.settings + }) + .then(function (response) { + console.log(response); + }) + .catch(function (error) { + console.log(error); + }); } } }) diff --git a/data/settings.json b/data/settings.json new file mode 100644 index 0000000..c642ea1 --- /dev/null +++ b/data/settings.json @@ -0,0 +1 @@ +{ "data": [ { "kedy": 0, "cas": "", "datum":"", "edit": true} ] } \ No newline at end of file diff --git a/src/data/index.html b/src/data/index.html deleted file mode 100644 index 1fd8afa..0000000 --- a/src/data/index.html +++ /dev/null @@ -1,166 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - -
-

Budík

-
-
-
- -
- -
-
- - - - -
-
-
-
- - - - -
-
- - -
- -
-
- - - \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5741069..156aa45 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,3 @@ -#include // change next line to use with another board/shield #include #include @@ -12,11 +11,13 @@ #include #include #include +#include +#include +#include #include "calendar.h" - #ifndef PSTR - #define PSTR // Make Arduino Due happy +#define PSTR // Make Arduino Due happy #endif #define COMMAND_PARAMETER_LENGTH 30 @@ -37,26 +38,68 @@ DNSServer dnsServer; WebServer server(80); IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword); - -enum Lines_names { TIME_LINE, DATE_LINE, WEATHER_LINE, NAMES_LINE, LAST_LINE }; +enum Lines_names +{ + TIME_LINE, + DATE_LINE, + WEATHER_LINE, + NAMES_LINE, + LAST_LINE +}; String Lines[LAST_LINE]; -Adafruit_NeoMatrix *matrix; +Adafruit_NeoMatrix *matrix; const uint16_t colors[] = { - Adafruit_NeoMatrix::Color(255, 0, 0), Adafruit_NeoMatrix::Color(0, 255, 0), Adafruit_NeoMatrix::Color(0, 0, 255) }; - + Adafruit_NeoMatrix::Color(255, 0, 0), Adafruit_NeoMatrix::Color(0, 255, 0), Adafruit_NeoMatrix::Color(0, 0, 255)}; + WiFiUDP ntpUDP; // You can specify the time server pool and the offset (in seconds, can be // changed later with setTimeOffset() ). Additionaly you can specify the // update interval (in milliseconds, can be changed using setUpdateInterval() ). -NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 60000); +bool initialized = false; +NTP ntp(ntpUDP); HTTPClient client; DynamicJsonDocument doc(1024); +String settings; -String get_name_of_day(int m,int d) { - for (int i=0;i index ? data.substring(strIndex[0], strIndex[1]) : ""; +} + +String get_name_of_day(int m, int d) +{ + for (int i = 0; i < sizeof(cal_names) / sizeof(cal_names[0]); i++) + { + if (cal_names[i].m == m && cal_names[i].d == d) return String(cal_names[i].name); } return String("Sviatok"); @@ -66,9 +109,11 @@ String get_weather_line() { client.begin("http://api.openweathermap.org/data/2.5/weather?q=Trnava,sk&lang=sk&APPID=d15c61931a053026e98769dd9fc46ba3"); int code = client.GET(); - if (code > 0) { + if (code > 0) + { - if(code == HTTP_CODE_OK) { + if (code == HTTP_CODE_OK) + { float temp; String w; String desc; @@ -80,32 +125,37 @@ String get_weather_line() w = obj["weather"][0]["main"].as(); desc = obj["weather"][0]["description"].as(); temp -= 273.15; - return String(temp) + String("C ") + desc; + return String(temp) + String("C ") + desc; } } return "Error"; } -String get_time_line(){ - return String(DateTime.format("%H:%M")); +String get_time_line() +{ + return String(ntp.formattedTime("%H:%M")); } -String get_date_line(){ - return String(DateTime.format("%d.%m")); +String get_date_line() +{ + return String(ntp.formattedTime("%d.%m")); } -String get_names_line() { +String get_names_line() +{ int d = DateTime.getParts().getMonthDay(); - int m = DateTime.getParts().getMonth();m++; - return get_name_of_day(m,d); + int m = DateTime.getParts().getMonth(); + m++; + return get_name_of_day(m, d); } -void setup_matrix() { +void setup_matrix() +{ matrix = new Adafruit_NeoMatrix(32, 8, PIN, - NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + - NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG, - NEO_GBR + NEO_KHZ800); + NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT + + NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG, + NEO_GBR + NEO_KHZ800); matrix->begin(); matrix->setTextWrap(false); @@ -113,47 +163,192 @@ void setup_matrix() { matrix->setTextColor(colors[1]); } -void setup(){ +void returnOK() { + server.send(200, "text/plain", ""); +} + +void handleSave() { + String data; + + Serial.println(server.uri()); + Serial.println(server.method()); + Serial.println(server.argName(0)); + Serial.println(server.arg(0)); + if (server.uri() != "/settings" && server.method() != HTTP_POST) { + return; + } + + data = server.arg(0); + + Serial.println("Data:" + data); + + File file = SPIFFS.open("/settings.json", "w"); + if (!file) + { + Serial.println("Failed to open file for writing"); + return; + } + + settings = data; + file.print(data.c_str()); + file.close(); + + server.send(200, "text/plain", ""); + +} + +void read_config() +{ + File file = SPIFFS.open("/settings.json", "r"); + if (!file) + { + Serial.println("Failed to open settings file for reading"); + return; + } + + Serial.println("====File Content SETTINGS===="); + String s; + while (file.available()) + { + s += (char)file.read(); + } + Serial.print(s); + + file.close(); + + ntimers = 0; + settings = s; + Serial.println("SETTINGS: " + settings); + + deserializeJson(doc, settings); + JsonObject obj = doc.as(); + for (int i=0;i < obj["data"].size(); i++) { + int t = obj["data"][i]["kedy"].as(); + String c = obj["data"][i]["cas"].as(); + String datum = obj["data"][i]["datum"].as(); + + int h = getValue(c,':',0).toInt(); + int m = getValue(c,':',1).toInt(); + int d = 0; + int mh = 0; + int y = 0; + + Serial.println(t); + Serial.println(c); + Serial.println(datum); + + if (datum != NULL) { + d = getValue(datum,'.',0).toInt(); + mh = getValue(datum,'.',1).toInt(); + y = getValue(datum,'.',2).toInt(); + } + + Timers[ntimers].when = t; + Timers[ntimers].h = h; + Timers[ntimers].m = m; + Timers[ntimers].d = d; + Timers[ntimers].mh = mh; + Timers[ntimers].y = y; + + ntimers++; + } + Serial.println("N timers:" + ntimers); +} + +void calculate_timers() +{ + timer_def *t; + struct tm tm; + for (int i=0; im; + tm.tm_hour = t->h; + + if (t->d !=0) { + tm.tm_mday = t->d; + tm.tm_mon = t->mh; + tm.tm_year = t->y; + } else { + tm.tm_mday = 0; + tm.tm_mon = 0; + tm.tm_year = 0; + } + + if (t->d == 0) { + if (t->when == WORK_DAYS) { + + } else if (t->when == WEEKENDS) { + + } else if (t->when == TOMOROW ) { + + } + } + + } +} + +void setup() +{ Serial.begin(115200); //iotWebConf.setStatusPin(PIN); iotWebConf.init(); - // -- Set up required URL handlers on the web server. + if (!SPIFFS.begin(true)) + { + Serial.println("An Error has occurred while mounting SPIFFS"); + + return; + } + + + // -- Set up required URL handlers on the web server. server.on("/", handleRoot); - server.on("/config", []{ iotWebConf.handleConfig(); }); - server.onNotFound([](){ iotWebConf.handleNotFound(); }); + server.on("/config", [] { iotWebConf.handleConfig(); }); + server.on("/settings", HTTP_POST, handleSave); + server.onNotFound([]() { iotWebConf.handleNotFound(); }); setup_matrix(); - - + read_config(); } -void scroll_line(String line) { - int x = 0, mx = 32; - int y = 0; +void scroll_line(String line) +{ + int x = 0, mx = 32; + int y = 0; - if (line.length() * 6 > mx) { - for (x = 0; x < line.length()*6 - mx; x++) { - matrix->fillScreen(0); - matrix->setCursor(-x, y); - matrix->setTextColor(colors[1]); - matrix->print(line); - matrix->show(); - if (x == 0) delay(300); - else delay(100); - } - delay(1000); - } else { + if (line.length() * 6 > mx) + { + for (x = 0; x < line.length() * 6 - mx; x++) + { matrix->fillScreen(0); - matrix->setCursor(x, y); + matrix->setCursor(-x, y); matrix->setTextColor(colors[1]); matrix->print(line); matrix->show(); - delay(3000); + if (x == 0) + delay(300); + else + delay(100); } + delay(1000); + } + else + { + matrix->fillScreen(0); + matrix->setCursor(x, y); + matrix->setTextColor(colors[1]); + matrix->print(line); + matrix->show(); + delay(3000); + } } + + void handleRoot() { // -- Let IotWebConf test and handle captive portal requests. @@ -162,36 +357,60 @@ void handleRoot() // -- Captive portal request were already served. return; } - String s = ""; - s += "IotWebConf 02 Status and ResetHello world!"; - s += "Go to configure page to change settings."; - s += "\n"; + + File file = SPIFFS.open("/index.html", "r"); + if (!file) + { + Serial.println("Failed to open file for reading"); + return; + } + + Serial.println("====File Content===="); + String s; + while (file.available()) + { + s += (char)file.read(); + } + s.replace("%%SETTINGS%%",settings); + Serial.print(s); + + file.close(); server.send(200, "text/html", s); } -void loop() { +void loop() +{ unsigned long sec; - unsigned long n=0; + unsigned long n = 0; // -- doLoop should be called as frequently as possible. iotWebConf.doLoop(); - if (WiFi.status() == WL_CONNECTED) { - timeClient.begin(); - timeClient.update(); - sec = timeClient.getEpochTime(); + if (WiFi.status() == WL_CONNECTED) + { + if (initialized == false) + { + ntp.ruleDST("CEST", Last, Sun, Mar, 2, 120); // last sunday in march 2:00, timetone +120min (+1 GMT + 1h summertime offset) + ntp.ruleSTD("CET", Last, Sun, Oct, 3, 60); // last sunday in october 3:00, timezone +60min (+1 GMT) + ntp.begin(); + initialized = true; + } + sec = ntp.epoch(); DateTime.setTime(sec); - - if (n++ % 25 == 0) Lines[WEATHER_LINE] = get_weather_line(); - + + if (n++ % 25 == 0) + Lines[WEATHER_LINE] = get_weather_line(); + Lines[TIME_LINE] = get_time_line(); Lines[DATE_LINE] = get_date_line(); Lines[NAMES_LINE] = get_names_line(); - for (int i=0;i