netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Conditional statement fails to initialize sound file

Mon Jun 11, 2018 7:24 pm

I have a python script that listens for incoming payloads and should act according to the publication from the client.
When the topic is published to it should play an mp3 file using SoX,

Code: Select all

os.system('play --no-show-progress %s --channels 2 synth %s' % (sound1, duration))
but it doesn't.
I figured that maybe SoX has some compatibility issues so i use aplay

Code: Select all

os.system('aplay -d {} {}'.format(duration_time, response_path)) 
. Still doesn't initialize the sound.

Would someone take a look at the code to see if any anomalies can be spotted?

Code: Select all

duration = 5  # second

sound1 = "/home/pi/auto_response/alert.mp3"


mqtt_topics = ["laser", "motion", "door", "light"] # Change to multiple topics that suits your needs. 
mqtt_broker_ip = "192.168.0.55" 
client = mqtt.Client() 


def on_connect(client, userdata, flags, rc):
    print "Connected!", str(rc)  # rc is the error code returned when connecting to the broker
    for topic in mqtt_topics:
        client.subscribe(topic) # Once the client has connected to the broker, subscribe to the topic
  
def on_message(client, userdata, msg):  
        if msg.payload == "close":
	os.system('play --no-show-progress %s --channels 2 synth %s' % (sound1, duration))

    print "Topic: ", msg.topic + "\nMessage: " + str(msg.payload) 
    
client.on_connect = on_connect
client.on_message = on_message

client.connect(mqtt_broker_ip, 1883)

client.loop_forever()
client.disconnect()
EDIT
Upon further investigation i discovered that when i put the call to initialize the sound file directly under the on_message fucntion

Code: Select all

on_message(client, userdata, msg):
the sound file plays. But if i make the statement conditional and the indent the call to play the sound file, it fails to do so without giving any error.
Last edited by netromaster on Tue Jun 12, 2018 9:56 am, edited 1 time in total.

User avatar
DougieLawson
Posts: 36104
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Mqtt script fails to initialize mp3 file with SoX and aplay

Mon Jun 11, 2018 7:36 pm

You're not importing the mqtt stuff.

You've got a desperately bad indentation problem at

Code: Select all

def on_message(client, userdata, msg):  
        if msg.payload == "close":
	os.system('play --no-show-progress %s --channels 2 synth %s' % (sound1, duration))

    print "Topic: ", msg.topic + "\nMessage: " + str(msg.payload) # The message itself is stored in the msg variable and details about who sent it are stored in userdata
    
or you've mucked up the cut'n'paste into the forum.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

netromaster
Posts: 33
Joined: Thu Jul 02, 2015 11:49 pm

Re: Mqtt script fails to initialize mp3 file with SoX and aplay

Mon Jun 11, 2018 8:40 pm

I imported mqtt and all the required libraries. About the indentation problem, do i indent to the left of right?

User avatar
DougieLawson
Posts: 36104
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Mqtt script fails to initialize mp3 file with SoX and aplay

Mon Jun 11, 2018 8:45 pm

I don't know because I don't know what you're trying to make conditional on the badly indented IF statement.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

texy
Forum Moderator
Forum Moderator
Posts: 5157
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Conditional statement fails to initialize sound file

Tue Jun 12, 2018 12:50 pm

This is guess, but probably what you are looking for :

Code: Select all

def on_message(client, userdata, msg):  
    if msg.payload == "close":
        os.system('play --no-show-progress %s --channels 2 synth %s' % (sound1, duration))

    print "Topic: ", msg.topic + "\nMessage: " + str(msg.payload) 
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Python”