Controlling a USB Relay board through Raspberry PI

Hi All
I am relatively new to the Raspberry PI and have not used Cayenne before, however:

I am working on building an irrigation controller. So far I have built it up using an inherited raspberry PI 2B and a Denkovi 8 channel USB relay board. Denkovi supply their own GUI control software which works on Raspian. I have had all this working quite nicely.

However I have recently stumbled upon Cayenne and it is a no-brainer to try set it up for the ultimate remote control. As of yet, I haven’t found any guidelines on how I should go about controlling the USB board through the Raspberry PI, as this of course does not use the GPIO pins (in hindsight would have been a cleaner solution with using Cayenne). Is anyone able to give me some pointers on where to start?

As an aside, there is also command line software which can run basic control of the relays. As one method I assume Cayanne can execute command line script on the RPI?

Cheers

@mattyjb88 welcome to cayenne community.
have a look at this Executing custom code on your Raspberry Pi from Cayenne

Hi @mattyjb88

Run sudo raspi-config
Select interfacing options
Enable P6 Serial
Reboot
dmesg | grep tty
you should see:

dmesg | grep tty
[ 0.000000] Kernel command line: bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000 dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=PARTUUID=8026ae82-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
[ 0.000350] console [tty1] enabled
[ 0.799993] 3f201000.serial: ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2
[ 0.800063] console [ttyAMA0] enabled
[ 2.381345] systemd[1]: Created slice system-serial\x2dgetty.slice.

Add this to your Python script

import serial

port = serial.Serial(“/dev/ttyAMA0”, baudrate=115200, timeout=3.0)
#Then you can write to relay board

Use : port.write(" ") # Relay board command

Hope this help
Regards

2 Likes

This is the way I would do it too as the serial command structure is dead simple.

See Section 8.

Cheers,

Craig

Thanks all for you help.
So far I have managed to connect the rpi as a BYOT device and get it to toggle a basic print command as per the posted instructions. All good there - I can see how this will work with the board.

Before trying to stitch the whole thing together I have then attempted to control the board with a basic python script to turn one relay on only to make sure this works ok. This is where I am coming unstuck. The script runs, but does nothing, throws no errors or anything. Note I have to run it as sudo otherwise it throws a permission denied error.

As confirmation, upon entering:
dmesg | grep tty

I get:

[ 0.000000] Kernel command line: bcm2708_fb.fbwidth=640 bcm2708_fb.fbheight=480 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000 dwc_otg.lpm_enable=0 console=tty1 console=ttyAMA0,115200 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait splash plymouth.ignore-serial-consoles
[ 0.001330] console [tty1] enabled
[ 1.003745] 3f201000.serial: ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2
[ 1.948461] console [ttyAMA0] enabled
[ 6.638645] usb 1-1.3: FTDI USB Serial Device converter now attached to ttyUSB0

The script i have written to turn on the relay is:

import serial

port = serial.Serial(
    "/dev/ttyAMA0",
    baudrate=115200,
    timeout=3.0)

port.write("04+//")

print('script complete')

This should be turning on relay 4, however nothing happens.

Can anyone point out my error here?
Many thanks for assistance to date.

HI @mattyjb88 ,

Use ttyUSB0 instead of ttyAMA0
Sorry for my mistake I am used to use GPIO 14 and 15 (UART TX RX)

Cheers

Thanks, I have made the change but still no joy.

I have searched around a little more and this relay board is based on a FTDI chip. The driver installation instructions say the following:

the kernel automatically loaded another driver for the
FTDI USB device.

sudo lsmod

If "ftdi_sio" is listed:
    Unload it (and its helper module, usbserial), as follows.

    sudo rmmod ftdi_sio
    sudo rmmod usbserial

If I complete this then the script I wrote completely flips an error and the device is listed as disconnected.

I am wondering if the FTDI chip requires to be written to by a different method?

Hi @mattyjb88

Usually, under Linux FTDI works without having to install anything.
According your dmesg | grep tty result:
[ 6.638645] usb 1-1.3: FTDI USB Serial Device converter now attached to ttyUSB0
So, OK for the raspberry pi
I use FTDI out of the box many times.
Cheers

Thanks all
I ended up using a python program drcontrol from here, and making some mods so it worked correctly with the 8-relay denkovi board.

1 Like