mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-30 20:09:14 +03:00
esp32 рабочая версия
This commit is contained in:
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"fstream": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -442,16 +442,18 @@
|
|||||||
"num": 33
|
"num": 33
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "34. PWM ESP8266",
|
"name": "34. PWM ESP32",
|
||||||
"type": "Writing",
|
"type": "Writing",
|
||||||
"subtype": "Pwm8266",
|
"subtype": "Pwm32",
|
||||||
"id": "pwm",
|
"id": "pwm",
|
||||||
"widget": "range",
|
"widget": "range",
|
||||||
"page": "Кнопки",
|
"page": "Кнопки",
|
||||||
"descr": "PWM",
|
"descr": "PWM",
|
||||||
"int": 0,
|
"int": 0,
|
||||||
"pin": 15,
|
"pin": 2,
|
||||||
"freq": 5000,
|
"freq": 5000,
|
||||||
|
"ledChannel": 2,
|
||||||
|
"PWM_resolution": 10,
|
||||||
"val": 0,
|
"val": 0,
|
||||||
"apin": -1,
|
"apin": -1,
|
||||||
"num": 34
|
"num": 34
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
//Версия прошивки
|
//Версия прошивки
|
||||||
#define FIRMWARE_VERSION 412
|
#define FIRMWARE_VERSION 413
|
||||||
|
|
||||||
#ifdef esp8266_4mb
|
#ifdef esp8266_4mb
|
||||||
#define FIRMWARE_NAME "esp8266_4mb"
|
#define FIRMWARE_NAME "esp8266_4mb"
|
||||||
@@ -32,7 +32,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef esp32_4mb
|
#ifdef esp32_4mb
|
||||||
#define USE_LITTLEFS true
|
#define USE_LITTLEFS false
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define START_DATETIME 1640995200 // 01.01.2022 00:00:00 константа для сокращения unix time
|
#define START_DATETIME 1640995200 // 01.01.2022 00:00:00 константа для сокращения unix time
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
},
|
},
|
||||||
"projectProp": {
|
"projectProp": {
|
||||||
"platformio": {
|
"platformio": {
|
||||||
"default_envs": "esp8266_4mb",
|
"default_envs": "esp32_4mb",
|
||||||
"data_dir": "data_svelte"
|
"data_dir": "data_svelte"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ build_src_filter =
|
|||||||
${env:esp32_4mb_fromitems.build_src_filter}
|
${env:esp32_4mb_fromitems.build_src_filter}
|
||||||
|
|
||||||
[platformio]
|
[platformio]
|
||||||
default_envs = esp8266_4mb
|
default_envs = esp32_4mb
|
||||||
data_dir = data_svelte
|
data_dir = data_svelte
|
||||||
|
|
||||||
[common_env_data]
|
[common_env_data]
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ void* getAPI_ButtonOut(String subtype, String params);
|
|||||||
void* getAPI_IoTServo(String subtype, String params);
|
void* getAPI_IoTServo(String subtype, String params);
|
||||||
void* getAPI_Mcp23017(String subtype, String params);
|
void* getAPI_Mcp23017(String subtype, String params);
|
||||||
void* getAPI_Mp3(String subtype, String params);
|
void* getAPI_Mp3(String subtype, String params);
|
||||||
void* getAPI_Pwm8266(String subtype, String params);
|
void* getAPI_Pwm32(String subtype, String params);
|
||||||
void* getAPI_SysExt(String subtype, String params);
|
void* getAPI_SysExt(String subtype, String params);
|
||||||
void* getAPI_Lcd2004(String subtype, String params);
|
void* getAPI_Lcd2004(String subtype, String params);
|
||||||
|
|
||||||
@@ -50,7 +50,7 @@ if ((tmpAPI = getAPI_ButtonOut(subtype, params)) != nullptr) return tmpAPI;
|
|||||||
if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI;
|
if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI;
|
||||||
if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI;
|
if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI;
|
||||||
if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI;
|
if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI;
|
||||||
if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI;
|
if ((tmpAPI = getAPI_Pwm32(subtype, params)) != nullptr) return tmpAPI;
|
||||||
if ((tmpAPI = getAPI_SysExt(subtype, params)) != nullptr) return tmpAPI;
|
if ((tmpAPI = getAPI_SysExt(subtype, params)) != nullptr) return tmpAPI;
|
||||||
if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI;
|
if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ class Loging : public IoTItem {
|
|||||||
jsonRead(parameters, F("logid"), logid);
|
jsonRead(parameters, F("logid"), logid);
|
||||||
jsonRead(parameters, F("id"), id);
|
jsonRead(parameters, F("id"), id);
|
||||||
jsonRead(parameters, F("points"), points);
|
jsonRead(parameters, F("points"), points);
|
||||||
|
if (points >= 300) {
|
||||||
|
points = 300;
|
||||||
|
SerialPrint("E", F("Loging"), "'" + id + "' user set more points than allowed, value reset to 300");
|
||||||
|
}
|
||||||
jsonRead(parameters, F("int"), interval);
|
jsonRead(parameters, F("int"), interval);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,10 +54,10 @@ class Loging : public IoTItem {
|
|||||||
//прочитаем путь к файлу последнего сохранения
|
//прочитаем путь к файлу последнего сохранения
|
||||||
String filePath = readDataDB(id);
|
String filePath = readDataDB(id);
|
||||||
|
|
||||||
Serial.println("filePath " + filePath);
|
// Serial.println("filePath " + filePath);
|
||||||
|
|
||||||
//если данные о файле отсутствуют, создадим новый
|
//если данные о файле отсутствуют, создадим новый
|
||||||
if (filePath == "failed") {
|
if (filePath == "failed" || filePath == "") {
|
||||||
SerialPrint("E", F("Loging"), "'" + id + "' file path not found");
|
SerialPrint("E", F("Loging"), "'" + id + "' file path not found");
|
||||||
createNewFileWithData(logData);
|
createNewFileWithData(logData);
|
||||||
return;
|
return;
|
||||||
@@ -74,15 +78,15 @@ class Loging : public IoTItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void createNewFileWithData(String &logData) {
|
void createNewFileWithData(String &logData) {
|
||||||
String filePath = "/lg/" + id + "-" + String(unixTimeShort) + ".txt"; //создадим путь
|
String path = "/lg/" + id + "-" + String(unixTimeShort) + ".txt"; //создадим путь
|
||||||
addFileLn(filePath, logData); //запишем файл и данные в него
|
addFileLn(path, logData); //запишем файл и данные в него
|
||||||
saveDataDB(id, filePath); //запишем путь к файлу в базу данных
|
saveDataDB(id, path); //запишем путь к файлу в базу данных
|
||||||
SerialPrint("i", F("Loging"), "'" + id + "' file created http://" + WiFi.localIP().toString() + filePath);
|
SerialPrint("i", F("Loging"), "'" + id + "' file created http://" + WiFi.localIP().toString() + path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addNewDataToExistingFile(String &filePath, String &logData) {
|
void addNewDataToExistingFile(String &path, String &logData) {
|
||||||
addFileLn(filePath, logData);
|
addFileLn(path, logData);
|
||||||
SerialPrint("i", F("Loging"), "'" + id + "' loging in file http://" + WiFi.localIP().toString() + filePath);
|
SerialPrint("i", F("Loging"), "'" + id + "' loging in file http://" + WiFi.localIP().toString() + path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendChart() {
|
void sendChart() {
|
||||||
@@ -90,109 +94,127 @@ class Loging : public IoTItem {
|
|||||||
String reqUnixTimeStr = "27.08.2022"; //нужно получить эту дату из окна ввода под графиком.
|
String reqUnixTimeStr = "27.08.2022"; //нужно получить эту дату из окна ввода под графиком.
|
||||||
unsigned long reqUnixTime = strDateToUnix(reqUnixTimeStr);
|
unsigned long reqUnixTime = strDateToUnix(reqUnixTimeStr);
|
||||||
|
|
||||||
String directory = "lg";
|
|
||||||
// SerialPrint("i", F("Loging"), "'" + id + "' in directory '" + directory + "' files:");
|
|
||||||
auto dir = FileFS.openDir(directory);
|
|
||||||
String oneSingleJson;
|
String oneSingleJson;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
#if defined(ESP8266)
|
||||||
|
String directory = "lg";
|
||||||
|
auto dir = FileFS.openDir(directory);
|
||||||
while (dir.next()) {
|
while (dir.next()) {
|
||||||
String fname = dir.fileName();
|
String fname = dir.fileName();
|
||||||
String idInFileName = selectToMarker(fname, "-");
|
#endif
|
||||||
unsigned long fileUnixTime = deleteBeforeDelimiter(deleteToMarkerLast(fname, "."), "-").toInt() + START_DATETIME;
|
#if defined(ESP32)
|
||||||
if (isItemExist(id)) {
|
String directory = "/lg";
|
||||||
//если id в имени файла совпадает с id данного экземпляра, пусть каждый экземпляр класса шлет только свое
|
File root = FileFS.open(directory);
|
||||||
if (idInFileName == id) {
|
directory = String();
|
||||||
//выбираем только те файлы которые входят в выбранные пользователем сутки
|
if (root.isDirectory()) {
|
||||||
// if (fileUnixTime > reqUnixTime && fileUnixTime < reqUnixTime + 86400) {
|
File file = root.openNextFile();
|
||||||
SerialPrint("i", F("Loging"), "'" + id + "' matching file found '" + fname + "'");
|
while (file) {
|
||||||
//выгрузка по частям, по одному файлу
|
String fname = file.name();
|
||||||
publishJsonPartly("/lg/" + fname, calculateMaxCount(), i);
|
fname = selectToMarkerLast(fname, "/");
|
||||||
//}
|
file = root.openNextFile();
|
||||||
|
#endif
|
||||||
|
String idInFileName = selectToMarker(fname, "-");
|
||||||
|
unsigned long fileUnixTime = deleteBeforeDelimiter(deleteToMarkerLast(fname, "."), "-").toInt() + START_DATETIME;
|
||||||
|
if (isItemExist(id)) {
|
||||||
|
//если id в имени файла совпадает с id данного экземпляра, пусть каждый экземпляр класса шлет только свое
|
||||||
|
if (idInFileName == id) {
|
||||||
|
//выбираем только те файлы которые входят в выбранные пользователем сутки
|
||||||
|
// if (fileUnixTime > reqUnixTime && fileUnixTime < reqUnixTime + 86400) {
|
||||||
|
SerialPrint("i", F("Loging"), "'" + id + "' matching file found '" + fname + "'");
|
||||||
|
//выгрузка по частям, по одному файлу
|
||||||
|
publishJsonPartly("/lg/" + fname, calculateMaxCount(), i);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SerialPrint("i", F("Loging"), "'" + id + "' file '" + fname + "' not used, deleted");
|
||||||
|
removeFile(directory + "/" + fname);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
#if defined(ESP32)
|
||||||
SerialPrint("i", F("Loging"), "'" + id + "' file '" + fname + "' not used, deleted");
|
}
|
||||||
removeFile(directory + "/" + fname);
|
#endif
|
||||||
|
|
||||||
|
SerialPrint("i", F("Loging"), "'" + id + "'--------------'" + String(i) + "'--------------");
|
||||||
|
}
|
||||||
|
|
||||||
|
void publishJsonPartly(String file, int maxCount, int &i) {
|
||||||
|
File configFile = FileFS.open(file, "r");
|
||||||
|
if (!configFile) {
|
||||||
|
SerialPrint("E", F("Loging"), "'" + id + "' open file error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
configFile.seek(0, SeekSet);
|
||||||
|
String buf = "{}";
|
||||||
|
String dividedJson;
|
||||||
|
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() + START_DATETIME);
|
||||||
|
value = deleteBeforeDelimiter(line, " ");
|
||||||
|
jsonWriteFloat(buf, "y1", value.toFloat());
|
||||||
|
if (unix_time != "" || value != "") {
|
||||||
|
dividedJson += buf + ",";
|
||||||
|
}
|
||||||
|
} while (psn < sz);
|
||||||
|
|
||||||
|
configFile.close();
|
||||||
|
publishJson(dividedJson, maxCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
void publishJson(String & oneSingleJson, int &maxCount) {
|
||||||
|
oneSingleJson = "{\"maxCount\":" + String(maxCount) + ",\"status\":[" + oneSingleJson + "]}";
|
||||||
|
oneSingleJson.replace("},]}", "}]}");
|
||||||
|
if (!publishChart(id, oneSingleJson)) {
|
||||||
|
SerialPrint("E", F("Loging"), "'" + id + "' mqtt publish error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SerialPrint("i", F("Loging"), "'" + id + "'--------------'" + String(i) + "'--------------");
|
|
||||||
}
|
|
||||||
|
|
||||||
void publishJsonPartly(String file, int maxCount, int &i) {
|
//примерный подсчет максимального количества точек
|
||||||
File configFile = FileFS.open(file, "r");
|
int calculateMaxCount() {
|
||||||
if (!configFile) {
|
return 86400 / interval;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
configFile.seek(0, SeekSet);
|
};
|
||||||
String buf = "{}";
|
|
||||||
String dividedJson;
|
|
||||||
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() + START_DATETIME);
|
|
||||||
value = deleteBeforeDelimiter(line, " ");
|
|
||||||
jsonWriteFloat(buf, "y1", value.toFloat());
|
|
||||||
if (unix_time != "" || value != "") {
|
|
||||||
dividedJson += buf + ",";
|
|
||||||
}
|
|
||||||
} while (psn < sz);
|
|
||||||
|
|
||||||
configFile.close();
|
void *getAPI_Loging(String subtype, String param) {
|
||||||
publishJson(dividedJson, maxCount);
|
if (subtype == F("Loging")) {
|
||||||
|
return new Loging(param);
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void publishJson(String &oneSingleJson, int &maxCount) {
|
//то что не пригодилось но пригодится потом может быть
|
||||||
oneSingleJson = "{\"maxCount\":" + String(maxCount) + ",\"status\":[" + oneSingleJson + "]}";
|
// void createOneSingleJson(String &oneSingleJson, String file, int &maxCount, int &i) {
|
||||||
oneSingleJson.replace("},]}", "}]}");
|
// File configFile = FileFS.open(file, "r");
|
||||||
publishChart(id, oneSingleJson);
|
// if (!configFile) {
|
||||||
}
|
// return;
|
||||||
|
// }
|
||||||
//примерный подсчет максимального количества точек
|
// configFile.seek(0, SeekSet);
|
||||||
int calculateMaxCount() {
|
// String buf = "{}";
|
||||||
return 86400 / interval;
|
// String unix_time;
|
||||||
}
|
// String value;
|
||||||
};
|
// unsigned int psn;
|
||||||
|
// unsigned int sz = configFile.size();
|
||||||
void *getAPI_Loging(String subtype, String param) {
|
// do {
|
||||||
if (subtype == F("Loging")) {
|
// maxCount++;
|
||||||
return new Loging(param);
|
// i++;
|
||||||
} else {
|
// psn = configFile.position();
|
||||||
return nullptr;
|
// String line = configFile.readStringUntil('\n');
|
||||||
}
|
// unix_time = selectToMarker(line, " ");
|
||||||
}
|
// jsonWriteInt(buf, "x", unix_time.toInt() + START_DATETIME);
|
||||||
|
// value = deleteBeforeDelimiter(line, " ");
|
||||||
//то что не пригодилось но пригодится потом может быть
|
// jsonWriteFloat(buf, "y1", value.toFloat());
|
||||||
// void createOneSingleJson(String &oneSingleJson, String file, int &maxCount, int &i) {
|
// if (unix_time != "" || value != "") {
|
||||||
// File configFile = FileFS.open(file, "r");
|
// oneSingleJson += buf + ",";
|
||||||
// if (!configFile) {
|
// }
|
||||||
// return;
|
//
|
||||||
// }
|
// } while (psn < sz);
|
||||||
// configFile.seek(0, SeekSet);
|
//
|
||||||
// String buf = "{}";
|
// configFile.close();
|
||||||
// String unix_time;
|
// }
|
||||||
// String value;
|
|
||||||
// unsigned int psn;
|
|
||||||
// unsigned int sz = configFile.size();
|
|
||||||
// do {
|
|
||||||
// maxCount++;
|
|
||||||
// i++;
|
|
||||||
// psn = configFile.position();
|
|
||||||
// String line = configFile.readStringUntil('\n');
|
|
||||||
// unix_time = selectToMarker(line, " ");
|
|
||||||
// jsonWriteInt(buf, "x", unix_time.toInt() + START_DATETIME);
|
|
||||||
// value = deleteBeforeDelimiter(line, " ");
|
|
||||||
// jsonWriteFloat(buf, "y1", value.toFloat());
|
|
||||||
// if (unix_time != "" || value != "") {
|
|
||||||
// oneSingleJson += buf + ",";
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// } while (psn < sz);
|
|
||||||
//
|
|
||||||
// configFile.close();
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -192,6 +192,7 @@ void removeFile(const String& filename) {
|
|||||||
|
|
||||||
//очищаем директорию с файлами
|
//очищаем директорию с файлами
|
||||||
void cleanDirectory(String path) {
|
void cleanDirectory(String path) {
|
||||||
|
#if defined(ESP8266)
|
||||||
auto dir = FileFS.openDir(path);
|
auto dir = FileFS.openDir(path);
|
||||||
while (dir.next()) {
|
while (dir.next()) {
|
||||||
String fname = dir.fileName();
|
String fname = dir.fileName();
|
||||||
@@ -199,6 +200,21 @@ void cleanDirectory(String path) {
|
|||||||
SerialPrint("I", "Files", path + "/" + fname + " => deleted");
|
SerialPrint("I", "Files", path + "/" + fname + " => deleted");
|
||||||
}
|
}
|
||||||
onFlashWrite();
|
onFlashWrite();
|
||||||
|
#endif
|
||||||
|
#if defined(ESP32)
|
||||||
|
path = "/" + path;
|
||||||
|
File root = FileFS.open(path);
|
||||||
|
path = String();
|
||||||
|
if (root.isDirectory()) {
|
||||||
|
File file = root.openNextFile();
|
||||||
|
while (file) {
|
||||||
|
String fname = file.name();
|
||||||
|
removeFile(fname);
|
||||||
|
SerialPrint("I", "Files", fname + " => deleted");
|
||||||
|
file = root.openNextFile();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveDataDB(String id, String data) {
|
void saveDataDB(String id, String data) {
|
||||||
@@ -252,7 +268,7 @@ String FileList(String path) {
|
|||||||
output += "{\"type\":\"";
|
output += "{\"type\":\"";
|
||||||
output += (file.isDirectory()) ? "dir" : "file";
|
output += (file.isDirectory()) ? "dir" : "file";
|
||||||
output += "\",\"name\":\"";
|
output += "\",\"name\":\"";
|
||||||
output += String(file.path()).substring(1);
|
// output += String(file.path()).substring(1);
|
||||||
output += "\"}";
|
output += "\"}";
|
||||||
file = root.openNextFile();
|
file = root.openNextFile();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user