Arduino Yún probelms with RAM?

Hi, I bought Arduino Yún and I´m trying to control a mobile robot over wifi. I created a Java program that sends me a string of 4 numbers that looks exactly like IPv4 address and program in Arduino that should receive message and transfer numbers to motors. It works but after receiving about 100 messages Arduino stops working and comes to life after resetting board. It stops on the same place every time. Did I fill up a RAM memory or my code is not good enough? I made shortcut of my code. Thanks for help.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;                     

void setup() {
  Serial.begin(9600);                      
  Bridge.begin();                        
  server.noListenOnLocalhost();        
  server.begin();                       
}

void loop() {
  wifi();
}

void wifi(){
  byte speed[4];
  int i=0;
  while(true){ 
    YunClient client = server.accept();
    client.setTimeout(100);   
    if (client){
      String message = client.readString();                       //HERE IT STOPS
      speed[0] = (message.charAt(7)-48)*100 + (message.charAt(9)-48)*10 + (message.charAt(11)-48);
      speed[1] = (message.charAt(15)-48)*100 + (message.charAt(17)-48)*10 + (message.charAt(19)-48);
      speed[2] = (message.charAt(23)-48)*100 + (message.charAt(25)-48)*10 + (message.charAt(27)-48);
      speed[3] = (message.charAt(31)-48)*100 + (message.charAt(33)-48)*10 + (message.charAt(35)-48);
      movement(speed[0],speed[1],speed[2],speed[3]);
      }
   }
}

It stops even when I move declaration of message as String into global variables.

First of all, you're using the String class which fragments the memory and results in failing allocations although theoretically enough memory is available.
Then you call readString() which just reads until there is an error or a timeout occurred. Was that really your intention?

Then make some examples of content sent to the Yun. In your code it looks like it's easily parsable, so it might not be necessary to read in the complete message but directly parse it as it comes in.

Message I have sent looks like this "192.093.140.001". Somehow I received samothing like this " p x 1 9 2 . 0 9 3 . 1 4 0 . 0 0 1". I received extra space between numbers and extra char at the beginning. So I have tried use an array of chars instead of one big string and it works until 256th message when it crushed again.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

YunServer server;                      
int counter = 0;

void setup() {
  Serial.begin(9600);                    
  Bridge.begin();                        
  server.noListenOnLocalhost();          
  server.begin();                       
}

void loop() {
  wifi();
}

void wifi(){
  byte speed[4];
  char message[64];
  byte i = 0;
  while(true){
    YunClient client = server.accept();
    client.setTimeout(100);   
    if (client){
      counter += 1;
      i=0;
      while (true){   
        if ((message[i] = client.read()) == -1){
          break;
        } 
        else {
          i += 1;
        }
      }
      i=0;
      while(true){
        if (message[i] = -1)
          break;
        Serial.print(message[i]);
        i += 1; 
      }
    }    
  } 
}

Have you upgraded the yun? Old versions of the linux side of the Bridge had a similar issue. See link in my signature

I have upgraded it as you said and it helped a little. Now it receives 256 messages and then it dies. Before it was sometimes only 80 messages. But my problem is not solved. Is there a function to erase the memory something like flush() ? Or is the allocating memory the possible way? Pointing on the one place in memory will force the Yún to save variables exactly on the same place every time and not to use and fill all the memory? Thanks.

From your sketch, I see a final "client.close()" is missing from inside the "if" and the end of it

It's always at the 256th message where it crashes? How fast are you sending the data to the Yun? You don't check for a buffer overflow.
If you're sending fast enough, it's possible that the Arduino reads more than one message and after some time the buffer fills up, memory gets corrupted and the Arduino freezes.

Problem solved :slight_smile: Federico was right. I forgot "client.stop()". I added it to my code and Yún received messages whole time while I was watching last episode of GoT. I am ashamed about that stupid mistake. Thank you guys.