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