MQTT Python troubles

Earlier this evening, I was able to execute the example code #3 from Cayenne-MQTT-Python on GitHub. However, now I’m getting “ImportError: no module named client”. Why?

cayenne-mqtt is installed using pip.

Did you use sudo when installing the library with pip? Any errors during the install?

Yes, I did use sudo. No errors were reported during or after the install. It’s really quite strange–it was working before. I’m working on an RPi, Debian 4.9.73+.

Can you paste the output of pip freeze

Here it is:

pi@raspberrypi ~ $ pip freeze

WebIOPi==0.7.23
argparse==1.2.1
cayenne-mqtt==1.0.1
cmd2==0.6.5.1
distribute==0.6.40
feedparser==5.1.3
lxml==3.2.1
nose==1.3.0
numpy==1.6.2
paho-mqtt==1.3.1
pygame==1.9.1release
pygobject==3.8.2
pyparsing==1.5.7
pyserial==2.6
python-eeml==3.2.0
virtualenv==1.9.1
wsgiref==0.1.2

pi@raspberrypi ~ $

I think I’ve found the problem, but I don’t know how it could be a problem in the first place:
I made a file called “cay1.py” which has as its contents the script from example 3. In the Traceback in the Python Shell, there are two calls reported. The first one is a reference to the initial “import cayenne.client” in line 1 of “cay1.py”.

41 PM

However, the second call referred to line 34 in a completely different file in the same directory, one which I had made earlier, called cayenne.py. The interpreter somehow jumped from cay1.py to cayenne.py.

I removed all the other cayenne scripts in that directory and ran cay1.py again.

This time, it worked, without error. I think that I’m back on track, but I’m flummoxed as to why it would behave in this bizarre manner. Could it be that “cayenne.py” is reserved for the module and not meant to be “user-serviceable”?

1 Like

Off the top of my head I’m not sure how that works, but you can call classes, etc from files in the same directory so I would say that’s probably the issue.

Thanks for your help. I’m now up and running after deleting those other files. Despite the teething issues, I’m really pleased with the relative simplicity of the Cayenne system.

1 Like

Hi. I’m new to RPi and Cayenne and I’m also trying to connect my RPi to Cayenne via the Cayenne MQTT Python option. My Python script gives me the error 'ModuleNotFoundError: No module named ‘cayenne’.
I installed via sudo pip install cayenne-mqtt. Because of my problems I tried to re-install the package and I’m getting the message that the requirement is already satisfied and referring to python2.7. Mybe this causes my problem because I also have Python 3.7 installed, but I’m not sure.
Can anybody help me with this?
My requirement is to caonnect my RPi to Cayenne thru the MQTT-api so I can trigger python code on the RPI an recieve the results back in Cayenne.
Regards, John

if you are using python3 then run this command pip3 install cayenne-mqtt

Hi Shramik. that did the trickallright :smile: but now I got an error on ’ client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)’ the message was a type error on begin(). The keyword loglevel does not exist. I removed that part but in Cayenne it’s still waiting for my rpi to connect.

which part you have removed?

loglevel=logging.INFO

can you share a screenshot of the the error you are getting?

i’m not getting an error now, it just doesn’t connect. message in Thonny: Python 3.7.3 (/usr/bin/python3)

%Run Connect-Cayenne.py
Connecting to mqtt.mydevices.com:1883

In cayenne just ‘waiting for board to connect’

ah, wait. i found it. my MQTT_CLIENT_ID was incorrect. Now it works! thanks for your effort :smiley:

1 Like

Hi. With help I managed to connect my pi via MQTT. Now I’m running some code which has to return a value to Cayenne. My problem is that I can’t get the value there. I used sample code to make things work. In that part i have the following code: client.virtualWrite(trigger_channel, sensor_value, “lum”, “p”)
trigger_channel = 2, sensor_value = 13. I expected to see the value in Cayenne in my widget. I created a widget type value with unit % on channel 2. Somebody an idea what I’m doing wrong?

can you share your entire code.

#!/usr/bin/env python
import cayenne.client
import time
import logging
import urllib, urllib.request, json
       
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "xx"
MQTT_PASSWORD  = "xx"
MQTT_CLIENT_ID = "xx"

TRIGGER_CHANNEL = 2  #Virtual channel for publishing the trigger value.
DATA_CHANNEL = 1     #Virtual channel for publishing the sensor data.
THRESHOLD = 10        #Threshold for the trigger.

i=0
timestamp = 0
crossed_threshold = False

# The callback for when a message is received from Cayenne.
def on_message(message):    
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
# For a secure connection use port 8883 when calling client.begin:
# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883, loglevel=logging.INFO)

def send_trigger_value(trigger_channel, sensor_value, threshold):
    global crossed_threshold
    if sensor_value >= threshold:
        if not crossed_threshold:
            client.virtualWrite(trigger_channel, sensor_value, "lum", "p")
            crossed_threshold = True
    else:
        client.virtualWrite(trigger_channel, 0, "lum", "p")
        crossed_threshold = False


while True:
    if (time.time() > timestamp + 100):
 
# Let er even op dat je je key aanpast
        key = "xx"
        plaats = "Enschede"
        url = "http://weerlive.nl/api/json-data-10min.php?key=" + key + "&locatie=" + plaats
        req = urllib.request.Request(url)

        response = urllib.request.urlopen(req).read()
        data = json.loads(response.decode('utf-8'))
        Zonpct = data['liveweer'][0]['d0zon']
        sensor_value = int(Zonpct)
        send_trigger_value(TRIGGER_CHANNEL, sensor_value, THRESHOLD)
        timestamp = time.time()
        i = i + 1
    if (i == 12): # Reset the sensor value to test that the trigger gets reset.
        i = 0

are you having any use of triggers?