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'
#!/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
@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.