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

3
  • Move int mint = util::getMinute(); outside the loop function. Otherwise util::getMinute() - mint will always be 0. – Gerben Dec 22 '16 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. – uint128_t Dec 29 '16 at 17:57
  • Oh sweet I didn't knew that it's DS3231 – Ciasto piekarz Dec 29 '16 at 23:27
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.

1
  • done that but I don't see database getting inserted even after making the change – Ciasto piekarz Dec 23 '16 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.");
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.