Using find() after receiving data from ESP8266 works intermittently

Hello all,
I’ve been working on a project using the ESP8266 with an Arduino UNO for a few days now. The hope is that the Arduino, sending AT commands to the ESP-01, can start a softAP that requests a form, and then the Arduino can read the form that is sent back. However, when I try to use find() after receiving the data, it doesn’t work. I receive “IDP” but everything after that doesn’t read. I should add that when I first request the webpage, find(“GET / HTTP”) works as expected, but after that find(“GET /getData?”) or even “GET” doesn’t work. Any ideas?

#include <EEPROM.h>
#include <SoftwareSerial.h>

SoftwareSerial wifiModule(2, 3);

boolean first;

unsigned long currentTime = millis();
unsigned long previousTime = 0;

void setup() {
  EEPROM.begin();
  
  Serial.begin(9600);
  wifiModule.begin(9600);

  Serial.println("Resetting EEPROM");

  if(EEPROM.read(96) == 0) {
    for(int i = 0; i < EEPROM.length(); i++) {
      EEPROM.write(i, 0);
    }

    Serial.println("Reset EEPROM successfully");
  }

  //checks if SSID is null, if so start setup process
  if(EEPROM.read(0) == 0) {
    first = true;
  } else {
    first = false;
  }

  Serial.println();
  
  if(first == true) {
    sendData("AT+CWMODE=2", 50); // configure as access point
    sendData("AT+CWSAP=\"testserver\",\"hello12345\",5,3", 100); // start ap

    sendData("AT+CIPMUX=1", 50); // needed for server to work properly
    sendData("AT+CIPSERVER=1,80", 50); // turn on server on port 80

    sendData("AT+CIFSR", 50);
    
  }
}

String sendData(String command, int responseTime) {
  String response = "";
  wifiModule.println(command);
  long int time = millis();
  
  while ((time+responseTime) > millis()) {
    while(wifiModule.available()) {
      char c = wifiModule.read();
      response += c;
    }
  }
  
  Serial.println("Response: " + response);
  return response;
}

void loop() {
  if(first == true) {
    if(wifiModule.find("+IPD,")) {
      delay(1000); //delay required for 9600 baud
      int connectionId = wifiModule.read() - 48; // -48 because .read() return decimal, 0 in decimal is 48
      Serial.print("IPD with id ");
      Serial.println(connectionId);
      
      if(wifiModule.find("GET / HTTP")) {
        Serial.println("Client detected");
        
        String webpage = "<!DOCTYPE html><html><body><form action='getData' method='GET'><input type='text' name='ssid' placeholder='Network'><input type='password' name='pass' placeholder='Passcode'><input type='submit'></form></body></html>";
    
        String cipSend = "AT+CIPSEND=";
        cipSend += connectionId;
        cipSend += ",";
        cipSend += webpage.length();
        cipSend += "\r\n";
        
        sendData(cipSend, 1000);
        sendData(webpage, 2000);
        
        String closeCommand = "AT+CIPCLOSE="; 
        closeCommand+=connectionId; // append connection id
        closeCommand+= "\r\n";
        
        sendData(closeCommand, 3000);
      
      } else if(wifiModule.find("GET /getData?")) {
        Serial.println("Received Data");
        
        String webpage = "<!DOCTYPE html><html><body><h1>Successful transmission, thank you</h1></body></html>";
        
        String cipSend = "AT+CIPSEND=";
        cipSend += connectionId;
        cipSend += ",";
        cipSend += webpage.length();
        cipSend += "\r\n";
        
        sendData(cipSend, 1000);
        sendData(webpage, 1000);
        
        String closeCommand = "AT+CIPCLOSE="; 
        closeCommand+=connectionId; // append connection id
        closeCommand+= "\r\n";
        
        sendData(closeCommand, 3000);

        delay(1000);
      }
    }
  }

  //for debugging purposes
  while(Serial.available()) {
    wifiModule.write(Serial.read());
  }

  while(wifiModule.available()) {
    Serial.write(wifiModule.read());
  }
}

Thanks!

you could use a library. WiFiEsp for old AT firmware or my WiFiEspAT with new AT firmware versions

  long int time = millis();
 
  while ((time+responseTime) > millis()) {

Two problems:

  • millis() returns ‘unsigned long’ so ‘time’ should be ‘unsigned long’.
  • Comparison should always subtract older time from newer time.

The corrected version would be:

  unsigned long int time = millis();
 
  while (millis() - time < responseTime) {

Alright, I've changed the ms counter, but there still is no change. I might use a library but I've made it this far so I want to see if I can get this to work. Any Ideas?

EverythingTechPro:
Alright, I've changed the ms counter, but there still is no change. I might use a library but I've made it this far so I want to see if I can get this to work. Any Ideas?

sorry, this far is not far enough. see here what does it take to process the response of esp AT firmware:

Alright, I'll try that and let you know what I get.