Reputation: 3
I'm pretty new to all of this, so it's possible I'm missing something obvious. I've searched all over, but I'm not seeing what I've gotten wrong.
I'm trying to use Python to connect to a Mosquitto broker I have running on a Raspberry Pi 3. I have the broker running and tested with the Mosquitto-Client tool. When I try to run my Python script I get the following error:
File "mqtt_sub.py", line 20
client = mqtt.Client()
SyntaxError: invalid syntax
Here is my mqtt_sub.py script:
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() - if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("Kastor/#")
#client.subscribe("<MainTopic/SubTopic>") # Add additional subscriptions here.
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
if msg.topic == "Kastor/event/PrintDone":
print(msg.payload["name"] + " has finsihed printing in " + int(msg.payload["time"] + "seconds.")
# Create an MQTT client and attach our routines to it.
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(”myUsername”, ”myPassword”)
client.connect("localhost", 1883, 60)
client.loop_forever()
I have Python 2.7.9 installed on the Pi where the script is being ran. If there is any additional info I can provide to help troubleshoot please let me know.
Upvotes: 0
Views: 1379
Reputation: 155546
Like all cases where an unambiguously correct line pops a SyntaxError
, your error was on the previous line:
print(msg.payload["name"] + " has finsihed printing in " + int(msg.payload["time"] + "seconds.")
Count the parentheses.
Upvotes: 1