Am I interfacing with my ESP8266 correctly here? (Using Arduino to do it)

I’m working on a project to send temperature and moisture data to Thingspeak. I’ve got my Arduino communicating with my ESP8266 and it seems to work fine except for a few times where the Arduino doesn’t detect the “OK” response from the ESP. I’m not sure if it’s the way I’m going about the communication or if there’s some trick in getting it right or maybe something I’m missing.

I’m using the softwareSerial library to talk with the ESP. I have the Arduino sending commands to the ESP by using the .print method. I’ve got no trouble sending commands but receiving responses sometimes doesn’t work. After most commands the ESP should send back “OK”. I’m trying to detect when it does this by using the .find method of the softwareSerial library.

For example:

I want to send the AT command to see if the ESP is working so I do:

  softSerial.print(atKey);
    delay(800);

I’m not sure if I need the delay or if it helps or anything. This is followed by:

 if (softSerial.find(okKey)){
      Serial.println("AT WORKING OK");
      Serial.println(setModeKey);
      softSerial.print(setModeKey);
      delay(500);

If it finds the “OK” response, it lets me know in the serial monitor and prints the next command and waits again for a response.

The problem is that sometimes, the Arduino just doesn’t detect a response from the ESP. I have it written so it retries again after sometime and resets itself so it’s not that big of an issue, but It would be nice to have it detect the “OK” response each time.

Is there some trick to this that I’m not implementing? Or is there a better way of communicating with the ESP using the Arduino?

Here’s my function for sending data to Thingspeak:

int wifiSend(){
  int tries = 0;
  int sendTries = 0;

  
  
  //All the next lines print the various commands to send data. If there is no response back, the while loop restarts and
  //a message is printed in the serial monitor stating what failed.
  while (true){

    if (tries == 3) {
     Serial.println("TOO MANY TRIES TO SEND, RESETTING UNIT");
    return 0;
      }
    
    tries ++;

    
    Serial.print(">>>>>>>>>>>>SENDING DATA TRY:");
    Serial.println(tries);

    float temp = measureTemp();
    float light = measureLight();
    float moist = SoilMoisture();

    

    Serial.println(startTCPKey);
    softSerial.print(startTCPKey);
    delay(500);

      if (softSerial.find(okKey)){
        Serial.println("TCP CONNECTED OK");
        Serial.println(dataLenKey);
        softSerial.print(dataLenKey);
        delay(500);
      
        if (softSerial.find(okKey)){
          Serial.println("CIPSEND RECEIVED OK");
          Serial.print(getUpdateKey);
          Serial.print(apiKey);
          Serial.print(field1Key);
          Serial.print(temp);
          Serial.print(field2Key);
          Serial.println(moist);
          softSerial.print(getUpdateKey);
          softSerial.print(apiKey);
          softSerial.print(field1Key);
          softSerial.print(temp);
          softSerial.print(field2Key);
          softSerial.println(moist);

          delay(500);

          while (softSerial.find(sendOkKey) != 1){
            sendTries = 1;

            if (sendTries == 20){
              Serial.print("FAILED TO SEND DATA, 20 TRIES");
              break;
            }
            
            softSerial.println();
            delay(200);
          }

           
              Serial.println("DATA SEND OK");
              softSerial.print("AT+CIPCLOSE");
              //Serial.println("TURNING OFF ESP");
              delay(200);
             // digitalWrite(espEnablePin,LOW); //Turns off power to ESP
              break;
            

    
   
   }
   else{
            Serial.println("CIPSEND FAILED RETRYING...");
            continue;
            }
  
  }
  else{
          Serial.println("TCP FAILED TO CONNECT RETRYING...");
          continue;
        }
  
    }

return 1;
 
}

Here’s my main code with all my defines:

//Program that utilizes the ESP8266-12 Wifi board to send temperature data to thingspeak
//Lots of the code is mainly for serial debugging to check for errors and fine tune the delays
//Lots of the .find methods are time sensitive so delays might need to be tweaked if there are problems

#include <SoftwareSerial.h>
#include "LowPower.h"

//Various ESP8266 commands, these might change for different firmwares/versions of the board.
#define atKey "AT\r\n"
#define setModeKey "AT+CWMODE=1\r\n"
#define connectWifiKey "AT+CWJAP=\"LanBeforeTime\",\"adamadam\"\r\n" //first quotes: SSID name, second quotes: SSID password
#define okKey "OK"
#define setCMKey "AT+CIPMUX=0\r\n" 
#define startTCPKey "AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80\r\n"
#define dataLenKey "AT+CIPSEND=80\r\n" //change the number to the length of the data you're sending or more.
#define getUpdateKey "GET /update?key=" //first part "get" of keyword
#define apiKey "7SIJZ63RD612UQZG" //write api key from thingspeak
#define field1Key "&field1=" //change field number as needed or add more.
#define field2Key "&field2="
#define field3Key "&field3="
#define field4Key "&field4="
#define sendOkKey "SEND OK"
#define atCloseKey "AT+CIPCLOSE"


#define moisture_input 2
#define divider_top 2
#define divider_bottom 3



//Use either of these variables in the sleep() function. To use minutes, convert to seconds.
const int minutesToSleep = 15; 
const int secondsToSleep = 10;

SoftwareSerial softSerial(10, 11); //RX , TX ,connect TX of esp8266 to RX of arduino and vice-versa

int espEnablePin = 4; //Pin connected to NPN transistor to turn on and off ESP during sleep
int lm35Pin = 0; //Pin for temperature probe
int photoPin = 1; //Pin for photo resistor

void setup() {

  pinMode(espEnablePin,OUTPUT);
  Serial.begin(9600);
  softSerial.begin(9600);

}

void loop() {

  resetESP(); //turns ESP off and back on to reset it
  wifiSetup(); //sets modes and connects to network
  
  while (wifiSend() == 1){   //measures temperature and send data to thingspeak, if wifiSend succeeds, return 1 and go to sleep
    //sleep(); //puts Arduino into power down sleep mode, draws about 19mA
    delay(60000);
    
  }


  
}