working wersion with elements

This commit is contained in:
Dmitry Borisenko
2020-08-26 15:44:19 +03:00
parent e3f3ae7664
commit 90e6822d3b
23 changed files with 247 additions and 203 deletions

View File

@@ -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 это количество точек

View File

@@ -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

View File

@@ -1 +1 @@
button-in;id;toggle;Кнопки;Освещение;order;pin[2];db[20]
button-in;id;toggle;Кнопки;Освещение;order;pin;db[20]

View File

@@ -1 +1 @@
button-out;id;toggle;Кнопки;Освещение;order;pin[12];inv[1];st[1]
button-out;id;toggle;Кнопки;Освещение;order;pin;inv[1];st[1]

View File

@@ -1 +1 @@
button-out;id;toggle;Кнопки;Освещение;order;pin[12];st[0]
button-out;id;toggle;Кнопки;Освещение;order;pin;st[0]

View File

@@ -1 +1 @@
pwm-out;id;range;Ползунки;Яркость;order;pin[12];st[500]
pwm-out;id;range;Ползунки;Яркость;order;pin;st[500]

1
data/s.scen.txt Normal file
View File

@@ -0,0 +1 @@
//

View File

@@ -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": {
"#": "Выберите элемент из списка<span class=\"caret\"></span>",
"/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",

View File

@@ -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");
}
}

View File

@@ -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
/*

View File

@@ -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();

View File

@@ -15,3 +15,5 @@ String jsonWriteInt(String& json, String name, int value);
String jsonWriteFloat(String& json, String name, float value);
String jsonWriteBool(String& json, String name, boolean value);
void saveConfig();

View File

@@ -4,6 +4,8 @@
#include "Utils\StringUtils.h"
#include "Utils\TimeUtils.h"
#include "Errors.h"
#include "Global.h"
#define pm PrintMessage(MODULE)

View File

@@ -1,9 +1,11 @@
#pragma once
#include <Arduino.h>
#include "Global.h"
const String getChipId();
void setLedStatus(LedStatus_t status);
const String getUniqueId(const String& name);
const String printMemoryStatus();
@@ -11,3 +13,5 @@ const String printMemoryStatus();
const String getHeapStats();
const String getMacAddress();
void setChipId();

View File

@@ -1,73 +1,7 @@
#pragma once
#include "HttpServer.h"
#include <Arduino.h>
#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);

6
include/Web.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include "Arduino.h"
void web_init();
void setConfigParam(const char* param, const String& value);

View File

@@ -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));
// }
//}
//

View File

@@ -1,4 +1,6 @@
#include "Utils\JsonUtils.h"
#include "Utils/FileUtils.h"
#include "Global.h"
#include <ArduinoJson.h>
@@ -50,3 +52,7 @@ String jsonWriteFloat(String& json, String name, float value) {
root.printTo(json);
return json;
}
void saveConfig() {
writeFile(String("config.json"), configSetupJson);
}

View File

@@ -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) {

88
src/Utils/WebUtils.cpp Normal file
View File

@@ -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;
}

View File

@@ -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 = "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + MqttClient::getStateStr();
@@ -296,3 +300,9 @@ void web_init() {
request->send(200, "text/html");
});
}
void setConfigParam(const char* param, const String& value) {
pm.info("set " + String(param) + ": " + value);
jsonWriteStr(configSetupJson, param, value);
saveConfig();
}

View File

@@ -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();