-1
#include <SoftwareSerial.h>
#define DEBUG true



SoftwareSerial esp8266(2, 3); // RX, TX

void setup() { // Open serial communications and wait for port to open:
Serial.begin(9600); //for monitoring purposes
esp8266.begin(9600);



  sendCommand("AT+CIFS+RST\r\n", 2000, DEBUG); // reset module
  sendCommand("AT+IPR=115200\r\n", 1000, DEBUG);
  sendCommand("AT+CWMODE=1\r\n", 1000, DEBUG); // configure as access point
  sendCommand("AT+CWJAP=\"ABCDEFG\",\"12345678\"\r\n", 3000, DEBUG);//connect to a network with name ABCDEFG with password 12345678
  delay(1000);
  sendCommand("AT+CIFSR\r\n", 1000, DEBUG); // get ip address
  sendCommand("AT+CIPSTA=\"192.168.43.16\"\r\n", 1000, DEBUG);
  sendCommand("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
  sendCommand("AT+CIPSERVER=1,6625\r\n", 1000, DEBUG); // turn on server on port 6625
  Serial.println("Server Ready");

  }

  void loop() { // run over and over
  if (esp8266.available()) {
  if (esp8266.find("+IPD,0,")) {
  delay(10);
  esp8266.find(":");
  delay(10);
  char letter = esp8266.read();
  Serial.print(letter); //for monitoring purposes
  //Gets the value/char from android app
   }
  }}

  String sendCommand(String command, const int timeout, boolean debug) {
  String response = "";
  esp8266.print(command); // send the read character to the esp8266
  long int time = millis();
  while ((time + timeout) > millis()) {
  while (esp8266.available()) {
  // The esp has data so display its output to the serial window
  char c = esp8266.read(); // read the next character.
  response += c;
  }
  }

  if (debug) {
   Serial.print(response);
  }
  return response;
  }
  • 1
    This isn't a "dump your code and make a statement" site. What is your question? This looks amazingly similar to your code at arduino.stackexchange.com/questions/52285/… – Nick Gammon Apr 29 '18 at 8:34
  • sory sir..thats also my post... that time...it already solve... sory sir.. – Wyeth Gamba Apr 29 '18 at 9:04
  • Questions like these are fine and well-answerable when they include a problem description, the expected behaviour, the actual behavior and the things you've tried, as it is highlighted in the How-To-Ask page (arduino.stackexchange.com/help/how-to-ask). But in this state no one can answer your question. If you solved it yourself you can also answer it yourself and accept it. – Maximilian Gerhardt Apr 29 '18 at 9:33
1

I don't know where you found (or how you came up with) this bit of code:

long int time = millis();
while ((time + timeout) > millis()) {
    while (esp8266.available()) {
        // The esp has data so display its output to the serial window
        char c = esp8266.read(); // read the next character.
        response += c;
    }
}

But it's horrible.

Not only is your time calculation completely wrong and will suffer a nasty death when millis() rollover occurs, but the whole concept of what you are doing there is bad.

The ESP8266 (assuming it's working, which hasn't been confirmed) sends a response that is terminated by a line-feed ("\n") character. You are ignoring that and just reading for a pre-defined amount of time (badly).

Instead you should be reading up until you get that "\n" character, or breaking if your timeout has been exceeded.

For instance:

String sendCommand(String command, const int timeout, boolean debug) {
    String response = ""; // ugh... strings... yuck
    esp8266.println(command);
    uint32_t ts = millis();
    while (millis() - ts < timeout) {
        if (esp8266.available()) {
            char c = esp8266.read();
            if (c == '\n') {
                #ifdef DEBUG
                    Serial.println(response);
                #endif
                return response;
            } else {
                response += c;
            }
        }
    }
    return String("timeout");
}

Better that that though is to ditch those nasty String objects. Instead pass a char* buffer to collect the response in:

int sendCommand(const char *command, uint32_t timeout, char *response, uint32_t rlen) {
    esp8266.println(command);
    response[0] = 0;
    uint32_t rpos = 0;
    uint32_t ts = millis();
    while (millis() - ts < timeout) {
        if (esp8266.available()) {
            char c = esp8266.read();
            if (c == '\n') {
                if (debug) Serial.println(response);
                return rpos;
            } else {
                if (rpos < rlen - 2) {
                    response[rpos++] = c;
                    response[rpos] = 0;
                }
            }
        }
    }
    return -1;
}

Then you use that like this:

char response[80];

if (sendCommand("AT+CIFS+RST", 1000, response, 80) == -1) {
    Serial.println("CIFS Timed out!");
}
if (sendCommand("AT+IPR=115200", 1000, response, 80) == -1) {
    Serial.println("IPR Timed out!");
}

And so on.

By the way - that AT+IPR=115200 is a bit of a stupid thing to do. You create a software serial port at 9600 baud (fine), then almost immediately instruct the ESP8266 to switch to 115200 baud instead. You don't then start communicating at 115200 baud, but stay at 9600 baud, so communication can never continue from that point on. Not only that, but SoftwareSerial is highly unstable at 115200 baud and is not recommended. Stick to 9600 baud.

  • thank you sir for the response sir.. i tried this change but the response is = cannot convert 'bool' to 'char*' for argument '3' to 'int sendCommand(const char*, uint32_t, char*, uint32_t)' i dont know why sir.. – Wyeth Gamba Apr 29 '18 at 14:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.