0

I have a little python script I got from here. It listens for MQTT messages and plays a .wav file when a topic is published to. However, I get this error when the script is run:

TypeError: on_connect() takes exactly 3 arguments (4 given)

Where is the above error coming from? More importantly, how can I fix it?

Here's my full code:

import paho.mqtt.client as MQTT
# Don't forget to change the variables for the MQTT broker!
mqtt_topic = "button"
mqtt_broker_ip = "192.168.0.55"
song_path = '/home/pi/alerts/buzzer_alert.wav'
# change it to desire duration in seconds
duration_time = 5
client = mqtt.Client()
# These functions handle what happens when the MQTT client connects
# to the broker, and what happens then the topic receives a message
def on_connect(client, userdata, rc):
 # rc is the error code returned when connecting to the broker
 print "Connected!", str(rc)
 # Once the client has connected to the broker, subscribe to the topic
 client.subscribe(mqtt_topic)
def on_message(client, userdata, msg):
 # This function is called everytime the topic is published to.
 # If you want to check each message, and do something depending on
 # the content, the code to do this should be run in this function
 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
 os.system('aplay -d {} {}'.format(duration_time, song_path))
# Here, we are telling the client which functions are to be run
# on connecting, and on receiving a message
client.on_connect = on_connect
client.on_message = on_message
# Once everything has been set up, we can (finally) connect to the broker
# 1883 is the listener port that the MQTT broker is using
client.connect(mqtt_broker_ip, 1883)
# Once we have told the client to connect, let the client object run itself
client.loop_forever()
client.disconnect()
asked Jan 28, 2018 at 13:39
2
  • When you get the error, you should see multiple lines of output (a 'stack trace'). This will help you figure out which line is causing the problem; you should include it here to get help. Also, did you paste part of your code twice? It appears that some of it might have been repeated in your code. Commented Jan 28, 2018 at 13:44
  • Ok, I accidentally pasted it twice. Commented Jan 28, 2018 at 14:27

1 Answer 1

3

change your on_connect callback to this definition:

def on_connect(client, userdata, flags, rc):
 # rc is the error code returned when connecting to the broker
 print "Connected!", str(rc)
 # Once the client has connected to the broker, subscribe to the topic
 client.subscribe(mqtt_topic)

as mentioned in the paho documentation:

@on_connect.setter
 def on_connect(self, func):
 """ Define the connect callback implementation.
 Expected signature is:
 connect_callback(client, userdata, flags, rc)
 client: the client instance for this callback
 userdata: the private user data as set in Client() or userdata_set()
 flags: response flags sent by the broker
 rc: the connection result
answered Jan 28, 2018 at 14:09
3
  • Awesome! now working. I appreciate your prompt help. Commented Jan 28, 2018 at 14:38
  • One more issue though. The wave file is played at the start of the script, not at the press of the button. Commented Jan 28, 2018 at 14:41
  • Everytime a msg is published to this topic the on_message callback is called. So you have to ensure why is this callback were called at the beginning of your script. I dont know the esp programs. Make some further investigations Commented Jan 28, 2018 at 15:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.