0

So i have my arduino and esp8266 wifi module. Everything is connected correctly and sending data to arduino lets me control connection via AT commands. My skletch looks like this:

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{

  if (Serial.available() > 0) {
    char ch = Serial.read();
    Serial1.print(ch);
  }
  if (Serial1.available() > 0) {
    char ch = Serial1.read();
    Serial.print(ch);
}

The code above lets me send commands and see response from esp. In spite of different response time and different answers I need to store response in variable when wifi module such a response creates. Unfortunatelly I cant do this because of Serial1.read() grabing only one char from Serial1.available() buffer instead of full buffer.

I tried approach like this:

  if (Serial1.available() > 0) {
      while (Serial1.available() > 0) {
        char ch = Serial1.read();
        Serial.print(ch);
        response = response.concat(ch);
       }
    } else {
      String response = "";
    }

So as long ther eis something in a buffer it is sent to response variable that concatens last char with itself. And later it can be searched via indefOf command for "OK" marker or "ERROR". But that doesnt work as intended :( It for example may print my variable 8 times (dont know why). I need full response from wifi module to analaze it for example to make the led on in my arduino board if proper command comes from wifi network, but also send some data if i press button on arduino to the network. Any ideas would be appreciated.

Kalreg.

Kalreg
  • 982
  • 1
  • 13
  • 27
  • 1
    I would like to recommend you search the web for "why not to use Strings in arduino". String can cause issues if you don't know why and how to properly use it. If you keep on using them then at least know what the pitfalls are. Try to implement char arrays (C strings). – Blurry Sterk Feb 26 '16 at 06:21

1 Answers1

1

Try this rather:

String response = ""; // No need to recreate the String each time no data is available
char ch; // No need to recreate the variable in a loop

while (Serial1.available() > 0) {
  ch = Serial1.read();
  response = response.concat(ch);
}

// Now do whatever you want with the string by first checking if it is empty or not. Then do something with it

Also remember to clear the buffer before you send a command like I suggested in your previous question: how to get AT response from ESP8266 connected to arduino

Community
  • 1
  • 1
Blurry Sterk
  • 1,595
  • 2
  • 9
  • 18
  • I tried your advice, but still i cant figure out what is wrong. I am familiar with javascript and php and all those char, long, and String things in simple c makes me crazy. Please look at my code: `void setup() { Serial.begin(9600); Serial1.begin(115200); Serial1.read(); Serial1.println("AT"); } void loop() { String response = ""; while (Serial1.available() > 0) { char ch = Serial1.read(); response += ch; } if ( response.length() > 0 ) { Serial.println("{" + response + "}"); } }` Reponse is: `{A} {T} { O} {K }` I'd expect: `{AT OK}` – Kalreg Feb 26 '16 at 23:13
  • Dont use String. Try to use a buffer with an initial length of say 200bytes. First fill it with 0x00 then keep an index and with the index, fill the buffer. – Mert Gülsoy Feb 28 '16 at 00:05
  • You are not doing as suggested. Your implementation is different. Don't shy away from learning how to do things properly. We did not suggest you stop using Strings for no reason. There is a reason and it is a very good one. – Blurry Sterk Feb 29 '16 at 05:27