How to download data from database to arduino mega using ESP8266?

I’m using arduino mega for my project, i am able to communicate from arduino to database by sending data to the table. But i want to receive data from database so the arduino can receive a command from the website

i need to download data from mysql database to my arduino. i use framework codeigniter for my php file by the way.

here is the code i’ve done till now. I would be happy if you could help solve my problem.

#include <Wire.h> //library wire i2c
#include <RTClib.h> //library real time clock
#include <DS1307RTC.h>
//#include <ESP8266HTTPClient.h>
#include <TimeLib.h>
#include <SoftwareSerial.h> //library
#include <Sensirion.h> //library sht11
#include <LiquidCrystal_I2C.h> //library lcd
#define DEBUG true
#define RELAY_ON 0
#define RELAY_OFF 1
#define RELAY_1 7
#define ds1307_address 0x68 //alamat I2C RTC
#define dataPin 2 //pin data SHT11
#define clockPin 3 //pin clk SHT11
#define idx_SM 1 //index soil moisture
#define idx_SHT 2 //index sht 
#define idx_RTC 3 // index rtc
SoftwareSerial esp(10, 11); //RX, TX
Sensirion sht11 = Sensirion(dataPin, clockPin); //declare sht11
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); //declare i2c lcd
RTC_DS1307 rtc;// declare rtc
int sensorPin = A0; //pin soil moisture
int sensorValue = 0; //variabel soil moisture 
int percent = 0; //variabel soil moisture
int ledPin = 5;
float temperature; //variabel sht11
float humidity; //variabel sht11
float dewpoint; //variabel sht11
String tanggal; String jam;  //variabel RTC
int tanah;
float suhu; float udara; float dew;
unsigned long sekarang; //variabel penjadwalan
unsigned long interval = 1000; //variabel penjadwalan
unsigned long mininterval = 50; //variabel penjadwalan
unsigned long tadi = 0; //variabel penjadwalan
unsigned long t0,t1,t2,t3 = 0; //variabel penjadwalan

void setup() {
  //pinMode(LED1, OUTPUT);
  pinMode(RELAY_1, OUTPUT);
  //digitalWrite(RELAY_1, LOW);
  //untuk lcd
  lcd.begin(16,2);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("TUGAS AKHIR");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("Azam Fikri Ilham");
  delay(1000);
  lcd.clear();
  rtc.begin();// nggo rtc
  Serial.begin(9600);
  esp.begin(9600);
}

void loop() 
{
  sekarang = millis();
  sensorValue = analogRead(sensorPin);
  percent = convertToPercent(sensorValue);
  cetakdata();
  cetaklcd();
  bacasensor();
  SiramTanaman();
  sendDataHTTP();
  delay(1000);
}

int convertToPercent(int value)
{
  int percentValue = 0;
  percentValue = map(value, 1023, 465, 0, 100);
  return percentValue;
}

void cetakdata()
{
  if (sekarang>=2*interval)
  {
    if (sekarang-tadi>=interval)
    {
      tadi = sekarang;
      String data;
      data += String(tanah)+ " % ";
      data += String(suhu)+ " C ";
      data += String(udara)+ " % ";
      data += String(dew)+ " C ";
      data += String(tanggal)+ ", ";
      data += String(jam)+ ", ";
      Serial.println(data);
    }
  }
}


void cetaklcd()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("SM=");
  lcd.print((float)tanah,0);
  lcd.print("%");
  lcd.setCursor(8,0);
  lcd.print("C=");
  lcd.print((float)suhu,2);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("H=");
  lcd.print((float)udara,2);
  lcd.print("%");
  lcd.print("D=");
  lcd.print((float)dew,2);
  lcd.print("C");
}

void bacasensor()
{
  if (sekarang-t1>=interval+(mininterval*idx_SM))
  {
    t0+= interval;
    tanah = getTanah();
  }
  if (sekarang-t1>=interval+(mininterval*idx_SHT))
  {
    t1+= interval;
    suhu = getSuhu();
    udara = getUdara();
    dew = getDew();
  }
  if (sekarang-t2>=interval+(mininterval*idx_RTC))
  {
    t2+= interval;
    tanggal = getTanggal();
    jam = getJam();
  }
}

//Soil Moisture
int getTanah()
{
  int tanah = percent;
  return tanah;
}

//SHT11
float getSuhu()
{
 sht11.measure(&temperature, &humidity, &dewpoint);
  float suhu = temperature;
  return suhu;
}

float getUdara()
{
  sht11.measure(&temperature, &humidity, &dewpoint);
  float udara = humidity;
  return udara;
}

float getDew()
{
  sht11.measure(&temperature, &humidity, &dewpoint);
  float dew = dewpoint;
  return dew;
}

//nggo rtc
String cetak2digit(int angka) {
  if (angka >= 0 && angka < 10) {
    String angka_;
    angka_ += "0";
    angka_ += String(angka, DEC);
    return angka_;
  }
  String angka_;
  angka_ = String(angka, DEC);
  return angka_;
}
String getJam(){
  String jam;
  if (!rtc.isrunning()){
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    jam = "-";
  }
  else {
    DateTime now = rtc.now();
    jam += cetak2digit(now.hour())+ ":";
    jam += cetak2digit(now.minute())+ ":";
    jam += cetak2digit(now.second());
  }
  return jam;
}
String getTanggal(){
  String tanggal;
  if (rtc.isrunning()){
  DateTime now = rtc.now();
  tanggal += cetak2digit(now.day())+ '/';
  tanggal += cetak2digit(now.month())+ '/';
  tanggal += now.year();
  }
  else tanggal = "-";
  return tanggal;
}


//start ESP
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    esp.print(command); // send the read character to the esp8266
    long int time = millis();
    while( (time+timeout) > millis())
    {
      while(esp.available())
      {
        // The esp has data so display its output to the serial window 
        char c = esp.read(); // read the next character.
        response+=c;
      }  
    }
    if(debug)
    {
      Serial.print(response);
    }
    return response;
}

void sendDataHTTP() //sending data to database
{
  Serial.println("Kirim Data");
  sendData("AT+RST\r\n",1000,DEBUG); 
  sendData("AT+CWMODE=3\r\n",1000,DEBUG); 
  sendData("AT+CIPMODE=0\r\n",1000,DEBUG); 
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); 
  String ssid = ("\"TugasAkhir\"");                                     
  String pass = ("\"tugasakhirazam\"");                             
  String connectstring = "AT+CWJAP=";                               
          connectstring += ssid;                                     
          connectstring += ",";                                    
          connectstring += pass;                                   
          connectstring += "\r\n";                                 
  sendData(connectstring,4000,DEBUG);            
  sendData("AT+CIFSR\r\n",1000,DEBUG); 
  sendData("AT+CWJAP?\r\n",1000,DEBUG); 
  String  mod      = ("4"); 
  String  id       = ("\"TCP\"");                                     
  String  domain   = ("\"www.penyiramantanaman.hol.es\"");  
  String  port     = ("80");                             
  String  connectcomand = "AT+CIPSTART=";                              
          connectcomand += mod; 
          connectcomand += ",";  
          connectcomand += id;                                   
          connectcomand += ",";                                    
          connectcomand += domain; 
          connectcomand += ",";  
          connectcomand += port;                                    
          connectcomand += "\r\n";                                 
  sendData(connectcomand,1000,DEBUG);
  sendData("AT+CIPSEND=4,155\r\n",100,DEBUG);          
  sendData("GET /index.php/Terima?suhu=",100,DEBUG);
  //sendData("&suhu=",10,DEBUG);  
  esp.print(suhu);
  sendData("&udara=",10,DEBUG);
  esp.print(udara);
  sendData("&tanah=",10,DEBUG);
  esp.print(percent);
  sendData("&dew=",10,DEBUG);
  esp.print(dew);
  sendData("&jam=",10,DEBUG);
  esp.print(jam);
  sendData("&tanggal=",10,DEBUG);
  esp.print(tanggal);
  sendData(" HTTP/1.1\n",100,DEBUG);
  sendData("Host: www.penyiramantanaman.hol.es \r\n",1000,DEBUG);
  sendData("\n",5000,DEBUG);
  esp.println();
  delay(1000);
}



//PENGKONDISIAN AIR
void SiramTanaman()
{
  if (tanah<=461)
  {
    digitalWrite(RELAY_1,HIGH);
  }
  else
  {
    digitalWrite(RELAY_1,LOW);
  }
}

i am able to communicate from arduino to database by sending data to the table.

What you are doing is executing a script on a server, and supplying some data. What that script actually does is not relevant.

i need to download data from mysql database to my arduino.

So, the first thing you need to do is to develop the script that runs on the server. That script needs to get the data from the database, based on whatever input it needs.

You can test that script from any browser.

When that works, you can make the Arduino/ESPxxxx make the GET request to execute that script.

I would be happy if you could help solve my problem.

If only we knew what the problem was...