i cant running two loop in my arduino uno

i use arduino uno, ethernet shield, dht11 and relay 5v
first i want to connect mysql db to control my relay and that is successful
secondly i want to send my dht11 data to mysql db and that are successful too..
but if i try to combine that two code,, i just get hang in serial monitor

this is my code:

#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <Dns.h>
#include <MySQL_Cursor.h>
#include <DHT.h>
DHT dht(8, DHT11);
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char hostname[] = "***.com";
char user[] = "*******";
char password[] = "*****";
char query[] = "SELECT * FROM ******* where user='***'";

EthernetClient client;
IPAddress server_ip;
MySQL_Connection conn((Client *)&client);
DNSClient dns_client;
MySQL_Cursor cur = MySQL_Cursor(&conn);

void setup() {
  Serial.begin(115200);  
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(5, HIGH);
  while (!Serial);
  dht.begin();
  Ethernet.begin(mac_addr);
  // Begin DNS lookup
  dns_client.begin(Ethernet.dnsServerIP());
  dns_client.getHostByName(hostname, server_ip);
  Serial.println(server_ip);
  // End DNS lookup
  Serial.println("Connecting...");
  if (conn.connect(server_ip, 3306, user, password)) {
    Serial.print("connected!");
  }
  else
    Serial.print("Connection failed.");
}

void loop() {
  delay(1000);
sendDHT();
Relay();
}
void sendDHT()
{
char INSERT_SQL[] = "INSERT INTO *****.*** (user,themp,humidity) VALUES ('%s',%d,%d)";
int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
char query[128];
 
Serial.print("Recording data.");
    sprintf(query, INSERT_SQL, "***", temperature, humidity);
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Execute the query
  cur_mem->execute(query);
  Serial.print(query);
  // Note: since there are no results, we do not need to read any data
  // Deleting the cursor also frees up memory used
  delete cur_mem;
  }
void Relay()
{
    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  cur_mem->execute(query);
  column_names *cols = cur_mem->get_columns();
  row_values *row = NULL;
    do {
    row = cur_mem->get_next_row();
    if (row != NULL) {
        String pin_value=row->values[5];
        String pin_number=row->values[2];
        const int number=pin_number.toInt();
        if(pin_value=="LOW")
        {
          digitalWrite(number, HIGH);
          Serial.print("nyuntik high ke pin");
          Serial.print(number);
          }
          else if(pin_value=="HIGH")
          {
          digitalWrite(number, LOW);
          Serial.print("nyuntik low ke pin");
          Serial.print(number);
            }
    }
    }
  while (row != NULL);
  delete cur_mem;
  }

that is make in serial monitor hanging in connecting...
but if i use sendDHT() only or Relay() only that is work!
what is my source of problem??
sram out of memory or else??
answer please, i hoping for your answer :smiley:

I don't have all those libraries, so I can't compile. When you compile, what does the IDE tell you about how much memory is used? I'm guessing the SQL library and all those String variables are eating up a ton of memory.

Also, where does the Serial monitor 'hang'? Does anything print out? You could place a few more debug statements in the code to try and determine exactly where it is hanging.

this is are displayed in serial monitor

..**.185
Connecting...

and this is message while i compile code

Sketch uses 22592 bytes (70%) of program storage space. Maximum is 32256 bytes.
Global variables use 1246 bytes (60%) of dynamic memory, leaving 802 bytes for local variables. Maximum is 2048 bytes.

If that is where it stops, it looks like your connection is not successful

but if i just running one loop that is success..
sendDHT ()
or Relay ()
that is the problem...

Why get the column names each time the query is run? Don't you already know what the columns are going to be called? Is it really necessary to do this?

What happens to the memory containing the column names? How is it freed? I suppose the delete on the cursor takes care of that. Meh, ok.

Why use sprintf to rewrite the query text? Doesn't this library support prepared statements? A pair of prepared statements would be a far better approach, if the library supports it.

Where are the transaction boundaries? Is the connection automatically autocommit? Do you need to pass the connection something to make it autocommit? Is the problem that the insert leaves an uncommitted transaction, and the select decides to not work as a result?

Does cur_mem->extecute() return a dynamically-allocated value that needs to be cleaned up?

The loop to read the data would be nicer (to a C programmer) thus:

row_values *row;
while(row = cur_mem->get_next_row()) {
  // rest of the stuff goes here 
}

Quibbles aside: look at transaction boundaries, and consider using stored procedures.

thanks for code :slight_smile:
because if worth i want to be a c programmer too.

for the case i deleted this in top of my code

MySQL_Cursor cur = MySQL_Cursor(&conn);

and then i use cur_mem for my first function, and cur_insert for my second function and that's work. :slight_smile:

once again thanks very much for member response in this forum. :slight_smile: