ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

Connection Problem #21

Closed Papaille closed 2 years ago

Papaille commented 6 years ago

Hello,

I'm using MySQL_Connector_Arduino, it's working fine on MEGA 2560 with ethernet shield W5100, thanks for this library. The aim of this project is to measure some different temperatures and store it in database. But my Arduino runs at the same as EthernetServer, to send a JSON file with temperature value to client connected. When I check if there is a client making a request, the Arduino reboot. Why ? Memory problem ?

If I delete the server code, all works fine. If I delete the MySQL code, the server part works fine.

Thanks in advance.

Papaille commented 6 years ago

include

include

include

include

include

include

include

include

include

include

include

include

include

define RREF 430.0

const byte SENSOR_ADDRESS_1[] = { 0x28, 0xFF, 0x75, 0x19, 0xC1, 0x16, 0x04, 0x83 }; const byte SENSOR_ADDRESS_2[] = { 0x28, 0xFF, 0x4F, 0xCC, 0xB4, 0x16, 0x05, 0xBE }; const char INSERT_DATA[] = "INSERT INTO domotic.test(id_device,value) VALUES (%s, %s)";

// L'adresse MAC du shield byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xA5, 0x7E };

EthernetClient client; EthernetServer serveur(80);

OneWire ds(22);

MySQL_Connection sql_conn((Client *)&client); IPAddress db_addr(192,168,0,103); Adafruit_MAX31865 pt100 = Adafruit_MAX31865(23);

char user[] = "heatdom"; char password[] = "heatdom";

long prevtime = 0; long interval = 10000; float temperature[3];

void setup() { Serial.begin(115200); int DHCP = 0;
DHCP = Ethernet.begin(mac); if (!DHCP) { Serial.println("DHCP echoue"); return; } Serial.println("Adressage par DHCP OK."); serveur.begin(); client.setTimeout(100);

pt100.begin(MAX31865_3WIRE);

}

void loop() {

client = serveur.available();
if (client) {
    if (client.connected()) {
        webrequest();
    }
}

if (millis() - prevtime > interval) {
    acquistemp();
    insertobdd();
    prevtime = millis();
}

}

void acquistemp() { Serial.println("Acquisition des temperatures...");

temperature[0] = getTemperature(SENSOR_ADDRESS_1);
temperature[1] = getTemperature(SENSOR_ADDRESS_2);
temperature[2] = pt100.temperature(100, RREF);

}

void webrequest() { while (client.available()) { char c = client.read(); Serial.write(c); }
client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println("Connection: close"); client.println(); client.println("{"); client.print("\t\"uptime\": "); client.print(millis()); client.println(","); client.print("\t\"temperature 1\": "); client.print(temperature[0], 2); client.println(","); client.print("\t\"temperature 2\": "); client.print(temperature[1], 2); client.println(","); client.print("\t\"temperature 3\": "); client.print(temperature[2], 2); client.println(); client.println("}");

client.flush();
client.stop();
Serial.println("Client deconnecte");

}

void insertobdd() { Serial.println("Connection base de donnees Domotic ..."); if (sql_conn.connect(db_addr, 3306, user, password)) { delay(1000); for (int i = 0; i < 3; i++) { MySQL_Cursor *cur_mem = new MySQL_Cursor(&sql_conn); char query[128]; char tempstr[4]; char id[1]; dtostrf(temperature[i], 1, 2, tempstr); dtostrf(i + 1, 1, 0, id); sprintf(query, INSERT_DATA, id, tempstr); cur_mem->execute(query); delete cur_mem; } } else Serial.println("Connection echouee."); sql_conn.close(); }###

ChuckBell commented 6 years ago

I see some issues with your sketch. First, you are connecting each time you insert into the database but you are not disconnecting. This is incorrect. If you want to connect each time through the loop (nothing wrong so far), you must disconnect too (this is what is missing).

You need to close the cursor first, then delete it (deleting it does not close it), then close the connection.

It is possible this can cause the hang (run out of memory).

On 10/4/17 9:30 AM, Papaille wrote:

include

include

include

include

include

include

include

include

include

include

include

include

include

define RREF 430.0

const byte SENSOR_ADDRESS_1[] = { 0x28, 0xFF, 0x75, 0x19, 0xC1, 0x16, 0x04, 0x83 }; const byte SENSOR_ADDRESS_2[] = { 0x28, 0xFF, 0x4F, 0xCC, 0xB4, 0x16, 0x05, 0xBE }; const char INSERT_DATA[] = "INSERT INTO domotic.test(id_device,value) VALUES (%s, %s)";

// L'adresse MAC du shield byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xA5, 0x7E };

EthernetClient client; EthernetServer serveur(80);

OneWire ds(22);

MySQL_Connection sql_conn((Client *)&client); IPAddress db_addr(192,168,0,103); Adafruit_MAX31865 pt100 = Adafruit_MAX31865(23);

char user[] = "heatdom"; char password[] = "heatdom";

long prevtime = 0; long interval = 10000; float temperature[3];

void setup() { Serial.begin(115200); int DHCP = 0; DHCP = Ethernet.begin(mac); if (!DHCP) { Serial.println("DHCP echoue"); return; } Serial.println("Adressage par DHCP OK."); serveur.begin(); client.setTimeout(100);

|pt100.begin(MAX31865_3WIRE); |

}

void loop() {

|client = serveur.available(); if (client) { if (client.connected()) { webrequest(); } } if (millis() - prevtime > interval) { acquistemp(); insertobdd(); prevtime = millis(); } |

}

void acquistemp() { Serial.println("Acquisition des temperatures...");

|temperature[0] = getTemperature(SENSOR_ADDRESS_1); temperature[1] = getTemperature(SENSOR_ADDRESS_2); temperature[2] = pt100.temperature(100, RREF); |

}

void webrequest() { while (client.available()) { char c = client.read(); Serial.write(c); } client.println("HTTP/1.1 200 OK"); client.println("Content-Type: application/json"); client.println("Connection: close"); client.println(); client.println("{"); client.print("\t"uptime": "); client.print(millis()); client.println(","); client.print("\t"temperature 1": "); client.print(temperature[0], 2); client.println(","); client.print("\t"temperature 2": "); client.print(temperature[1], 2); client.println(","); client.print("\t"temperature 3": "); client.print(temperature[2], 2); client.println(); client.println("}");

|client.flush(); client.stop(); Serial.println("Client deconnecte"); |

}

void insertobdd() { Serial.println("Connection base de donnees Domotic ..."); if (sql_conn.connect(db_addr, 3306, user, password)) { delay(1000); for (int i = 0; i < 3; i++) { MySQL_Cursor *cur_mem = new MySQL_Cursor(&sql_conn); char query[128]; char tempstr[4]; char id[1]; dtostrf(temperature[i], 1, 2, tempstr); dtostrf(i + 1, 1, 0, id); sprintf(query, INSERT_DATA, id, tempstr); cur_mem->execute(query); delete cur_mem; } } else Serial.println("Connection echouee."); sql_conn.close(); }###

— 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/21#issuecomment-334155388, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4FyhCsChkUAgB_EdY5PSvGq_Qsz5ks5so4hkgaJpZM4Ptk0k.

Papaille commented 6 years ago

Thanks for your answer.

I modified the "insertobdd" function, adding the closing of cursor and the closing of connection.

void insertobdd() { Serial.println("Connection base de donnees Domotic ..."); if (sql_conn.connect(db_addr, 3306, user, password)) { delay(1000); for (int i = 0; i < 3; i++) { MySQL_Cursor *cur_mem = new MySQL_Cursor(&sql_conn); char query[128]; char tempstr[4]; char id[1]; dtostrf(temperature[i], 1, 2, tempstr); dtostrf(i + 1, 1, 0, id); sprintf(query, INSERT_DATA, id, tempstr); cur_mem->execute(query); cur_mem->close(); delete cur_mem; } Serial.println("Integration dans base donnees OK."); sql_conn.close(); } else { Serial.println("Connection echouee."); sql_conn.close(); } }

But it changes nothing, I have the same bug.

Here is the serial monitor output :

Uploading to I/O board Opening port Port open Adressage par DHCP OK. Connecte avec l'adresse IP : 192.168.0.118 Found 28 FF 75 19 C1 16 04 83 Found 28 FF 4F CC B4 16 05 BE End of Scan. HeatDom est pret Acquisition des temperatures... Connection base de donnees Domotic ... Connected to server version 5.7.14 Integration dans baseAdressage par DHCP OK. Connecte avec l'adresse IP : 192.168.0.118 Found 28 FF 75 19 C1 16 04 83 Found 28 FF 4F CC B4 16 05 BE End of Scan. HeatDom est pret Acquisition des temperatures... Connection base de donnees Domotic ... Connected to server version 5.7.14 Integration dans baseAdressage par DHCP OK. Connecte avec l'adresse IP : 192.168.0.118 Found 28 FF 75 19 C1 16 04 83 Found 28 FF 4F CC B4 16 05 BE End of Scan. HeatDom est pret"

He fails when he prints Serial.println("Integration dans base donnees OK."); just before sql_conn.close();

ChuckBell commented 6 years ago

Ok, you're likely running out of memory still. How much memory does the compiler report?

On 10/4/17 10:09 AM, Papaille wrote:

Thanks for your answer.

I modified the "insertobdd" function, adding the closing of cursor and the closing of connection.

void insertobdd() { Serial.println("Connection base de donnees Domotic ..."); if (sql_conn.connect(db_addr, 3306, user, password)) { delay(1000); for (int i = 0; i < 3; i++) { MySQL_Cursor *cur_mem = new MySQL_Cursor(&sql_conn); char query[128]; char tempstr[4]; char id[1]; dtostrf(temperature[i], 1, 2, tempstr); dtostrf(i + 1, 1, 0, id); sprintf(query, INSERT_DATA, id, tempstr); cur_mem->execute(query); cur_mem->close(); delete cur_mem; } Serial.println("Integration dans base donnees OK."); sql_conn.close(); } else { Serial.println("Connection echouee."); sql_conn.close(); } }

But it changes nothing, I have the same bug.

Here is the serial monitor output :

Uploading to I/O board Opening port Port open Adressage par DHCP OK. Connecte avec l'adresse IP : 192.168.0.118 Found 28 FF 75 19 C1 16 04 83 Found 28 FF 4F CC B4 16 05 BE End of Scan. HeatDom est pret Acquisition des temperatures... Connection base de donnees Domotic ... Connected to server version 5.7.14 Integration dans baseAdressage par DHCP OK. Connecte avec l'adresse IP : 192.168.0.118 Found 28 FF 75 19 C1 16 04 83 Found 28 FF 4F CC B4 16 05 BE End of Scan. HeatDom est pret Acquisition des temperatures... Connection base de donnees Domotic ... Connected to server version 5.7.14 Integration dans baseAdressage par DHCP OK. Connecte avec l'adresse IP : 192.168.0.118 Found 28 FF 75 19 C1 16 04 83 Found 28 FF 4F CC B4 16 05 BE End of Scan. HeatDom est pret"

He fails when he prints Serial.println("Integration dans base donnees OK."); just before sql_conn.close();

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/21#issuecomment-334167575, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4ONupZLyO01OvT4ONxGb_9b69QsYks5so5GXgaJpZM4Ptk0k.

Papaille commented 6 years ago

Compiler output : Compiling debug version of 'HeatDom' for 'Arduino/Genuino Mega w/ ATmega2560 (Mega 2560)' Program size: 30 234 bytes (used 12% of a 253 952 byte maximum) (1.45 secs) Minimum Memory Usage: 1518 bytes (19% of a 8192 byte maximum)

Uploading 'HeatDom' to 'Arduino/Genuino Mega w/ ATmega2560 (Mega 2560)' using 'COM3' The upload process has finished.

If I comment the first line ("client = serveur.available();") in the loop, it works fine

Papaille commented 6 years ago

For more details see attached file.

Compiler_output.txt

ChuckBell commented 6 years ago

Hmmm... that is not the output I expected. Are you using the Arduino IDE? That will tell you precisely how much memory is available. However, that shouldn't matter if you're using the mega board. Only the smaller boards have an issue with memory.

On Oct 4, 2017, at 10:24 AM, Papaille notifications@github.com wrote:

For more details see attached file.

Compiler_output.txt

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

ed-lab commented 6 years ago

Daear you all Since I had my w5500 with mega and never had a running SAMPLE without a reboot I thinked my problem was the W5500 shield. Now I'm back again in my datalogger. I spent 2 weeks and a lot of headache but I have the same problem as you with 5100 ethernet and the same mega. atmega2560

The mysql forum https://forums.mysql.com/read.php?175,652039,652039#msg-652039 is not useful

So with the maximum wdt time I have a reboot. Without wdt Arduino Mega hangs immediately in the setup initialization of mysql conn.

the sketch is the sample sketch.

What can I do?

Ugo