ESP8236 can not working as expected when i include SD Card on arduino

I am doing a small project , it was Arduino UNO with SD Card module, Relay module and ESP8236 WIFI module. The plan is Arduino UNO will read file inside the SDCard which have Access POINT value and Password value (APNAME:PASSWORD), Arduino will use it to connect to WIFI, then after wifi connected, it will request to website url to get response. The problem is in line 206 (script is below)

#include <SD.h>
#include <SPI.h>
#include <SoftwareSerial.h>
#define TX 7
#define RX 6
int relay = 4;
int CS_PIN = 10;

int countTrueCommand;
int countTimeCommand;
String hasilSD;
boolean found = false;
String HOST = "iot.myappxxx.com";
String PORT = "80";

SoftwareSerial esp8266(TX,RX);

String Data ="";
void setup() {
  Serial.begin(9600);
  esp8266.begin(9600);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  sendCommand("AT",5,"OK",false);
  esp8266.println("AT+UART_DEF=9600,8,1,0,0");
  if(CheckConnection()){
    Serial.println("WIFI Connected");
  }
}
void loop() {

String uri="/api/v1/iot/string?area_code=eg";

String getData=
"GET " + uri + " HTTP/1.0\n" +
"Host: " + HOST + "\n" +
"Accept: text/plain\n" +
"Content-Type: text/plain\n" +
"Connection: Keep-Alive\n" +
"\n";

 sendCommand("AT+CIPSTART=4,\"TCP\",\""+ HOST +"\","+ PORT,25,"OK",false);
 sendCommand("AT+CIPSEND=4," +String(getData.length()+4),25,">",false);
 sendCommand(getData,30,"OK",true);

 countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",10,"OK",false);
}


bool CheckConnection(){
  File file; char ch;
  String mywifi; String wifiid; String wifipass;         
  String received = ""; 
  pinMode(CS_PIN, OUTPUT);
  
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return false;
  }

  file = SD.open("wifi.txt");
  
  if (file)
  {
    Serial.println("File opened with success!");
  }else{
    Serial.println("File failed to open!");      
  }

  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      mywifi    = received;
      wifiid    = getValue(mywifi, ':', 0);
      wifipass  = getValue(mywifi, ':', 1);
      
    }
    else
    {
      received += ch;
    }

  }
  
  if(ConnectToWifi(wifiid, wifipass)){
      return true;
  }else{
      return false;        
  }
}


String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

bool ConnectToWifi( String AP, String PASS){
  for (int a=0; a<15; a++)
  {
    sendCommand("AT",5,"OK",false);
    sendCommand("AT+CWMODE=1",5,"OK",false);
    boolean isConnected = sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",5,"OK",false);
    if(isConnected)
    {
      return true;
    }
  }
}



String readSD(){
  File file;
  String mywifi;         
  String received = "";
  char ch;
  initializeSD();

  file = SD.open("wifi.txt");
  
  if (file)
  {
    Serial.println("File opened with success!");
  }else{
    Serial.println("File failed to open!");      
  }

  while (file.available())
  {
    ch = file.read();
    if (ch == '\n')
    {
      mywifi    = received;
      //wifiid    = getValue(mywifi, ':', 0);
      //wifipass  = getValue(mywifi, ':', 1);
      
      return mywifi;
    }
    else
    {
      received += ch;
    }
  }
            
}


 
void initializeSD()
{
  Serial.println("Initializing SD card...");
  pinMode(CS_PIN, OUTPUT);

  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
}

bool sendCommand(String command, int maxTime, char readReplay[],boolean isGetData) {
  boolean result=false;

  //Test Purpose
  //Serial.print(". at command => ");
  Serial.print(command);
  //Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);
    if(esp8266.find(readReplay))//ok
    {
      if(isGetData)
      {
        if(esp8266.find(readReplay))
        {
          Serial.println("Success : Request is taken from the server");
        }

        String section="header"; int count = 0;
        while(esp8266.available())
        {
            String line = esp8266.readStringUntil('\r');
            Serial.println(line); // Problem is here , can not get response

            if (section=="header") { // headers..

              if (line=="\n") { // skips the empty space at the beginning
                section="json";
              }
            }
            else {  // print the good stuff

                if(count == 0){

                if (line.indexOf("1") > 0) {
                  Serial.println("TUTUP");
                  digitalWrite(relay, LOW);                  
                }else{
                  Serial.println("BUKA");
                  digitalWrite(relay, HIGH);
                }

                }
                count++;
            }
        }
      }
      result = true;
      break;
    }
    countTimeCommand++;
  }

  if(result == true)
  {
    Serial.println("Success");
    countTrueCommand++;
    countTimeCommand = 0;
  }

  if(result == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }

  found = false;
  return result;
 }

, which esp8266 allready successfull got the response , but the result value (string) is return with not completed…

retromd:
I am doing a small project , it was Arduino UNO with SD Card module, Relay module and ESP8236 WIFI module.
[/quote]

When I see this, I expect problems. The ESP8266 module is a very poor excuse for WiFi on a UNO. (In fact, I’ve never heard of anyone happy with this arrangement).

If you need WiFi, start with a board that has WiFi on it. (My personal favorite is the Wemos D1 Mini).

You don’t say which SD card module you are using, but the ESP8266 already remembers the last AP and password connected and will reconnect when it powers up.

Also the ESP8266 on the Wemos D1 Mini is capable of performing HTTP GET Requests, and it has 512 bytes of EEPROM, so why again do you need the UNO and SD reader?

"Also the ESP8266 on the Wemos D1 Mini is capable of performing HTTP GET Requests"
I'd tried it, but i failed to trigger the 1 channel relay module, and it is working while i am using arduino uno.

I'd tried it, but i failed to trigger the 1 channel relay module, and it is working while i am using arduino uno.

The ESP8266 is a 3.3v device, depending on your relay module that may not be sufficient. Usually it is possible to modify the module in such a way that it does work. Please post a link to your unit. Also it is possible to use a TTL chip (7400-series) to increase the output voltage to 5v. from a 3.3v logic-level.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.