diff --git a/data/dev_conf.txt b/data/dev_conf.txt deleted file mode 100644 index 2c5bb9ae..00000000 --- a/data/dev_conf.txt +++ /dev/null @@ -1,12 +0,0 @@ -button 1 na Включить#все Реле 0 1 -button 2 13 Прихожая Реле 0 2 -button 3 14 Кухня Реле 0 3 -pwm 1 3 Яркость#коредор: Реле 1023 4 -pwm 2 4 Яркость#ванная: Реле 510 5 -analog adc 0 Аналоговый#вход Датчики fillgauge 1 1023 1 1023 6 -logging adc 1 100 Аналоговый#вход Датчики 7 - -//Это демо конфигурация. В ней показано как связать кнопки c помощью сценариев -//Кнопка номер 1 связана с кнопкой 2, 3 и с pwm 2 -//Так же продемонстрированна система логгирования данных строкой logging -//1 - это интервал между точками в минутах, 100 это количество точек diff --git a/data/dev_scen.txt b/data/dev_scen.txt deleted file mode 100644 index 4927ac1e..00000000 --- a/data/dev_scen.txt +++ /dev/null @@ -1,13 +0,0 @@ -button1 = 1 -buttonSet 2 1 -buttonSet 3 1 -pwmSet 2 1024 -end -button1 = 0 -buttonSet 2 0 -buttonSet 3 0 -pwmSet 2 0 -end -adc > 50 -buttonSet 2 1 -end \ No newline at end of file diff --git a/data/items/button-in.txt b/data/items/button-in.txt index 5b6c3d66..ada34b20 100644 --- a/data/items/button-in.txt +++ b/data/items/button-in.txt @@ -1 +1 @@ -button-in;id;toggle;Кнопки;Освещение;order;pin[2];db[20] \ No newline at end of file +button-in;id;toggle;Кнопки;Освещение;order;pin;db[20] \ No newline at end of file diff --git a/data/items/button-out-i.txt b/data/items/button-out-i.txt index 31098078..c24ec540 100644 --- a/data/items/button-out-i.txt +++ b/data/items/button-out-i.txt @@ -1 +1 @@ -button-out;id;toggle;Кнопки;Освещение;order;pin[12];inv[1];st[1] \ No newline at end of file +button-out;id;toggle;Кнопки;Освещение;order;pin;inv[1];st[1] \ No newline at end of file diff --git a/data/items/button-out.txt b/data/items/button-out.txt index bc0b4148..d2a213fe 100644 --- a/data/items/button-out.txt +++ b/data/items/button-out.txt @@ -1 +1 @@ -button-out;id;toggle;Кнопки;Освещение;order;pin[12];st[0] \ No newline at end of file +button-out;id;toggle;Кнопки;Освещение;order;pin;st[0] \ No newline at end of file diff --git a/data/items/pwm-out.txt b/data/items/pwm-out.txt index cdb96ec0..b3ecac6e 100644 --- a/data/items/pwm-out.txt +++ b/data/items/pwm-out.txt @@ -1 +1 @@ -pwm-out;id;range;Ползунки;Яркость;order;pin[12];st[500] \ No newline at end of file +pwm-out;id;range;Ползунки;Яркость;order;pin;st[500] \ No newline at end of file diff --git a/data/conf.csv b/data/s.conf.csv similarity index 100% rename from data/conf.csv rename to data/s.conf.csv diff --git a/data/s.scen.txt b/data/s.scen.txt new file mode 100644 index 00000000..ab0c0141 --- /dev/null +++ b/data/s.scen.txt @@ -0,0 +1 @@ +// \ No newline at end of file diff --git a/data/set.device.json b/data/set.device.json index e906a530..260af361 100644 --- a/data/set.device.json +++ b/data/set.device.json @@ -43,7 +43,7 @@ }, { "type": "h4", - "title": "LittleFS version: 2.3.5" + "title": "LittleFS version: 2.4.0" }, { "type": "hr" @@ -56,7 +56,7 @@ "title": { "#": "Выберите элемент из списка", "/set?addItem=button-out": "1.Кнопка управляющая пином", - "/set?addItem=pwm-out": "2.Широтно импульсная подуляция", + "/set?addItem=pwm-out": "2.Широтно импульсная модуляция(pwm)", "/set?addItem=button-in": "3.Физическая кнопка", "/set?addItem=input-digit": "4.Окно ввода цифровых значений", "/set?addItem=input-time": "5.Окно ввода времени" @@ -75,7 +75,7 @@ "text", "text" ], - "state": "conf.csv", + "state": "s.conf.csv", "style": "width:100%;", "action": "/set?saveItems", "class": "btn btn-block btn-default" @@ -102,7 +102,7 @@ }, { "type": "file", - "state": "dev_scen.txt", + "state": "s.scen.txt", "style": "width:100%;height:350px", "title": "Сохранить", "action": "/set?sceninit", diff --git a/include/Clock.h b/include/Clock.h index 743a61cc..53685c1f 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -2,6 +2,8 @@ #include "Utils/TimeUtils.h" #include "Utils/PrintMessage.h" +#include "Global.h" +#include "Clock.h" #ifdef ESP8266 #include "sntp.h" @@ -69,7 +71,7 @@ class Clock { void startSync() { if (!_configured) { - pm.info("sync to: " + _ntp + " timezone: " + String(_timezone)); + //pm.info("sync to: " + _ntp + " timezone: " + String(_timezone)); setupSntp(); _configured = true; // лучше не ждать, проверим в следующий раз @@ -77,9 +79,9 @@ class Clock { } _hasSynced = hasTimeSynced(); if (_hasSynced) { - pm.info("synced " + getDateDotFormated() + " " + getTime()); + //pm.info("synced " + getDateDotFormated() + " " + getTime()); } else { - pm.error("failed to obtain"); + //pm.error("failed to obtain"); } } diff --git a/include/Consts.h b/include/Consts.h index bab4e689..cf398039 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -2,16 +2,18 @@ /* * Main consts */ -#define FIRMWARE_VERSION "2.3.5" +#define FIRMWARE_VERSION "2.4.0" #define NUM_BUTTONS 6 #define LED_PIN 2 #define FLASH_4MB true + #define MQTT_RECONNECT_INTERVAL 20000 #define TELEMETRY_UPDATE_INTERVAL 7200000 -#define DEVICE_CONFIG_FILE "conf.csv" -#define DEVICE_SCENARIO_FILE "dev_scen.txt" +#define DEVICE_CONFIG_FILE "s.conf.csv" +#define DEVICE_SCENARIO_FILE "s.scen.txt" + #define DEFAULT_PRESET 100 #define DEFAULT_SCENARIO 100 @@ -26,17 +28,22 @@ //#define MDNS_ENABLED //#define WEBSOCKET_ENABLED //#define LAYOUT_IN_RAM -#define UDP_ENABLED +//#define UDP_ENABLED /* * Sensor */ #define TANK_LEVEL_SAMPLES 10 #define LEVEL_ENABLED + #define ANALOG_ENABLED + + #define DALLAS_ENABLED #define DHT_ENABLED + #define BMP_ENABLED + #define BME_ENABLED /* diff --git a/include/DeviceList.h b/include/ItemsList.h similarity index 60% rename from include/DeviceList.h rename to include/ItemsList.h index 44bd7801..a72f2d12 100644 --- a/include/DeviceList.h +++ b/include/ItemsList.h @@ -5,4 +5,5 @@ extern void addItem(String name); extern void delAllItems(); -extern int getNewElementNumber(String file); +extern uint8_t getNewElementNumber(String file); +extern uint8_t getFreePin(); diff --git a/include/Utils/JsonUtils.h b/include/Utils/JsonUtils.h index f759361c..c244a14e 100644 --- a/include/Utils/JsonUtils.h +++ b/include/Utils/JsonUtils.h @@ -14,4 +14,6 @@ String jsonWriteInt(String& json, String name, int value); String jsonWriteFloat(String& json, String name, float value); -String jsonWriteBool(String& json, String name, boolean value); \ No newline at end of file +String jsonWriteBool(String& json, String name, boolean value); + +void saveConfig(); \ No newline at end of file diff --git a/include/Utils/PrintMessage.h b/include/Utils/PrintMessage.h index d6425544..833342b6 100644 --- a/include/Utils/PrintMessage.h +++ b/include/Utils/PrintMessage.h @@ -4,6 +4,8 @@ #include "Utils\StringUtils.h" #include "Utils\TimeUtils.h" #include "Errors.h" +#include "Global.h" + #define pm PrintMessage(MODULE) diff --git a/include/Utils/SysUtils.h b/include/Utils/SysUtils.h index 6e40c358..1686e51f 100644 --- a/include/Utils/SysUtils.h +++ b/include/Utils/SysUtils.h @@ -1,13 +1,17 @@ #pragma once - #include +#include "Global.h" const String getChipId(); +void setLedStatus(LedStatus_t status); + const String getUniqueId(const String& name); const String printMemoryStatus(); const String getHeapStats(); -const String getMacAddress(); \ No newline at end of file +const String getMacAddress(); + +void setChipId(); \ No newline at end of file diff --git a/include/Utils/WebUtils.h b/include/Utils/WebUtils.h index fe8caec4..5479314c 100644 --- a/include/Utils/WebUtils.h +++ b/include/Utils/WebUtils.h @@ -1,73 +1,7 @@ #pragma once +#include "HttpServer.h" +#include -#include "ESPAsyncWebServer.h" - -const String getMethodName(AsyncWebServerRequest* request) { - String res = F("UNKNOWN"); - if (request->method() == HTTP_GET) - res = F("GET"); - else if (request->method() == HTTP_POST) - res = F("POST"); - else if (request->method() == HTTP_DELETE) - res = F("DELETE"); - else if (request->method() == HTTP_PUT) - res = F("PUT"); - else if (request->method() == HTTP_PATCH) - res = F("PATCH"); - else if (request->method() == HTTP_HEAD) - res = F("HEAD"); - else if (request->method() == HTTP_OPTIONS) - res = F("OPTIONS"); - return res; -} - -const String getRequestInfo(AsyncWebServerRequest* request) { - String res = getMethodName(request); - res += ' '; - res += "http://"; - res += request->host(); - res += request->url(); - res += '\n'; - if (request->contentLength()) { - res += "content-type: "; - res += request->contentType(); - res += " content-lenght: "; - res += prettyBytes(request->contentLength()); - res += '\n'; - } - - if (request->headers()) { - res += "headers:\n"; - for (size_t i = 0; i < request->headers(); i++) { - AsyncWebHeader* h = request->getHeader(i); - res += h->name(); - res += '='; - res += h->value(); - res += '\n'; - } - } - - if (request->params()) { - res += "params:\n"; - for (size_t i = 0; i < request->params(); i++) { - AsyncWebParameter* p = request->getParam(i); - if (p->isFile()) { - res += "FILE"; - } else if (p->isPost()) { - res += "POST"; - } else { - res += "GET"; - } - res += ' '; - res += p->name(); - res += ':'; - res += p->value(); - if (p->isFile()) { - res += " size:"; - res += p->size(); - } - res += '\n'; - } - } - return res; -} +extern String getURL(const String& urls); +extern const String getMethodName(AsyncWebServerRequest* request); +extern const String getRequestInfo(AsyncWebServerRequest* request); \ No newline at end of file diff --git a/include/Web.h b/include/Web.h new file mode 100644 index 00000000..a80a08d2 --- /dev/null +++ b/include/Web.h @@ -0,0 +1,6 @@ +#pragma once +#include "Arduino.h" + + +void web_init(); +void setConfigParam(const char* param, const String& value); diff --git a/src/DeviceList.cpp b/src/ItemsList.cpp similarity index 73% rename from src/DeviceList.cpp rename to src/ItemsList.cpp index b3411920..73d5071e 100644 --- a/src/DeviceList.cpp +++ b/src/ItemsList.cpp @@ -1,39 +1,57 @@ -#include "DeviceList.h" +#include "ItemsList.h" static const char* firstLine PROGMEM = "Тип элемента;Id;Виджет;Имя вкладки;Имя виджета;Позиция виджета"; void addItem(String name) { String item = readFile("items/" + name + ".txt", 1024); - item.replace("id", "id" + String(getNewElementNumber("id.txt"))); + + item.replace("id", name + String(getNewElementNumber("id.txt"))); item.replace("order", String(getNewElementNumber("order.txt"))); + item.replace("pin", "pin[" + String(getFreePin()) + "]"); + item.replace("\r\n", ""); item.replace("\r", ""); item.replace("\n", ""); - addFile("conf.csv", "\n" + item); + addFile(DEVICE_CONFIG_FILE, "\n" + item); } void delAllItems() { - removeFile("conf.csv"); - addFile("conf.csv", String(firstLine)); + removeFile(DEVICE_CONFIG_FILE); + addFile(DEVICE_CONFIG_FILE, String(firstLine)); removeFile("id.txt"); removeFile("order.txt"); + removeFile("pins.txt"); } -int getNewElementNumber(String file) { - int number = readFile(file, 100).toInt(); +uint8_t getNewElementNumber(String file) { + uint8_t number = readFile(file, 100).toInt(); number++; removeFile(file); addFile(file, String(number)); return number; } - +uint8_t getFreePin() { + #ifdef ESP8266 + uint8_t pins[] = {0, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5}; + #endif + #ifdef ESP32 + uint8_t pins[] = {0, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5}; + #endif + uint8_t array_sz = sizeof(pins) / sizeof(pins[0]); + uint8_t i = getNewElementNumber("pins.txt"); + if (i < array_sz) { + return pins[i]; + } else { + return 0; + } +} //void do_getJsonListFromCsv() { // if (getJsonListFromCsvFlag) { // getJsonListFromCsvFlag = false; // removeFile("items/items.json"); -// addFile("items/items.json", getJsonListFromCsv("conf.csv", 1)); +// addFile("items/items.json", getJsonListFromCsv(DEVICE_CONFIG_FILE, 1)); // } //} // diff --git a/src/Utils/JsonUtils.cpp b/src/Utils/JsonUtils.cpp index 8bc3de8f..04b4b926 100644 --- a/src/Utils/JsonUtils.cpp +++ b/src/Utils/JsonUtils.cpp @@ -1,4 +1,6 @@ #include "Utils\JsonUtils.h" +#include "Utils/FileUtils.h" +#include "Global.h" #include @@ -49,4 +51,8 @@ String jsonWriteFloat(String& json, String name, float value) { json = ""; root.printTo(json); return json; +} + +void saveConfig() { + writeFile(String("config.json"), configSetupJson); } \ No newline at end of file diff --git a/src/Utils/SysUtils.cpp b/src/Utils/SysUtils.cpp index d4eb5039..d5c957d6 100644 --- a/src/Utils/SysUtils.cpp +++ b/src/Utils/SysUtils.cpp @@ -1,7 +1,9 @@ #include "Utils/SysUtils.h" - +#include "Utils/PrintMessage.h" #include "Global.h" +static const char* MODULE = "Main"; + const String getUniqueId(const char* name) { return String(name) + getMacAddress(); } @@ -20,6 +22,11 @@ const String getChipId() { return res; } +void setChipId() { + chipId = getChipId(); + pm.info("id: " + chipId); +} + #ifdef ESP8266 static uint32_t total_memory = 52864; #else @@ -69,6 +76,48 @@ const String getMacAddress() { return String(buf); } + +#ifdef ESP8266 +void setLedStatus(LedStatus_t status) { + pinMode(LED_PIN, OUTPUT); + switch (status) { + case LED_OFF: + noTone(LED_PIN); + digitalWrite(LED_PIN, HIGH); + break; + case LED_ON: + noTone(LED_PIN); + digitalWrite(LED_PIN, LOW); + break; + case LED_SLOW: + tone(LED_PIN, 1); + break; + case LED_FAST: + tone(LED_PIN, 20); + break; + default: + break; + } +} +#else +void setLedStatus(LedStatus_t status) { + pinMode(LED_PIN, OUTPUT); + switch (status) { + case LED_OFF: + digitalWrite(LED_PIN, HIGH); + break; + case LED_ON: + digitalWrite(LED_PIN, LOW); + break; + case LED_SLOW: + break; + case LED_FAST: + break; + default: + break; + } +} +#endif //=================================================================== /* void web_print (String text) { diff --git a/src/Utils/WebUtils.cpp b/src/Utils/WebUtils.cpp new file mode 100644 index 00000000..dc87a06c --- /dev/null +++ b/src/Utils/WebUtils.cpp @@ -0,0 +1,88 @@ +#include "Utils\WebUtils.h" +#include "ESPAsyncWebServer.h" + +String getURL(const String& urls) { + String res = ""; + HTTPClient http; + http.begin(urls); + int httpCode = http.GET(); + if (httpCode == HTTP_CODE_OK) { + res = http.getString(); + } else { + res = "error"; + } + http.end(); + return res; +} + + + +const String getMethodName(AsyncWebServerRequest* request) { + String res = F("UNKNOWN"); + if (request->method() == HTTP_GET) + res = F("GET"); + else if (request->method() == HTTP_POST) + res = F("POST"); + else if (request->method() == HTTP_DELETE) + res = F("DELETE"); + else if (request->method() == HTTP_PUT) + res = F("PUT"); + else if (request->method() == HTTP_PATCH) + res = F("PATCH"); + else if (request->method() == HTTP_HEAD) + res = F("HEAD"); + else if (request->method() == HTTP_OPTIONS) + res = F("OPTIONS"); + return res; +} + +const String getRequestInfo(AsyncWebServerRequest* request) { + String res = getMethodName(request); + res += ' '; + res += "http://"; + res += request->host(); + res += request->url(); + res += '\n'; + if (request->contentLength()) { + res += "content-type: "; + res += request->contentType(); + res += " content-lenght: "; + res += prettyBytes(request->contentLength()); + res += '\n'; + } + + if (request->headers()) { + res += "headers:\n"; + for (size_t i = 0; i < request->headers(); i++) { + AsyncWebHeader* h = request->getHeader(i); + res += h->name(); + res += '='; + res += h->value(); + res += '\n'; + } + } + + if (request->params()) { + res += "params:\n"; + for (size_t i = 0; i < request->params(); i++) { + AsyncWebParameter* p = request->getParam(i); + if (p->isFile()) { + res += "FILE"; + } else if (p->isPost()) { + res += "POST"; + } else { + res += "GET"; + } + res += ' '; + res += p->name(); + res += ':'; + res += p->value(); + if (p->isFile()) { + res += " size:"; + res += p->size(); + } + res += '\n'; + } + } + return res; +} diff --git a/src/Web.cpp b/src/Web.cpp index 8cf71556..b312d64f 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -1,4 +1,5 @@ -#include "DeviceList.h" +#include "Web.h" +#include "ItemsList.h" #include "Global.h" #include "Init.h" #include "Class/NotAsinc.h" @@ -93,17 +94,17 @@ void web_init() { loadScenario(); request->send(200); } - //-------------------------------------------------------------------------------- + if (request->hasArg("updatelist")) { removeFile("/dev.csv"); addFileLn("dev.csv", "device id;device name;ip address"); request->redirect("/?set.udp"); } - //-------------------------------------------------------------------------------- + if (request->hasArg("updatepage")) { request->redirect("/?set.udp"); } - //-------------------------------------------------------------------------------- + if (request->hasArg("devname")) { jsonWriteStr(configSetupJson, "name", request->getParam("devname")->value()); saveConfig(); @@ -115,34 +116,37 @@ void web_init() { saveConfig(); request->send(200); } + if (request->hasArg("routerpass")) { jsonWriteStr(configSetupJson, "routerpass", request->getParam("routerpass")->value()); saveConfig(); request->send(200); } - //-------------------------------------------------------------------------------- + if (request->hasArg("apssid")) { jsonWriteStr(configSetupJson, "apssid", request->getParam("apssid")->value()); saveConfig(); request->send(200, "text/text", "OK"); } + if (request->hasArg("appass")) { jsonWriteStr(configSetupJson, "appass", request->getParam("appass")->value()); saveConfig(); request->send(200); } - //-------------------------------------------------------------------------------- + if (request->hasArg("weblogin")) { jsonWriteStr(configSetupJson, "weblogin", request->getParam("weblogin")->value()); saveConfig(); request->send(200); } + if (request->hasArg("webpass")) { jsonWriteStr(configSetupJson, "webpass", request->getParam("webpass")->value()); saveConfig(); request->send(200); } - //-------------------------------------------------------------------------------- + if (request->hasArg("timezone")) { String timezoneStr = request->getParam("timezone")->value(); jsonWriteStr(configSetupJson, "timezone", timezoneStr); @@ -150,6 +154,7 @@ void web_init() { timeNow->setTimezone(timezoneStr.toInt()); request->send(200); } + if (request->hasArg("ntp")) { String ntpStr = request->getParam("ntp")->value(); jsonWriteStr(configSetupJson, "ntp", ntpStr); @@ -157,7 +162,7 @@ void web_init() { timeNow->setNtpPool(ntpStr); request->send(200); } - //-------------------------------------------------------------------------------- + if (request->hasArg("blink")) { bool value = request->getParam("blink")->value().toInt(); jsonWriteBool(configSetupJson, "blink", value); @@ -196,13 +201,12 @@ void web_init() { myNotAsincActions->make(do_MQTTPARAMSCHANGED); request->send(200); } - //-------------------------------------------------------------------------------- + if (request->hasArg("mqttsend")) { myNotAsincActions->make(do_MQTTUDP); request->send(200); } - //-------------------------------------------------------------------------------- if (request->hasArg("mqttcheck")) { String buf = "" + MqttClient::getStateStr(); @@ -295,4 +299,10 @@ void web_init() { myNotAsincActions->make(do_UPGRADE);; request->send(200, "text/html"); }); +} + +void setConfigParam(const char* param, const String& value) { + pm.info("set " + String(param) + ": " + value); + jsonWriteStr(configSetupJson, param, value); + saveConfig(); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9994f67a..be59f86e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,9 +3,9 @@ #include "Class/NotAsinc.h" #include "Class/Switch.h" #include "Cmd.h" -#include "DeviceList.h" +#include "ItemsList.h" #include "Global.h" -#include "HttpServer.h" +#include "Utils\WebUtils.h" #include "Init.h" #include "Utils/Timings.h" @@ -106,76 +106,15 @@ void loop() { -String getURL(const String& urls) { - String res = ""; - HTTPClient http; - http.begin(urls); - int httpCode = http.GET(); - if (httpCode == HTTP_CODE_OK) { - res = http.getString(); - } else { - res = "error"; - } - http.end(); - return res; -} -void setChipId() { - chipId = getChipId(); - pm.info("id: " + chipId); -} -void saveConfig() { - writeFile(String("config.json"), configSetupJson); -} -void setConfigParam(const char* param, const String& value) { - pm.info("set " + String(param) + ": " + value); - jsonWriteStr(configSetupJson, param, value); - saveConfig(); -} -#ifdef ESP8266 -void setLedStatus(LedStatus_t status) { - pinMode(LED_PIN, OUTPUT); - switch (status) { - case LED_OFF: - noTone(LED_PIN); - digitalWrite(LED_PIN, HIGH); - break; - case LED_ON: - noTone(LED_PIN); - digitalWrite(LED_PIN, LOW); - break; - case LED_SLOW: - tone(LED_PIN, 1); - break; - case LED_FAST: - tone(LED_PIN, 20); - break; - default: - break; - } -} -#else -void setLedStatus(LedStatus_t status) { - pinMode(LED_PIN, OUTPUT); - switch (status) { - case LED_OFF: - digitalWrite(LED_PIN, HIGH); - break; - case LED_ON: - digitalWrite(LED_PIN, LOW); - break; - case LED_SLOW: - break; - case LED_FAST: - break; - default: - break; - } -} -#endif + + + + + void clock_init() { timeNow = new Clock();