0

I have a big problem. When I use the following code, at the time of reply (+IPD) I do not receive the full answer along with the text in the file or text in echo (php file) or text in json file.

How can I solve my problem?

enter image description here

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX,TX

#define DEBUG true

String mySSID = "*****";  // WiFi SSID
String myPWD = "*******"; // WiFi Password
String myHOST = "iothomeviewer.ir";
String myPORT = "80";

void setup()
{
  Serial.begin(115200); // communication with the host computer
  mySerial.begin(115200);
  Serial.println("");
  Serial.println("Remember to to set Both NL & CR in the serial monitor.");
  Serial.println("Ready");
  Serial.println("");

  espData("AT+RST", 1000, DEBUG);      // Reset the ESP8266 module
  espData("AT+CWMODE=1", 1000, DEBUG); // Set the ESP mode as station mode
  espData("AT+CWJAP=\"" + mySSID + "\",\"" + myPWD + "\"", 1000,
          DEBUG); // Connect to WiFi network

  delay(1000);
}

void loop()
{

  String sendData
      = "GET /ali.txt HTTP/1.1\r\nHost: iothomeviewer.ir\r\nContent-Type: "
        "text/html\r\nConnection: close\r\n\r\n";

  // OR     String sendData ="GET /light.json HTTP/1.1\r\nHost:
  // iothomeviewer.ir\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n";

  espData("AT+CIPMUX=1", 1000, DEBUG); // Allow multiple connections
  espData("AT+CIPSTART=4,\"TCP\",\"" + myHOST + "\"," + myPORT, 1000, DEBUG);
  espData("AT+CIPSEND=4," + String(sendData.length() + 4), 1000, DEBUG);
  espData(sendData, 7000, DEBUG);

  while (!mySerial.readStringUntil("IPD")) {
    delay(500);
  }
  delay(1000);

  while (mySerial.available()) {
    String c = mySerial.readString();
    Serial.println(c);
  }
  delay(1000);

  espData("AT+CIPCLOSE=4", 1000, DEBUG);
  delay(12000);
}

String espData(String command, const int timeout, boolean debug)
{
  Serial.println("     ");
  Serial.print("AT Command ==> ");
  Serial.print(command);
  Serial.println("     ");

  String response = "";
  mySerial.println(command);

  long int time = millis();

  while ((time + timeout) > millis()) {
    while (mySerial.available()) {
      char c = mySerial.read();
      response += c;
    }
  }

  if (debug) {
    Serial.print(response);
  }

  return response;
}
2
  • you stop reading on a gap between received characters. Serial is no so fast – Juraj Nov 28 '20 at 6:40
  • thanks for your help . My problem was solved when I changed the Serial baud rate to 9600 – Ali_Azure Nov 28 '20 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.