Cayenne with Pi and Sainsmart 8channel relay

First off…total Cayenne noob here. Please excuse any stupidity on my part.
I have added a couple of “relay” widgets but the problem I am having is on a reboot of my Pi when Cayenne starts up it turns all relays to on. Yes…this is an active LOW relay board. Adding the relay “inverted” just flops when the icon is litup but doesn’t change the underlying relay setting.

I have tried setting the gpio state in /etc/rc.local but it seems that it isn’t an initial state thing…it is a Cayenne thing. Should there be 2 options for relays? Active Hi and Active Low? Or…a way to set initial state?

not all GPIO pulled Low, there are some which are pulled also. you can try this approach RPi GPIO High at boot time and keep high when booted and we will take into consideration the 2 option for relay.

OK…here is what I have now. I am using the following GPIO pins 2,3,5,6 (high) and 17,18,22,23 (low) according to that document you mentioned.

I have tried adding raspi-gpio set 17 op dh for all of the 8 gpio pins to /etc/rc.local as mentioned. (/etc/rc.local has 755 perms).

When Cayenne starts up…all relays turn “on” aka…

Here I believe is smoking gun that this is Cayenne issue. If I remove a relay from the Cayenne config and reboot. That relay does not turn on after reboot…but all others do. I removed relay 4 first (gpio 18) and rebooted. All relays turned on except R4 (still connected). I then removed relay 1 (gpio 6) and rebooted. All relays EXCEPT 1/4 are on.

@jiohman we are working on trying to find a solution for the same. till then you can try this approach arduino - Why is designed active low? - Electrical Engineering Stack Exchange or add an invester circuit for yout GPIO output.

the way I have solved it is by doing the following

I created a script with the name

this file contains the following info

gpio -g mode 22 out
gpio -g write 22 1
gpio -g mode 12 out
gpio -g write 12 1

with the command gpio we can change setting of the GPIO ports

-g is to use the gpio number, not the pin number
the mode option is used to put it in IN or OUT mode
write option is used to change the state to high(1) or low(0).

the 12 and 22 are the gpio port numbers (not the pin numbers)

the script I is added to the crontab with the command sudo crontab -e

Bellow syntax is added to the crontab

@reboot sleep 15 && sh /home/pi/

now after a reboot and 15 sec delay I set the GPIO´s to high

Hope this helps to solve your problem


1 Like

You actually wouldn’t need to change on the coding level. Each relay switch has 3 input options, it is because on of them is common but the other two behave in opposite way i.e. if you connect your wire to the leftmost then switching on the relay might disconnect the circuit, but if you put your wire to rightmost slot of same relay channel, then switching if on will connect/ complete the circuit.

If you are confused about what I just explained, see the channel of the relay properly, it has 3 input options for wires. One combination will switch circuit on when relay is on and one combination will switch circuit off when the relay is off.

That does not in any way solve the problem. While swapping the output wires of the relay from NC<>NO does put the relay in the “correct” mode after Cayenne starts…while the Pi is rebooting the relay is now on (since you have swapped what is on/off). This is TRAGICALLY bad. This needs to be a software fix. On relays you should be able to specify active HI/LOW in configuration AND you should be able to specify one of two or three start options (Hi / Low / READ - I would be happy with just Hi / Low options).

So what you are proposing is a script that after Cayenne starts up and turns everything on the cron job turns everything off (assuming Cayenne starts in the 15 seconds after the script starts). This isn’t an option.

I believe if I have to do this with hardware to “invert” the signal I could use a ULN2803 and do it. They are cheap…just seems the better solution is to add Active Low relays and the ability to set a startup mode in Cayenne.

Is the code for this readable or compiled? I’m pretty sure I could resolve this myself if I could find and edit the software.

After some thought. I believe the only update needed is the ability to change the initialization setting. If I could set INIT_HIGH instead of LOW I could make everything work by using the FLIP option that is already there. You guys obviously have an INIT function already in place. Can you simply change the init variables from static to editable?

we are trying to make some change but it will take some time.

@jlohman You have two other options:
Use an Arduino and define your startup options in the setup function.
Use the MQTT connection method and the Python libraries. Define your startup values in the script.

Honestly if you are worried about resets and default settings those options will be a better solution anyway. The Aruidno has a few second boot up time and MQTT should be a little faster than the Pi Agent.

1 Like

Nice solution @adam

Even using an Arduino the default for Cayenne for a relay is incorrect. I am quoting from another website,

"Arduino microcontrollers start up with all pins as inputs that float high. (Pi starts with all pins as input also)

Any pins that are intended to be active high outputs will be ‘active’ until the program makes them outputs and resets them low. In other words if this board was active high, your relay would be energized when you start up, and would remain so until you reset your pins as outputs and take them low.

The manufacturer thinks you don’t want that (you probably don’t) so this board is set up the opposite of that (active low)."

The problem here to me is that Cayenne in the code assumes relays are Active High…and I am just not finding that to be true. So I would believe either correcting the relay default in the code or setup the option to set an initial state.

Go online and search for relay boards. Your search will return 1000 active lows for every active high you find.

There are about 30 different solutions we could design to deal with the fact that Cayenne believes that an active high relay is normal (or that you want all relays turned on during startup).

I believe this is either:
a) a bug in the default setting for Cayenne for a relay
b) a configuration option that is desperately needed.

So a recap

  1. While the Pi reboots everything is in the correct state. OFF
  2. Any relays NOT configured in Cayenne stay off even after the Pi/relay is energized and Cayenne is active.
  3. Cayenne drives all configured channels LOW as a default, thus activating all configured relays.

what about using cayenne MQTT python library GitHub - myDevicesIoT/Cayenne-MQTT-Python: Python Library for Cayenne MQTT API

With Arduino in the setup function you define what you want your default state to be, it doesn’t have anything to do with Cayenne. This happens in milliseconds after the board is powered up.

This is a “feature” of the Pi Agent, not Arduino or MQTT connection method. I’m not disagreeing that we should be able to change it with the Pi agent.

Adam…you are missing the point. The default state is correct (off). I am not having flutter issues. It is when Cayenne starts up that the relays turn on. They don’t flick on and then back off. Cayenne appears to believe that the default state for a relay should be off HOWEVER it also appears to believe that the way to turn a relay off is to turn the gpio to low. That is not correct. Most relay boards are active low.

1 Like

I’m not missing the point, see my post above.

To me the issue boils down to this.

  1. Cayenne believes there should be a default state for a relay when Cayenne starts. (Agreed).
  2. Cayenne using a relay/GPIO believes that default state should be LOW.

All I am saying is that is an incorrect belief. Most relay boards are active LOW (no matter what device they are connected to (Pi or Arduino).

@jlohman we working on trying to get solution for this, till then can you try GitHub - myDevicesIoT/Cayenne-MQTT-Python: Python Library for Cayenne MQTT API to connect and set the state as you want.