Cayenne MQTT Python Library


#1

I have some Python code that is sending data to a Thingspeak page. I would like to switch it to send to my Cayenne app. I haven’t found much python-to-Cayenne documentation, am I missing it somewhere?


#2

Hi @jonrvoss,

Welcome to the Cayenne community!

What data would you like to send? Is it coming from a device like an Arduino or ESP?

-B


#3

I have some Python code that I wrote that grabs GPIO data from my RPI with a temp/humidity sensor attached. I then write this data out to Thingspeak, but would prefer to send it to Cayenne. Are there Python library calls I can make to Cayenne?


#4

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:

-B


#5

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("mqtt.mydevices.com", port=1883, keepalive=60)
mqttc.loop_start()

topic = "v1/username/things/clientid/digital/22"
while True:
    try:
        MQTTpayload = MQTTpayload + 1
        print(MQTTpayload)
        mqttc.loop()
        mqttc.publish(topic, payload=MQTTpayload, retain=True)
        time.sleep(5)
    except KeyboardInterrupt:
        mqttc.loop_stop()
        mqttc.disconnect()
        sys.exit()

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
#6

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

Would LOVE to get something going for RPI.
-B


#7

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


#8

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.


#9

There is a Cayenne MQTT Python library you can try that is available here: https://github.com/myDevicesIoT/Cayenne-MQTT-Python.

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.


#10

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.


#11

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!


#12

Thanks all. Will give it a try.


#13

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


#14

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

Cheers,

Craig


#15

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

Thanks


#16

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

-B


#17

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

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

Cheers,

Craig


#18

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
https://github.com/myDevicesIoT/Cayenne-MQTT-Python 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.

Thanks


#19

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


#20

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