Cayenne MQTT Python Library


Currently there is no Python lib for interfacing with RPI, though we are working on this ability. You would be able to read temp./humidity data from any sensor using an Arduino and the Cayenne libs if that’s a possible substitute.

We do have MQTT API that might work as substitute…we recently released it. No one has shared MQTT code for RPI yet though. So you could be the first! :slight_smile:



I have one semi working if you want to give it a try:

import paho.mqtt.client as mqtt
import time
import sys

MQTTpayload = 0

mqttc = mqtt.Client(client_id="")
mqttc.username_pw_set("username", password="")
mqttc.connect("", port=1883, keepalive=60)

topic = "v1/username/things/clientid/digital/22"
while True:
        MQTTpayload = MQTTpayload + 1
        mqttc.publish(topic, payload=MQTTpayload, retain=True)
    except KeyboardInterrupt:

For me it works for about 15 seconds and then starts sending a 1 on channel 1. I tested with a local MQTT server and it appears to be working there so I’m not sure if it’s a Cayenne issue or something with the code?

Multiple Pi's for home automation control

@croczey @jburhenn @kreggly any input to why it might fizzle out after 15 seconds? ^^?

Would LOVE to get something going for RPI.


This was happening to me too when using generic mqtt libs. Would connect and then nothing.


I did some packet captures and it looks like the problem is that the Cayenne MQTT server is disconnecting my client after 15-20 seconds or so. With the localhost connection I only ever see one connect command. Also seems odd that it creates a widget for channel 1 when I never send any data for channel 1.

I also noticed this with an Uno running the standard arduino libraries - not MQTT - but I assumed it was my knockoff ethernet shield dropping the connection (might be?) but could be related.


There is a Cayenne MQTT Python library you can try that is available here:

That is an Alpha release so it hasn’t been extensively tested yet, but you might find it useful.

I’m not sure if that would fix the connect issue @adam is seeing. If the same problem happens with the Cayenne-MQTT-Python library it may need a server side fix.

Another possible source of disconnect issues would be re-using the same client id for different connections/devices. The server will disconnect one of them in that case.


This could be the issue. I don’t think I have my other device online right now but it’s possible. I’ll create a new MQTT device and try with it.


That must have been it. Created a new device and it’s working fine now.

So anyway… sorry to hijack your thread @jonrvoss You can just use this script here and put in your bits to make it send the data to Cayenne. MQTTpayload = MQTTpayload + 1 is the line you want to change to send the value and then update username/password/clientID. Let me know if you need any help with it!


Thanks all. Will give it a try.


One thing I left out… you’ll have to install that MQTT library with sudo pip install paho-mqtt


I have the Cayenne Python API installed on my Raspberry Pi, but am playing with the C library right now :slight_smile:




So is there any documentation, real documentation explaining how to use the pyhton API around somewhere?



You are looking for something other than this right? Or is link below what you were thinking?



Also, you can invoke pydoc cayenne.client for additional detail.

Is there something specific you are looking for, @manuelgod69g?




Yes, I am just starting messing with python and I do not know what pydoc it is or where to get it. The poor example is hard to see what it actually does or any kind of real documentation on how to use it or what other functions can I use within Python, etc… Bunch of questions, no real answers.

I am trying to control my home using Raspberry Pi Zero W with Cayenne but the documentation is very limited, at least of what I have found so far. I also have a Omega2 but that one too is linux based. Arduino has a bunch of libraries, examples, etc but the hardware is limited to my needs. I am now trying to focus on Raspberry since it has more power and it is now cheaper $10 for the latest Raspeberry Pi Zero Wireless.

Right now I wanted to get some values from Cayenne dashboard to be read by Rapsberry Pi Zero but nothing I have found so far. I was looking at a post that Cayenne has json data download? Not clear on that either and no info on how to.



I will be watching this with keen interest also. It would be great to see some sort of an example hello world / LED project. Even a basic step thru would be great thanks. Would we be able to pass variables into Cayenne ? Read them back remotely ?

I agree with above and am finding the Pi0/w are looking like a serious bit of kit now with more than enough horsepower and more than a few cool tools. An optional full desktop and headless remote access ! Jessie I am sold on it now. It is getting pretty sorted. Minimal apt-get patches and mysterious command line fiddling for beginners like me. All the power features we used to bolt on can now be simply toggled on and off the config-sys menu (or desktop).

I would be looking to see if this can work as the glue between some local python talking to some creative original devices. I am missing dead simple Pi py variables connected thru the pi py reading and writing to and from Cayenne. In most cases my Pi connect wirelessly to uAmp idling devices up to a few hundred meters away. A 433MHz local mesh couples into the Pi via ttyAMA0 with PySerial very nicely. This part works ! The problem has is getting variables into and out of Cayenne.

~ Andrew


What kind of documentation are you looking for? MQTT is pretty straight forward, it’s just a protocol to send and receive data. What you do with that data is up to you in the rest of the code you dream up. If you just need an example here is a project I posted DHT11/DHT22 with Raspberry Pi


Agree it is a starting point thanks. If I have time I will try to ‘crack’ the code and get this example reworked into some basic examples. But I am pretty much a beginner but am starting to get the feel of the MQTT process.

It would be great if a 5th Cayenne MQTT Library for python on a Pi could be added in the same way the four options have already been (very nicely) documented thanks.

The documentation so far for 4 other systems is most readable thanks.
These are very well sorted and easy to work through.

I will have a tinker with the MQTT.fx and do some manual poking about first for now to build up my ideas and skills with virtual cayenne dashboard devices. Thanks for including this link. It is a great looking tool for demo and testing.

I will be keen to know if someone gets’s a demo pi py based DS18B20 / Switch, LED, Slider sorted as a demo. That would be great.

~ Andrew


ı made one for switch. ı will continue led, slider and other things.
Here the link is:İnput/


Thanks very much for the DS18B20 especially. A great Cayenne concept and example. This is a great key example of reading and plotting an input to a plot etc. The DS18B20 digital approach (-55 <> + 125 Deg C) seems to me to be the best method since messing with ADC converters seems a bit complex. As well multiplee DS18B20 to the same single GPIO pin 1w bus it seems a wayyyy cooler way to go ?

Last thing i might be able to help with a Tute ‘5’ is to crack myself next will be the Serial (Pyserial Readline) off the GPIO serial port from an HC-12 TTL transparent serial link radio module. I propose stripping the variables from a generic CSV serial radio data packet. A bit like this…

It is already looking like the HC-12 + Pi GPIO may be a match made in heaven :wink: If someone wants to help be mix the working serial py CSV un-pack-ager / (split csv / tab, comma delimiter) into the DS18B20 Blob I will furnish the circuit.

Note: The Picaxe remote radio in the background is an example of typical student project: 3xAA for a year + of remote connectivity. "look mum no wires"
The HC-12 claims 1800m max but I have yet to confirm this…

~ Andrew