hamilc
Posts: 177
Joined: Fri May 16, 2014 9:59 pm

Need help installing MQTT using these python commands

Wed Jan 08, 2020 10:24 pm

Following is a snippit from a website instructing me as to how to install a python mqtt client.
First, I started Python and got the >>> prompt
The first bold line works successfully.

The second bold line gives me this error.

>>>Client(client_id=””, clean_session=True, userdata=None, protocol=MQTTv311, transport=”tcp”)
Traceback (most recent call last):
file "<stdin>, line 1, in <module>
NameError:'Client' is not defined
>>>

Any thoughts? I have tried inserting a name between the "" but same error.

********************* Web snippit fiollows *******************************


Importing The Client Class

To use the client class you need to import it. Use the following:

Import paho.mqtt.client as mqtt

Creating a Client Instance

The client constructor takes 4 optional parameters, as shown below .but only the client_id is necessary, and should be unique.

Client(client_id=””, clean_session=True, userdata=None, protocol=MQTTv311, transport=”tcp”)

To create a instance use:

client =mqtt.Client(client_name)

DirkS
Posts: 10084
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need help installing MQTT using these python commands

Wed Jan 08, 2020 10:52 pm

Please post your exact code and *full* error message(s). Use code tags.
hamilc wrote:
Wed Jan 08, 2020 10:24 pm
Import paho.mqtt.client as mqtt

Creating a Client Instance

The client constructor takes 4 optional parameters, as shown below .but only the client_id is necessary, and should be unique.

Client(client_id=””, clean_session=True, userdata=None, protocol=MQTTv311, transport=”tcp”)

To create a instance use:

client =mqtt.Client(client_name)
I suspect that it has to be 'client' instead of 'Client'.

BTW: the line starting with 'Import' should also give an error message as it should be 'import' (not starting with a capital).

hamilc
Posts: 177
Joined: Fri May 16, 2014 9:59 pm

Re: Need help installing MQTT using these python commands

Thu Jan 09, 2020 1:29 am

My code consists of only the two bold lines shown.

The first is to import the client class, and the second is, I think. the constructor for the class.

I am following the instructions in this web tutorial:
http://www.steves-internet-guide.com/in ... on-client/

The instructions seem simple but I get the error following the second bold line.
The import statement does not return an error.

I have typed the exact error message I received.

DirkS
Posts: 10084
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need help installing MQTT using these python commands

Thu Jan 09, 2020 5:58 am

hamilc wrote:
Thu Jan 09, 2020 1:29 am
My code consists of only the two bold lines shown.
Please post the stuff I asked for...

I'm not interested what's in the tutorial, but what's on your Pi.
As I said:: the import statement as it's in your post will result in an error.
If it does not give an error the information you have given so far is not reliable.

User avatar
paddyg
Posts: 2460
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Need help installing MQTT using these python commands

Fri Jan 10, 2020 12:03 am

This seems to work ok i.e. client = mqtt.Client() creates an instance of the mqtt.Client class which can subsequently be used (set id and pw then connect etc). Passing args as client_name will assume it's the first arg i.e. client_id so you have to set that variable correctly.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

hamilc
Posts: 177
Joined: Fri May 16, 2014 9:59 pm

Re: Need help installing MQTT using these python commands

Fri Jan 10, 2020 12:14 am

I am having a bit of trouble copying my Pi screen to a file I can insert so here is a screen shot of my PI screen.
I hope this will suffice.
code.jpg
code.jpg (49.81 KiB) Viewed 158 times

hamilc
Posts: 177
Joined: Fri May 16, 2014 9:59 pm

Re: Need help installing MQTT using these python commands

Fri Jan 10, 2020 1:35 am

Paddyg, do you mean I should replace the line client(client_id.........
with client = mqtt.Client()
then all should work?

DirkS
Posts: 10084
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Need help installing MQTT using these python commands

Fri Jan 10, 2020 4:11 am

hamilc wrote:
Fri Jan 10, 2020 1:35 am
Paddyg, do you mean I should replace the line client(client_id.........
with client = mqtt.Client()
then all should work?
Yes.

The bit you copied is just an explanation of the functions in the module. The last line below shows an example of how to use it:
Creating a Client Instance
The client constructor takes 4 optional parameters, as shown below .but only the client_id is necessary, and should be unique.

Client(client_id=””, clean_session=True, userdata=None, protocol=MQTTv311, transport=”tcp”)
To create a instance use:

client =mqtt.Client(client_name)
If you look further down in the tutorial you can actually see how you would initiate and use the module:

Code: Select all

import paho.mqtt.client as mqtt #import the client1
broker_address="192.168.1.184" 
#broker_address="iot.eclipse.org" #use external broker
client = mqtt.Client("P1") #create new instance
client.connect(broker_address) #connect to broker
client.publish("house/main-light","OFF")#publish

User avatar
paddyg
Posts: 2460
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Need help installing MQTT using these python commands

Fri Jan 10, 2020 7:58 am

@hamilc, I know it's frustrating not to jump straight in and make something useful... However, if you've decided to make a project using python, I would strongly recommend spending a day or two working through an introductory course. Lots available including ones on this website.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

hamilc
Posts: 177
Joined: Fri May 16, 2014 9:59 pm

Re: Need help installing MQTT using these python commands

Fri Jan 10, 2020 1:16 pm

Thanks Paddyg, and Dirk. I was confused because I did not realize that the line "Client(client_id..........." was just explaining the constructor and was not an executable line of code. I thought it was funny but I tried to follow the tutorial as written.

I think I understand now.

Thanks again all.

Return to “Python”