Issue to play a mp3 file with a Python script from Arduino data
Question: I am trying to perform the following action with an Arduino Uno and a Python
script: if the length calculated by the ultrawave sensor of Arduino is below
36 then play a music which is on my hard drive.

My Python code is the following one:

    
    
    import serial, webbrowser
    
    arduino = serial.Serial('/dev/ttyACM0', 9600)
    data = arduino.readline()
    
    while (1==1):
        if (arduino.inWaiting()>0) and data < 36:
            webbrowser.open("/home/path/my-music.mp3")
    

But nothing is happening when I am launching it, the script kept running in my
shell. If I perform a print data I noticed that the value of data is different
from the Arduino console and it seems that the Arduino console is not working
properly (the length value of the ultrawave sensor seem truncated) when the
Python script is running at the same time.

when I run the following Python script:

    
    
    import serial, webbrowser
    webbrowser.open("/home/path/my-music.mp3")
    

My mp3 is played properly. Any ideas?


Answer: I modified your script to tell you what is happening. I can not test it out.

    
    
    # import the modules
    import serial
    import webbrowser
    
    # open a serial connection to the Arduino to read data from
    arduino = serial.Serial('/dev/ttyACM0', 9600)
    
    # we want to read everything the Arduino tells us
    while True:
    
        # read one line (a str) that the Arduino wrote with Serial.println()
        line = arduino.readline()
    
        # convert the line into a string that contains only the numbers
        # by stripping away the line break characters and spaces
        string_with_number_in_it = line.strip()
    
        # convert the string into a number that can be compared
        number = float(string_with_number_in_it)
    
        if number < 36:
            webbrowser.open("/home/path/my-music.mp3")
    

Also, I suggest you install [mplayer](https://en.wikipedia.org/wiki/MPlayer)
and use it instead of the webbrowser.

    
    
    import subprocess
    def play_music_file(filename):
        subprocess.call(('mplayer', filename))
    

