title: Reading data from Arduino to Python using bluetooth
body: <p>I'm sending data with Arduino through Bluetooth (jy-mcu HC-06), using this simple code:</p>
  
  <pre><code>int a;
  
  void setup() {
    Serial.begin(57600);
    a = 0;
    delay(2000);
  }
  
  void loop() {
    Serial.println(a);
    a++;
    delay(40);
  }
  </code></pre>
  
  <p>My issue is that when I try to read the data with Python (on Linux), the first 20 seconds (approx.), some data is corrupted. But after this, I can read the data correctly without any other error.
  Python code:</p>
  
  <pre><code>import serial
  
  ser = serial.Serial()
  ser.port = "/dev/rfcomm0"
  ser.baudrate = 57600
  ser.open()
  
  while True:
    print(ser.readline())
  </code></pre>
  
  <p>Arduino code is a loop that sends an integer every 40ms and sums one to this integer. An example of the data corrupted:</p>
  
  <pre><code>108
  109
  11161831191
  192
  193
  194
  195
  1111
  122222222222
  2220
  22266
  267
  268
  269
  270
  133333533333633333373
  33333333344444444444444444444444444444544444444444444444491
  492
  493
  494
  </code></pre>
  
  <p>I power the Arduino with an external power supply that gives 5V and 3A with a lot of stability.</p>
  
  <p>I tried to read the data with an Android Bluetooth terminal app and it worked perfectly all the time. For that reason, I suppose that the error is in the Python code.</p>
  
  <p>I have read a lot of information on the Internet and I have made a lot of changes in the code but nothing solves the problem. This is one of my latest codes, just to show some of my tests:</p>
  
  <pre><code>import serial, time
  import sys
  
  ser = serial.Serial()
  ser.port = "/dev/rfcomm0"
  ser.baudrate = 57600
  ser.timeout = 1
  ser.setDTR(False)
  ser.setRTS(False)
  ser.open()
  old_data = 0
  new_data = 0
  ser.flushInput()
  ser.flush()
  ser.flushOutput()
  time.sleep(1)
  
  print("Start")
  while True:
    raw_data = ser.readline()
    try:
      ser.flushInput()
      new_data = int(raw_data)
      ser.flush()
    except ValueError:
      pass
    print raw_data
    if (old_data != new_data -1 and old_data != 0):
  print('ERROR#####################################################')
    old_data = new_data
  
    time.sleep(0.01)
  </code></pre>
  
  <p>I appreciate any help, and of course, I will post here any update on this issue.</p>
  
  <p>Thank you in advance!</p>