1

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

asked Dec 22, 2016 at 18:51
3
  • Move int mint = util::getMinute(); outside the loop function. Otherwise util::getMinute() - mint will always be 0. Commented Dec 22, 2016 at 19:43
  • Just a tip, you didn't specify what RTC you're using, but most RTCs have an internal alarm functionality that will trigger an alert when a specific time of day is reached. This allows you to eliminate the polling from your code, and even sleep in between the 5 mins, which can greatly improve your power consumption. Commented Dec 29, 2016 at 17:57
  • Oh sweet I didn't knew that it's DS3231 Commented Dec 29, 2016 at 23:27

2 Answers 2

0
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.

answered Dec 22, 2016 at 19:45
1
  • done that but I don't see database getting inserted even after making the change Commented Dec 23, 2016 at 2:13
0

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.");
 }
 }
}
answered Dec 30, 2016 at 17:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.