i want to use 3 function in this case, controlling ir remote, sending dht data, and controlling relay with mysql connector lib.
but if i run 2 function or 3 function after that my code is broke or cant run or nothing response.
i try to displaying free memory in serial monitor and yes, every my code is running, my free memory is lose.
how to fix it?
because i want to running this code for 24H non stop every day.
and this is my code
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <Dns.h>
#include <MySQL_Cursor.h>
#include <IRremote.h>
#include <DHT.h>
#include <MemoryFree.h>
DHT dht(6, DHT11);
IRsend irsend;
uint16_t rawPower[68]={
4510, 4498, 558, 1682, 554, 1682, 554, 1682,
558, 566, 554, 566, 554, 566, 550, 566,
554, 566, 554, 1682, 554, 1686, 554, 1682,
554, 566, 554, 566, 554, 566, 554, 566,
554, 566, 550, 566, 554, 1682, 558, 566,
554, 566, 550, 566, 554, 566, 554, 566,
554, 566, 554, 1682, 554, 566, 554, 1682,
554, 1682, 558, 1682, 554, 1682, 558, 1682,
554, 1682, 554, 1000};
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char hostname[] = "*******.com";
char user[] = "*******";
char password[] = "*******";
char query[] = "SELECT * FROM ******* where user='*******'";
char INSERT_SQL[] = "UPDATE *******.dynamic SET themp= '%d', humidity= '%d' WHERE user= '%s'";
char queryInsert[128];
char queryIR[] = "SELECT * FROM *******.IR where user='*******'";
char INSERT_IR_SQL[] = "UPDATE *******.IR SET value ='%s' WHERE user ='%s'";
char queryInsertIR[128];
EthernetClient client;
IPAddress server_ip;
MySQL_Connection conn((Client *)&client);
DNSClient dns_client;
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);
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.println("CONNECTED!");
}
else
Serial.println("Connection failed.");
}
void IR_void()
{
MySQL_Cursor *cur_IR = new MySQL_Cursor(&conn);
MySQL_Cursor *cur_insertIR = new MySQL_Cursor(&conn);
sprintf(queryInsertIR, INSERT_IR_SQL, "NO","*******");
cur_IR->execute(queryIR);
column_names *coloums = cur_IR->get_columns();
row_values *rowIR;
while(rowIR = cur_IR->get_next_row())
{
if (rowIR != NULL) {
String rawValue=rowIR->values[3];
String catIR=rowIR->values[2];
if(rawValue=="POWER"){
irsend.sendRaw(rawPower, 68, 38); //send raw data at 38KHz frequency
cur_insertIR->execute(queryInsertIR);
delete cur_insertIR;
break;
}
}
}
delete cur_IR;
}
void loop() {
IR_void();
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query);
column_names *cols = cur_mem->get_columns();
row_values *row;
while(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);
}
else if(pin_value=="HIGH")
{
digitalWrite(number, LOW);
}
}
}
delete cur_mem;
int temperature = dht.readTemperature();
int humidity = dht.readHumidity();
sprintf(queryInsert, INSERT_SQL, temperature, humidity, "*******");
MySQL_Cursor *cur_insert = new MySQL_Cursor(&conn);
cur_insert->execute(queryInsert);
delete cur_insert;
Serial.print("freeMemory()=");
Serial.println(freeMemory());
}