reading empty string

Before posting tried many variations with for loops, serial.peek() etc. couldnt make it work.
Looked at esp libraries and what I found were using Strings. not cstrings.
Here is my function which search string for endmarker ‘\n’ then compares received string with const string. Arduino is sending to ESP at commands and waiting for reply.

ESP is replying this way:
1.echoed command \n
2.empty string i.e. \r\n
3.reply \n

echoed command is same command which was sent but echoed back thats the way esp is responding.
its possible to switch echoing off but first 2 commands (at+rst and at) need to verify that ESP is actually working and after that echoing off (ate0) can work.

how to modify this code to read and wait to fully read until “reply” ?
I`m doing my best to deal with char arrays and not use Strings. With Strings everything is very easy.

char ans=“OK”

byte receive_command(char ans[]) {
  char c = 0;
  char a[input_buffer_size] = {0};
  byte b = 0;
  unsigned long _time = 0;
  const int max_wait_time = 10000;
  byte i = 0;
  _time = millis();

  while (!mySerial.available()) {
    if ((millis() - _time) > max_wait_time) return b;
  while ((c = != '\n') {
    if (c >= 32 && c <= 127) { // check if ascii if not dont store
      a[i] = c;
  a[i + 1] = '\0';
  if (b = strstr(a, ans)) b = 1;
  return b;

and this is sending function did in do while loop.

do {
    if (timout > max_attempts) return text_response;
    for (byte i = 0; i <(strlen(_command)); i++) {
    text_response = receive_command(ans);

  } while (!text_response);

The way that you have written the while loop which tests .available(), you are only guaranteed that there is ONE character in the buffer if .available() returns non-zero. So, the second and subsequent might return -1 instead of a valid character. You have to rewrite those loops so that when you read a character you are sure that there was one available.


You mean checking every time while available then calling

I encountered with that problem solved by putting 1ms delay.

Will correct that loop though thanks for right direction.

But how to skip that empty line?

Output after reset command: Echoed at+rst Empty line OK Then these lines:

 ets Jan  8 2013,rst cause:4, boot mode:(3,7)

wdt reset
load 0x40100000, len 24444, room 16
tail 12
chksum 0xe0
ho 0 tail 12 room 4
load 0x3ffe8000, len 3168, room 12
tail 4
chksum 0x93
load 0x3ffe8c60, len 4956, room 4
tail 8
chksum 0xbd
csum 0xbd


So i have to read all the chars make string compare with predefined answer “ok” and continue checking till “ready” included. Then buffer will be empty.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.


Yes i have read your great tutorial And i used that principle in one of my projects arduino to arduino communication where startmarker and endmarker were present.

In this case there is only endmarker.

surepic: In this case there is only endmarker.

robin also has an example for that in the same thread :)