Arduino Yun serial connection problems

I've been struggling with this issue a lot lately and I need help to understand and make my project working.
I am using an Arduino Yun.
I need to be sampling from a microphone at 5000 kHz and send data to the AR9331 for further programming. The Analog circuit has been tested and gives values properly from 0 to 5V. However, I cannot get the code to be working properly

This is the code for the ATmega.

int sensorPin = A0;
int sensorValue;
int ledPin = 13;
bool coughMode = false;


void setup() {
  Serial1.begin(120000);
  while(!Serial1) {};
}

void loop() {
// sample at 5kHz
  coughMode = !coughMode;
  if (coughMode)
    sensorValue = analogRead(sensorPin);
  else
    return;

  if(sensorValue > 999)
    sensorValue = 999;

  Serial1.print(sensorValue,DEC);
  delayMicroseconds(200);
 
  if(sensorValue > 800)
    digitalWrite(ledPin, HIGH);
  else
    digitalWrite(ledPin, LOW);

}

This is the python script

import httplib,urllib
import sys
import datetime
import serial
import pycurl
from array import array

def findHigh(array):
    for item in array:
        if(item > 700):
            return True
    return False

def sendToServer():
    s = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');
    payload = {'date' : s}
    postdata = urllib.urlencode(payload)
    resp = urllib.urlopen('some url here', data=postdata)
    print resp.code


while True:
    intarray =  array('i')
    result = ""
    while True:
 	ser = serial.Serial(sys.argv[1], 120000, timeout = None)
 	
      	if(ser.inWaiting() >= 3):
            c = ser.read(3) 
	    print c
	    try:
    		result = int(c)
    		intarray.insert(result)
		print result
    	    except Exception, e:	
    		print "Cast not working"
            ser.close()   
    	    if (len(intarray) == 5000):
    	        print (intarray)
                if(findHigh(intarray) == True):
                    sendToServer()
                intarray = []

I run the python script: python client.py /dev/ttyATH0 > file.txt
and it currently doesn't print anything. (however, sometimes it would print integer values - but from what I tested then, they were not really printed fast enough and the ADC would lose some of the samples -).

I chose 250 000 baud rate since there are 5000 samples to be sent in a second: 5000 samples * 6 chars * 8bits/char = 240 000 bits to be sent in a second. I know that based on the documentation, that makes the board unhappy, but based on what I read on forums, it should still be working.

The idea is to record a second on the AR9331 and then do some processing and send the data to the server. For now, I want that whenever in that 1s, there is a value from the ADC that is higher than 700 to send the current timestamp to the server.

Mentions:
1.
Many times I receive this errors:
File "client.py", line 30, in
c = ser.readline(size = None, eol = '\n')
File "/usr/lib/python2.7/site-packages/serial/serialutil.py", line 62, in readline
c = self.read(1)
File "/usr/lib/python2.7/site-packages/serial/serialposix.py", line 364, in read
buf = os.read(self.fd, size-len(read))
OSError: [Errno 11] Resource temporarily unavailable

But sometimes running it again doesn't give this errors. It really happens sporadically.

  1. I tested the internet code. the function sendToServer() works perfectly

I appreciate any help. I spent very much time and energy trying to make this connection working properly, and now the deadline is approaching.

Thank you in advance

LATER EDIT:
The code above has been updated with what I currently have.
the fact that I am using only 3 digits should allow me to use a smaller baud rate. 5000 38 = 120 000

Now I do receive data, but it doesn't arrive as an int. The string c that is printed looks like: "$$$", "I$", "$&i", .. and other random things.

To be mentioned:

  • The same code worked perfectly fine for some seconds and then when I ran it again, it decided not to.
  • I haven't received any errors like the one posted above anymore.
  • the debugging Pin lights up properly (when it receives a value higher than something)
  • I tried all sorts of delays knowing that the print happens asynchronously, but nothing really worked

Disable the serial console, make clean connection and python code will run fater

http://forum.arduino.cc/index.php?topic=191820.msg1421548#msg1421548

https://forum.openwrt.org/viewtopic.php?id=15165

The catch of disabled serial console (device debugging and recovering impossible)

http://forum.arduino.cc/index.php?topic=191820.msg1425151#msg1425151

http://forum.arduino.cc/index.php?topic=191820.msg1480507#msg1480507

http://forum.arduino.cc/index.php?topic=191820.msg1424302#msg1424302

The work around:

http://forum.arduino.cc/index.php?topic=191820.msg1679588#msg1679588