Raspberry Pi Connection With Cayenne API

Hello!
I’m trying to connect my Raspberry Pi with using Cayenne API. I’ve searched on the internet but I could not figure out how to connect. Please help me.

you can use GitHub - myDevicesIoT/Cayenne-MQTT-Python: Python Library for Cayenne MQTT API if you meant cayenne MQTT API.

I saw it but I’m stuck in Cayenne Setup → 2. Add a new device using the Bring Your Own Thing API section. I don’t know how to add my Raspberry pi.

from step 2 you will get MQTT credentials on the dashboard. copy and paste them is this code and run it:

import cayenne.client
import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "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)
    timestamp = time.time()
    i = i+1

This did not worked for me. I used this code and it worked!
Here is the code:
import paho.mqtt.client as mqtt

user = “USERNAME”
pswd = “PASSWORD”
client_id = “CLIENT”
server = “mqtt.mydevices.com
port = 1883

client = mqtt.Client(client_id)
client.username_pw_set(user, pswd)
client.connect(server, port)

Thank you for your help!

what was the error you were getting?

I wasn’t xd. I was able to run the code but it somehow did not connect to cayenne. Then i found this code and worked!

there is no issue using this code but then you will have to create your own topic string for publish. in cayenne library you have to just publish data using one command.