ESP8266+Arduino - Response from api gets cut

82 views Asked by At

I'm using an ESP8266 connected to an Arduino Uno via SoftwareSerial to make a get request to a telegram api. The data with AT-commands prints correct, but the response from the server is incomplete (the same way all the time).

This is my code:

#include <SoftwareSerial.h>
 
SoftwareSerial mySerial(8, 9);
 
#define WIFI_SERIAL mySerial


String command(const char *toSend, unsigned com_delay, unsigned milliseconds) {
  String result;
  Serial.print("Sending: ");
  Serial.println(toSend);
  WIFI_SERIAL.println(toSend);
  unsigned long startTime = millis();
  Serial.print("Received: ");
  delay(com_delay);
  while (millis() - startTime - com_delay < milliseconds) {
    if (WIFI_SERIAL.available()) {
      delay(1);
      char c = WIFI_SERIAL.read();
      Serial.write(c);
      result += c;  // append to the result string
    }
  }
  Serial.println();  // new line after timeout.
  return result;
}

void setup()
{
  Serial.begin(9600);
  while (!Serial);

  Serial.print("Serial init OK\r\n");
  WIFI_SERIAL.begin(9600);
  char concom[] = "AT+CWJAP_DEF=\"---\",\"---\"";
  char sendcom[] = "AT+CIPSTART=\"TCP\",\"api.telegram.org\",443";
  char api[] = "GET /botxxxxxxxxxxxxxxxxxxxx/ HTTP/1.1\nHost: api.telegram.org\nAccept: application/json\nCache-Control: no-cache\n\n";
  
  //command("AT+CWMODE=1", 10000, 100);
  
  
  command(concom, 7000, 500);
  command(sendcom, 10000, 500);
  command("AT+CIPSEND=139", 3000, 500);
  Serial.println("+"+command(api, 40000, 15000));
}


 
void loop()
{
  while(WIFI_SERIAL.available())
  {
      char line = WIFI_SERIAL.read();        // read one char at a time
      delay(1);                      // prevent freezing
      Serial.print(line);
      // if (line == '\0') continue;    // terminate the `while` when end of the data
  }
}

This is answer from server:

Serial init OK
00:02:08.438 -> Sending: AT+CWJAP_DEF="---","---"
00:02:08.469 -> Received: T+CWJAP_DEF="---","----"

00:02:15.526 -> busy s...
00:02:15.531 -> 
00:02:15.531 -> Recv 1
00:02:15.959 -> Sending: AT+CIPSTART="TCP","api.telegram.org",443
00:02:16.026 -> Received: 
00:02:26.021 -> Sending: AT+CIPSEND=139
00:02:26.082 -> Received: AT+CIPSTART="TCP","api.telegram.org",443

00:02:29.090 -> CONNECT
00:02:29.090 -> 
00:02:29.090 -> OK
00:02:29.090 -> AT+CI
00:02:29.551 -> Sending: GET /botxxxxxxxxxxxxxx/ HTTP/1.1
00:02:29.613 -> Host: api.telegram.org
00:02:29.646 -> Accept: application/json
00:02:29.679 -> Cache-Control: no-cache
00:02:29.679 -> 
00:02:29.679 -> 
00:02:29.779 -> Received: 
00:03:09.792 -> +
00:03:09.853 -> 
00:03:09.853 -> busy s...
00:03:09.853 -> 
00:03:09.853 -> Recv 139 bytes
00:03:09.853 -> 
00:03:09.853 -> SEND OK
00:03:09.858 -> 
00:03:09.858 -> +IPD,595:HTTP/1.1 4 // <---- HERE IS INCOMPLETED ANSWER

I don't know why it's can happen

1

There are 1 answers

0
abdullah-1307 On

Your code seems to be good, However, I would recommend you modify your code a little bit.

  • Try to use delay(1000); after sending AT commands.

  • Since, there are chances that ESP8266 can send asynchronous messages, make sure you handle them properly like this:

     void loop()
     {
         while (WIFI_SERIAL.available())
         {
             char line = WIFI_SERIAL.read();
             delay(1);
             Serial.print(line);
             if (line == '\n')
             {
                 // Check for asynchronous response
                 if (WIFI_SERIAL.find("+IPD,"))
                 {
                     // Handle asynchronous response here
                 }
             }
         }
     }