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
2 Answers 2
int mint = util::getMinute();
if (util::getMinute() - mint >= 5)
{
Well, if mint
is util::getMinute()
then surely util::getMinute() - mint
will always be 0.
I think you meant mint
to be static
, or a variable in the global scope.
-
done that but I don't see database getting inserted even after making the changeCiasto piekarz– Ciasto piekarz2016年12月23日 02:13:29 +00:00Commented Dec 23, 2016 at 2:13
So I figured it out myself and used millis
instead.
in global declaration I did:
unsigned long sqlInsertInterval = 300000; // the repeat interval after 5 minutes
then initiate timer in setup method
timer = millis(); // start timer
followed by below changes in the loop method.
void loop() {
// perform sql insert after 5 minutes
if ((millis() - timer) > sqlInsertInterval) {
// timed out
timer += sqlInsertInterval;// reset timer by moving it along to the next interval
// 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();
if (!connected) {
my_conn.mysql_connect(server_addr, 3306, user, password);
connected = true;
}
else if (connected == true)
{
Serial.print("Inserting : ");
Serial.println(INSERT_SQL);
Serial.println("Connection Successfull,inserting to database.");
my_conn.cmd_query(mycharp);
}
else {
Serial.println("Connection failed.");
}
}
}
int mint = util::getMinute();
outside the loop function. Otherwiseutil::getMinute() - mint
will always be0
.