How to add a custom send function to Cayenne-MQTT-Python


#1

Hi,

I want to add a custom send function to Cayenne-MQTT-Python, So I copy from “kelvinWrite” and modify it, like this:
def batteryWrite(self, channel, value):
self.virtualWrite(channel, value, TYPE_BATTERY, UNIT_PERCENT)

And add it on the top of “kelvinWrite” function. But I got an error like this:

AttributeError: 'CayenneMQTTClient' object has no attribute 'batteryWrite'


#2

@jburhenn tagging here.


#3

Thanks!
I am waiting some one answer.


#4

can you share the entire code. @adam tagging here.


#5

@overheat1984 i was able to add a tank widget to my dashboard and i think it should work for you to show it like a battery level.

#!/usr/bin/env python
import cayenne.client
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = ""
MQTT_PASSWORD  = ""
MQTT_CLIENT_ID = ""


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

i=0
timestamp = 0

while True:
    client.loop()

    if (time.time() > timestamp + 10):
        client.celsiusWrite(1, i)
        client.luxWrite(2, i*10)
        client.hectoPascalWrite(3, i+800)
        client.virtualWrite(6, i, "tl", "null")
        timestamp = time.time()
        i = i+1

#6

you can also use below line to get a battery widget.

client.virtualWrite(7, i, "batt","p")


#7

Hi,

Thanks for your reply, but where is the “batt” come from? your client.py?
Could you kindly share with me the client.py file?


#8

have a look at this Data types for Cayenne MQTT API

i have posted it above. just add client.virtualWrite(7, i, "batt","p") in it.


#9

@overheat1984 Your initial batteryWrite function seems correct, not sure why it would raise that error. You placed the function at the same level as the kelvinWrite function inside CayenneMQTTClient, correct? If you can post the full code for that file it might help.