This commit is contained in:
Dmitry Borisenko
2021-01-04 00:39:35 +01:00
parent bf9855aa60
commit 5eb3c6d3a3
339 changed files with 21164 additions and 0 deletions

166
src/BufferExecute.cpp Normal file
View File

@@ -0,0 +1,166 @@
#include "BufferExecute.h"
#include "Global.h"
#include "SoftUART.h"
//
#include "items/vSensorDallas.h"
#include "items/vSensorUltrasonic.h"
#include "items/vButtonOut.h"
#include "items/vPwmOut.h"
#include "items/vInOutput.h"
#include "items/vLogging.h"
#include "items/vImpulsOut.h"
#include "items/vCountDown.h"
#include "items/vSensorAnalog.h"
#include "items/vSensorDht.h"
#include "items/vSensorBme280.h"
#include "items/vSensorBmp280.h"
void loopCmdAdd(const String& cmdStr) {
if (cmdStr.endsWith(",")) {
orderBuf += cmdStr;
#ifdef uartEnable
if (jsonReadBool(configSetupJson, "uart")) {
if (jsonReadBool(configSetupJson, "uartEvents")) {
if (myUART) {
myUART->print(cmdStr);
SerialPrint("I", "<=UART", cmdStr);
}
}
}
#endif
}
}
void fileCmdExecute(const String& filename) {
String cmdStr = readFile(filename, 4096);
csvCmdExecute(cmdStr);
}
void csvCmdExecute(String& cmdStr) {
cmdStr.replace(";", " ");
cmdStr += "\r\n";
cmdStr.replace("\r\n", "\n");
cmdStr.replace("\r", "\n");
int count = 0;
while (cmdStr.length()) {
String buf = selectToMarker(cmdStr, "\n");
buf = deleteBeforeDelimiter(buf, " "); //отсечка чекбокса
count++;
if (count > 1) {
SerialPrint("I", "Items", buf);
String order = selectToMarker(buf, " "); //отсечка самой команды
if (order == F("button-out")) {
sCmd.addCommand(order.c_str(), buttonOut);
}
#ifdef PwmOutEnable
else if (order == F("pwm-out")) {
sCmd.addCommand(order.c_str(), pwmOut);
}
#endif
else if (order == F("button-in")) {
sCmd.addCommand(order.c_str(), buttonIn);
}
else if (order == F("inoutput")) {
sCmd.addCommand(order.c_str(), inOutput);
}
else if (order == F("analog-adc")) {
sCmd.addCommand(order.c_str(), analogAdc);
}
else if (order == F("ultrasonic-cm")) {
sCmd.addCommand(order.c_str(), ultrasonic);
}
else if (order == F("dallas-temp")) {
sCmd.addCommand(order.c_str(), dallas);
}
#ifdef SensorDhtEnabled
else if (order == F("dht")) {
sCmd.addCommand(order.c_str(), dhtSensor);
}
#endif
#ifdef SensorBme280Enabled
else if (order == F("bme280")) {
sCmd.addCommand(order.c_str(), bme280Sensor);
}
#endif
#ifdef SensorBmp280Enabled
else if (order == F("bmp280")) {
sCmd.addCommand(order.c_str(), bmp280Sensor);
}
#endif
else if (order == F("uptime")) {
sCmd.addCommand(order.c_str(), sysUptime);
}
else if (order == F("logging")) {
sCmd.addCommand(order.c_str(), logging);
}
else if (order == F("impuls-out")) {
sCmd.addCommand(order.c_str(), impuls);
}
else if (order == F("count-down")) {
sCmd.addCommand(order.c_str(), countDown);
}
sCmd.readStr(buf);
}
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
}
}
void spaceCmdExecute(String& cmdStr) {
cmdStr += "\r\n";
cmdStr.replace("\r\n", "\n");
cmdStr.replace("\r", "\n");
while (cmdStr.length()) {
String buf = selectToMarker(cmdStr, "\n");
sCmd.readStr(buf);
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
}
}
void loopCmdExecute() {
if (orderBuf.length()) {
String tmp = selectToMarker(orderBuf, ","); //выделяем первую команду rel 5 1,
SerialPrint("I", "CMD", "do: " + tmp);
sCmd.readStr(tmp); //выполняем
orderBuf = deleteBeforeDelimiter(orderBuf, ","); //осекаем
}
}
void addKey(String& key, String& keyNumberTable, int number) {
keyNumberTable += key + " " + String(number) + ",";
}
int getKeyNum(String& key, String& keyNumberTable) {
String keyNumberTableBuf = keyNumberTable;
int number = -1;
while (keyNumberTableBuf.length()) {
String tmp = selectToMarker(keyNumberTableBuf, ",");
String keyIncomming = selectToMarker(tmp, " ");
if (keyIncomming == key) {
number = selectToMarkerLast(tmp, " ").toInt();
}
keyNumberTableBuf = deleteBeforeDelimiter(keyNumberTableBuf, ",");
}
return number;
}
String getValue(String& key) {
String live = jsonReadStr(configLiveJson, key);
String store = jsonReadStr(configStoreJson, key);
if (live != nullptr) {
return live;
}
else if (store != nullptr) {
return store;
}
else if (store == nullptr && live == nullptr) {
return "no value";
}
else {
return "data error";
}
}

41
src/Bus.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include "Bus.h"
#include "Class/NotAsync.h"
#include "Global.h"
void busInit() {
myNotAsyncActions->add(
do_BUSSCAN, [&](void*) {
String tmp = i2c_scan();
if (tmp == "error") {
tmp = i2c_scan();
Serial.println(tmp);
jsonWriteStr(configLiveJson, "i2c", tmp);
}
else {
Serial.println(tmp);
jsonWriteStr(configLiveJson, "i2c", tmp);
}
},
nullptr);
SerialPrint("I", F("Bus"), F("Bus Init"));
}
String i2c_scan() {
String out;
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++) {
Wire.beginTransmission(i);
if (Wire.endTransmission() == 0) {
count++;
out += String(count) + ". 0x" + String(i, HEX) + "; ";
delay(1);
}
}
if (count == 0) {
return "error";
}
else {
return out;
}
}

View File

@@ -0,0 +1,49 @@
#include "Class/CallBackTest.h"
CallBackTest::CallBackTest(){};
void CallBackTest::loop() {
count++;
if (count > 5000) {
// Проверяем что переменная содержит указатель - не пустая не null
// и непосредственно вызываем то, на что это указывает
// просто пишем имя - без () - это указатель на фунецию.
// () - вызываем функцию - с пустым набором параметров
if (_cb != NULL) {
_cb();
}
//или ровно тоже самое
//if (_cb) _cb();
if (_pcb) {
if (_pcb("SomeTextValue")) {
Serial.println("Got true!");
} else {
Serial.println("Got false!");
}
}
count = 0;
}
}
//передаем внутрь класса функцию любую void функцию без агрументов
void CallBackTest::setCallback(AsyncActionCb cb) {
_cb = cb;
}
//передаем внутрь класса функцию любую void функцию с аргументами
void CallBackTest::setCallback(AsyncParamActionCb pcb) {
_pcb = pcb;
}
//CallBackTest* CB;
//CB->setCallback([]() {
// Serial.println("123");
//});
//
//CB->setCallback([](const String str) {
// Serial.println(str);
// return true;
//});

View File

@@ -0,0 +1,2 @@
#include "Class/LineParsing.h"
LineParsing myLineParsing;

30
src/Class/NotAsync.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include "Class/NotAsync.h"
NotAsync::NotAsync(uint8_t size) {
this->items = new NotAsyncItem[size];
this->size = size;
}
NotAsync::~NotAsync() {}
void NotAsync::add(uint8_t i, NotAsyncCb f, void* arg) {
this->items[i].cb = f;
this->items[i].cb_arg = arg;
this->items[i].is_used = true;
}
void NotAsync::loop() {
if (this->items[task].is_used) {
handle(this->items[task].cb, this->items[task].cb_arg);
task = 0;
}
}
void NotAsync::make(uint8_t task) {
this->task = task;
}
void NotAsync::handle(NotAsyncCb f, void* arg) {
f(arg);
}
NotAsync* myNotAsyncActions;

View File

@@ -0,0 +1,33 @@
#include "Class/ScenarioClass3.h"
#include "MqttClient.h"
#include "RemoteOrdersUdp.h"
Scenario* myScenario;
void eventGen2(String eventName, String eventValue) {
if (!jsonReadBool(configSetupJson, "scen")) {
return;
}
String event = eventName + " " + eventValue + ",";
eventBuf += event;
if (jsonReadBool(configSetupJson, "MqttOut")) {
if (eventName != "timenow") {
publishEvent(eventName, eventValue);
}
}
}
void streamEventUDP(String event) {
#ifdef UDP_ENABLED
if (!jsonReadBool(configSetupJson, "snaUdp")) {
return;
}
if (event.indexOf("timenow") == -1) {
event = "iotm;event:" + event;
asyncUdp.broadcastTo(event.c_str(), 4210);
}
#endif
}

16
src/Clock.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include "Clock.h"
#include "Global.h"
Clock* timeNow;
void clockInit() {
timeNow = new Clock;
timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp"));
timeNow->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt());
ts.add(
TIME_SYNC, 30000, [&](void*) {
timeNow->hasSync();
},
nullptr, true);
SerialPrint("I", F("NTP"), F("Clock Init"));
}

334
src/FSEditor.cpp Normal file
View File

@@ -0,0 +1,334 @@
#include "FSEditor.h"
#include "FileSystem.h"
#define FS_MAXLENGTH_FILEPATH 32
const char *excludeListFile = "/.exclude.files";
typedef struct ExcludeListS {
char *item;
ExcludeListS *next;
} ExcludeList;
static ExcludeList *excludes = NULL;
static bool matchWild(const char *pattern, const char *testee) {
const char *nxPat = NULL, *nxTst = NULL;
while (*testee) {
if ((*pattern == '?') || (*pattern == *testee)) {
pattern++;
testee++;
continue;
}
if (*pattern == '*') {
nxPat = pattern++;
nxTst = testee;
continue;
}
if (nxPat) {
pattern = nxPat + 1;
testee = ++nxTst;
continue;
}
return false;
}
while (*pattern == '*') {
pattern++;
}
return (*pattern == 0);
}
static bool addExclude(const char *item) {
size_t len = strlen(item);
if (!len) {
return false;
}
ExcludeList *e = (ExcludeList *)malloc(sizeof(ExcludeList));
if (!e) {
return false;
}
e->item = (char *)malloc(len + 1);
if (!e->item) {
free(e);
return false;
}
memcpy(e->item, item, len + 1);
e->next = excludes;
excludes = e;
return true;
}
static void loadExcludeList(fs::FS &_fs, const char *filename) {
static char linebuf[FS_MAXLENGTH_FILEPATH];
fs::File excludeFile = _fs.open(filename, "r");
if (!excludeFile) {
return;
}
if (excludeFile.isDirectory()) {
excludeFile.close();
return;
}
if (excludeFile.size() > 0) {
uint8_t idx;
bool isOverflowed = false;
while (excludeFile.available()) {
linebuf[0] = '\0';
idx = 0;
int lastChar;
do {
lastChar = excludeFile.read();
if (lastChar != '\r') {
linebuf[idx++] = (char)lastChar;
}
} while ((lastChar >= 0) && (lastChar != '\n') && (idx < FS_MAXLENGTH_FILEPATH));
if (isOverflowed) {
isOverflowed = (lastChar != '\n');
continue;
}
isOverflowed = (idx >= FS_MAXLENGTH_FILEPATH);
linebuf[idx - 1] = '\0';
if (!addExclude(linebuf)) {
excludeFile.close();
return;
}
}
}
excludeFile.close();
}
static bool isExcluded(fs::FS &_fs, const char *filename) {
if (excludes == NULL) {
loadExcludeList(_fs, excludeListFile);
}
if (strcmp(excludeListFile, filename) == 0) return true;
ExcludeList *e = excludes;
while (e) {
if (matchWild(e->item, filename)) {
return true;
}
e = e->next;
}
return false;
}
// WEB HANDLER IMPLEMENTATION
#ifdef ESP32
FSEditor::FSEditor(const fs::FS &fs, const String &username, const String &password)
#else
FSEditor::FSEditor(const String &username, const String &password, const fs::FS &fs)
#endif
: _fs(fs), _username(username), _password(password), _authenticated(false), _startTime(0) {
}
bool FSEditor::canHandle(AsyncWebServerRequest *request) {
if (request->url().equalsIgnoreCase("/edit")) {
if (request->method() == HTTP_GET) {
if (request->hasParam("list"))
return true;
if (request->hasParam("edit")) {
request->_tempFile = _fs.open(request->arg("edit"), "r");
if (!request->_tempFile) {
return false;
}
if (request->_tempFile.isDirectory()) {
request->_tempFile.close();
return false;
}
}
if (request->hasParam("download")) {
request->_tempFile = _fs.open(request->arg("download"), "r");
if (!request->_tempFile) {
return false;
}
if (request->_tempFile.isDirectory()) {
request->_tempFile.close();
return false;
}
}
request->addInterestingHeader("If-Modified-Since");
return true;
} else if (request->method() == HTTP_POST)
return true;
else if (request->method() == HTTP_DELETE)
return true;
else if (request->method() == HTTP_PUT)
return true;
}
return false;
}
#ifdef ESP8266
void FSEditor::getDirList(const String &path, String &output) {
auto dir = _fs.openDir(path.c_str());
while (dir.next()) {
String fname = dir.fileName();
if (!path.endsWith("/") && !fname.startsWith("/")) {
fname = "/" + fname;
}
fname = path + fname;
if (isExcluded(_fs, fname.c_str())) {
continue;
}
if (dir.isDirectory()) {
getDirList(fname, output);
continue;
}
if (output != "[") output += ',';
char buf[128];
sprintf(buf, "{\"type\":\"file\",\"name\":\"%s\",\"size\":%d}", fname.c_str(), dir.fileSize());
output += buf;
}
}
#else
void FSEditor::getDirList(const String &path, String &output) {
auto dir = _fs.open(path, FILE_READ);
dir.rewindDirectory();
while (dir.openNextFile()) {
String fname = dir.name();
if (!path.endsWith("/") && !fname.startsWith("/")) {
fname = "/" + fname;
}
fname = path + fname;
if (isExcluded(_fs, fname.c_str())) {
continue;
}
if (dir.isDirectory()) {
getDirList(fname, output);
continue;
}
if (output != "[") output += ',';
char buf[128];
sprintf(buf, "{\"type\":\"file\",\"name\":\"%s\",\"size\":%d}", fname.c_str(), dir.size());
output += buf;
}
}
#endif
void FSEditor::handleRequest(AsyncWebServerRequest *request) {
if (_username.length() && _password.length() && !request->authenticate(_username.c_str(), _password.c_str()))
return request->requestAuthentication();
if (request->method() == HTTP_GET) {
if (request->hasParam("list")) {
if (request->hasParam("list")) {
String path = request->getParam("list")->value();
String output = "[";
getDirList(path, output);
output += "]";
request->send(200, "application/json", output);
output = String();
}
} else if (request->hasParam("edit") || request->hasParam("download")) {
#ifdef ESP8266
request->send(request->_tempFile, request->_tempFile.fullName(), String(), request->hasParam("download"));
#else
request->send(request->_tempFile, request->_tempFile.name(), String(), request->hasParam("download"));
#endif
} else {
const char *buildTime = __DATE__ " " __TIME__ " GMT";
if (request->header("If-Modified-Since").equals(buildTime)) {
request->send(304);
} else {
AsyncWebServerResponse *response = request->beginResponse(FileFS, "/edit.htm", "text/html");
// response->addHeader("Content-Encoding", "gzip");
response->addHeader("Last-Modified", buildTime);
request->send(response);
}
}
} else if (request->method() == HTTP_DELETE) {
if (request->hasParam("path", true)) {
if (!(_fs.remove(request->getParam("path", true)->value()))) {
#ifdef ESP32
_fs.rmdir(request->getParam("path", true)->value()); // try rmdir for littlefs
#endif
}
request->send(200, "", "DELETE: " + request->getParam("path", true)->value());
} else
request->send(404);
} else if (request->method() == HTTP_POST) {
if (request->hasParam("data", true, true) && _fs.exists(request->getParam("data", true, true)->value()))
request->send(200, "", "UPLOADED: " + request->getParam("data", true, true)->value());
else if (request->hasParam("rawname", true) && request->hasParam("raw0", true)) {
String rawnam = request->getParam("rawname", true)->value();
if (_fs.exists(rawnam)) _fs.remove(rawnam); // delete it to allow a mode
int k = 0;
uint16_t i = 0;
fs::File f = _fs.open(rawnam, "a");
while (request->hasParam("raw" + String(k), true)) { //raw0 .. raw1
if (f) {
i += f.print(request->getParam("raw" + String(k), true)->value());
}
k++;
}
f.close();
request->send(200, "", "IPADWRITE: " + rawnam + ":" + String(i));
} else {
request->send(500);
}
} else if (request->method() == HTTP_PUT) {
if (request->hasParam("path", true)) {
String filename = request->getParam("path", true)->value();
if (_fs.exists(filename)) {
request->send(200);
} else {
/*******************************************************/
#ifdef ESP32
if (strchr(filename.c_str(), '/')) {
// For file creation, silently make subdirs as needed. If any fail,
// it will be caught by the real file open later on
char *pathStr = strdup(filename.c_str());
if (pathStr) {
// Make dirs up to the final fnamepart
char *ptr = strchr(pathStr, '/');
while (ptr) {
*ptr = 0;
_fs.mkdir(pathStr);
*ptr = '/';
ptr = strchr(ptr + 1, '/');
}
}
free(pathStr);
}
#endif
/*******************************************************/
fs::File f = _fs.open(filename, "w");
if (f) {
f.write((uint8_t)0x00);
f.close();
request->send(200, "", "CREATE: " + filename);
} else {
request->send(500);
}
}
} else
request->send(400);
}
}
void FSEditor::handleUpload(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!index) {
if (!_username.length() || request->authenticate(_username.c_str(), _password.c_str())) {
_authenticated = true;
request->_tempFile = _fs.open(filename, "w");
_startTime = millis();
}
}
if (_authenticated && request->_tempFile) {
if (len) {
request->_tempFile.write(data, len);
}
if (final) {
request->_tempFile.close();
}
}
}

39
src/FileSystem.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "FileSystem.h"
#include "Global.h"
#ifdef ESP8266
bool getInfo(FSInfo& info) {
return FileFS.info(info);
}
// Информация о ФС
void getFSInfo() {
FSInfo buf;
if (getInfo(buf)) {
//SerialPrint("I", F("FS"), F("Get FS info completed"));
size_t totalBytes = buf.totalBytes; // всего
size_t usedBytes = buf.usedBytes; // использовано
size_t maxOpenFiles = buf.maxOpenFiles; // лимит на открые файлы
size_t blockSize = buf.blockSize;
size_t pageSize = buf.pageSize;
size_t maxPathLength = buf.maxPathLength; // лимит на пути и имена файлов
size_t freeBytes = totalBytes - usedBytes;
float freePer = ((float)freeBytes / totalBytes) * 100;
jsonWriteStr(configSetupJson, F("freeBytes"), String(freePer) + "% (" + prettyBytes(freeBytes) + ")");
//SerialPrint("I", F("FS"), "totalBytes=" + String(totalBytes));
//SerialPrint("I", F("FS"), "usedBytes=" + String(usedBytes));
//SerialPrint("I", F("FS"), "maxOpenFiles=" + String(maxOpenFiles));
//SerialPrint("I", F("FS"), "blockSize=" + String(blockSize));
//SerialPrint("I", F("FS"), "pageSize=" + String(pageSize));
//SerialPrint("I", F("FS"), "maxPathLength=" + String(maxPathLength));
//SerialPrint("I", F("FS"), "freeBytes=" + String(freeBytes));
//SerialPrint("I", F("FS"), "freePer=" + String(freePer));
} else {
SerialPrint("E", F("FS"), F("FS info error"));
}
}
#endif

79
src/Global.cpp Normal file
View File

@@ -0,0 +1,79 @@
#include "Global.h"
#ifdef WS_enable
AsyncWebSocket ws;
//AsyncEventSource events;
#endif
TickerScheduler ts(SYGNAL + 1);
WiFiClient espClient;
PubSubClient mqtt(espClient);
StringCommand sCmd;
AsyncWebServer server(80);
OneWire* oneWire;
DallasTemperature sensors;
/*
* Global vars
*/
boolean just_load = true;
boolean telegramInitBeen = false;
// Json
String configSetupJson = "{}";
String configLiveJson = "{}";
String configStoreJson = "{}";
String configOptionJson = "{}";
String telegramMsgJson = "{}";
// Mqtt
String chipId = "";
String prex = "";
String all_widgets = "";
String scenario = "";
//orders and events
String orderBuf = "";
String eventBuf = "";
String itemsFile = "";
String itemsLine = "";
//key lists and numbers
//=========================================
String impuls_KeyList = "";
int impuls_EnterCounter = -1;
//=========================================
String buttonOut_KeyList = "";
int buttonOut_EnterCounter = -1;
//=========================================
String inOutput_KeyList = "";
int inOutput_EnterCounter = -1;
//=========================================
String pwmOut_KeyList = "";
int pwmOut_EnterCounter = -1;
//=========================================
String countDown_KeyList = "";
int countDown_EnterCounter = -1;
//=========================================
String logging_KeyList = "";
int logging_EnterCounter = -1;
//=========================================
int dht_EnterCounter = -1;
//=========================================
String itemName;
String presetName;
// Upgrade
String serverIP;
// Scenario
int scenario_line_status[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int lastVersion;
boolean busScanFlag = false;
boolean fsCheckFlag = false;
boolean delElementFlag = false;

134
src/Init.cpp Normal file
View File

@@ -0,0 +1,134 @@
#include "Init.h"
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Cmd.h"
#include "Global.h"
#include "items/vButtonOut.h"
#include "items/vCountDown.h"
#include "items/vImpulsOut.h"
#include "items/vInOutput.h"
#include "items/vLogging.h"
#include "items/vPwmOut.h"
#include "items/vSensorDallas.h"
#include "items/vSensorUltrasonic.h"
void loadConfig() {
configSetupJson = readFile("config.json", 4096);
configSetupJson.replace("\r\n", "");
configStoreJson = readFile("store.json", 4096);
configStoreJson.replace("\r\n", "");
jsonWriteStr(configSetupJson, "warning1", "");
jsonWriteStr(configSetupJson, "warning2", "");
jsonWriteStr(configSetupJson, "warning3", "");
jsonWriteStr(configSetupJson, "chipID", chipId);
jsonWriteInt(configSetupJson, "firmware_version", FIRMWARE_VERSION);
prex = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipId;
//Serial.println(configSetupJson);
serverIP = jsonReadStr(configSetupJson, "serverip");
SerialPrint("I", F("Conf"), F("Config Json Init"));
}
void espInit() {
deviceInit();
loadScenario();
SerialPrint("I", F("esp"), F("esp Init"));
}
void deviceInit() {
//======clear dallas params======
if (mySensorDallas2 != nullptr) {
mySensorDallas2->clear();
}
//======clear ultrasonic params======
if (mySensorUltrasonic != nullptr) {
mySensorUltrasonic->clear();
}
//======clear logging params======
if (myLogging != nullptr) {
myLogging->clear();
}
logging_KeyList = "";
logging_EnterCounter = -1;
//======clear impuls params=======
if (myImpulsOut != nullptr) {
myImpulsOut->clear();
}
impuls_KeyList = "";
impuls_EnterCounter = -1;
//======clear buttonOut params=======
if (myButtonOut != nullptr) {
myButtonOut->clear();
}
buttonOut_KeyList = "";
buttonOut_EnterCounter = -1;
//======clear input params=======
if (myInOutput != nullptr) {
myInOutput->clear();
}
inOutput_KeyList = "";
inOutput_EnterCounter = -1;
//======clear pwm params=======
#ifdef PwmOutEnable
if (myPwmOut != nullptr) {
myPwmOut->clear();
}
pwmOut_KeyList = "";
pwmOut_EnterCounter = -1;
#endif
//===================================
if (myCountDown != nullptr) {
myCountDown->clear();
}
countDown_KeyList = "";
countDown_EnterCounter = -1;
//===================================
dht_EnterCounter = -1;
//=========================================
#ifdef LAYOUT_IN_RAM
all_widgets = "";
#else
removeFile(String("layout.txt"));
#endif
myLineParsing.clearErrors();
fileCmdExecute(String(DEVICE_CONFIG_FILE));
int errors = myLineParsing.getPinErrors();
if (errors > 0) {
jsonWriteStr(configSetupJson, F("warning3"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Обнаружен неверный номер пина</p></font></div>"));
} else {
jsonWriteStr(configSetupJson, F("warning3"), "");
}
//outcoming_date();
}
//-------------------------------сценарии-----------------------------------------------------
void loadScenario() {
if (jsonReadStr(configSetupJson, "scen") == "1") {
scenario = readFile(String(DEVICE_SCENARIO_FILE), 2048);
}
}
void uptime_init() {
ts.add(
UPTIME, 5000, [&](void*) {
handle_uptime();
},
nullptr, true);
SerialPrint("I", F("Uptime"), F("Uptime Init"));
}
void handle_uptime() {
jsonWriteStr(configSetupJson, "uptime", timeNow->getUptime());
}

186
src/ItemsList.cpp Normal file
View File

@@ -0,0 +1,186 @@
#include "ItemsList.h"
#include "Class/NotAsync.h"
#include "FileSystem.h"
#include "Init.h"
#include "Utils/StringUtils.h"
static const char* firstLine PROGMEM = "Удалить;Тип элемента;Id;Виджет;Имя вкладки;Имя виджета;Позиция виджета";
void itemsListInit() {
myNotAsyncActions->add(
do_deviceInit, [&](void*) {
deviceInit();
},
nullptr);
myNotAsyncActions->add(
do_delChoosingItems, [&](void*) {
delChoosingItems();
},
nullptr);
SerialPrint("I", F("Items"), F("Items Init"));
}
void addItem2(int num) {
File configFile = FileFS.open("/items/items.txt", "r");
if (!configFile) {
return;
}
configFile.seek(0, SeekSet);
String seachingLine;
int i = 0;
while (configFile.position() != configFile.size()) {
i++;
String item = configFile.readStringUntil('*');
if (i == num) {
if (i == 1) {
seachingLine = "\n" + item;
} else {
seachingLine = item;
}
break;
}
}
configFile.close();
randomSeed(micros());
unsigned int rnd = random(0, 1000);
seachingLine.replace("id", String(rnd));
seachingLine.replace("order", String(getNewElementNumber("order.txt")));
if (seachingLine.indexOf("gpio") != -1) {
seachingLine.replace("gpio", "pin[" + String(getFreePinAll()) + "]");
}
addFile(DEVICE_CONFIG_FILE, seachingLine);
Serial.println(seachingLine);
}
void addPreset2(int num) {
File configFile = FileFS.open("/presets/presets.c.txt", "r");
if (!configFile) {
return;
}
configFile.seek(0, SeekSet);
String config;
int i1 = 0;
while (configFile.position() != configFile.size()) {
i1++;
String item = configFile.readStringUntil('*');
if (i1 == num) {
if (i1 == 1) {
config = "\n" + item;
} else {
config = item;
}
break;
}
}
configFile.close();
addFile(DEVICE_CONFIG_FILE, config);
//===========================================================================
File scenFile = FileFS.open("/presets/presets.s.txt", "r");
if (!scenFile) {
return;
}
scenFile.seek(0, SeekSet);
String scen;
int i2 = 0;
while (scenFile.position() != scenFile.size()) {
i2++;
String item = scenFile.readStringUntil('*');
if (i2 == num) {
if (i1 == 1) {
scen = "\n" + item;
} else {
scen = item;
}
break;
}
}
scenFile.close();
if (readFile(String(DEVICE_SCENARIO_FILE), 2048) == "//") {
removeFile(DEVICE_SCENARIO_FILE);
scen = deleteBeforeDelimiter(scen, "\n");
addFile(DEVICE_SCENARIO_FILE, scen);
} else {
addFile(DEVICE_SCENARIO_FILE, scen);
}
}
void delAllItems() {
removeFile(DEVICE_CONFIG_FILE);
addFile(DEVICE_CONFIG_FILE, String(firstLine));
removeFile(DEVICE_SCENARIO_FILE);
addFile(DEVICE_SCENARIO_FILE, "//");
removeFile("id.txt");
removeFile("order.txt");
removeFile("pins.txt");
}
uint8_t getNewElementNumber(String file) {
uint8_t number = readFile(file, 100).toInt();
number++;
removeFile(file);
addFile(file, String(number));
return number;
}
uint8_t getFreePinAll() {
#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;
}
}
bool isPinExist(unsigned int num) {
bool ret = false;
unsigned int pins[] = {0, 1, 2, 3, 4, 5, 9, 10, 12, 13, 14, 15, 16};
uint8_t array_sz = sizeof(pins) / sizeof(pins[0]);
for (uint8_t i = 0; i < array_sz; i++) {
if (pins[i] == num) ret = true;
}
return ret;
}
uint8_t getFreePinAnalog() {
#ifdef ESP8266
return 0;
#endif
}
void delChoosingItems() {
File configFile = FileFS.open("/" + String(DEVICE_CONFIG_FILE), "r");
if (!configFile) {
return;
}
configFile.seek(0, SeekSet);
String finalConf;
bool firstLine = true;
while (configFile.position() != configFile.size()) {
String item = configFile.readStringUntil('\n');
if (firstLine) {
finalConf += item;
} else {
int checkbox = selectToMarker(item, ";").toInt();
if (checkbox == 0) {
finalConf += "\n" + item;
}
}
firstLine = false;
}
removeFile(String(DEVICE_CONFIG_FILE));
addFile(String(DEVICE_CONFIG_FILE), finalConf);
Serial.println(finalConf);
configFile.close();
}

378
src/MqttClient.cpp Normal file
View File

@@ -0,0 +1,378 @@
#include "MqttClient.h"
#include "BufferExecute.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"
#include "items/vLogging.h"
enum MqttBroker { MQTT_PRIMARY,
MQTT_RESERVE };
MqttBroker activeBroker = MQTT_PRIMARY;
String mqttPrefix;
String mqttRootDevice;
String mqttPass;
String mqttServer;
String mqttUser;
uint16_t mqttPort{0};
uint16_t reconnectionCounter{0};
uint16_t fallbackCounter{0};
const String getParamName(const char* param, MqttBroker broker) {
return String("mqtt") + param + (broker == MQTT_RESERVE ? "2" : "");
}
bool checkBrokerParams(MqttBroker broker) {
return !jsonReadStr(configSetupJson, getParamName("Server", broker)).isEmpty();
}
void mqttInit() {
myNotAsyncActions->add(
do_MQTTPARAMSCHANGED, [&](void*) {
mqttReconnect();
},
nullptr);
mqtt.setCallback(mqttCallback);
ts.add(
WIFI_MQTT_CONNECTION_CHECK, MQTT_RECONNECT_INTERVAL,
[&](void*) {
if (WiFi.status() == WL_CONNECTED) {
SerialPrint("I", "WIFI", "OK");
if (mqtt.connected()) {
if (activeBroker == MQTT_RESERVE) {
// при 20 cекундных интервалах проверки, каждые 100 сек
if (fallbackCounter++ > 5) {
if (checkBrokerParams(MQTT_PRIMARY)) {
activeBroker = MQTT_PRIMARY;
fallbackCounter = 0;
mqttReconnect();
}
}
} else {
SerialPrint("I", "MQTT", "OK");
setLedStatus(LED_OFF);
}
} else {
SerialPrint("E", "MQTT", "lost connection");
if (reconnectionCounter++ > 5) {
if (activeBroker == MQTT_PRIMARY) {
if (checkBrokerParams(MQTT_RESERVE)) {
activeBroker = MQTT_RESERVE;
}
} else {
activeBroker = MQTT_PRIMARY;
}
reconnectionCounter = 0;
}
mqttConnect();
}
} else {
SerialPrint("E", "WIFI", "Lost WiFi connection");
ts.remove(WIFI_MQTT_CONNECTION_CHECK);
startAPMode();
}
},
nullptr, true);
myNotAsyncActions->add(
do_sendScenMQTT, [&](void*) {
String scen = readFile(String(DEVICE_SCENARIO_FILE), 2048);
publishInfo("scen", scen);
},
nullptr);
}
void mqttDisconnect() {
SerialPrint("I", "MQTT", "disconnect");
mqtt.disconnect();
}
void mqttReconnect() {
mqttDisconnect();
mqttConnect();
}
void mqttLoop() {
if (!isNetworkActive() || !mqtt.connected()) {
return;
}
mqtt.loop();
}
void mqttSubscribe() {
SerialPrint("I", "MQTT", "subscribe");
mqtt.subscribe(mqttPrefix.c_str());
mqtt.subscribe((mqttRootDevice + "/+/control").c_str());
mqtt.subscribe((mqttRootDevice + "/update").c_str());
if (jsonReadBool(configSetupJson, "MqttIn")) {
mqtt.subscribe((mqttPrefix + "/+/+/event").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/order").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
}
}
bool readBrokerParams(MqttBroker broker) {
if (!checkBrokerParams(broker)) {
return false;
}
mqttServer = jsonReadStr(configSetupJson, getParamName("Server", broker));
mqttPort = jsonReadInt(configSetupJson, getParamName("Port", broker));
mqttUser = jsonReadStr(configSetupJson, getParamName("User", broker));
mqttPass = jsonReadStr(configSetupJson, getParamName("Pass", broker));
return true;
}
boolean mqttConnect() {
SerialPrint("I", "MQTT", String("use ") + (activeBroker == MQTT_PRIMARY ? "primary" : "reserve"));
if (!checkBrokerParams(activeBroker)) {
SerialPrint("E", "MQTT", "empty broker address");
return false;
}
readBrokerParams(activeBroker);
SerialPrint("I", "MQTT", "start connection");
mqttPrefix = jsonReadStr(configSetupJson, "mqttPrefix");
mqttRootDevice = mqttPrefix + "/" + chipId;
SerialPrint("I", "MQTT", "broker " + mqttServer + ":" + String(mqttPort, DEC));
SerialPrint("I", "MQTT", "topic " + mqttRootDevice);
setLedStatus(LED_FAST);
mqtt.setServer(mqttServer.c_str(), mqttPort);
bool res = false;
if (!mqtt.connected()) {
if (mqtt.connect(chipId.c_str(), mqttUser.c_str(), mqttPass.c_str())) {
SerialPrint("I", "MQTT", "connected");
setLedStatus(LED_OFF);
mqttSubscribe();
res = true;
} else {
SerialPrint("E", "MQTT", "could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
setLedStatus(LED_FAST);
}
}
return res;
}
void mqttCallback(char* topic, uint8_t* payload, size_t length) {
String topicStr = String(topic);
//SerialPrint("I", "=>MQTT", topicStr);
String payloadStr;
payloadStr.reserve(length + 1);
for (size_t i = 0; i < length; i++) {
payloadStr += (char)payload[i];
}
//SerialPrint("I", "=>MQTT", payloadStr);
if (payloadStr.startsWith("HELLO")) {
SerialPrint("I", "MQTT", "Full update");
publishWidgets();
publishState();
choose_log_date_and_send();
}
else if (topicStr.indexOf("control") != -1) {
String key = selectFromMarkerToMarker(topicStr, "/", 3);
String order;
order += key;
order += " ";
order += payloadStr;
order += ",";
loopCmdAdd(order);
SerialPrint("I", "=>MQTT", "Msg from iotmanager app: " + key + " " + payloadStr);
}
else if (topicStr.indexOf("event") != -1) {
if (!jsonReadBool(configSetupJson, "MqttIn")) {
return;
}
if (topicStr.indexOf(chipId) == -1) {
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
String key = selectFromMarkerToMarker(topicStr, "/", 3);
SerialPrint("I", "=>MQTT", "Received event from other device: '" + devId + "' " + key + " " + payloadStr);
String event = key + " " + payloadStr + ",";
eventBuf += event;
}
}
else if (topicStr.indexOf("order") != -1) {
if (!jsonReadBool(configSetupJson, "MqttIn")) {
return;
}
String devId = selectFromMarkerToMarker(topicStr, "/", 2);
String key = selectFromMarkerToMarker(topicStr, "/", 3);
SerialPrint("I", "=>MQTT", "Received direct order " + key + " " + payloadStr);
String order = key + " " + payloadStr + ",";
orderBuf += order;
}
else if (topicStr.indexOf("info") != -1) {
if (topicStr.indexOf("scen") != -1) {
writeFile(String(DEVICE_SCENARIO_FILE), payloadStr);
loadScenario();
SerialPrint("I", "=>MQTT", "Scenario received");
}
}
}
boolean publish(const String& topic, const String& data) {
if (mqtt.beginPublish(topic.c_str(), data.length(), false)) {
mqtt.print(data);
return mqtt.endPublish();
}
return false;
}
boolean publishData(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic;
if (!publish(path, data)) {
SerialPrint("[E]", "MQTT", "on publish data");
return false;
}
return true;
}
boolean publishChart(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/status";
if (!publish(path, data)) {
SerialPrint("[E]", "MQTT", "on publish chart");
return false;
}
return true;
}
boolean publishControl(String id, String topic, String state) {
String path = mqttPrefix + "/" + id + "/" + topic + "/control";
return mqtt.publish(path.c_str(), state.c_str(), false);
}
boolean publishChart_test(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/status";
return mqtt.publish(path.c_str(), data.c_str(), false);
}
boolean publishStatus(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/status";
String json = "{}";
jsonWriteStr(json, "status", data);
return mqtt.publish(path.c_str(), json.c_str(), false);
}
boolean publishEvent(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/event";
return mqtt.publish(path.c_str(), data.c_str(), true);
}
boolean publishInfo(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/info";
return mqtt.publish(path.c_str(), data.c_str(), false);
}
#ifdef LAYOUT_IN_RAM
void publishWidgets() {
if (all_widgets != "") {
int counter = 0;
String line;
int psn_1 = 0;
int psn_2;
do {
psn_2 = all_widgets.indexOf("\r\n", psn_1); //\r\n
line = all_widgets.substring(psn_1, psn_2);
line.replace("\n", "");
line.replace("\r\n", "");
//jsonWriteStr(line, "id", String(counter));
//jsonWriteStr(line, "pageId", String(counter));
counter++;
sendMQTT("config", line);
Serial.println("[V] " + line);
psn_1 = psn_2 + 1;
} while (psn_2 + 2 < all_widgets.length());
getMemoryLoad("I after send all widgets");
}
}
#endif
#ifndef LAYOUT_IN_RAM
void publishWidgets() {
auto file = seekFile("layout.txt");
if (!file) {
SerialPrint("[E]", "MQTT", "no file layout.txt");
return;
}
while (file.available()) {
String payload = file.readStringUntil('\n');
SerialPrint("I", "MQTT", "widgets: " + payload);
publishData("config", payload);
}
file.close();
}
#endif
void publishState() {
// берет строку json и ключи превращает в топики а значения колючей в них посылает
String str = configLiveJson + "," + configStoreJson;
str.replace("{", "");
str.replace("}", "");
str += ",";
while (str.length()) {
String tmp = selectToMarker(str, ",");
String topic = selectToMarker(tmp, "\":");
topic.replace("\"", "");
String state = selectToMarkerLast(tmp, "\":");
state.replace("\"", "");
if (topic != "timenow") {
publishStatus(topic, state);
}
str = deleteBeforeDelimiter(str, ",");
}
}
const String getStateStr() {
switch (mqtt.state()) {
case -4:
return F("no respond");
break;
case -3:
return F("connection was broken");
break;
case -2:
return F("connection failed");
break;
case -1:
return F("client disconnected");
break;
case 0:
return F("client connected");
break;
case 1:
return F("doesn't support the requested version");
break;
case 2:
return F("rejected the client identifier");
break;
case 3:
return F("unable to accept the connection");
break;
case 4:
return F("wrong username/password");
break;
case 5:
return F("not authorized to connect");
break;
default:
return F("unspecified");
break;
}
}

110
src/RemoteOrdersUdp.cpp Normal file
View File

@@ -0,0 +1,110 @@
#include "RemoteOrdersUdp.h"
#include <Arduino.h>
#include "Global.h"
#include "Class/NotAsync.h"
#include "Init.h"
#ifdef UDP_ENABLED
AsyncUDP asyncUdp;
void asyncUdpInit() {
if (!jsonReadBool(configSetupJson, "snaUdp")) {
return;
}
if (asyncUdp.listenMulticast(IPAddress(239, 255, 255, 255), 4210)) {
asyncUdp.onPacket([](AsyncUDPPacket packet) {
//Serial.print("UDP Packet Type: ");
//Serial.println(packet.isBroadcast() ? "Broadcast" : packet.isMulticast() ? "Multicast" : "Unicast");
//
//Serial.print("From: ");
//Serial.print(packet.remoteIP());
//Serial.print(":");
//Serial.println(packet.remotePort());
//
//Serial.print("To: ");
//Serial.print(packet.localIP());
//Serial.print(":");
//Serial.println(packet.localPort());
//Serial.print(", Length: ");
//Serial.print(packet.length());
//
//Serial.print(", Data: ");
//Serial.write(packet.data(), packet.length());
String data = uint8tToString(packet.data(), packet.length());
Serial.print("[i] [udp] Packet received: '");
Serial.print(data);
if (udpPacketValidation(data)) {
Serial.println("', packet valid");
udpPacketParse(data);
}
//else {
// //Serial.println("', Packet invalid");
//}
//reply to the client
//String ip = WiFi.localIP().toString();
//asyncUdp.broadcastTo(ip.c_str(), packet.remotePort());
//packet.printf(ip.c_str(), packet.length());
});
}
myNotAsyncActions->add(
do_sendScenUDP, [&](void*) {
String scen = "iotm;scen:";
scen += readFile(String(DEVICE_SCENARIO_FILE), 2048);
asyncUdp.broadcastTo(scen.c_str(), 4210);
},
nullptr);
//ts.add(
//UDP, 10000, [&](void*) {
//Serial.println("sended");
//asyncUdp.broadcastTo("iotm;Anyone here?", 4210);
//asyncUdp.broadcast("test");
//asyncUdp.print("Hello Server!");
//},
//nullptr, true);
SerialPrint("I", F("UDP"), "Udp Init");
}
bool udpPacketValidation(String& data) {
if (data.indexOf("iotm;") != -1) {
return true;
}
else {
return false;
}
}
void udpPacketParse(String& data) {
if (data.indexOf("scen:") != -1) {
data = deleteBeforeDelimiter(data, ":");
writeFile(String(DEVICE_SCENARIO_FILE), data);
loadScenario();
}
else if (data.indexOf("event:") != -1) {
data = deleteBeforeDelimiter(data, ":");
eventBuf += data;
}
}
String uint8tToString(uint8_t* data, size_t len) {
String ret;
while (len--) {
ret += (char)*data++;
}
return ret;
}
#endif

63
src/SSDP.cpp Normal file
View File

@@ -0,0 +1,63 @@
#include "SSDP.h"
#include "Global.h"
#ifdef SSDP_ENABLED
#ifdef ESP8266
#include <ESP8266SSDP.h>
#endif
#ifdef ESP32
#include <ESP32SSDP.h>
#endif
String xmlNode(String tags, String data);
String decToHex(uint32_t decValue, byte desiredStringLength);
void SsdpInit() {
server.on("/description.xml", HTTP_GET, [](AsyncWebServerRequest* request) {
String ssdpSend = F("<root xmlns=\"urn:schemas-upnp-org:device-1-0\">");
String ssdpHeder = xmlNode(F("major"), "1");
ssdpHeder += xmlNode(F("minor"), "0");
ssdpHeder = xmlNode(F("specVersion"), ssdpHeder);
ssdpHeder += xmlNode(F("URLBase"), "http://" + WiFi.localIP().toString());
String ssdpDescription = xmlNode(F("deviceType"), F("upnp:rootdevice"));
ssdpDescription += xmlNode(F("friendlyName"), jsonReadStr(configSetupJson, F("name")));
ssdpDescription += xmlNode(F("presentationURL"), "/");
ssdpDescription += xmlNode(F("serialNumber"), getChipId());
#ifdef ESP8266
ssdpDescription += xmlNode(F("modelName"), F("ESP8266"));
#endif
#ifdef ESP32
ssdpDescription += xmlNode(F("modelName"), F("ESP32"));
#endif
ssdpDescription += xmlNode(F("modelNumber"), getChipId());
ssdpDescription += xmlNode(F("modelURL"), F("https://github.com/IoTManagerProject/IoTManager/wiki"));
ssdpDescription += xmlNode(F("manufacturer"), F("Borisenko Dmitry"));
ssdpDescription += xmlNode(F("manufacturerURL"), F("https://github.com/IoTManagerProject/IoTManager"));
ssdpDescription += xmlNode(F("UDN"), "uuid:38323636-4558-4dda-9188-cda0e6" + decToHex(ESP_getChipId(), 6));
ssdpDescription = xmlNode("device", ssdpDescription);
ssdpHeder += ssdpDescription;
ssdpSend += ssdpHeder;
ssdpSend += "</root>";
Serial.println("->!!!SSDP Get request received");
request->send(200, "text/xml", ssdpSend);
});
//Если версия 2.0.0 закаментируйте следующую строчку
SSDP.setDeviceType(F("upnp:rootdevice"));
SSDP.setSchemaURL(F("description.xml"));
SSDP.begin();
SerialPrint("I", F("SSDP"), F("Ssdp Init"));
}
String xmlNode(String tags, String data) {
String temp = "<" + tags + ">" + data + "</" + tags + ">";
return temp;
}
String decToHex(uint32_t decValue, byte desiredStringLength) {
String hexString = String(decValue, HEX);
while (hexString.length() < desiredStringLength) hexString = "0" + hexString;
return hexString;
}
#endif

40
src/Servo/Servs.cpp Normal file
View File

@@ -0,0 +1,40 @@
#include "Servo/Servos.h"
Servos myServo;
Servos::Servos(){};
Servo *Servos::create(uint8_t num, uint8_t pin) {
// Ищем среди ранее созданных
for (size_t i = 0; i < _items.size(); i++) {
auto item = _items.at(i);
if (item.num == num) {
if (item.pin != pin) {
item.obj->attach(pin);
item.pin = pin;
};
return item.obj;
}
}
// Добавляем новый
Servo_t newItem{num, pin};
newItem.obj = new Servo();
newItem.obj->attach(pin);
_items.push_back(newItem);
return newItem.obj;
}
Servo *Servos::get(uint8_t num) {
// Ищем среди ранее созданных
for (size_t i = 0; i < _items.size(); i++) {
auto item = _items.at(i);
if (item.num == num) {
return item.obj;
}
}
return nullptr;
}
size_t Servos::count() {
return _items.size();
}

57
src/SoftUART.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "Consts.h"
#ifdef uartEnable
#include "SoftUART.h"
#include "Global.h"
#include "BufferExecute.h"
#ifdef ESP8266
SoftwareSerial* myUART = nullptr;
#else
HardwareSerial* myUART = nullptr;
#endif
void uartInit() {
if (!jsonReadBool(configSetupJson, "uart")) {
return;
}
if (!myUART) {
#ifdef ESP8266
myUART = new SoftwareSerial(jsonReadInt(configSetupJson, "uartTX"), jsonReadInt(configSetupJson, "uartRX"));
myUART->begin(jsonReadInt(configSetupJson, "uartS"));
#else
myUART = new HardwareSerial(2);
myUART->begin(4, 5);
#endif
}
SerialPrint("I", F("UART"), F("UART Init"));
}
void uartHandle() {
if (myUART) {
if (!jsonReadBool(configSetupJson, "uart")) {
return;
}
static String incStr;
if (myUART->available()) {
char inc;
inc = myUART->read();
incStr += inc;
if (inc == '\n') {
parse(incStr);
incStr = "";
}
}
}
}
void parse(String& incStr) {
incStr.replace("\r\n", "");
incStr.replace("\r", "");
incStr.replace("\n", "");
if (incStr.indexOf("set") != -1) {
incStr = deleteBeforeDelimiter(incStr, " ");
loopCmdAdd(incStr);
SerialPrint("I", "=>UART", incStr);
}
}
#endif

121
src/Telegram.cpp Normal file
View File

@@ -0,0 +1,121 @@
#include "Consts.h"
#ifdef telegramEnable
#include "BufferExecute.h"
#include "Telegram.h"
CTBot* myBot{nullptr};
void telegramInit() {
if (isTelegramEnabled()) {
telegramInitBeen = true;
sCmd.addCommand("telegram", sendTelegramMsg);
String token = jsonReadStr(configSetupJson, "telegramApi");
if (!myBot) {
myBot = new CTBot();
}
myBot->setTelegramToken(token);
myBot->enableUTF8Encoding(true);
if (myBot->testConnection()) {
SerialPrint("I", "Telegram", "Connected");
} else {
SerialPrint("E", "Telegram", "Not connected");
}
SerialPrint("I", F("Telegram"), F("Telegram Init"));
}
}
void handleTelegram() {
if (telegramInitBeen) {
if (isTelegramEnabled()) {
if (isTelegramInputOn()) {
TBMessage msg;
static unsigned long prevMillis;
unsigned long currentMillis = millis();
unsigned long difference = currentMillis - prevMillis;
if (difference >= 10000) {
prevMillis = millis();
if (myBot->getNewMessage(msg)) {
SerialPrint("->", "Telegram", "chat ID: " + String(msg.sender.id) + ", msg: " + String(msg.text));
if (jsonReadBool(configSetupJson, "autos")) {
jsonWriteInt(configSetupJson, "chatId", msg.sender.id);
saveConfig();
}
telegramMsgParse(String(msg.text));
}
}
}
}
}
}
void telegramMsgParse(String msg) {
if (msg.indexOf("set") != -1) {
msg = deleteBeforeDelimiter(msg, "_");
msg.replace("_", " ");
loopCmdAdd(String(msg) + ",");
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), "order done");
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg));
} else if (msg.indexOf("get") != -1) {
msg = deleteBeforeDelimiter(msg, "_");
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), getValue(msg)); //jsonReadStr(configLiveJson , msg));
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg));
} else if (msg.indexOf("all") != -1) {
String list = returnListOfParams();
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), list);
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + "\n" + list);
} else {
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), "ID: " + chipId + ", Name: " + jsonReadStr(configSetupJson, F("name")));
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), F("Wrong order, use /all to get all values, /get_id to get value, or /set_id_value to set value"));
}
}
void sendTelegramMsg() {
String sabject = sCmd.next();
String msg = sCmd.next();
if (sabject == "often") {
msg.replace("#", " ");
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg);
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg);
} else {
String prevMsg = jsonReadStr(telegramMsgJson, sabject);
if (prevMsg != msg) {
jsonWriteStr(telegramMsgJson, sabject, msg);
msg.replace("#", " ");
sabject.replace("#", " ");
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), sabject + " " + msg);
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + sabject + " " + msg);
}
}
}
bool isTelegramEnabled() {
return jsonReadBool(configSetupJson, "telegonof");
}
bool isTelegramInputOn() {
return jsonReadBool(configSetupJson, "teleginput");
}
String returnListOfParams() {
String cmdStr = readFile(DEVICE_CONFIG_FILE, 4096);
cmdStr += "\r\n";
cmdStr.replace("\r\n", "\n");
cmdStr.replace("\r", "\n");
int count = 0;
String out;
while (cmdStr.length()) {
String buf = selectToMarker(cmdStr, "\n");
count++;
if (count > 1) {
String id = selectFromMarkerToMarker(buf, ";", 2);
String value = getValue(id); //jsonReadStr(configLiveJson , id);
String page = selectFromMarkerToMarker(buf, ";", 4);
page.replace("#", " ");
String name = selectFromMarkerToMarker(buf, ";", 5);
name.replace("#", " ");
out += page + " " + " " + name + " " + value + "\n";
}
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
}
return out;
}
#endif

18
src/Tests.cpp Normal file
View File

@@ -0,0 +1,18 @@
#include "Tests.h"
#include "Global.h"
#include "ItemsList.h"
#include "Macro.h"
#include "Utils/StringUtils.h"
void testsPerform() {
Serial.println("====some tests section====");
//Serial.println(isDigitDotCommaStr("-12552.5555"));
//String str = "Geeks for Geeks ";
//Serial.println(itemsCount2(str, " "));
Serial.println("==========end============");
}

136
src/UpgradeFirm.cpp Normal file
View File

@@ -0,0 +1,136 @@
#include "Upgrade.h"
#include "FileSystem.h"
#include "Class/NotAsync.h"
#ifdef ESP8266
#include "ESP8266.h"
#else
#include <HTTPUpdate.h>
#endif
#include "Global.h"
void upgradeInit() {
myNotAsyncActions->add(
do_UPGRADE, [&](void*) {
upgrade_firmware(3);
},
nullptr);
myNotAsyncActions->add(
do_GETLASTVERSION, [&](void*) {
getLastVersion();
},
nullptr);
if (isNetworkActive()) {
getLastVersion();
if (lastVersion > 0) {
SerialPrint("I", "Update", "available version: " + String(lastVersion));
if (lastVersion > FIRMWARE_VERSION) {
jsonWriteStr(configSetupJson, "warning2", F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Вышла новая версия прошивки, нажмите <b>обновить прошивку</b></p></font></div>"));
}
}
};
SerialPrint("I", F("Update"), F("Updater Init"));
}
void getLastVersion() {
if ((WiFi.status() == WL_CONNECTED)) {
#ifdef ESP8266
String tmp = getURL(serverIP + F("/projects/iotmanager/esp8266/esp8266ver/esp8266ver.txt"));
#else
String tmp = getURL(serverIP + F("/projects/iotmanager/esp32/esp32ver/esp32ver.txt"));
#endif
if (tmp == "error") {
lastVersion = -1;
}
else {
lastVersion = tmp.toInt();
}
}
else {
lastVersion = -2;
}
jsonWriteInt(configSetupJson, "last_version", lastVersion);
}
void upgrade_firmware(int type) {
String scenario_ForUpdate;
String devconfig_ForUpdate;
String configSetup_ForUpdate;
scenario_ForUpdate = readFile(String(DEVICE_SCENARIO_FILE), 4096);
devconfig_ForUpdate = readFile(String(DEVICE_CONFIG_FILE), 4096);
configSetup_ForUpdate = configSetupJson;
if (type == 1) { //only build
if (upgradeBuild()) restartEsp();
}
else if (type == 2) { //only spiffs
if (upgradeFS()) {
writeFile(String(DEVICE_SCENARIO_FILE), scenario_ForUpdate);
writeFile(String(DEVICE_CONFIG_FILE), devconfig_ForUpdate);
writeFile("config.json", configSetup_ForUpdate);
restartEsp();
}
}
else if (type == 3) { //spiffs and build
if (upgradeFS()) {
writeFile(String(DEVICE_SCENARIO_FILE), scenario_ForUpdate);
writeFile(String(DEVICE_CONFIG_FILE), devconfig_ForUpdate);
writeFile("config.json", configSetup_ForUpdate);
saveConfig();
if (upgradeBuild()) {
restartEsp();
}
}
}
}
bool upgradeFS() {
WiFiClient wifiClient;
bool ret = false;
Serial.printf("Start upgrade %s, please wait...", FS_NAME);
#ifdef ESP8266
ESPhttpUpdate.rebootOnUpdate(false);
t_httpUpdate_return retFS = ESPhttpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp8266/") + FS_NAME + "/"+ FS_NAME+ ".bin");
#else
httpUpdate.rebootOnUpdate(false);
HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, serverIP + F("/projects/iotmanager/esp32/littlefs/spiffs.bin"));
#endif
if (retFS == HTTP_UPDATE_OK) { //если FS обновилась успешно
SerialPrint("I", "Update", "FS upgrade done!");
ret = true;
}
return ret;
}
bool upgradeBuild() {
WiFiClient wifiClient;
bool ret = false;
Serial.println("Start upgrade BUILD, please wait...");
#ifdef ESP8266
ESPhttpUpdate.rebootOnUpdate(false);
t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp8266/firmware/firmware.bin"));
#else
httpUpdate.rebootOnUpdate(false);
HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, serverIP + F("/projects/iotmanager/esp32/firmware/firmware.bin"));
#endif
if (retBuild == HTTP_UPDATE_OK) { //если BUILD обновился успешно
SerialPrint("I", "Update", "BUILD upgrade done!");
ret = true;
}
return ret;
}
void restartEsp() {
Serial.println("Restart ESP....");
delay(1000);
ESP.restart();
}

183
src/Utils/FileUtils.cpp Normal file
View File

@@ -0,0 +1,183 @@
#include "Utils/FileUtils.h"
#include "FileSystem.h"
#include "Utils/StringUtils.h"
#include "Utils\SerialPrint.h"
const String filepath(const String& filename) {
return filename.startsWith("/") ? filename : "/" + filename;
}
bool fileSystemInit() {
if (!FileFS.begin()) {
SerialPrint("E", F("FS"), F("FS Init ERROR, may be FS was not flashed"));
return false;
}
SerialPrint("I", F("FS"), F("FS Init completed"));
return true;
}
void removeFile(const String& filename) {
String path = filepath(filename);
if (FileFS.exists(path)) {
if (!FileFS.remove(path)) {
SerialPrint("I", "Files", "remove " + path);
}
} else {
SerialPrint("E", "Files", "not exist" + path);
}
}
File seekFile(const String& filename, size_t position) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
SerialPrint("[E]", "Files", "open " + path);
}
// поставим курсор в начало файла
file.seek(position, SeekSet);
return file;
}
const String readFileString(const String& filename, const String& to_find) {
String path = filepath(filename);
String res = "failed";
auto file = FileFS.open(path, "r");
if (!file) {
return "failed";
}
if (file.find(to_find.c_str())) {
res = file.readStringUntil('\n');
}
file.close();
return res;
}
const String addFileLn(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "a");
if (!file) {
return "failed";
}
file.println(str);
file.close();
return "sucсess";
}
const String addFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "a");
if (!file) {
return "failed";
}
file.print(str);
file.close();
return "sucсess";
}
bool copyFile(const String& src, const String& dst, bool overwrite) {
String srcPath = filepath(src);
String dstPath = filepath(dst);
SerialPrint("I", "Files", "copy " + srcPath + " to " + dstPath);
if (!FileFS.exists(srcPath)) {
SerialPrint("[E]", "Files", "not exist: " + srcPath);
return false;
}
if (FileFS.exists(dstPath)) {
if (!overwrite) {
SerialPrint("[E]", "Files", "already exist: " + dstPath);
return false;
}
FileFS.remove(dstPath);
}
auto srcFile = FileFS.open(srcPath, "r");
auto dstFile = FileFS.open(dstPath, "w");
uint8_t buf[512];
while (srcFile.available()) {
size_t len = srcFile.read(buf, 512);
dstFile.write(buf, len);
}
srcFile.close();
dstFile.close();
return true;
}
const String writeFile(const String& filename, const String& str) {
String path = filepath(filename);
auto file = FileFS.open(path, "w");
if (!file) {
return "failed";
}
file.print(str);
file.close();
return "sucсess";
}
const String readFile(const String& filename, size_t max_size) {
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
return "failed";
}
size_t size = file.size();
if (size > max_size) {
file.close();
return "large";
}
String temp = file.readString();
file.close();
return temp;
}
size_t countLines(const String filename) {
size_t cnt = -1;
String path = filepath(filename);
auto file = FileFS.open(path, "r");
if (!file) {
return cnt;
}
file.seek(0, SeekSet);
size_t size = file.size();
size_t psn;
do {
cnt++;
file.readStringUntil('\n');
psn = file.position();
} while (psn < size);
file.close();
return cnt;
}
size_t getFileSize(const String filename) {
size_t size = -1;
String filepath(filename);
auto file = FileFS.open(filepath, "r");
if (!file) {
return size;
}
size = file.size();
file.close();
return size;
}
const String getFSSizeInfo() {
String res;
#ifdef ESP8266
FSInfo info;
if (FileFS.info(info)) {
res = prettyBytes(info.usedBytes) + " of " + prettyBytes(info.totalBytes);
} else {
res = "error";
}
#else
res = prettyBytes(FileFS.usedBytes()) + " of " + prettyBytes(FileFS.totalBytes());
#endif
return res;
}
const String getConfigFile(uint8_t preset, ConfigType_t type) {
char buf[64];
sprintf(buf, "/conf/%s%03d.txt", (type == CT_CONFIG) ? "c" : "s", preset);
return String(buf);
}

60
src/Utils/JsonUtils.cpp Normal file
View File

@@ -0,0 +1,60 @@
#include "Utils/JsonUtils.h"
#include "Utils/FileUtils.h"
#include "Global.h"
String jsonReadStr(String& json, String name) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
return root[name].as<String>();
}
boolean jsonReadBool(String& json, String name) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
return root[name].as<boolean>();
}
int jsonReadInt(String& json, String name) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
return root[name];
}
String jsonWriteStr(String& json, String name, String value) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
root[name] = value;
json = "";
root.printTo(json);
return json;
}
String jsonWriteBool(String& json, String name, boolean value) {
return jsonWriteStr(json, name, value ? "1" : "0");
}
String jsonWriteInt(String& json, String name, int value) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
root[name] = value;
json = "";
root.printTo(json);
return json;
}
String jsonWriteFloat(String& json, String name, float value) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
root[name] = value;
json = "";
root.printTo(json);
return json;
}
void saveConfig() {
writeFile(String("config.json"), configSetupJson);
}
void saveStore() {
writeFile(String("store.json"), configStoreJson);
}

View File

@@ -0,0 +1,9 @@
#include "Utils\SerialPrint.h"
#include "Global.h"
void SerialPrint(String errorLevel, String module, String msg) {
//if (module == "Stat") {
Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg);
//}
}

136
src/Utils/StringUtils.cpp Normal file
View File

@@ -0,0 +1,136 @@
#include "Utils/StringUtils.h"
#include "Consts.h"
String selectToMarkerLast(String str, String found) {
int p = str.lastIndexOf(found);
return str.substring(p + found.length());
}
String selectToMarker(String str, String found) {
int p = str.indexOf(found);
return str.substring(0, p);
}
String extractInner(String str) {
int p1 = str.indexOf("[");
int p2 = str.indexOf("]");
return str.substring(p1 + 1, p2);
}
String deleteAfterDelimiter(String str, String found) {
int p = str.indexOf(found);
return str.substring(0, p);
}
String deleteBeforeDelimiter(String str, String found) {
int p = str.indexOf(found) + found.length();
return str.substring(p);
}
String deleteBeforeDelimiterTo(String str, String found) {
int p = str.indexOf(found);
return str.substring(p);
}
String deleteToMarkerLast(String str, String found) {
int p = str.lastIndexOf(found);
return str.substring(0, p);
}
String selectToMarkerPlus(String str, String found, int plus) {
int p = str.indexOf(found);
return str.substring(0, p + plus);
}
String selectFromMarkerToMarker(String str, String tofind, int number) {
if (str.indexOf(tofind) == -1) {
return "not found";
}
str += tofind; // добавим для корректного поиска
uint8_t i = 0; // Индекс перебора
do {
if (i == number) {
// если индекс совпал с позицией
return selectToMarker(str, tofind);
}
// отбросим проверенный блок до разделителя
str = deleteBeforeDelimiter(str, tofind);
i++;
} while (str.length() != 0);
return "not found";
}
uint8_t hexStringToUint8(String hex) {
uint8_t tmp = strtol(hex.c_str(), NULL, 0);
if (tmp >= 0x00 && tmp <= 0xFF) {
return tmp;
}
}
uint16_t hexStringToUint16(String hex) {
uint16_t tmp = strtol(hex.c_str(), NULL, 0);
if (tmp >= 0x0000 && tmp <= 0xFFFF) {
return tmp;
}
}
size_t itemsCount2(String str, const String& separator) {
// если строки поиск нет сразу выход
if (str.indexOf(separator) == -1) {
return 0;
}
// добавим для корректного поиска
str += separator;
size_t cnt = 0;
while (str.length()) {
// отбросим проверенный блок до разделителя
str = deleteBeforeDelimiter(str, separator);
cnt++;
}
return cnt;
}
size_t itemsCount(String& str, const char* delim) {
size_t cnt = 0;
char* cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
char* token;
while ((token = strtok_r(cstr, delim, &cstr))) {
cnt++;
//printf("%s\n", token);
}
delete[] cstr;
return cnt;
}
boolean isDigitStr(const String& str) {
for (size_t i = 0; i < str.length(); i++) {
if (!isDigit(str.charAt(i))) {
return false;
}
}
return str.length();
}
boolean isDigitDotCommaStr(const String& str) {
for (size_t i = 0; i < str.length(); i++) {
char latter = str.charAt(i);
if (!isDigit(latter) && latter != '.' && latter != '-') {
return false;
}
}
return true;
}
String prettyBytes(size_t size) {
if (size < 1024)
return String(size) + "b";
else if (size < (1024 * 1024))
return String(size / 1024.0) + "kB";
else if (size < (1024 * 1024 * 1024))
return String(size / 1024.0 / 1024.0) + "MB";
else
return String(size / 1024.0 / 1024.0 / 1024.0) + "GB";
}

250
src/Utils/SysUtils.cpp Normal file
View File

@@ -0,0 +1,250 @@
#include "Utils/SysUtils.h"
#include "Global.h"
const String getUniqueId(const char* name) {
return String(name) + getMacAddress();
}
uint32_t ESP_getChipId(void) {
#ifdef ESP32
uint32_t id = 0;
for (uint32_t i = 0; i < 17; i = i + 8) {
id |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
return id;
#else
return ESP.getChipId();
#endif
}
uint32_t ESP_getFlashChipId(void) {
#ifdef ESP32
// Нет аналогичной (без доп.кода) функций в 32
// надо использовать другой id - варианты есть
return ESP_getChipId();
#else
return ESP.getFlashChipId();
#endif
}
const String getChipId() {
return String(ESP_getChipId()) + "-" + String(ESP_getFlashChipId());
}
void setChipId() {
chipId = getChipId();
SerialPrint("I", "System", "id: " + chipId);
}
#ifdef ESP8266
static uint32_t total_memory = 52864;
#else
static uint32_t total_memory = ESP.getHeapSize();
#endif
const String printMemoryStatus() {
uint32_t free = ESP.getFreeHeap();
uint32_t used = total_memory - free;
uint32_t memory_load = (used * 100) / total_memory;
char buf[64];
sprintf(buf, "used: %d%% free: %s", memory_load, getHeapStats().c_str());
return String(buf);
}
#ifdef ESP8266
const String getHeapStats() {
uint32_t free;
uint16_t max;
uint8_t frag;
ESP.getHeapStats(&free, &max, &frag);
String buf;
buf += prettyBytes(free);
buf += " frag: ";
buf += frag;
buf += '%';
return buf;
}
#else
const String getHeapStats() {
String buf;
buf = prettyBytes(ESP.getFreeHeap());
return buf;
}
#endif
const String getMacAddress() {
uint8_t mac[6];
char buf[13] = {0};
#if defined(ESP8266)
WiFi.macAddress(mac);
sprintf(buf, MACSTR, MAC2STR(mac));
#else
esp_read_mac(mac, ESP_MAC_WIFI_STA);
sprintf(buf, MACSTR, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
#endif
return String(buf);
}
#ifdef ESP8266
void setLedStatus(LedStatus_t status) {
if (jsonReadBool(configSetupJson, "blink") == 1) {
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) {
if (jsonReadBool(configSetupJson, "blink") == 1) {
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) {
if (WiFi.status() == WL_CONNECTED) {
jsonWriteStr(json, "test1", jsonReadStr(json, "test2"));
jsonWriteStr(json, "test2", jsonReadStr(json, "test3"));
jsonWriteStr(json, "test3", jsonReadStr(json, "test4"));
jsonWriteStr(json, "test4", jsonReadStr(json, "test5"));
jsonWriteStr(json, "test5", jsonReadStr(json, "test6"));
jsonWriteStr(json, "test6", GetTime() + " " + text);
ws.textAll(json);
}
}
*/
//===================================================================
/*
"socket": [
"ws://{{ip}}/ws"
],
*/
//===================================================================
/*
{
"type": "h4",
"title": "('{{build2}}'=='{{firmware_version}}'?'NEW':'OLD')"
},
*/
//===================================================================
/*
{
"type": "button",
"title": "Конфигурация устройства",
"socket": "test2",
"class": "btn btn-block btn-primary"
},
{
"type": "hr"
},
{
"type": "h6",
"title": "{{test1}}"
},
{
"type": "h6",
"title": "{{test2}}"
},
{
"type": "h6",
"title": "{{test3}}"
},
{
"type": "h6",
"title": "{{test4}}"
},
{
"type": "h6",
"title": "{{test5}}"
},
{
"type": "h6",
"title": "{{test6}}"
},
{
"type": "hr"
},
*/
//===================================================================
/*
String getResetReason(uint8_t core) {
int reason = rtc_get_reset_reason(core);
switch (reason) {
case 1 : return "Power on"; break; //Vbat power on reset
case 3 : return "Software reset digital core"; break; //Software reset digital core
case 4 : return "Legacy watch dog reset digital core"; break; //Legacy watch dog reset digital core
case 5 : return "Deep Sleep reset digital core"; break; //Deep Sleep reset digital core
case 6 : return "Reset by SLC module, reset digital core"; break; //Reset by SLC module, reset digital core
case 7 : return "Timer Group0 Watch dog reset digital core"; break; //Timer Group0 Watch dog reset digital core
case 8 : return "Timer Group1 Watch dog reset digital core"; break; //Timer Group1 Watch dog reset digital core
case 9 : return "RTC Watch dog Reset digital core"; break; //
case 10 : return "Instrusion tested to reset CPU"; break;
case 11 : return "Time Group reset CPU"; break;
case 12 : return "Software reset CPU"; break;
case 13 : return "RTC Watch dog Reset CPU"; break;
case 14 : return "for APP CPU, reseted by PRO CPU"; break;
case 15 : return "Reset when the vdd voltage is not stable"; break;
case 16 : return "RTC Watch dog reset digital core and rtc module"; break;
default : return "NO_MEAN";
}
}
String EspClass::getResetReason(void) {
char buff[32];
if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on
strcpy_P(buff, PSTR("Power on"));
} else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset
strcpy_P(buff, PSTR("Hardware Watchdog"));
} else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status wont change
strcpy_P(buff, PSTR("Exception"));
} else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status wont change
strcpy_P(buff, PSTR("Software Watchdog"));
} else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status wont change
strcpy_P(buff, PSTR("Software/System restart"));
} else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep
strcpy_P(buff, PSTR("Deep-Sleep Wake"));
} else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset
strcpy_P(buff, PSTR("External System"));
} else {
strcpy_P(buff, PSTR("Unknown"));
}
return String(buff);
}
*/

237
src/Utils/TimeUtils.cpp Normal file
View File

@@ -0,0 +1,237 @@
#include "Utils/TimeUtils.h"
#include "Global.h"
#include "Utils/StringUtils.h"
static const uint8_t days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//static const char* week_days[7] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
// String getTimeUnix() {
// time_t t;
// struct tm* tm;
// t = time(NULL);
// tm = localtime(&t);
// Serial.printf("%04d/%02d/%02d(%s) %02d:%02d:%02d\n",
// tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, week_days[tm->tm_wday],
// tm->tm_hour, tm->tm_min, tm->tm_sec);
// delay(1000);
// time_t now = time(nullptr);
// if (now < 30000) {
// return "failed";
// }
// return String(now);
// }
// String getTime() {
// time_t now = time(nullptr);
// int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
// now = now + zone;
// String Time = ""; // Строка для результатов времени
// Time += ctime(&now); // Преобразуем время в строку формата Thu Jan 19 00:55:35 2017
// int i = Time.indexOf(":"); //Ишем позицию первого символа :
// Time = Time.substring(i - 2, i + 6); // Выделяем из строки 2 символа перед символом : и 6 символов после
// return Time; // Возврашаем полученное время
// }
// String getTimeWOsec() {
// time_t now = time(nullptr);
// int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
// now = now + zone;
// String Time = ""; // Строка для результатов времени
// Time += ctime(&now); // Преобразуем время в строку формата Thu Jan 19 00:55:35 2017
// int i = Time.indexOf(":"); //Ишем позицию первого символа :
// Time = Time.substring(i - 2, i + 3); // Выделяем из строки 2 символа перед символом : и 6 символов после
// return Time; // Возврашаем полученное время
// }
// String getDate() {
// time_t now = time(nullptr);
// int zone = 3600 * jsonReadStr(configSetupJson, "timezone").toInt();
// now = now + zone;
// String Data = ""; // Строка для результатов времени
// Data += ctime(&now); // Преобразуем время в строку формата Thu Jan 19 00:55:35 2017
// Data.replace("\n", "");
// uint8_t i = Data.lastIndexOf(" "); //Ишем позицию последнего символа пробел
// String Time = Data.substring(i - 8, i + 1); // Выделяем время и пробел
// Data.replace(Time, ""); // Удаляем из строки 8 символов времени и пробел
// return Data; // Возврашаем полученную дату
// }
// String getDateDigitalFormated() {
// String date = getDate();
// date = deleteBeforeDelimiter(date, " ");
// date.replace("Jan", "01");
// date.replace("Feb", "02");
// date.replace("Mar", "03");
// date.replace("Apr", "04");
// date.replace("May", "05");
// date.replace("Jun", "06");
// date.replace("Jul", "07");
// date.replace("Aug", "08");
// date.replace("Sep", "09");
// date.replace("Oct", "10");
// date.replace("Nov", "11");
// date.replace("Dec", "12");
// String month = date.substring(0, 2);
// String day = date.substring(3, 5);
// String year = date.substring(8, 10);
// String out = day;
// out += ".";
// out += month;
// out += ".";
// out += year;
// return out;
// }
// int timeToMin(String Time) {
// //"00:00:00" время в секунды
// long min = selectToMarker(Time, ":").toInt() * 60; //общее количество секунд в полных часах
// Time = deleteBeforeDelimiter(Time, ":"); // Теперь здесь минуты секунды
// min += selectToMarker(Time, ":").toInt(); // Добавим секунды из полных минут
// return min;
// }
static const char* TIME_FORMAT PROGMEM = "%02d:%02d:%02d";
static const char* TIME_FORMAT_WITH_DAYS PROGMEM = "%dd %02d:%02d";
const String prettySeconds(unsigned long time_s) {
unsigned long tmp = time_s;
unsigned long seconds;
unsigned long minutes;
unsigned long hours;
unsigned long days;
seconds = tmp % 60;
tmp = tmp / 60;
minutes = tmp % 60;
tmp = tmp / 60;
hours = tmp % 24;
days = tmp / 24;
char buf[32];
if (days) {
sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds);
}
else {
sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds);
}
return String(buf);
}
const String prettyMillis(unsigned long time_ms) {
return prettySeconds(time_ms / 1000);
}
unsigned long millis_since(unsigned long sinse) {
return millis_passed(sinse, millis());
}
unsigned long millis_passed(unsigned long start, unsigned long finish) {
unsigned long result = 0;
if (start <= finish) {
unsigned long passed = finish - start;
if (passed <= __LONG_MAX__) {
result = static_cast<long>(passed);
}
else {
result = static_cast<long>((__LONG_MAX__ - finish) + start + 1u);
}
}
else {
unsigned long passed = start - finish;
if (passed <= __LONG_MAX__) {
result = static_cast<long>(passed);
result = -1 * result;
}
else {
result = static_cast<long>((__LONG_MAX__ - start) + finish + 1u);
result = -1 * result;
}
}
return result;
}
int getOffsetInSeconds(int timezone) {
return getOffsetInMinutes(timezone) * ONE_MINUTE_s;
}
int getOffsetInMinutes(int timezone) {
return timezone * ONE_HOUR_m;
}
void breakEpochToTime(unsigned long epoch, Time_t& tm) {
// break the given time_input into time components
// this is a more compact version of the C library localtime function
unsigned long time = epoch;
tm.second = time % 60;
time /= 60; // now it is minutes
tm.minute = time % 60;
time /= 60; // now it is hours
tm.hour = time % 24;
time /= 24; // now it is days
tm.days = time;
tm.day_of_week = ((time + 4) % 7) + 1; // Sunday is day 1
uint8_t year = 0;
unsigned long days = 0;
while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++;
}
tm.year = year - 30;
days -= LEAP_YEAR(year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0
tm.day_of_year = time;
uint8_t month;
uint8_t month_length;
for (month = 0; month < 12; month++) {
if (1 == month) { // february
if (LEAP_YEAR(year)) {
month_length = 29;
}
else {
month_length = 28;
}
}
else {
month_length = days_in_month[month];
}
if (time >= month_length) {
time -= month_length;
}
else {
break;
}
}
tm.month = month + 1; // jan is month 1
tm.day_of_month = time + 1; // day of month
tm.valid = (epoch > MIN_DATETIME);
}
void timeInit() {
ts.add(
TIME, 1000, [&](void*) {
String timenow = timeNow->getTimeWOsec();
static String prevTime;
if (prevTime != timenow) {
prevTime = timenow;
jsonWriteStr(configLiveJson, "timenow", timenow);
eventGen2("timenow", timenow);
SerialPrint("I", F("NTP"), timenow);
}
},
nullptr, true);
SerialPrint("I", F("NTP"), F("Handle time init"));
}

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

@@ -0,0 +1,92 @@
#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;
}

173
src/Utils/WiFiUtils.cpp Normal file
View File

@@ -0,0 +1,173 @@
#include "Utils/WiFiUtils.h"
#include "FileSystem.h"
void routerConnect() {
WiFi.setAutoConnect(false);
WiFi.persistent(false);
setLedStatus(LED_SLOW);
WiFi.mode(WIFI_STA);
byte tries = 20;
String _ssid = jsonReadStr(configSetupJson, "routerssid");
String _password = jsonReadStr(configSetupJson, "routerpass");
if (_ssid == "" && _password == "") {
WiFi.begin();
} else {
WiFi.begin(_ssid.c_str(), _password.c_str());
#ifdef ESP32
WiFi.setTxPower(WIFI_POWER_19_5dBm);
#else
WiFi.setOutputPower(20.5);
#endif
SerialPrint("I", "WIFI", "ssid: " + _ssid);
}
while (--tries && WiFi.status() != WL_CONNECTED) {
if (WiFi.status() == WL_CONNECT_FAILED) {
SerialPrint("E", "WIFI", "password is not correct");
tries = 1;
jsonWriteInt(configOptionJson, "pass_status", 1);
}
Serial.print(".");
delay(1000);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("");
startAPMode();
} else {
Serial.println("");
SerialPrint("I", "WIFI", "http://" + WiFi.localIP().toString());
jsonWriteStr(configSetupJson, "ip", WiFi.localIP().toString());
setLedStatus(LED_OFF);
mqttInit();
}
SerialPrint("I", F("WIFI"), F("Network Init"));
}
bool startAPMode() {
setLedStatus(LED_ON);
SerialPrint("I", "WIFI", "AP Mode");
WiFi.disconnect();
WiFi.mode(WIFI_AP);
String _ssidAP = jsonReadStr(configSetupJson, "apssid");
String _passwordAP = jsonReadStr(configSetupJson, "appass");
WiFi.softAP(_ssidAP.c_str(), _passwordAP.c_str());
IPAddress myIP = WiFi.softAPIP();
SerialPrint("I", "WIFI", "AP IP: " + myIP.toString());
jsonWriteStr(configSetupJson, "ip", myIP.toString());
//if (jsonReadInt(configOptionJson, "pass_status") != 1) {
ts.add(
WIFI_SCAN, 10 * 1000, [&](void*) {
String sta_ssid = jsonReadStr(configSetupJson, "routerssid");
SerialPrint("I", "WIFI", "scanning for " + sta_ssid);
if (RouterFind(sta_ssid)) {
ts.remove(WIFI_SCAN);
WiFi.scanDelete();
routerConnect();
}
},
nullptr, true);
//}
return true;
}
boolean RouterFind(String ssid) {
bool res = false;
int n = WiFi.scanComplete();
SerialPrint("I", "WIFI", "scan result: " + String(n, DEC));
if (n == -2) { //Сканирование не было запущено, запускаем
SerialPrint("I", "WIFI", "start scanning");
WiFi.scanNetworks(true, false); //async, show_hidden
}
else if (n == -1) { //Сканирование все еще выполняется
SerialPrint("I", "WIFI", "scanning in progress");
}
else if (n == 0) { //ни одна сеть не найдена
SerialPrint("I", "WIFI", "no networks found");
WiFi.scanNetworks(true, false);
}
else if (n > 0) {
for (int8_t i = 0; i < n; i++) {
if (WiFi.SSID(i) == ssid) {
res = true;
}
SerialPrint("I", "WIFI", (res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
}
}
WiFi.scanDelete();
return res;
}
boolean isNetworkActive() {
return WiFi.status() == WL_CONNECTED;
}
uint8_t RSSIquality() {
uint8_t res = 0;
if (WiFi.status() == WL_CONNECTED) {
int rssi = WiFi.RSSI();
if (rssi >= -50) {
res = 6; //"Excellent";
} else if (rssi < -50 && rssi >= -60) {
res = 5; //"Very good";
} else if (rssi < -60 && rssi >= -70) {
res = 4; //"Good";
} else if (rssi < -70 && rssi >= -80) {
res = 3; //"Low";
} else if (rssi < -80 && rssi > -100) {
res = 2; //"Very low";
} else if (rssi <= -100) {
res = 1; //"No signal";
}
}
return res;
}
void wifiSignalInit() {
ts.add(
SYGNAL, 1000 * 60, [&](void*) {
SerialPrint("I", "System", printMemoryStatus());
#ifdef ESP8266
getFSInfo();
#endif
switch (RSSIquality()) {
case 0:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='red'>не подключено к роутеру</font>"));
break;
case 1:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='red'>нет сигнала</font>"));
break;
case 2:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='red'>очень низкий</font>"));
break;
case 3:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='orange'>низкий</font>"));
break;
case 4:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='green'>хороший</font>"));
break;
case 5:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='green'>очень хороший</font>"));
break;
case 6:
jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: <font color='green'>отличный</font>"));
break;
}
},
nullptr, true);
}

316
src/Utils/statUtils.cpp Normal file
View File

@@ -0,0 +1,316 @@
#include "Utils/StatUtils.h"
#include <Arduino.h>
#include <EEPROM.h>
#include "Global.h"
#include "ItemsList.h"
#ifdef ESP32
#include <rom/rtc.h>
#endif
String ESP_getResetReason(void);
void initSt() {
addNewDevice();
decide();
if (TELEMETRY_UPDATE_INTERVAL_MIN) {
ts.add(
STATISTICS, TELEMETRY_UPDATE_INTERVAL_MIN * 60000, [&](void*) {
static bool secondTime = false;
if (secondTime) getNextNumber("totalhrs.txt");
secondTime = true;
updateDeviceStatus();
},
nullptr, true);
}
SerialPrint("I", F("Stat"), F("Stat Init"));
}
void decide() {
if ((WiFi.status() == WL_CONNECTED)) {
uint8_t cnt = getNextNumber("stat.txt");
SerialPrint("I", "Stat", "Total resets number: " + String(cnt));
if (cnt <= 3) {
Serial.println("(get)");
getPsn();
}
else {
if (cnt % 5) {
Serial.println("(skip)");
}
else {
Serial.println("(get)");
getPsn();
}
}
}
}
void getPsn() {
String res = getURL(F("http://ipinfo.io/?token=c60f88583ad1a4"));
if (res != "") {
String line = jsonReadStr(res, "loc");
String lat = selectToMarker(line, ",");
String lon = deleteBeforeDelimiter(line, ",");
String geo = jsonReadStr(res, "city") + ", " + jsonReadStr(res, "country") + ", " + jsonReadStr(res, "region");
updateDevicePsn(lat, lon, "0", geo);
}
}
String addNewDevice() {
String ret;
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
String json = "{}";
String mac = WiFi.macAddress().c_str();
//==============================================
jsonWriteStr(json, "uniqueId", mac);
jsonWriteStr(json, "name", FIRMWARE_NAME);
jsonWriteStr(json, "model", getChipId());
//==============================================
http.begin(client, serverIP + F(":8082/api/devices/"));
http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json");
int httpCode = http.POST(json);
if (httpCode > 0) {
ret = httpCode;
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
ret += " " + payload;
//saveId("statid.txt", jsonReadInt(payload, "id"));
}
}
else {
ret = http.errorToString(httpCode).c_str();
}
http.end();
}
SerialPrint("I", "Stat", "New device registaration: " + ret);
return ret;
}
String updateDevicePsn(String lat, String lon, String accur, String geo) {
String ret;
if ((WiFi.status() == WL_CONNECTED)) {
float latfl = lat.toFloat();
float lonfl = lon.toFloat();
randomSeed(micros());
float c = random(2, 9);
if (c > 5) {
latfl = latfl + (c / 100);
lonfl = lonfl + (c / 100);
}
else {
latfl = latfl - (c / 100);
lonfl = lonfl - (c / 100);
}
WiFiClient client;
HTTPClient http;
http.begin(client, serverIP + F(":5055/"));
http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json");
String mac = WiFi.macAddress().c_str();
int httpCode = http.POST("?id=" + mac +
"&lat=" + String(latfl) +
"&lon=" + String(lonfl) +
"&accuracy=" + accur + "");
if (httpCode > 0) {
ret = httpCode;
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
ret += " " + payload;
}
}
else {
ret = http.errorToString(httpCode).c_str();
}
http.end();
}
SerialPrint("I", "Stat", "Update device psn: " + ret);
return ret;
}
String updateDeviceStatus() {
String ret;
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverIP + F(":5055/"));
http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json");
String mac = WiFi.macAddress().c_str();
int httpCode = http.POST("?id=" + mac +
"&resetReason=" + ESP_getResetReason() +
"&uptime=" + timeNow->getUptime() +
"&uptimeTotal=" + getUptimeTotal() +
"&version=" + FIRMWARE_VERSION +
"&resetsTotal=" + String(getCurrentNumber("stat.txt")) +
"&heap=" + String(ESP.getFreeHeap()) + "");
if (httpCode > 0) {
ret = httpCode;
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
ret += " " + payload;
}
}
else {
ret = http.errorToString(httpCode).c_str();
}
http.end();
}
SerialPrint("I", "Stat", "Update device data: " + ret);
return ret;
}
String getUptimeTotal() {
uint8_t hrs = getCurrentNumber("totalhrs.txt");
String hrsStr = prettySeconds(hrs * 60 * 60);
SerialPrint("I", "Stat", "Total running time: " + hrsStr);
return hrsStr;
}
uint8_t getNextNumber(String file) {
uint8_t number = readFile(file, 100).toInt();
number++;
removeFile(file);
addFile(file, String(number));
return number;
}
uint8_t getCurrentNumber(String file) {
uint8_t number = readFile(file, 100).toInt();
return number;
}
#ifdef ESP8266
String ESP_getResetReason(void) {
return ESP.getResetReason();
}
#else
String ESP32GetResetReason(uint32_t cpu_no) {
// tools\sdk\include\esp32\rom\rtc.h
switch (rtc_get_reset_reason((RESET_REASON)cpu_no)) {
case POWERON_RESET:
return F("Vbat power on reset"); // 1
case SW_RESET:
return F("Software reset digital core"); // 3
case OWDT_RESET:
return F("Legacy watch dog reset digital core"); // 4
case DEEPSLEEP_RESET:
return F("Deep Sleep reset digital core"); // 5
case SDIO_RESET:
return F("Reset by SLC module, reset digital core"); // 6
case TG0WDT_SYS_RESET:
return F("Timer Group0 Watch dog reset digital core"); // 7
case TG1WDT_SYS_RESET:
return F("Timer Group1 Watch dog reset digital core"); // 8
case RTCWDT_SYS_RESET:
return F("RTC Watch dog Reset digital core"); // 9
case INTRUSION_RESET:
return F("Instrusion tested to reset CPU"); // 10
case TGWDT_CPU_RESET:
return F("Time Group reset CPU"); // 11
case SW_CPU_RESET:
return F("Software reset CPU"); // 12
case RTCWDT_CPU_RESET:
return F("RTC Watch dog Reset CPU"); // 13
case EXT_CPU_RESET:
return F("or APP CPU, reseted by PRO CPU"); // 14
case RTCWDT_BROWN_OUT_RESET:
return F("Reset when the vdd voltage is not stable"); // 15
case RTCWDT_RTC_RESET:
return F("RTC Watch dog reset digital core and rtc module"); // 16
default:
return F("NO_MEAN"); // 0
}
}
String ESP_getResetReason(void) {
return ESP32GetResetReason(0); // CPU 0
}
#endif
//String getUptimeTotal() {
// static int hrs;
// EEPROM.begin(512);
// hrs = eeGetInt(0);
// SerialPrint("I","Stat","Total running hrs: " + String(hrs));
// String hrsStr = prettySeconds(hrs * 60 * 60);
// SerialPrint("I","Stat","Total running hrs (f): " + hrsStr);
// return hrsStr;
//}
//int plusOneHour() {
// static int hrs;
// EEPROM.begin(512);
// hrs = eeGetInt(0);
// hrs++;
// eeWriteInt(0, hrs);
// return hrs;
//}
//
//void eeWriteInt(int pos, int val) {
// byte* p = (byte*)&val;
// EEPROM.write(pos, *p);
// EEPROM.write(pos + 1, *(p + 1));
// EEPROM.write(pos + 2, *(p + 2));
// EEPROM.write(pos + 3, *(p + 3));
// EEPROM.commit();
//}
//
//int eeGetInt(int pos) {
// int val;
// byte* p = (byte*)&val;
// *p = EEPROM.read(pos);
// *(p + 1) = EEPROM.read(pos + 1);
// *(p + 2) = EEPROM.read(pos + 2);
// *(p + 3) = EEPROM.read(pos + 3);
// if (val < 0) {
// return 0;
// } else {
// return val;
// }
//}
//========for updating list of device=================
/*
void updateDeviceList() {
if ((WiFi.status() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
String json = "{}";
String mac = WiFi.macAddress().c_str();
//===============================================
jsonWriteStr(json, "uniqueId", mac);
jsonWriteStr(json, "name", FIRMWARE_NAME);
jsonWriteStr(json, "model", FIRMWARE_VERSION);
jsonWriteInt(json, "id", getId("statid.txt"));
//===============================================
http.begin(client, "http://") + serverIP + F(":8082/api/devices/" + mac + "/");
http.setAuthorization("admin", "admin");
http.addHeader("Content-Type", "application/json");
int httpCode = http.PUT(json);
if (httpCode > 0) {
Serial.printf("update Device List... code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
const String& payload = http.getString();
Serial.println("received payload:\n<<");
Serial.println(payload);
Serial.println(">>");
}
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}
void saveId(String file, int id) {
removeFile(file);
addFile(file, String(id));
}
int getId(String file) {
return readFile(file, 100).toInt();
}
*/

397
src/Web.cpp Normal file
View File

@@ -0,0 +1,397 @@
#include "Web.h"
#include "Class/NotAsync.h"
#include "Global.h"
#include "Init.h"
#include "ItemsList.h"
#include "RemoteOrdersUdp.h"
#include "SoftUART.h"
#include "Telegram.h"
#include "items/vLogging.h"
bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) {
if (request->hasArg("preset")) {
preset = request->getParam("preset")->value().toInt();
return true;
}
return false;
}
void web_init() {
server.on("/set", HTTP_GET, [](AsyncWebServerRequest* request) {
//==============================set.device.json====================================================================================================
if (request->hasArg(F("addItem"))) {
addItem2(request->getParam("addItem")->value().toInt());
request->redirect("/?set.device");
}
if (request->hasArg(F("addPreset"))) {
addPreset2(request->getParam(F("addPreset"))->value().toInt());
jsonWriteStr(configSetupJson, F("warning1"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Требуется перезагрузка</p></font></div>"));
request->redirect(F("/?set.device"));
}
if (request->hasArg(F("delChoosingItems"))) {
jsonWriteStr(configSetupJson, F("warning1"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Требуется перезагрузка</p></font></div>"));
myNotAsyncActions->make(do_delChoosingItems);
request->send(200);
}
if (request->hasArg(F("delAllItems"))) {
delAllItems();
cleanLogAndData();
jsonWriteStr(configSetupJson, F("warning1"), F("<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #ffc7c7; padding: 10px;'>Требуется перезагрузка</p></font></div>"));
request->redirect(F("/?set.device"));
}
if (request->hasArg(F("saveItems"))) {
myNotAsyncActions->make(do_deviceInit);
request->send(200);
}
if (request->hasArg(F("scen"))) {
bool value = request->getParam(F("scen"))->value().toInt();
jsonWriteBool(configSetupJson, F("scen"), value);
saveConfig();
loadScenario();
request->send(200);
}
if (request->hasArg(F("sceninit"))) {
loadScenario();
request->send(200);
}
if (request->hasArg(F("MqttIn"))) {
bool value = request->getParam(F("MqttIn"))->value().toInt();
jsonWriteBool(configSetupJson, "MqttIn", value);
saveConfig();
mqtt.subscribe((mqttPrefix + "/+/+/event").c_str());
mqtt.subscribe((mqttPrefix + "/+/+/info").c_str());
request->send(200);
}
if (request->hasArg(F("MqttOut"))) {
bool value = request->getParam(F("MqttOut"))->value().toInt();
jsonWriteBool(configSetupJson, F("MqttOut"), value);
saveConfig();
request->send(200);
}
if (request->hasArg(F("scenMqtt"))) {
myNotAsyncActions->make(do_sendScenMQTT);
request->send(200);
}
if (request->hasArg(F("cleanlog"))) {
cleanLogAndData();
request->send(200);
}
//==============================wifi settings=============================================
if (request->hasArg(F("devname"))) {
jsonWriteStr(configSetupJson, F("name"), request->getParam(F("devname"))->value());
saveConfig();
request->send(200);
}
if (request->hasArg(F("routerssid"))) {
jsonWriteStr(configSetupJson, F("routerssid"), request->getParam(F("routerssid"))->value());
saveConfig();
request->send(200);
}
if (request->hasArg(F("routerpass"))) {
jsonWriteStr(configSetupJson, F("routerpass"), request->getParam(F("routerpass"))->value());
saveConfig();
request->send(200);
}
if (request->hasArg(F("apssid"))) {
jsonWriteStr(configSetupJson, F("apssid"), request->getParam(F("apssid"))->value());
saveConfig();
request->send(200, "text/text", "OK");
}
if (request->hasArg(F("appass"))) {
jsonWriteStr(configSetupJson, F("appass"), request->getParam(F("appass"))->value());
saveConfig();
request->send(200);
}
if (request->hasArg(F("weblogin"))) {
jsonWriteStr(configSetupJson, F("weblogin"), request->getParam(F("weblogin"))->value());
saveConfig();
request->send(200);
}
if (request->hasArg(F("webpass"))) {
jsonWriteStr(configSetupJson, F("webpass"), request->getParam(F("webpass"))->value());
saveConfig();
request->send(200);
}
if (request->hasArg(F("timezone"))) {
String timezoneStr = request->getParam(F("timezone"))->value();
jsonWriteStr(configSetupJson, F("timezone"), timezoneStr);
saveConfig();
timeNow->setTimezone(timezoneStr.toInt());
request->send(200);
}
if (request->hasArg(F("ntp"))) {
String ntpStr = request->getParam(F("ntp"))->value();
jsonWriteStr(configSetupJson, F("ntp"), ntpStr);
saveConfig();
timeNow->setNtpPool(ntpStr);
request->send(200);
}
if (request->hasArg(F("blink"))) {
bool value = request->getParam(F("blink"))->value().toInt();
jsonWriteBool(configSetupJson, F("blink"), value);
saveConfig();
request->send(200);
}
if (request->hasArg(F("reqReset"))) {
String tmp = "{}";
jsonWriteStr(tmp, "title", F("<button class=\"close\" onclick=\"toggle('reset-block')\">×</button>Вы действительно хотите перезагрузить устройство?<a href=\"#\" class=\"btn btn-block btn-danger\" onclick=\"send_request(this, '/set?reset');setTimeout(function(){ location.href='/?set.device'; }, 15000);html('reset-block','<span class=loader></span>Идет перезагрузка устройства')\">Перезагрузить</a>"));
jsonWriteStr(tmp, "class", "pop-up");
request->send(200, "text/html", tmp);
}
if (request->hasArg(F("reset"))) {
ESP.restart();
request->send(200);
}
//==============================mqtt settings=============================================
//primary
if (request->hasArg(F("mqttServer"))) {
jsonWriteStr(configSetupJson, F("mqttServer"), request->getParam(F("mqttServer"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttPort"))) {
int port = (request->getParam(F("mqttPort"))->value()).toInt();
jsonWriteInt(configSetupJson, F("mqttPort"), port);
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttPrefix"))) {
jsonWriteStr(configSetupJson, F("mqttPrefix"), request->getParam(F("mqttPrefix"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttUser"))) {
jsonWriteStr(configSetupJson, F("mqttUser"), request->getParam(F("mqttUser"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttPass"))) {
jsonWriteStr(configSetupJson, F("mqttPass"), request->getParam(F("mqttPass"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
//secondary
if (request->hasArg(F("mqttServer2"))) {
jsonWriteStr(configSetupJson, F("mqttServer2"), request->getParam(F("mqttServer2"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttPort2"))) {
int port = (request->getParam(F("mqttPort2"))->value()).toInt();
jsonWriteInt(configSetupJson, F("mqttPort2"), port);
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttPrefix2"))) {
jsonWriteStr(configSetupJson, F("mqttPrefix2"), request->getParam(F("mqttPrefix2"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttUser2"))) {
jsonWriteStr(configSetupJson, F("mqttUser2"), request->getParam(F("mqttUser2"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg(F("mqttPass2"))) {
jsonWriteStr(configSetupJson, F("mqttPass2"), request->getParam(F("mqttPass2"))->value());
saveConfig();
myNotAsyncActions->make(do_MQTTPARAMSCHANGED);
request->send(200);
}
if (request->hasArg("mqttsend")) {
//myNotAsyncActions->make(do_MQTTUDP);
request->send(200);
}
if (request->hasArg("mqttcheck")) {
String buf = "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + getStateStr();
String payload = "{}";
jsonWriteStr(payload, "title", buf);
jsonWriteStr(payload, "class", "pop-up");
request->send(200, "text/html", payload);
}
//==============================telegram settings=============================================
if (request->hasArg("telegramApi")) {
jsonWriteStr(configSetupJson, "telegramApi", request->getParam("telegramApi")->value());
saveConfig();
request->send(200);
}
if (request->hasArg("autos")) {
bool value = request->getParam("autos")->value().toInt();
jsonWriteBool(configSetupJson, "autos", value);
saveConfig();
request->send(200);
}
if (request->hasArg("chatId")) {
jsonWriteStr(configSetupJson, "chatId", request->getParam("chatId")->value());
saveConfig();
request->send(200);
}
if (request->hasArg("telegonof")) {
bool value = request->getParam("telegonof")->value().toInt();
jsonWriteBool(configSetupJson, "telegonof", value);
saveConfig();
request->send(200);
}
if (request->hasArg("teleginput")) {
bool value = request->getParam("teleginput")->value().toInt();
jsonWriteBool(configSetupJson, "teleginput", value);
saveConfig();
request->send(200);
}
//==============================utilities settings=============================================
if (request->hasArg("i2c")) {
myNotAsyncActions->make(do_BUSSCAN);
request->redirect("/?set.utilities");
}
if (request->hasArg("uart")) {
bool value = request->getParam("uart")->value().toInt();
jsonWriteBool(configSetupJson, "uart", value);
saveConfig();
#ifdef uartEnable
uartInit();
#endif
request->send(200);
}
if (request->hasArg("uartEvents")) {
bool value = request->getParam("uartEvents")->value().toInt();
jsonWriteBool(configSetupJson, "uartEvents", value);
saveConfig();
request->send(200);
}
if (request->hasArg("uartS")) {
jsonWriteStr(configSetupJson, "uartS", request->getParam("uartS")->value());
saveConfig();
#ifdef uartEnable
uartInit();
#endif
request->send(200);
}
if (request->hasArg("uartTX")) {
jsonWriteStr(configSetupJson, "uartTX", request->getParam("uartTX")->value());
saveConfig();
#ifdef uartEnable
uartInit();
#endif
request->send(200);
}
if (request->hasArg("uartRX")) {
jsonWriteStr(configSetupJson, "uartRX", request->getParam("uartRX")->value());
saveConfig();
#ifdef uartEnable
uartInit();
#endif
request->send(200);
}
//==============================developer settings=============================================
if (request->hasArg("serverip")) {
jsonWriteStr(configSetupJson, "serverip", request->getParam("serverip")->value());
saveConfig();
serverIP = jsonReadStr(configSetupJson, "serverip");
request->send(200);
}
//set?order=button_1
if (request->hasArg("order")) {
String order = request->getParam("order")->value();
order.replace("_", " ");
orderBuf += order + ",";
request->send(200);
}
if (request->hasArg("grafmax")) {
int value = request->getParam("grafmax")->value().toInt();
jsonWriteInt(configSetupJson, "grafmax", value);
saveConfig();
request->send(200);
}
});
server.on("/order", HTTP_GET, [](AsyncWebServerRequest* request) {
});
server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsyncActions->make(do_GETLASTVERSION);
SerialPrint("I", "Update", "firmware version: " + String(lastVersion));
String msg = "";
if (USE_OTA) {
if (lastVersion == FIRMWARE_VERSION) {
msg = F("Актуальная версия прошивки уже установлена.");
} else if (lastVersion > FIRMWARE_VERSION) {
msg = F("Новая версия прошивки<a href=\"#\" class=\"btn btn-block btn-danger\" onclick=\"send_request(this, '/upgrade');setTimeout(function(){ location.href='/?set.device'; }, 90000);html('my-block','<span class=loader></span>Идет обновление прошивки, после обновления страница перезагрузится автоматически...')\">Установить</a>");
} else if (lastVersion == -1) {
msg = F("Cервер не найден. Попробуйте повторить позже...");
} else if (lastVersion == -2) {
msg = F("Устройство не подключено к роутеру!");
} else if (lastVersion < FIRMWARE_VERSION) {
msg = F("Ошибка версии. Попробуйте повторить позже...");
}
} else {
msg = F("Обновление невозможно, память устройства 1 мб");
}
String tmp = "{}";
jsonWriteStr(tmp, "title", "<button class=\"close\" onclick=\"toggle('my-block')\">×</button>" + msg);
jsonWriteStr(tmp, "class", "pop-up");
request->send(200, "text/html", tmp);
});
/*
* Upgrade
*/
server.on("/upgrade", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsyncActions->make(do_UPGRADE);
request->send(200, "text/html");
});
SerialPrint("I", F("Web"), F("WebAdmin Init"));
}
void setConfigParam(const char* param, const String& value) {
SerialPrint("I", "Web", "set " + String(param) + ": " + value);
jsonWriteStr(configSetupJson, param, value);
saveConfig();
}

208
src/WebServer.cpp Normal file
View File

@@ -0,0 +1,208 @@
#include "HttpServer.h"
#include "BufferExecute.h"
#include "Utils/FileUtils.h"
#include "Utils/WebUtils.h"
#include "FSEditor.h"
namespace HttpServer {
/* Forward declaration */
void initOta();
void initMDNS();
void initWS();
void init() {
String login = jsonReadStr(configSetupJson, "weblogin");
String pass = jsonReadStr(configSetupJson, "webpass");
#ifdef ESP32
server.addHandler(new FSEditor(FileFS, login, pass));
#else
server.addHandler(new FSEditor(login, pass));
#endif
server.serveStatic("/css/", FileFS, "/css/").setCacheControl("max-age=600");
server.serveStatic("/js/", FileFS, "/js/").setCacheControl("max-age=600");
server.serveStatic("/favicon.ico", FileFS, "/favicon.ico").setCacheControl("max-age=600");
server.serveStatic("/icon.jpeg", FileFS, "/icon.jpeg").setCacheControl("max-age=600");
server.serveStatic("/edit", FileFS, "/edit").setCacheControl("max-age=600");
server.serveStatic("/", FileFS, "/").setDefaultFile("index.htm").setAuthentication(login.c_str(), pass.c_str());
server.onNotFound([](AsyncWebServerRequest *request) {
SerialPrint("[E]","WebServer","not found:\n" + getRequestInfo(request));
request->send(404);
});
server.onFileUpload([](AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final) {
// TODO
if (!index) {
SerialPrint("I","WebServer","start upload " + filename);
}
if (final) {
SerialPrint("I","WebServer","finish upload: " + prettyBytes(index + len));
}
});
// динамические данные
server.on("/config.live.json", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", configLiveJson);
});
server.on("/config.store.json", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", configStoreJson);
});
// данные не являющиеся событиями
server.on("/config.option.json", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", configOptionJson);
});
// для хранения постоянных данных
server.on("/config.setup.json", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", configSetupJson);
});
server.on("/cmd", HTTP_GET, [](AsyncWebServerRequest *request) {
String cmdStr = request->getParam("command")->value();
SerialPrint("I","WebServer","do: " + cmdStr);
loopCmdAdd(cmdStr);
request->send(200, "text/html", "OK");
});
server.begin();
initOta();
initMDNS();
initWS();
SerialPrint("I", F("HTTP"), F("HttpServer Init"));
}
void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
#ifdef WS_enable
if (type == WS_EVT_CONNECT) {
Serial.printf("ws[%s][%u] connect\n", server->url(), client->id());
client->printf(json.c_str(), client->id());
//client->ping();
} else if (type == WS_EVT_DISCONNECT) {
Serial.printf("ws[%s][%u] disconnect\n", server->url(), client->id());
} else if (type == WS_EVT_ERROR) {
Serial.printf("ws[%s][%u] error(%u): %s\n", server->url(), client->id(), *((uint16_t *)arg), (char *)data);
} else if (type == WS_EVT_PONG) {
Serial.printf("ws[%s][%u] pong[%u]: %s\n", server->url(), client->id(), len, (len) ? (char *)data : "");
} else if (type == WS_EVT_DATA) {
AwsFrameInfo *info = (AwsFrameInfo *)arg;
String msg = "";
if (info->final && info->index == 0 && info->len == len) {
//the whole message is in a single frame and we got all of it's data
Serial.printf("ws[%s][%u] %s-message[%llu]: ", server->url(), client->id(), (info->opcode == WS_TEXT) ? "text" : "binary", info->len);
if (info->opcode == WS_TEXT) {
for (size_t i = 0; i < info->len; i++) {
msg += (char)data[i];
}
} else {
char buff[3];
for (size_t i = 0; i < info->len; i++) {
sprintf(buff, "%02x ", (uint8_t)data[i]);
msg += buff;
}
}
Serial.printf("%s\n", msg.c_str());
if (info->opcode == WS_TEXT)
client->text("{}");
else
client->binary("{}");
} else {
//message is comprised of multiple frames or the frame is split into multiple packets
if (info->index == 0) {
if (info->num == 0)
Serial.printf("ws[%s][%u] %s-message start\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
Serial.printf("ws[%s][%u] frame[%u] start[%llu]\n", server->url(), client->id(), info->num, info->len);
}
Serial.printf("ws[%s][%u] frame[%u] %s[%llu - %llu]: ", server->url(), client->id(), info->num, (info->message_opcode == WS_TEXT) ? "text" : "binary", info->index, info->index + len);
if (info->opcode == WS_TEXT) {
for (size_t i = 0; i < len; i++) {
msg += (char)data[i];
}
} else {
char buff[3];
for (size_t i = 0; i < len; i++) {
sprintf(buff, "%02x ", (uint8_t)data[i]);
msg += buff;
}
}
Serial.printf("%s\n", msg.c_str());
if ((info->index + len) == info->len) {
Serial.printf("ws[%s][%u] frame[%u] end[%llu]\n", server->url(), client->id(), info->num, info->len);
if (info->final) {
Serial.printf("ws[%s][%u] %s-message end\n", server->url(), client->id(), (info->message_opcode == WS_TEXT) ? "text" : "binary");
if (info->message_opcode == WS_TEXT)
client->text("I got your text message");
else
client->binary("I got your binary message");
}
}
}
}
#endif
;
}
void initMDNS() {
#ifdef MDNS_ENABLED
MDNS.addService("http", "tcp", 80);
// TODO Add Adduino OTA
#endif
;
}
void initOta() {
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA.onStart([]() {
events.send("Update Start", "ota");
});
ArduinoOTA.onEnd([]() {
events.send("Update End", "ota");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
char p[32];
sprintf(p, "Progress: %u%%\n", (progress / (total / 100)));
events.send(p, "ota");
});
ArduinoOTA.onError([](ota_error_t error) {
if (error == OTA_AUTH_ERROR)
events.send("Auth Failed", "ota");
else if (error == OTA_BEGIN_ERROR)
events.send("Begin Failed", "ota");
else if (error == OTA_CONNECT_ERROR)
events.send("Connect Failed", "ota");
else if (error == OTA_RECEIVE_ERROR)
events.send("Recieve Failed", "ota");
else if (error == OTA_END_ERROR)
events.send("End Failed", "ota");
});
ArduinoOTA.setHostname(hostName);
ArduinoOTA.begin();
#endif
;
}
void initWS() {
#ifdef WS_enable
ws.onEvent(onWsEvent);
server.addHandler(&ws);
events.onConnect([](AsyncEventSourceClient *client) {
client->send("", NULL, millis(), 1000);
});
server.addHandler(&events);
#endif
;
}
} // namespace HttpServer

92
src/Widgets.cpp Normal file
View File

@@ -0,0 +1,92 @@
#include "Global.h"
const String getWidgetFile(const String& name);
bool loadWidget(const String& filename, String& buf) {
buf = readFile(getWidgetFile(filename), 2048);
bool res = !(buf == "Failed" || buf == "Large");
if (!res) {
SerialPrint("[E]","Widgets","on load" + filename);
}
return res;
}
void createWidget(String descr, String page, String order, String filename, String topic) {
String buf = "{}";
if (!loadWidget(filename, buf)) {
return;
}
descr.replace("#", " ");
page.replace("#", " ");
jsonWriteStr(buf, "page", page);
jsonWriteStr(buf, "order", order);
jsonWriteStr(buf, "descr", descr);
jsonWriteStr(buf, "topic", prex + "/" + topic);
#ifdef LAYOUT_IN_RAM
all_widgets += widget + "\r\n";
#else
addFileLn("layout.txt", buf);
#endif
}
//TODO Вот эта процедура, и несколько оберток
void createWidgetParam(String widget, String page, String pageNumber, String filename, String topic,
String name1, String param1, String name2, String param2, String name3, String param3) {
String buf = "";
if (!loadWidget(filename, buf)) {
return;
}
widget.replace("#", " ");
page.replace("#", " ");
jsonWriteStr(buf, "page", page);
jsonWriteStr(buf, "order", pageNumber);
jsonWriteStr(buf, "descr", widget);
jsonWriteStr(buf, "topic", prex + "/" + topic);
if (name1) jsonWriteStr(buf, name1, param1);
if (name2) jsonWriteStr(buf, name2, param2);
if (name3) jsonWriteStr(buf, name3, param3);
#ifdef LAYOUT_IN_RAM
all_widgets += widget + "\r\n";
#else
addFileLn("layout.txt", buf);
#endif
}
void createChart(String widget, String page, String pageNumber, String filename, String topic,
String maxCount) {
String buf = "";
if (!loadWidget(filename, buf)) {
return;
}
widget.replace("#", " ");
page.replace("#", " ");
jsonWriteStr(buf, "page", page);
jsonWriteStr(buf, "order", pageNumber);
//jsonWriteStr(widget, "descr", widget_name);
jsonWriteStr(buf, "series", widget);
jsonWriteStr(buf, "maxCount", maxCount);
jsonWriteStr(buf, "topic", prex + "/" + topic);
#ifdef LAYOUT_IN_RAM
all_widgets += widget + "\r\n";
#else
addFileLn("layout.txt", buf);
#endif
}
void createWidgetByType(String widget, String page, String pageNumber, String type, String topic) {
createWidget(widget, page, pageNumber, getWidgetFile(type), topic);
}
const String getWidgetFile(const String& name) {
return "/widgets/" + name + ".json";
}

View File

@@ -0,0 +1,25 @@
#include "BufferExecute.h"
#include "items/ButtonInClass.h"
//==========================================Модуль физических кнопок========================================
//button-in switch1 toggle Кнопки Свет 1 pin[2] db[20]
//==========================================================================================================
boolean but[NUM_BUTTONS];
Bounce *buttons = new Bounce[NUM_BUTTONS];
ButtonInClass myButtonIn;
void buttonIn() {
myButtonIn.update();
String key = myButtonIn.gkey();
String pin = myButtonIn.gpin();
sCmd.addCommand(key.c_str(), buttonInSet);
myButtonIn.init();
myButtonIn.switchStateSetDefault();
myButtonIn.clear();
}
void buttonInSet() {
String key = sCmd.order();
String state = sCmd.next();
myButtonIn.switchChangeVirtual(key, state);
}

31
src/items/SensorPower.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "items/SensorPower.h"
SensorPZEM* myPowerSensor;
void SensorPZEM::loop() {
uint32_t now = millis();
if ((_lastUpdate + _interval) < now) {
post("voltage", String(_pzem->values()->voltage, 2));
post("current", String(_pzem->values()->current, 2));
post("power", String(_pzem->values()->power, 2));
post("energy", String(_pzem->values()->energy, 2));
post("freq", String(_pzem->values()->freq, 0));
post("pf", String(_pzem->values()->pf, 2));
_lastUpdate = now;
}
}
String SensorPZEM::getDataKey(const char* param_key) {
String res = _key;
res += "_";
res += param_key;
return res;
}
void SensorPZEM::post(const char* key, const String& value) {
String dataKey = getDataKey(key);
eventGen2(dataKey, value);
jsonWriteStr(configLiveJson, dataKey, value);
publishStatus(dataKey, value);
SerialPrint("I", "Sensor", "'" + dataKey + "' data: " + value);
}

20
src/items/sysUptime.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "Class/LineParsing.h"
#include "BufferExecute.h"
#include "Global.h"
#include <Arduino.h>
void sysUptime() {
myLineParsing.update();
String key = myLineParsing.gkey();
sCmd.addCommand(key.c_str(), uptimeReading);
//ensorReadingMap30sec += key + ",";
myLineParsing.clear();
}
void uptimeReading() {
String key = sCmd.order();
String uptime = timeNow->getUptime();
jsonWriteStr(configLiveJson, key, uptime);
publishStatus(key, uptime);
SerialPrint("I", "Sensor", "'" + key + "' data: " + uptime);
}

80
src/items/vButtonOut.cpp Normal file
View File

@@ -0,0 +1,80 @@
#include "items/vButtonOut.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include "SoftUART.h"
#include <Arduino.h>
//this class save data to flash
ButtonOut::ButtonOut(String pin, boolean inv, String key, String type) {
_pin = pin;
_inv = inv;
_key = key;
_type = type;
if (_pin != "") {
pinMode(_pin.toInt(), OUTPUT);
}
int state = jsonReadInt(configStoreJson, key);
this->execute(String(state));
}
ButtonOut::~ButtonOut() {}
void ButtonOut::execute(String state) {
if (state != "" && _pin != "") {
if (state == "change") {
state = String(!digitalRead(_pin.toInt()));
digitalWrite(_pin.toInt(), state.toInt());
}
else {
if (_inv) {
digitalWrite(_pin.toInt(), !state.toInt());
}
else {
digitalWrite(_pin.toInt(), state.toInt());
}
}
}
eventGen2(_key, state);
jsonWriteInt(configStoreJson, _key, state.toInt());
saveStore();
publishStatus(_key, state);
}
MyButtonOutVector* myButtonOut = nullptr;
void buttonOut() {
myLineParsing.update();
String key = myLineParsing.gkey();
String pin = myLineParsing.gpin();
String inv = myLineParsing.ginv();
String type = myLineParsing.gtype();
bool invb = false;
if (inv.toInt() == 1) invb = true;
myLineParsing.clear();
buttonOut_EnterCounter++;
addKey(key, buttonOut_KeyList, buttonOut_EnterCounter);
static bool firstTime = true;
if (firstTime) myButtonOut = new MyButtonOutVector();
firstTime = false;
myButtonOut->push_back(ButtonOut(pin, invb, key, type));
sCmd.addCommand(key.c_str(), buttonOutExecute);
}
void buttonOutExecute() {
String key = sCmd.order();
String state = sCmd.next();
int number = getKeyNum(key, buttonOut_KeyList);
if (myButtonOut != nullptr) {
if (number != -1) {
myButtonOut->at(number).execute(state);
}
}
}

77
src/items/vCountDown.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include "items/vCountDown.h"
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
CountDownClass::CountDownClass(String key) {
_key = key;
}
CountDownClass::~CountDownClass() {}
void CountDownClass::execute(unsigned int countDownPeriod) {
_countDownPeriod = countDownPeriod * 1000;
_start = true;
}
void CountDownClass::loop() {
if (_countDownPeriod > 0 && _start) {
prevMillis1 = millis();
_start = false;
sec = (_countDownPeriod / 1000);
}
currentMillis = millis();
difference1 = currentMillis - prevMillis1;
difference2 = currentMillis - prevMillis2;
if (difference1 > _countDownPeriod && _countDownPeriod > 0) {
_countDownPeriod = 0;
eventGen2(_key, "0");
Serial.println(_key + " completed");
}
if (difference2 > 1000 && _countDownPeriod > 0) {
prevMillis2 = millis();
sec--;
Serial.println(_key + " " + String(sec));
publishStatus(_key, String(sec));
}
}
MyCountDownVector* myCountDown = nullptr;
void countDown() {
myLineParsing.update();
String key = myLineParsing.gkey();
myLineParsing.clear();
countDown_EnterCounter++;
addKey(key, countDown_KeyList, countDown_EnterCounter);
Serial.println(countDown_EnterCounter);
Serial.println(countDown_KeyList);
static bool firstTime = true;
if (firstTime) myCountDown = new MyCountDownVector();
firstTime = false;
myCountDown->push_back(CountDownClass(key));
sCmd.addCommand(key.c_str(), countDownExecute);
}
void countDownExecute() {
String key = sCmd.order();
String value = sCmd.next();
if (!isDigitStr(value)) { //если значение - текст
value = getValue(value);
}
int number = getKeyNum(key, countDown_KeyList);
if (myCountDown != nullptr) {
if (number != -1) {
myCountDown->at(number).execute(value.toInt());
}
}
}

69
src/items/vImpulsOut.cpp Normal file
View File

@@ -0,0 +1,69 @@
#include "items/vImpulsOut.h"
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
ImpulsOutClass::ImpulsOutClass(unsigned int impulsPin) {
_impulsPin = impulsPin;
pinMode(impulsPin, OUTPUT);
}
ImpulsOutClass::~ImpulsOutClass() {}
void ImpulsOutClass::execute(unsigned long impulsPeriod, unsigned int impulsCount) {
_impulsPeriod = impulsPeriod;
_impulsCount = impulsCount * 2;
_impulsCountBuf = _impulsCount;
}
void ImpulsOutClass::loop() {
currentMillis = millis();
difference = currentMillis - prevMillis;
if (_impulsCountBuf > 0) {
if (difference > _impulsPeriod) {
_impulsCountBuf--;
prevMillis = millis();
yield();
digitalWrite(_impulsPin, !digitalRead(_impulsPin));
yield();
}
}
if (_impulsCountBuf <= 0) {
digitalWrite(_impulsPin, LOW);
}
}
MyImpulsOutVector* myImpulsOut = nullptr;
void impuls() {
myLineParsing.update();
String key = myLineParsing.gkey();
String pin = myLineParsing.gpin();
myLineParsing.clear();
impuls_EnterCounter++;
addKey(key, impuls_KeyList, impuls_EnterCounter);
static bool firstTime = true;
if (firstTime) myImpulsOut = new MyImpulsOutVector();
firstTime = false;
myImpulsOut->push_back(ImpulsOutClass(pin.toInt()));
sCmd.addCommand(key.c_str(), impulsExecute);
}
void impulsExecute() {
String key = sCmd.order();
String impulsPeriod = sCmd.next();
String impulsCount = sCmd.next();
int number = getKeyNum(key, impuls_KeyList);
if (myImpulsOut != nullptr) {
if (number != -1) {
myImpulsOut->at(number).execute(impulsPeriod.toInt(), impulsCount.toInt());
}
}
}

79
src/items/vInOutput.cpp Normal file
View File

@@ -0,0 +1,79 @@
#include "items/vInOutput.h"
#include <Arduino.h>
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include "Clock.h"
//this class save date to flash
InOutput::InOutput(String key, String widget) {
_key = key;
String value = jsonReadStr(configStoreJson, key);
if (value == "") {
if (widget.indexOf("Digit") != -1) {
value = "25";
}
if (widget.indexOf("Time") != -1) {
value = "12:00";
}
}
this->execute(value);
}
InOutput::~InOutput() {}
void InOutput::execute(String value) {
eventGen2(_key, value);
jsonWriteStr(configStoreJson, _key, value);
saveStore();
publishStatus(_key, value);
}
MyInOutputVector* myInOutput = nullptr;
void inOutput() {
myLineParsing.update();
String widget = myLineParsing.gfile();
String key = myLineParsing.gkey();
myLineParsing.clear();
inOutput_EnterCounter++;
addKey(key, inOutput_KeyList, inOutput_EnterCounter);
static bool firstTime = true;
if (firstTime) myInOutput = new MyInOutputVector();
firstTime = false;
myInOutput->push_back(InOutput(key, widget));
sCmd.addCommand(key.c_str(), inOutputExecute);
}
void inOutputExecute() {
String key = sCmd.order();
String value = sCmd.next();
//String type = sCmd.next();
if (!isDigitStr(value)) { //если значение - текст
if (value.indexOf(":") == -1) { //если этот текст не время
String valueJson = getValue(value);
if (valueJson != "no value") { //если это ключ переменной
value = valueJson;
}
else { //если это просто текст
value.replace("#", " ");
value.replace("%date%", timeNow->getDateTimeDotFormated());
}
}
}
int number = getKeyNum(key, inOutput_KeyList);
if (myInOutput != nullptr) {
if (number != -1) {
myInOutput->at(number).execute(value);
}
}
}

190
src/items/vLogging.cpp Normal file
View File

@@ -0,0 +1,190 @@
#include "items/vLogging.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "FileSystem.h"
#include "Global.h"
LoggingClass::LoggingClass(unsigned long period, unsigned int maxPoints, String loggingValueKey, String key) {
_period = period * 1000;
_maxPoints = maxPoints;
_loggingValueKey = loggingValueKey;
_key = key;
}
LoggingClass::~LoggingClass() {}
void LoggingClass::loop() {
if (_period > 0) {
currentMillis = millis();
difference = currentMillis - prevMillis;
if (difference >= _period) {
prevMillis = millis();
execute("");
}
}
}
void LoggingClass::execute(String keyOrValue) {
String loggingValue = "";
if (keyOrValue == "") { //прилетело из лупа
if (getValue(_loggingValueKey) != "no value") {
loggingValue = getValue(_loggingValueKey);
} else {
SerialPrint("E", "Logging", "This value not found on this device");
}
} else { //прилетело из события
if (isDigitDotCommaStr(keyOrValue)) { //если это число или дробное число
loggingValue = keyOrValue;
} else { //если это ключ
if (getValue(_loggingValueKey) != "no value") {
loggingValue = getValue(keyOrValue);
} else {
SerialPrint("E", "Logging", "This value not found on this device");
}
}
}
String filename = "logs/" + _key + ".txt";
size_t cnt = countLines(filename);
size_t sz = getFileSize(filename);
SerialPrint("I", "Logging", "http://" + WiFi.localIP().toString() + "/" + filename + " lines " + String(cnt, DEC) + ", size " + String(sz));
if ((cnt > _maxPoints + 1)) {
removeFile(filename);
SerialPrint("E", "Logging", "file been remooved: " + filename + " " + String(cnt) + ">" + String(_maxPoints));
cnt = 0;
}
if (loggingValue != "") {
if (cnt >= _maxPoints) { //удаляем старую строку и добавляем новую
String logData = readFile(filename, 20480); //10240
SerialPrint("I", "Logging", "Free heap " + String(ESP.getFreeHeap()));
if (logData == "large") {
SerialPrint("E", "Logging", "File is very large");
}
logData = deleteBeforeDelimiter(logData, "\r\n");
if (timeNow->hasTimeSynced()) {
logData += timeNow->getTimeUnix() + " " + loggingValue + "\r\n";
writeFile(filename, logData);
}
} else { //просто добавляем новую строку
if (timeNow->hasTimeSynced()) {
addFileLn(filename, timeNow->getTimeUnix() + " " + loggingValue);
}
}
}
String buf = "{}";
jsonWriteInt(buf, "x", timeNow->getTimeUnix().toInt());
jsonWriteFloat(buf, "y1", loggingValue.toFloat());
buf = "{\"status\":[" + buf + "]}";
publishChart(_key, buf);
}
MyLoggingVector* myLogging = nullptr;
void logging() {
myLineParsing.update();
String loggingValueKey = myLineParsing.gval();
String key = myLineParsing.gkey();
String interv = myLineParsing.gint();
String maxcnt = myLineParsing.gcnt();
myLineParsing.clear();
logging_KeyList += key + ",";
logging_EnterCounter++;
addKey(key, logging_KeyList, logging_EnterCounter);
static bool firstTime = true;
if (firstTime) myLogging = new MyLoggingVector();
firstTime = false;
myLogging->push_back(LoggingClass(interv.toInt(), maxcnt.toInt(), loggingValueKey, key));
sCmd.addCommand(key.c_str(), loggingExecute);
}
void loggingExecute() {
String key = sCmd.order();
String value = sCmd.next();
int number = getKeyNum(key, logging_KeyList);
if (myLogging != nullptr) {
if (number != -1) {
myLogging->at(number).execute(value);
}
}
}
void choose_log_date_and_send() {
String all_line = logging_KeyList;
while (all_line.length() != 0) {
String tmp = selectToMarker(all_line, ",");
sendLogData("logs/" + tmp + ".txt", tmp);
all_line = deleteBeforeDelimiter(all_line, ",");
}
}
void sendLogData(String file, String topic) {
File configFile = FileFS.open(file, "r");
if (!configFile) {
return;
}
configFile.seek(0, SeekSet);
int i = 0;
String buf = "{}";
String json_array;
String unix_time;
String value;
unsigned int psn;
unsigned int sz = configFile.size();
do {
i++;
psn = configFile.position();
String line = configFile.readStringUntil('\n');
unix_time = selectToMarker(line, " ");
jsonWriteInt(buf, "x", unix_time.toInt());
value = deleteBeforeDelimiter(line, " ");
jsonWriteFloat(buf, "y1", value.toFloat());
if (unix_time != "" || value != "") {
json_array += buf + ",";
}
int grafmax = jsonReadInt(configSetupJson, "grafmax");
if (grafmax != 0) {
if (i >= grafmax) {
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
publishChart(topic, json_array);
json_array = "";
i = 0;
}
}
} while (psn < sz);
configFile.close();
json_array = "{\"status\":[" + json_array + "]}";
json_array.replace("},]}", "}]}");
publishChart(topic, json_array);
}
void cleanLogAndData() {
#ifdef ESP8266
auto dir = FileFS.openDir("logs");
while (dir.next()) {
String fname = dir.fileName();
SerialPrint("I", "System", fname);
removeFile("logs/" + fname);
}
#endif
removeFile("store.json");
configStoreJson = "";
}

60
src/items/vPwmOut.cpp Normal file
View File

@@ -0,0 +1,60 @@
#include "Consts.h"
#ifdef PwmOutEnable
#include "items/vPwmOut.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
//this class save data to flash
PwmOut::PwmOut(unsigned int pin, String key) {
_pin = pin;
_key = key;
pinMode(_pin, OUTPUT);
int state = jsonReadInt(configStoreJson, key);
this->execute(String(state));
}
PwmOut::~PwmOut() {}
void PwmOut::execute(String state) {
analogWrite(_pin, state.toInt());
eventGen2(_key, state);
jsonWriteInt(configStoreJson, _key, state.toInt());
saveStore();
publishStatus(_key, state);
}
MyPwmOutVector* myPwmOut = nullptr;
void pwmOut() {
myLineParsing.update();
String key = myLineParsing.gkey();
String pin = myLineParsing.gpin();
myLineParsing.clear();
pwmOut_EnterCounter++;
addKey(key, pwmOut_KeyList, pwmOut_EnterCounter);
static bool firstTime = true;
if (firstTime) myPwmOut = new MyPwmOutVector();
firstTime = false;
myPwmOut->push_back(PwmOut(pin.toInt(), key));
sCmd.addCommand(key.c_str(), pwmOutExecute);
}
void pwmOutExecute() {
String key = sCmd.order();
String state = sCmd.next();
int number = getKeyNum(key, pwmOut_KeyList);
if (myPwmOut != nullptr) {
if (number != -1) {
myPwmOut->at(number).execute(state);
}
}
}
#endif

View File

@@ -0,0 +1,70 @@
#include "items/vSensorAnalog.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
SensorAnalog::SensorAnalog(String key, unsigned long interval, unsigned int adcPin, int map1, int map2, int map3, int map4, float c) {
_interval = interval * 1000;
_key = key;
_adcPin = _adcPin;
_map1 = map1;
_map2 = map2;
_map3 = map3;
_map4 = map4;
_c = c;
}
SensorAnalog::~SensorAnalog() {}
void SensorAnalog::loop() {
currentMillis = millis();
difference = currentMillis - prevMillis;
if (difference >= _interval) {
prevMillis = millis();
readAnalog();
}
}
void SensorAnalog::readAnalog() {
int value;
#ifdef ESP32
value = analogRead(_adcPin);
#endif
#ifdef ESP8266
value = analogRead(A0);
#endif
value = map(value, _map1, _map2, _map3, _map4);
float valueFloat = value * _c;
eventGen2(_key, String(valueFloat));
jsonWriteStr(configLiveJson, _key, String(valueFloat));
publishStatus(_key, String(valueFloat));
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(valueFloat));
}
MySensorAnalogVector* mySensorAnalog = nullptr;
void analogAdc() {
myLineParsing.update();
String interval = myLineParsing.gint();
String pin = myLineParsing.gpin();
String key = myLineParsing.gkey();
String map = myLineParsing.gmap();
String c = myLineParsing.gc();
myLineParsing.clear();
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
static bool firstTime = true;
if (firstTime) mySensorAnalog = new MySensorAnalogVector();
firstTime = false;
mySensorAnalog->push_back(SensorAnalog(key, interval.toInt(), pin.toInt(), map1, map2, map3, map4, c.toFloat()));
}

100
src/items/vSensorBme280.cpp Normal file
View File

@@ -0,0 +1,100 @@
#include "items/vSensorBme280.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
Adafruit_BME280* bme = nullptr;
SensorBme280::SensorBme280(const paramsBme& paramsTmp, const paramsBme& paramsHum, const paramsBme& paramsPrs) {
_paramsTmp = paramsBme(paramsTmp);
_paramsHum = paramsBme(paramsHum);
_paramsPrs = paramsBme(paramsPrs);
if (!bme) {
bme = new Adafruit_BME280;
}
bme->getTemperatureSensor();
bme->getPressureSensor();
bme->getHumiditySensor();
bme->begin(hexStringToUint8(_paramsPrs.addr));
}
SensorBme280::~SensorBme280() {}
void SensorBme280::loop() {
difference = millis() - prevMillis;
if (difference >= _paramsPrs.interval) {
prevMillis = millis();
read();
}
}
void SensorBme280::read() {
float tmp = bme->readTemperature();
float hum = bme->readHumidity();
float prs = bme->readPressure();
prs = prs / 1.333224 / 100;
tmp = tmp * _paramsTmp.c;
hum = hum * _paramsHum.c;
prs = prs * _paramsPrs.c;
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsHum.key, String(hum));
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
publishStatus(_paramsHum.key, String(hum));
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
eventGen2(_paramsPrs.key, String(prs));
jsonWriteStr(configLiveJson, _paramsPrs.key, String(prs));
publishStatus(_paramsPrs.key, String(prs));
SerialPrint("I", "Sensor", "'" + _paramsPrs.key + "' data: " + String(prs));
}
MySensorBme280Vector* mySensorBme280 = nullptr;
void bme280Sensor() {
myLineParsing.update();
String key = myLineParsing.gkey();
String addr = myLineParsing.gaddr();
String interval = myLineParsing.gint();
String c = myLineParsing.gc();
myLineParsing.clear();
static int enterCnt = -1;
enterCnt++;
static paramsBme paramsTmp;
static paramsBme paramsHum;
static paramsBme paramsPrs;
if (enterCnt == 0) {
paramsTmp.key = key;
paramsTmp.c = c.toFloat();
}
if (enterCnt == 1) {
paramsHum.key = key;
paramsHum.c = c.toFloat();
}
if (enterCnt == 2) {
paramsPrs.key = key;
paramsPrs.addr = addr;
paramsPrs.interval = interval.toInt() * 1000;
paramsPrs.c = c.toFloat();
static bool firstTime = true;
if (firstTime) mySensorBme280 = new MySensorBme280Vector();
firstTime = false;
mySensorBme280->push_back(SensorBme280(paramsTmp, paramsHum, paramsPrs));
}
}

View File

@@ -0,0 +1,86 @@
#include "items/vSensorBmp280.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
Adafruit_BMP280* bmp = nullptr;
SensorBmp280::SensorBmp280(const paramsBmp& paramsTmp, const paramsBmp& paramsPrs) {
_paramsTmp = paramsBmp(paramsTmp);
_paramsPrs = paramsBmp(paramsPrs);
if (!bmp) {
bmp = new Adafruit_BMP280;
}
bmp->getTemperatureSensor();
bmp->getPressureSensor();
bmp->begin(hexStringToUint8(_paramsPrs.addr));
}
SensorBmp280::~SensorBmp280() {}
void SensorBmp280::loop() {
difference = millis() - prevMillis;
if (difference >= _paramsPrs.interval) {
prevMillis = millis();
read();
}
}
void SensorBmp280::read() {
float tmp = bmp->readTemperature();
float prs = bmp->readPressure();
prs = prs / 1.333224 / 100;
tmp = tmp * _paramsTmp.c;
prs = prs * _paramsPrs.c;
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsPrs.key, String(prs));
jsonWriteStr(configLiveJson, _paramsPrs.key, String(prs));
publishStatus(_paramsPrs.key, String(prs));
SerialPrint("I", "Sensor", "'" + _paramsPrs.key + "' data: " + String(prs));
}
MySensorBmp280Vector* mySensorBmp280 = nullptr;
void bmp280Sensor() {
myLineParsing.update();
String key = myLineParsing.gkey();
String addr = myLineParsing.gaddr();
String interval = myLineParsing.gint();
String c = myLineParsing.gc();
myLineParsing.clear();
static int enterCnt = -1;
enterCnt++;
static paramsBmp paramsTmp;
static paramsBmp paramsHum;
static paramsBmp paramsPrs;
if (enterCnt == 0) {
paramsTmp.key = key;
paramsTmp.c = c.toFloat();
}
if (enterCnt == 1) {
paramsPrs.key = key;
paramsPrs.addr = addr;
paramsPrs.interval = interval.toInt() * 1000;
paramsPrs.c = c.toFloat();
static bool firstTime = true;
if (firstTime) mySensorBmp280 = new MySensorBmp280Vector();
firstTime = false;
mySensorBmp280->push_back(SensorBmp280(paramsTmp, paramsPrs));
}
}

View File

@@ -0,0 +1,54 @@
#include "items/vSensorDallas.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
SensorDallas::SensorDallas(unsigned long interval, unsigned int pin, unsigned int index, String key) {
_interval = interval * 1000;
_key = key;
_pin = pin;
_index = index;
oneWire = new OneWire((uint8_t)_pin);
sensors.setOneWire(oneWire);
sensors.begin();
sensors.setResolution(12);
}
SensorDallas::~SensorDallas() {}
void SensorDallas::loop() {
currentMillis = millis();
difference = currentMillis - prevMillis;
if (difference >= _interval) {
prevMillis = millis();
readDallas();
}
}
void SensorDallas::readDallas() {
sensors.requestTemperaturesByIndex(_index);
float value = sensors.getTempCByIndex(_index);
eventGen2(_key, String(value));
jsonWriteStr(configLiveJson, _key, String(value));
publishStatus(_key, String(value));
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(value));
}
MySensorDallasVector* mySensorDallas2 = nullptr;
void dallas() {
myLineParsing.update();
String interval = myLineParsing.gint();
String pin = myLineParsing.gpin();
String index = myLineParsing.gindex();
String key = myLineParsing.gkey();
myLineParsing.clear();
static bool firstTime = true;
if (firstTime) mySensorDallas2 = new MySensorDallasVector();
firstTime = false;
mySensorDallas2->push_back(SensorDallas(interval.toInt(), pin.toInt(), index.toInt(), key));
}

94
src/items/vSensorDht.cpp Normal file
View File

@@ -0,0 +1,94 @@
#include "items/vSensorDht.h"
#include <Arduino.h>
#include "BufferExecute.h"
#include "Class/LineParsing.h"
#include "Global.h"
SensorDht::SensorDht(const paramsDht& paramsTmp, const paramsDht& paramsHum) {
_paramsTmp = paramsDht(paramsTmp);
_paramsHum = paramsDht(paramsHum);
dht = new DHTesp();
if (_paramsHum.type == "dht11") {
dht->setup(_paramsHum.pin, DHTesp::DHT11);
} else if (_paramsHum.type == "dht22") {
dht->setup(_paramsHum.pin, DHTesp::DHT22);
}
_paramsHum.interval = _paramsHum.interval + dht->getMinimumSamplingPeriod();
}
SensorDht::~SensorDht() {}
void SensorDht::loop() {
difference = millis() - prevMillis;
if (difference >= _paramsHum.interval) {
prevMillis = millis();
readTmpHum();
}
}
void SensorDht::readTmpHum() {
float tmp = dht->getTemperature();
float hum = dht->getHumidity();
if (String(tmp) != "nan" && String(hum) != "nan") {
tmp = tmp * _paramsTmp.c;
hum = hum * _paramsHum.c;
eventGen2(_paramsTmp.key, String(tmp));
jsonWriteStr(configLiveJson, _paramsTmp.key, String(tmp));
publishStatus(_paramsTmp.key, String(tmp));
SerialPrint("I", "Sensor", "'" + _paramsTmp.key + "' data: " + String(tmp));
eventGen2(_paramsHum.key, String(hum));
jsonWriteStr(configLiveJson, _paramsHum.key, String(hum));
publishStatus(_paramsHum.key, String(hum));
SerialPrint("I", "Sensor", "'" + _paramsHum.key + "' data: " + String(hum));
} else {
SerialPrint("E", "Sensor DHT", "Error");
}
}
MySensorDhtVector* mySensorDht = nullptr;
void dhtSensor() {
myLineParsing.update();
String type = myLineParsing.gtype();
String interval = myLineParsing.gint();
String pin = myLineParsing.gpin();
String key = myLineParsing.gkey();
String c = myLineParsing.gc();
myLineParsing.clear();
static int enterCnt = -1;
enterCnt++;
static paramsDht paramsTmp;
static paramsDht paramsHum;
if (enterCnt == 0) {
paramsTmp.key = key;
paramsTmp.interval = interval.toInt() * 1000;
paramsTmp.c = c.toFloat();
}
if (enterCnt == 1) {
paramsHum.type = type;
paramsHum.key = key;
paramsHum.interval = interval.toInt() * 1000;
paramsHum.pin = pin.toInt();
paramsHum.c = c.toFloat();
static bool firstTime = true;
if (firstTime) mySensorDht = new MySensorDhtVector();
firstTime = false;
mySensorDht->push_back(SensorDht(paramsTmp, paramsHum));
enterCnt = -1;
}
}

View File

@@ -0,0 +1,89 @@
#include "items/vSensorUltrasonic.h"
#include "Class/LineParsing.h"
#include "Global.h"
#include "BufferExecute.h"
#include <Arduino.h>
GMedian<5, int> testFilter;
SensorUltrasonic::SensorUltrasonic(String key, unsigned long interval, unsigned int trig, unsigned int echo, int map1, int map2, int map3, int map4, float c) {
_interval = interval * 1000;
_key = key;
_trig = trig;
_echo = echo;
_map1 = map1;
_map2 = map2;
_map3 = map3;
_map4 = map4;
_c = c;
pinMode(_trig, OUTPUT);
pinMode(_echo, INPUT);
}
SensorUltrasonic::~SensorUltrasonic() {}
void SensorUltrasonic::loop() {
currentMillis = millis();
difference = currentMillis - prevMillis;
if (difference >= _interval) {
prevMillis = millis();
readUltrasonic();
}
}
void SensorUltrasonic::readUltrasonic() {
static unsigned int counter;
counter++;
int value;
digitalWrite(_trig, LOW);
delayMicroseconds(2);
digitalWrite(_trig, HIGH);
delayMicroseconds(10);
digitalWrite(_trig, LOW);
long duration_ = pulseIn(_echo, HIGH, 30000); // 3000 µs = 50cm // 30000 µs = 5 m
value = duration_ / 29 / 2;
value = testFilter.filtered(value);
value = map(value, _map1, _map2, _map3, _map4);
float valueFloat = value * _c;
if (counter > 10) {
eventGen2(_key, String(valueFloat));
jsonWriteStr(configLiveJson, _key, String(valueFloat));
publishStatus(_key, String(valueFloat));
SerialPrint("I", "Sensor", "'" + _key + "' data: " + String(valueFloat));
}
}
MySensorUltrasonicVector* mySensorUltrasonic = nullptr;
void ultrasonic() {
myLineParsing.update();
String interval = myLineParsing.gint();
String pin = myLineParsing.gpin();
String key = myLineParsing.gkey();
String map = myLineParsing.gmap();
String c = myLineParsing.gc();
myLineParsing.clear();
unsigned int trig = selectFromMarkerToMarker(pin, ",", 0).toInt();
unsigned int echo = selectFromMarkerToMarker(pin, ",", 1).toInt();
int map1 = selectFromMarkerToMarker(map, ",", 0).toInt();
int map2 = selectFromMarkerToMarker(map, ",", 1).toInt();
int map3 = selectFromMarkerToMarker(map, ",", 2).toInt();
int map4 = selectFromMarkerToMarker(map, ",", 3).toInt();
static bool firstTime = true;
if (firstTime) mySensorUltrasonic = new MySensorUltrasonicVector();
firstTime = false;
mySensorUltrasonic->push_back(SensorUltrasonic(key, interval.toInt(), trig, echo, map1, map2, map3, map4, c.toFloat()));
}

156
src/main.cpp Normal file
View File

@@ -0,0 +1,156 @@
#include <SSDP.h>
#include "BufferExecute.h"
#include "Bus.h"
#include "Class/CallBackTest.h"
#include "Class/NotAsync.h"
#include "Class/ScenarioClass3.h"
#include "Cmd.h"
#include "FileSystem.h"
#include "Global.h"
#include "Init.h"
#include "ItemsList.h"
#include "RemoteOrdersUdp.h"
#include "SoftUART.h"
#include "Telegram.h"
#include "Tests.h"
#include "Utils/StatUtils.h"
#include "Utils/Timings.h"
#include "Utils/WebUtils.h"
#include "items/ButtonInClass.h"
#include "items/vCountDown.h"
#include "items/vImpulsOut.h"
#include "items/vLogging.h"
#include "items/vSensorAnalog.h"
#include "items/vSensorBme280.h"
#include "items/vSensorBmp280.h"
#include "items/vSensorDallas.h"
#include "items/vSensorDht.h"
#include "items/vSensorUltrasonic.h"
void not_async_actions();
Timings metric;
boolean initialized = false;
void setup() {
Serial.begin(115200);
Serial.flush();
Serial.println();
Serial.println(F("--------------started----------------"));
myNotAsyncActions = new NotAsync(do_LAST);
myScenario = new Scenario();
//=========================================initialisation==============================================================
setChipId();
fileSystemInit();
loadConfig();
#ifdef uartEnable
uartInit();
#endif
clockInit();
timeInit();
itemsListInit();
espInit();
routerConnect();
#ifdef telegramEnable
telegramInit();
#endif
uptime_init();
upgradeInit();
HttpServer::init();
web_init();
initSt();
busInit();
wifiSignalInit();
#ifdef UDP_ENABLED
asyncUdpInit();
#endif
#ifdef SSDP_ENABLED
SsdpInit();
#endif
#ifdef ESP8266
getFSInfo();
#endif
testsPerform();
just_load = false;
initialized = true;
}
void loop() {
if (!initialized) {
return;
}
#ifdef OTA_UPDATES_ENABLED
ArduinoOTA.handle();
#endif
#ifdef WS_enable
ws.cleanupClients();
#endif
timeNow->loop();
mqttLoop();
myButtonIn.loop();
myScenario->loop();
loopCmdExecute();
myNotAsyncActions->loop();
ts.update();
#ifdef telegramEnable
handleTelegram();
#endif
#ifdef uartEnable
uartHandle();
#endif
if (myLogging != nullptr) {
for (unsigned int i = 0; i < myLogging->size(); i++) {
myLogging->at(i).loop();
}
}
if (myImpulsOut != nullptr) {
for (unsigned int i = 0; i < myImpulsOut->size(); i++) {
myImpulsOut->at(i).loop();
}
}
if (mySensorDallas2 != nullptr) {
for (unsigned int i = 0; i < mySensorDallas2->size(); i++) {
mySensorDallas2->at(i).loop();
}
}
if (mySensorUltrasonic != nullptr) {
for (unsigned int i = 0; i < mySensorUltrasonic->size(); i++) {
mySensorUltrasonic->at(i).loop();
}
}
if (myCountDown != nullptr) {
for (unsigned int i = 0; i < myCountDown->size(); i++) {
myCountDown->at(i).loop();
}
}
if (mySensorAnalog != nullptr) {
for (unsigned int i = 0; i < mySensorAnalog->size(); i++) {
mySensorAnalog->at(i).loop();
}
}
if (mySensorDht != nullptr) {
for (unsigned int i = 0; i < mySensorDht->size(); i++) {
mySensorDht->at(i).loop();
}
}
if (mySensorBme280 != nullptr) {
for (unsigned int i = 0; i < mySensorBme280->size(); i++) {
mySensorBme280->at(i).loop();
}
}
if (mySensorBmp280 != nullptr) {
for (unsigned int i = 0; i < mySensorBmp280->size(); i++) {
mySensorBmp280->at(i).loop();
}
}
}