ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
331 stars 132 forks source link

ESP8266 resets when executing query from a webserver running in the ESP #125

Closed rybertm-old closed 4 years ago

rybertm-old commented 4 years ago

I'm using my ESP as a WebServer using AsyncWebServer library, I'm trying to execute some queries as response from a HTTP request from the server, but when I do it resets the board.

I tried 2 approaches: 1- Connect once in setup() and run queries when needed, which resets the board. 2- Connect whenever i need to run a query, which doesn't connect to the db server.

OBS: When running queries in the regular loop() it works fine with either of these approaches.

Here's relevant code and reset decoded from first approach:

Query:

SELECT * FROM db LIMIT 1
setup()
{
    ...
   server.on("/sql", HTTP_GET, [](AsyncWebServerRequest *req) {
    selectSQL(); // Execute Select query when it receives a HTTP GET request
    req->send(200);
  });
    ...
}

Reset:

0x402214f4: __delay(unsigned long) at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266\core_esp8266_wiring.cpp line 49
0x4022059d: __esp_yield() at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266\core_esp8266_main.cpp line 104
0x40221502: __delay(unsigned long) at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266\core_esp8266_wiring.cpp line 54
0x4021cba1: MySQL_Packet::wait_for_bytes(int) at C:\Users\Robert Morais\Documents\Arduino\libraries\MySQL_Connector_Arduino\src\MySQL_Packet.cpp line 235
0x4021dd38: Print::println() at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266\Print.cpp line 186
0x4021cc04: MySQL_Packet::read_packet() at C:\Users\Robert Morais\Documents\Arduino\libraries\MySQL_Connector_Arduino\src\MySQL_Packet.cpp line 279
0x4021ddac: Print::println(char const*) at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266\Print.cpp line 198
0x4021c456: MySQL_Cursor::execute_query(int) at C:\Users\Robert Morais\Documents\Arduino\libraries\MySQL_Connector_Arduino\src\MySQL_Cursor.cpp line 153
0x4021d6e0: HardwareSerial::write(unsigned char const*, unsigned int) at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266/HardwareSerial.h line 164
0x4021c552: MySQL_Cursor::execute(char const*, bool) at C:\Users\Robert Morais\Documents\Arduino\libraries\MySQL_Connector_Arduino\src\MySQL_Cursor.cpp line 121
0x4021dd38: Print::println() at C:\Users\Robert Morais\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.3\cores\esp8266\Print.cpp line 186
0x4020571b: selectSQL() at C:\Users\Robert Morais\Desktop\sketch_dec12a/sketch_dec12a.ino line 801
ChuckBell commented 4 years ago

I am not certain, but I suspect you're running out of memory. Try adding methods to track the memory you're using. If there is less than about 800 bytes of user data left, it can become unstable and random reboots are one of the results.

rybertm-old commented 4 years ago

@ChuckBell thank you for the response, I don't think it's a memory issue, but rather asynchronous callbacks doesn't allow delay/yield function calls, which is what "MySQL_Connection.cpp" does on

    boolean MySQL_Connection::connect(IPAddress server, int port, char *user, char *password)

on line 71:

71: delay(CONNECT_DELAY_MS);

Tried to change it by using a simple while but it still won't connect to the db server. obs: I am trying to make a connection per query rather than connecting once and keeping it open.

I got these from memory checks:

getFreeHeap: 30384
getHeapFragmentation: 1
getMaxFreeBlockSize: 30248
getFreeContStack: 2352
ChuckBell commented 4 years ago

I don't think you'll be able to use the connector in that manner. It is written so that you must read all rows when executing a query. So, an asynchronous model may timeout or just not work. At least, not how it is currently implemented.

rybertm-old commented 4 years ago

I see that now, thank you for the the help. Closing as it’s working as intended.