Controlling GPIO using Cayenne using python Code

Hey Fellow developers… I am trapped in a project and need help

I am trying to write a python code that can be executed on my pycharm or any other IDE, and when executed, it lights up the led connected to my raspberry pi. This is very simple using Cayenne dashboard and GPIO channels, but I want to use cayenna custom tools to build a new project and contribute.

I want to write a code that uses mqtt username, password and client, and using those credentials, when it gets executed, the led glows or write a python code to make it blink.

In simpler terms, please help me write a code that can make a led blink and uses mqtt username, password and client id provided by Cayenne so that it can be managed easily

use Cayenne Mqtt library to tun your led code GitHub - myDevicesIoT/Cayenne-MQTT-Python: Python Library for Cayenne MQTT API.
Do you want to run the led blink continuously or for a short period? You will have to find a way to run led blink code together with cayenne code or else you will get disconnected from the cayenne server.

In the Cayenne mqtt python code given, which line I have to edit or add, so that when that line ie executed, an LED connected to GPIO 17 gets on. Following is the code, please edit/ add that line to light up an LED connected to GPIO 17 when this code runs. It will really be helpful, thanks for the help mate

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

here is an tutorial and example on how to do it Executing custom code on your Raspberry Pi from Cayenne

1 Like

I have a box asking for code location. What should I write?

first you need to add a device using Bring your Own Thing and then select this device. Follow the procedure from the 1st link i shared on how to add a device.

1 Like