I have RTC module attached to arduino mega,
to calculate time difference of minutes I am getting minute from the RTC module
and checking if passed time is greater than 5 minutes then insert to MySQL table in database.
int mint = util::getMinute();
if (util::getMinute() - mint >= 5)
{
// inserting to sql database on mysql server
INSERT_SQL = "";
INSERT_SQL.concat("INSERT INTO arduinoSensorData.outTempLog (out_temperature) VALUES ('");
INSERT_SQL.concat(tempInC);
INSERT_SQL.concat("');");
const char *mycharp = INSERT_SQL.c_str();
delay(1000);
if (!connected) {
my_conn.mysql_connect(server_addr, 3306, user, password);
connected = true;
}
else if (connected == true)
{
delay(500);
Serial.print("Inserting : ");
Serial.println(INSERT_SQL);
Serial.println("Connection Successfull,inserting to database.");
my_conn.cmd_query(mycharp);
}
else {
Serial.println("Connection failed.");
}
mint = util::getMinute();
}
the full code is here
int mint = util::getMinute();outside the loop function. Otherwiseutil::getMinute() - mintwill always be0.