ChuckBell / MySQL_Connector_Arduino

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

ESP8266 crashed after some iteration on "cursor->execute(sql5);" #118

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hello,

i´m using the Nodemcu Lua V3 ESP 8266 Board.

I connect this ESP Board via wifi to a lokal mysql database (5.5.5-10.1.41-MariaDB) running on a raspberry pi with osmc.

The ESP is used to read temperature and humidity from a dht11 sensor and send this data the db. My Problem is that after some iterations the board crashed and i have to reset it. After some Debugging with Serial.prinln() i found that the Board hangs up while "cursor->execute(sql5);"

Here is my code:

`#include "DHTesp.h"

include

include

include

IPAddress server_addr(192,168,178,30); int i; char user[] = "nodemcu"; char password[] = "here i have to insert my password"; int y;

char sql1[] = "INSERT INTO mydb.dht (Datum,Temperatur, Feuchtigkeit) VALUES (NOW(),"; char sql2[] = ","; char sql3[] = ")"; String sql4; char sql5[90]; unsigned long startmillis; unsigned long stopmillis; int looptime ; char ssid[] = "FRITZ!Box 7560 RL"; //Hier SSID eures WLAN Netzes eintragen char pass[] = ""; //Hier euer Passwort des WLAN Netzes eintragen

WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn(&client); MySQL_Cursor* cursor;

ifdef ESP32

pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)

error Select ESP8266 board.

endif

DHTesp dht;

void setup() { Serial.begin(115200); Serial.printf("\nConnecting to %s", ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println("\nConnected to network"); Serial.print("My IP address is: "); Serial.println(WiFi.localIP());

Serial.print("Connecting to SQL... "); if (conn.connect(server_addr, 3306, user, password)) Serial.println("OK."); else Serial.println("FAILED.");

// create MySQL cursor object cursor = new MySQL_Cursor(&conn);

Serial.println(); Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)"); String thisBoard= ARDUINO_BOARD; Serial.println(thisBoard);

// Autodetect is not working reliable, don't use the following line // dht.setup(17); // use this instead: dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 17 }

void loop() {

//delay(dht.getMinimumSamplingPeriod()); startmillis = millis(); Serial.println("Vor Humidity get"); float humidity = dht.getHumidity(); Serial.println("nach Humidity get"); char humidbuf[6]; yield(); dtostrf(humidity,4,2,humidbuf); Serial.println("vor get temp"); float temperature = dht.getTemperature(); Serial.println("Nach get temp"); yield(); char tempbuf[5]; dtostrf(temperature,4,2,tempbuf); //Serial.print(dht.getStatusString()); //Serial.print(humidbuf);

//Serial.print("\t\t"); yield();

if (conn.connected()) sql4 = String(sql1) + String(tempbuf)+ String(sql2) + String(humidbuf) + String(sql3);

sql4.toCharArray(sql5,90); Serial.println(sql5); delay(0); Serial.println("Vor SQl Eintragung"); cursor->execute(sql5); Serial.println("Nach sql eintragung"); yield(); i=0; Serial.println("Vor While schleife"); while(i< 6000){ delay(10); i = i+1; } Serial.println("Nach While Schleife"); y = y+1; Serial.println(y); } `

Did someone knows a solution for this problem. I think it has something to do with the ESP delay error, but i´m not sure.

Thanks for your help.

ChuckBell commented 4 years ago

Hi. I suggest you slow your loop() down to match a more reasonable sample rate. Think about it - how often must you check temperature and humidity? Will it change that much in the environment where the sensors are located? What can you gain by sampling so frequently?

Aside from that, one technique that helps is connecting and disconnecting inside the loop() - don’t hold the connection open. This can help overcome wonky network connections.

Finally, and I’m sorry, but the connector is not supported on non-Oracle MySQL releases. In fact, it has known issues when used with other variants. Try the above first and if that doesn’t fix the issue, install an Oracle release of MySQL.

On Dec 7, 2019, at 2:12 PM, bewi93 notifications@github.com wrote:

Hello,

i´m using the Nodemcu Lua V3 ESP 8266 Board.

I connect this ESP Board via wifi to a lokal mysql database (5.5.5-10.1.41-MariaDB) running on a raspberry pi with osmc.

The ESP is used to read temperature and humidity from a dht11 sensor and send this data the db. My Problem is that after some iterations the board crashed and i have to reset it. After some Debugging with Serial.prinln() i found that the Board hangs up while "cursor->execute(sql5);"

Here is my code:

`#include "DHTesp.h"

include

include

include

IPAddress server_addr(192,168,178,30); int i; char user[] = "nodemcu"; char password[] = "here i have to insert my password"; int y;

char sql1[] = "INSERT INTO mydb.dht (Datum,Temperatur, Feuchtigkeit) VALUES (NOW(),"; char sql2[] = ","; char sql3[] = ")"; String sql4; char sql5[90]; unsigned long startmillis; unsigned long stopmillis; int looptime ; char ssid[] = "FRITZ!Box 7560 RL"; //Hier SSID eures WLAN Netzes eintragen char pass[] = "13393789918580235792"; //Hier euer Passwort des WLAN Netzes eintragen

WiFiClient client; // Use this for WiFi instead of EthernetClient MySQL_Connection conn(&client); MySQL_Cursor* cursor;

ifdef ESP32

pragma message(THIS EXAMPLE IS FOR ESP8266 ONLY!)

error Select ESP8266 board.

endif

DHTesp dht;

void setup() { Serial.begin(115200); Serial.printf("\nConnecting to %s", ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }

Serial.println("\nConnected to network"); Serial.print("My IP address is: "); Serial.println(WiFi.localIP());

Serial.print("Connecting to SQL... "); if (conn.connect(server_addr, 3306, user, password)) Serial.println("OK."); else Serial.println("FAILED.");

// create MySQL cursor object cursor = new MySQL_Cursor(&conn);

Serial.println(); Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)\tHeatIndex (C)\t(F)"); String thisBoard= ARDUINO_BOARD; Serial.println(thisBoard);

// Autodetect is not working reliable, don't use the following line // dht.setup(17); // use this instead: dht.setup(D4, DHTesp::DHT11); // Connect DHT sensor to GPIO 17 }

void loop() {

//delay(dht.getMinimumSamplingPeriod()); startmillis = millis(); Serial.println("Vor Humidity get"); float humidity = dht.getHumidity(); Serial.println("nach Humidity get"); char humidbuf[6]; yield(); dtostrf(humidity,4,2,humidbuf); Serial.println("vor get temp"); float temperature = dht.getTemperature(); Serial.println("Nach get temp"); yield(); char tempbuf[5]; dtostrf(temperature,4,2,tempbuf); //Serial.print(dht.getStatusString()); //Serial.print(humidbuf);

//Serial.print("\t\t"); yield();

if (conn.connected()) sql4 = String(sql1) + String(tempbuf)+ String(sql2) + String(humidbuf) + String(sql3);

sql4.toCharArray(sql5,90); Serial.println(sql5); delay(0); Serial.println("Vor SQl Eintragung"); cursor->execute(sql5); Serial.println("Nach sql eintragung"); yield(); i=0; Serial.println("Vor While schleife"); while(i< 6000){ delay(10); i = i+1; } Serial.println("Nach While Schleife"); y = y+1; Serial.println(y); } `

Did someone knows a solution for this problem. I think it has something to do with the ESP delay error, but i´m not sure.

Thanks for your help.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/118?email_source=notifications&email_token=AB6SHYDO564Z34F5MGB4KU3QXPYQTA5CNFSM4JXQ55MKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4H625KGQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYFTHYYGR6LWNZDYHR3QXPYQTANCNFSM4JXQ55MA.

ghost commented 4 years ago

Hello Chuck Bell,

thanks for your Response. You are right 1 Minute sampling frequency is to high. It is just a testing frequency to let the error occur faster. This example is just a learning Project for me to get started with html, php, mysql, and the esp 8266. The esp worked for 2 days without a disconnect.

Maybe you can explain what´s the Problem is? Why did i have to initialize the Cursor new every Loop. Is there something like a Overflow?

I found a working solution. I just integrated the "cursor = new MySQL_Cursor(&conn);" into the void Loop and not in the