MySQL Forums
Forum List  »  Connector/Arduino

Reconnecting to mysql connector
Posted by: Chris Watters
Date: November 01, 2013 01:10AM

Hello !

I've just created my first Arduino app with the MySQL connector and so far it is working fine. But I have a question regarding how to reconnect to the MySQL server if the connection breaks for whatever reason (server restart, etc)

First the details.
Within the decalrations section I have the following:

/* Setup for Ethernet Library */
byte mac_addr[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xF4, 0x79 };
byte ip[] = { 192, 168, 1, 201 };
IPAddress server_addr(192, 168, 1, 100);
/* Setup for the Connector/Arduino */
Connector my_conn;
char user[8] = "arduino";
char password[8] = "arduino";

Within the Setup routine, I have the following code to initialise ethernet and MySQL (works fine):

void setup()
{
Ethernet.begin(mac_addr, ip);
delay(500);
Serial.println("Ethernet begin");
my_conn.mysql_connect(server_addr, 3306, user, password);
delay(500);
}

Next, I have the RTC to fire an interrupt every five minutes to write some data to the MySQL client, as follows:

void WriteData()
{

if (my_conn.is_connected() == 0) {
my_conn.mysql_connect(server_addr, 3306, user, password);
delay(500);
}
// Some code here to construct the INSERT statement
my_conn.cmd_query(buf);
}
}


I tried, within the WriteData routine, to include some code to test if the connection is alive or not. If it's dead, then it should reconnect.

I test this by killing the connection in MySQL Server after the first INSERT command and seeing what happens.

What happens is that 'is_connected' returns zero (as expected), but the 'mysql_connect' instruction does not reconnect as expected. The instruction seems to execute, by observation that the code after is executed, but by the time the cmd_query command is executed, the connection is still not alive and the Arduino board resets itself.

Perhaps this is not so bad (restart server, then Arduino will 'self reset', but it's not by design!). It would be nice if I could do the folllwing in the Write Data function to connect to the server, do some stuff and then disconnect:

void WriteData()
{
if my_conn.mysql_connect(server_addr, 3306, user, password) {
delay(500);
// Some code here to construct the INSERT statement
my_conn.cmd_query(buf);
my_conn.close()
}
}


So, why would my Arduino reset itself if cmd_query is performed without a valid connection? Or is it mysql_connector (or even Ethernet library) that is doing this?

Is it possible to implement code (myconn.close) as I suggested just above, or is there some other method to achieve this functionality ? I searched for the 'close' function but didn't find anything.

Thanks for reading and answering my queries !

Options: ReplyQuote


Subject
Views
Written By
Posted
Reconnecting to mysql connector
8060
November 01, 2013 01:10AM
2395
November 01, 2013 10:22PM
2165
November 06, 2013 06:48AM
1902
November 06, 2013 09:35AM
2027
November 07, 2013 08:47AM
1736
November 11, 2013 02:03PM
2204
December 18, 2013 01:48PM
1810
December 18, 2013 03:48PM
1789
December 29, 2013 09:48AM
1482
December 30, 2013 03:15PM
1504
December 31, 2013 11:50AM
1706
January 06, 2014 01:59PM
1487
February 04, 2014 03:45PM
1425
February 04, 2014 04:20PM
1477
February 05, 2014 01:15PM
1628
February 05, 2014 02:36PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.