Male zmeny v index.html
- prerobenie settings handleru - presunutie do ~/data folderu - odstranenie starych suborov - pridanie parsovania settings.json - obsluha SPFFS
This commit is contained in:
345
src/main.cpp
345
src/main.cpp
@@ -1,4 +1,3 @@
|
||||
#include <NTPClient.h>
|
||||
// change next line to use with another board/shield
|
||||
#include <WiFi.h>
|
||||
#include <SPI.h>
|
||||
@@ -12,11 +11,13 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <DateTime.h>
|
||||
#include <IotWebConf.h>
|
||||
#include <NTP.h>
|
||||
#include <SPIFFS.h>
|
||||
#include <time.h>
|
||||
|
||||
#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<sizeof(cal_names)/sizeof(cal_names[0]); i++) {
|
||||
if (cal_names[i].m == m && cal_names[i].d ==d)
|
||||
enum when_names {
|
||||
WORK_DAYS,
|
||||
WEEKENDS,
|
||||
TOMOROW,
|
||||
ONCE
|
||||
};
|
||||
|
||||
struct timer_def {
|
||||
int when; int h; int m; int d; int mh; int y;
|
||||
};
|
||||
|
||||
int ntimers;
|
||||
#define MAX_TIMERS 10
|
||||
timer_def Timers[MAX_TIMERS];
|
||||
|
||||
|
||||
String getValue(String data, char separator, int index)
|
||||
{
|
||||
int found = 0;
|
||||
int strIndex[] = { 0, -1 };
|
||||
int maxIndex = data.length() - 1;
|
||||
|
||||
for (int i = 0; i <= maxIndex && found <= index; i++) {
|
||||
if (data.charAt(i) == separator || i == maxIndex) {
|
||||
found++;
|
||||
strIndex[0] = strIndex[1] + 1;
|
||||
strIndex[1] = (i == maxIndex) ? i+1 : i;
|
||||
}
|
||||
}
|
||||
return found > 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<String>();
|
||||
desc = obj["weather"][0]["description"].as<String>();
|
||||
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<JsonObject>();
|
||||
for (int i=0;i < obj["data"].size(); i++) {
|
||||
int t = obj["data"][i]["kedy"].as<int>();
|
||||
String c = obj["data"][i]["cas"].as<String>();
|
||||
String datum = obj["data"][i]["datum"].as<String>();
|
||||
|
||||
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; i<ntimers ; i++)
|
||||
{
|
||||
t = &Timers[i];
|
||||
|
||||
tm.tm_sec = 0;
|
||||
tm.tm_min = t->m;
|
||||
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 = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
|
||||
s += "<title>IotWebConf 02 Status and Reset</title></head><body>Hello world!";
|
||||
s += "Go to <a href='config'>configure page</a> to change settings.";
|
||||
s += "</body></html>\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<LAST_LINE;i++) {
|
||||
for (int i = 0; i < LAST_LINE; i++)
|
||||
{
|
||||
Serial.println(Lines[i]);
|
||||
scroll_line(Lines[i]);
|
||||
iotWebConf.doLoop();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user