MQTT Python troubles

Hi Shramik. At this moment not. The idea is that with a trigger from cayenne side, the coding to get the value is triggered and the value has to be returned to cayenne.

sorry, but i did not understand the above. before using the above code use this code Cayenne-MQTT-Python/Example-03-CayenneClient.py at master · myDevicesIoT/Cayenne-MQTT-Python · GitHub

Hi Shramik. I used example 4, but now I think I understand what went wrong. My development should work as follows: send a trigger from cayenne-side. In the python program this trigger causes an extra piece of program to be triggered. So when the trigger is send, a variable in my python is set to TRUE. when TRUE, the value is retrieved from the other api-call and returned to cayenne.

this trigger which you are referring is receiving data from cayenne when the button on the cayenne dashboard is changed?

but the command client.virtualWrite(2, 25, “lum”, “p”) is correct to send the value 25 to the channel 2 in my case?

yes indeed

to receive data or subscribe to button cmd topic you need to use this code Cayenne-MQTT-Python/Example-02-ReceiveData.py at master · myDevicesIoT/Cayenne-MQTT-Python · GitHub and then changing this part:

def on_message(message):
    print("message received: " + str(message))
    if (message.channel == 4) and (message.value == "1"):
        //execute whatever you want

whereas the word referrs to Cayenne Docs

Yes, I had that already, but ran into the problem that i couldn’t process my program. to avoid this I copied my logic into the main program. now I can alter my local variable when the trigger is recieved. The only problem that I have now is that client.virtualWrite(2, 25, “lum”, “p”) does not show my value of 25 in the widget in cayenne. This statement is correct?

Do you know why the example 4 is used?
anyways you can try something like this:

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

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

x = 0

# The callback for when a message is received from Cayenne.
def on_message(message):
    global x
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.
    if (message.channel == 4) and (message.value == "1"):
        print("button is pressed")
        x = 1

client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, loglevel=logging.INFO)
# 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)

i=0
timestamp = 0

while True:
    client.loop()    
    if (time.time() > timestamp + 10):
        client.celsiusWrite(1, i)
        timestamp = time.time()
        i = i+1
    if x == 1:
        #execute your code here
        client.virtualWrite(6, x, "lux", "p")
        x = 0

Hi. I will come back to you tomorrow. I’m now altering my code. Thnx so far

Hi Shramik. I managed to get it working. Thnx for your help. I’m now completing my code. Maybe I run into more problems, but then I will ask again.
Thnx for now.

3 Likes