Cayenne with Pi and Sainsmart 8channel relay


See above


If you are worried about widgets already set on the dashboard see this post to reset everything on device initialization Esp8266 reboot after power failure

Note: This was for the old connection method. I’d have to double check, but I don’t think the current MQTT connection method sends the current dashboard values when the device connects. The retain flag is currently set to false on the server.


Sorry Adam. Can you clarify what you mean by Pi Agent? I believe you are referring to a component of Cayenne that deals specifically with a Pi correct?


pi agent is used for running cayenne on pi. you can have a look at it here


Yeah, the Pi agent that shramiksalgaonkar linked is what is installed via the Cayenne dashboard on the Pi. If you want to try modifying the code for your particular issue you might take a look at the __setFunction__ in

I think the issue is that when using the sysfs GPIO interface (as the agent does) it automatically sets the value to low when you change it to an output using ‘out’. You might be able to change that to ‘high’ for your particular case and see if that works.

If other users are having this same issue we can look at either providing an option to select the state on boot, or perhaps just automatically save off the state at shutdown and restore it at reboot.


I think both of these options would be a great addition to Cayenne!


After more testing…

I have the entries in /etc/rc.local to set the pins all out/high in boot. This you can see happening on boot as the pins on the relay go from set as input (led is lightly lit on each relay), to output/high (off). This is good.

Cayenne on startup activates all configured relays (turns them on). I do not believe Cayenne should be changing states by default. Only by user input or configured triggers should it change state.

I tried changing the “status” for each relay in the devices.json file to see if that would change the functionality of Cayenne on startup (it does not).

Any progress on this?


@jlohman i did a trial with the relay and Cayenne Mqtt python to keep relay ON after reboot. there is a small time relay is OFF which is during the initial booting of pi and then relay stays ON.

you need to install

add a new device using BYOT and add your MQTT credential into below code.

#!/usr/bin/env python

import cayenne.client

import time

# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.**




import time

import RPi.GPIO as GPIO



GPIO.setup(27, GPIO.OUT)


# 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.**

     if ( == 20) and (message.value == "1"):

              GPIO.output(27, GPIO.HIGH)

     else :

              GPIO.output(27, GPIO.LOW)

client = cayenne.client.CayenneMQTTClient()

client.on_message = on_message


# For a secure connection use port 8883 when calling client.begin:**

# client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID, port=8883)**


timestamp = 0

while True:


      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

i have connected the relay to GPIO pin 27 and added a button on the cayenne dashboard on channel 20. you can change them accordingly and saved the file with name

Next, you need to create one more file which runs on reboot to turn ON the relay.

import time

import RPi.GPIO as GPIO



GPIO.setup(27, GPIO.OUT)


GPIO.output(27, GPIO.HIGH)


Add both this file in crontab to run them on reboot. sudo crontab -e

@reboot python2 /home/pi/

@reboot sleep 60 && python2 /home/pi/

the needs a delay of 60 sec so that the networking can be ready.


@jlohman I added some code to the feature/restore-actuator-state branch of Cayenne-Agent.

That should store off the current state of the relay pin and restore it on reboot when the pin is changed to an output. That hasn’t been tested by QA yet, but if you want to give it a try you can install that branch using sudo python3 install or sudo python3 develop and then restart the agent using sudo service myDevices restart.

If you try it and it doesn’t seem to work, please let me know.