簡體   English   中英

發出使用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中運行。 如果執行打印數據,我會注意到數據值與Arduino控制台不同,並且當Python腳本在同一時間運行時,Arduino控制台似乎無法正常工作(超聲波傳感器的長度值似乎被截斷了)時間。

當我運行以下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並使用它代替webbrowser。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM