使用来自 Arduino 数据的 Python 脚本播放 mp3 文件的问题
Issue to play a mp3 file with a Python script from Arduino data
我正在尝试使用 Arduino Uno 和 Python 脚本执行以下操作:如果 Arduino 的超声波传感器计算的长度低于 36,则播放我硬盘上的音乐。
我的Python代码如下:
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")
但是当我启动它时什么也没有发生,脚本在我的 shell 中保持 运行ning。
如果我执行打印数据,我注意到数据的值与 Arduino 控制台不同,并且 Arduino 控制台似乎无法正常工作(超声波传感器的长度值似乎 t运行cated)当 Python 脚本同时 运行ning。
当我 运行 以下 Python 脚本时:
import serial, webbrowser
webbrowser.open("/home/path/my-music.mp3")
我的mp3可以正常播放了。
有什么想法吗?
我修改了你的脚本来告诉你发生了什么。我无法测试它。
# 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")
此外,我建议您安装 mplayer 并使用它代替网络浏览器。
import subprocess
def play_music_file(filename):
subprocess.call(('mplayer', filename))
我正在尝试使用 Arduino Uno 和 Python 脚本执行以下操作:如果 Arduino 的超声波传感器计算的长度低于 36,则播放我硬盘上的音乐。
我的Python代码如下:
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")
但是当我启动它时什么也没有发生,脚本在我的 shell 中保持 运行ning。 如果我执行打印数据,我注意到数据的值与 Arduino 控制台不同,并且 Arduino 控制台似乎无法正常工作(超声波传感器的长度值似乎 t运行cated)当 Python 脚本同时 运行ning。
当我 运行 以下 Python 脚本时:
import serial, webbrowser
webbrowser.open("/home/path/my-music.mp3")
我的mp3可以正常播放了。 有什么想法吗?
我修改了你的脚本来告诉你发生了什么。我无法测试它。
# 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")
此外,我建议您安装 mplayer 并使用它代替网络浏览器。
import subprocess
def play_music_file(filename):
subprocess.call(('mplayer', filename))