siara-cc / esp32_arduino_sqlite3_lib

Sqlite3 Arduino library for ESP32
Apache License 2.0
350 stars 66 forks source link

ESP rebooting on insert record and issue in inserting values to fields #40

Closed ChazSingh closed 3 years ago

ChazSingh commented 3 years ago

Hi I am using the sqlite.h library on Arduinio IDE with an ESP32. I have push button that when pressed should insert a new record with data from a random number which is and integer and a string. if I push the button the ESP32 does a reset. Nothing else is connected to it except the button.

The button calls a function called insertrecord() I can see using serial monitor that with the insertrecord() function it gets all the way down to the Serial.println("Inserting new record"); before it reboots. It also does this when I call the printallrecords() function.

One other thing I am struggling with please, is how do you pass a value like a analog pin reading as an integer into the an integer field such as readingvalue or a string into a string field in a table? I want to eventually attach sensors so will need to store and read back the number they generate.

Any help on this would be much appreciated. I have treid for hours and googled lots but made very little progress.

The database is called test.db its path is /spiffs/test.db
The table is called aoreadings and has three fields which are id which is an integer, readvalue which is an integer and status which is used to hold a string value. I can see that the database and table are created correctly.

My code is below and I have pasted in the error code below that.

Many thanks in advanced Chazza

[code]

include

include

include

include

include

include

define FORMAT_SPIFFS_IF_FAILED true

const int buttonPin = 12; // the number of the pushbutton pin int buttonState = 0; // variable for reading the pushbutton status

char *zErrMsg = 0;

const char* data = "Callback function called";

// callback function static int callback(void *NotUsed, int argc, char argv, char azColName) { int i; for (i = 0; i < argc; i++) { printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } //**

// opens database if it does not exist it creates it. int db_open(const char *filename, sqlite3 *db) { int rc = sqlite3_open(filename, db); // Serial.print("rc is: ");Serial.println(rc); if (rc) { Serial.printf("Can't open database: %s\n", sqlite3_errmsg(db)); return rc; } else { Serial.printf("Opened database successfully\n"); } return rc; } //**

// executes database engine commands int db_exec(sqlite3 db, const char sql) { // Serial.println(sql); long start = micros(); int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg); if (rc != SQLITE_OK) { Serial.printf("SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { // Serial.printf("Operation done successfully\n"); } // Serial.print(F("Time taken:")); // Serial.println(micros() - start); return rc; } //**

void setup() { Serial.begin(115200); Serial.println("Setup Started");

pinMode(buttonPin, INPUT_PULLUP);

SPIFFS.begin(); // For SPIFFS // print files on on spiffs checkspifffiles();

// check if database exists. If not create it sqlite3 *db1; if (db_open("/spiffs/test.db", &db1)) return;

int rc = db_exec(db1, "CREATE TABLE IF NOT EXISTS aoreadings (id INTEGER, readvalue INTEGER, status);"); if (rc != SQLITE_OK) { sqlite3_close(db1); return; }

Serial.println("=============================");

Serial.println("Setup Finished"); } //**

// the loop function runs over and over again until power down or reset void loop() { buttonState = digitalRead(buttonPin);

if (buttonState == LOW) { Serial.println("Button Pressed"); insertrecord(); // printallrecords(); delay(1000); }

} //**

void insertrecord() {

int reading = random(0, 1023); String statusStr = ""; if (reading >= 750) { statusStr = "HIGH"; } else if (reading <= 749 and reading >= 500) { statusStr = "MEDIUM"; } else if (reading <= 499 and reading >= 0) { statusStr = "LOW"; }

Serial.print("AO reading is : "); Serial.println(reading); Serial.print("Status is : "); Serial.println(statusStr);

// insert values int rc; sqlite3 *db1;

Serial.println("Inserting new record"); delay(200);

// line below to add new record with default values when button pushed for testing rc = db_exec(db1, "INSERT INTO aoreadings VALUES (1, 2, 'abcdefg');");

// line below add new record with value 1 in id field, reading in readvalue field and // StatusStr into status field when button pushed // rc = db_exec(db1, "INSERT INTO aoreadings VALUES (id, readvalue, status);(1, reading, statusStr)");

if (rc != SQLITE_OK) { sqlite3_close(db1); return; }

} //**

void printallrecords() { // insert values int rc; sqlite3 *db1;

Serial.println("Print all records");

// select all records Serial.println("Printing all records"); rc = db_exec(db1, "SELECT * FROM aoreadings"); if (rc != SQLITE_OK) { sqlite3_close(db1); return; } Serial.println("=============================");

}

void checkspifffiles() { if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) { Serial.println("Failed to mount file system"); return; }

// list SPIFFS contents File root = SPIFFS.open("/"); if (!root) { Serial.println("- failed to open directory"); return; } if (!root.isDirectory()) { Serial.println(" - not a directory"); return; } File file = root.openNextFile(); while (file) { if (file.isDirectory()) { Serial.print(" DIR : "); Serial.println(file.name()); } else { Serial.print(" FILE: "); Serial.print(file.name()); Serial.print("\tSIZE: "); Serial.println(file.size()); } file = root.openNextFile(); } } [/code]

* ERROR ***

Setup Started FILE: /test.db SIZE: 8192 FILE: /test.db-journal SIZE: 512 Opened database successfully

Setup Finished Button Pressed AO reading is : 79

Status is : LOW Inserting new record Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled. Core 1 register dump: PC : 0x400014fd PS : 0x00060130 A0 : 0x801266b8 A1 : 0x3ffb1ab0
A2 : 0x00000000 A3 : 0xfffffffc A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000000 A9 : 0x00000001
A10 : 0x00000000 A11 : 0x00000000 A12 : 0x00000000 A13 : 0x00000001
A14 : 0x0000000a A15 : 0x3ffbc5f0 SAR : 0x0000000c EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xffffffff

Backtrace: 0x400014fd:0x3ffb1ab0 0x401266b5:0x3ffb1ac0 0x4012dc2e:0x3ffb1dd0 0x4012dc6a:0x3ffb1e60 0x40114c0a:0x3ffb1ea0 0x400d1546:0x3ffb1f40 0x400d15fe:0x3ffb1f60 0x400d165f:0x3ffb1f90 0x40115391:0x3ffb1fb0 0x40088385:0x3ffb1fd0

Rebooting... ets Jun 8 2016 00:22:57 ``

siara-cc commented 3 years ago

Hi, Your code looks ok - you might try to track that is causing the error from the above Backtrace. There are utilities available for ESP32 if you feed in the above dump on reset. Usually this is caused by a mem leak or out of memory.