inserting to table in database after every 5 minutes irrespective of loop cycle

Hi ,

So finally after lot of troubleshooting I got success uploading data to database , however I am facing one problem,

I want to insert data to table after every 5 minute if I use delay, the web server gets stuck and auto refresh doesn't load the page, so how should I have database inserting after every 5 minute irrespective of the loop function cycles

I have implemented client and server in the same sketch, here is the full code:

Since I have RTC module attached to arduino I just realised I can calculate 5 minute difference and insert after every 5 minute of last insert .

so I put this

  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.println("Connection Successfull,inserting to database.");

      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();
  }

Is it correct ?

    delay(1000);

Why? There is no earthly reason for this.

      delay(500);

Or this.

Regardless, take a look at just this code:

  int mint = util::getMinute();

  if (util::getMinute() - mint >= 5)

What is the likelihood that it will take 5 minutes or more between the two statements? A snowball in hell has a better chance.

PaulS:
Regardless, take a look at just this code:

  int mint = util::getMinute();

if (util::getMinute() - mint >= 5)



What is the likelihood that it will take 5 minutes or more between the two statements? A snowball in hell has a better chance.

I have moved below line out of loop function

 int mint = util::getMinute();

Have a look at how millis() is used to manage timing without blocking in several things at a time

Unless you need to synchronize with clock time I suspect there is no need to use an RTC.

...R

Robin2:
Have a look at how millis() is used to manage timing without blocking in several things at a time

Unless you need to synchronize with clock time I suspect there is no need to use an RTC.

...R

I tried the below but it doesn't seems to work,

  // 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.");
    }
  }

can anyone please look into: full latest code is here: arduinoProject/webServer_Displaying_everything_v01.ino at WIP · sanfx/arduinoProject · GitHub

seaurchin:
I tried the below but it doesn't seems to work,

How do you know?
What message do you get?
What line is it failing at?

...R

Robin2:
How do you know?
What message do you get?
What line is it failing at?

...R

Sorry bout the wrong message, i ran select query in db before 5 minutes after first run obviously there won't be, so cocked later and it does work thank you very much (y)

Thanks for the update. Glad to hear it is working.

...R