I am trying to dock these 3 incompatible things in my current opinion. I have a large sketch, with information being displayed on a display of four LED matrices 16x16. There is a RTC, an ESP8266 and two sensors bme280 and DS18B20(temperature in the room and outside, pressure, humidity). It all works seamlessly. I decided to try to maintain a database with showings for, say, half a year. I connected the sqlite library. But somehow it does not work to the end! After making 24 entries, I get the out of memory. Then I simplified everything, took just one database file with 1 table. Scheduler base fills up every 10 seconds. So now on 112 records the same story. The database file is only 6656 bytes in size. I attach the sketch. So it’s not clear to me why, with the specified size of flesh memory 4 megabytes, 6 kilobytes runs out of memory? Look, maybe I'm doing something wrong? May be I have to finalize somehow? May be clear memory somehow?
So I use ESP8266 NodeMCU V1.
#include <ESP8266WiFi.h>
#include <TickerScheduler.h>
#include <sqlite3.h>
#include <vfs.h>
#include <FS.h>
File fsUploadFile;
TickerScheduler ts(1);
void setup() {
Serial.begin(115200);
FS_init();
sqlite_init();
}
void loop() {
ts.update();
if (Serial.available() > 0) {
String str = Serial.readString();
if (str != "") {
char query[str.length() + 1];
strcpy(query, str.c_str());
str = "";
db_exec(query);
}
}
}
void FS_init(void) {
system_update_cpu_freq(SYS_CPU_160MHZ);
if (!SPIFFS.begin()) {
Serial.println("Failed to mount SPIFFS");
return;
}
Dir dir = SPIFFS.openDir("/");
while (dir.next()) {
String fileName = dir.fileName();
size_t fileSize = dir.fileSize();
Serial.print(fileName); Serial.println(fileSize);
}
}
void sqlite_init() {
sqlite3_initialize();
int cmin = 0;
int chour = 0;
int csec = 0;
ts.add(0, 10000, [&](void*) {
String tIn = "27.5";//(String)GettIn();
String tOut = "-4.3";//(String)GettOut();
String humidity = "21";//(String)Gethumidity();
String pressure = "745";//(String)Getpressure();
String str = "INSERT INTO day_p (cyear, cmonth, cday, chour, cmin, tin, tout, vlag, davl) ";
str += "VALUES (2021, 2, 28, " + String(chour) + ", " + String(cmin);
str += ", " + tIn + ", " + tOut + ", " + humidity + ", " + pressure + ");";
str += "Select count(cyear) from day_p group by cyear;";
char query[str.length() + 1];
strcpy(query, str.c_str());
str = "";
db_exec(query);
csec = csec + 10;
if (csec == 60) {
csec = 0;
cmin++;
}
if (cmin == 60) {
cmin = 0;
chour++;
}
}, nullptr, true);
}
void db_exec(const char *sql) {
sqlite3 *db;
int rc;
const char *dbf = "/FLASH/weather.sql3";
char *ErrMsg = 0;
const char* data = 0;
Serial.println(sql);
File db_file_obj;
vfs_set_spiffs_file_obj(&db_file_obj);
if (sqlite3_open(dbf, &db)) {
Serial.print(F("Can't open database: "));
Serial.println(sqlite3_errmsg(db));
return;
}
else Serial.println("DB opened");
rc = sqlite3_exec(db, sql, callback, (void*)data, &ErrMsg);
if (rc != SQLITE_OK) {
Serial.print(F("SQL error: "));
Serial.println(ErrMsg);
sqlite3_free(ErrMsg);
}
Serial.println(rc);
sqlite3_close(db);
}
static int callback(void *data, int argc, char **argv, char **azColName) {
for (int i = 0; i < argc; i++){
if (i > 0) {
Serial.print(" | ");
}
Serial.print(argv[i]);
}
Serial.println();
return 0;
}