A relay box for control from Linux

Aus DL8RDS Wiki
Wechseln zu: Navigation, Suche

1 Project Scope

This little project is for DB0MHB. We want to be able to switch all sorts of devices from remote. We have a little Raspberry Pi as core machine which consumes decently little power, and I was looking for a relay board that uses bistable relays and thus does not consume power during the switching operation.

I found the following board:

http://www.robot-electronics.co.uk/products/relay-modules/usb-relay/usb-rly16l-8-x-16a-latching-relay-module.html

Usb-rly16l-8-x-16a-latching-relay-module.jpg

The next challenge was to integrate it in an accordingly stable and shielded metal box. I chose the Hammond 1590F aluminium enclosure, with stable plugs and interfaces.

Even though the relays allegedly cope with 16 A, I didn't trust the PBC lines, so I decided to limit the current by using a bank of car fuses rating 10 A.

Notice: Gerry from Robot Electronics informed me that there will be no worries about the PCB. Here is what he wrote me:
"A standard PCB uses 0.5oz copper built up to 1oz during plating. The USB-RLY16L PCB uses 3oz copper built up to 3.5oz. Those traces are 0.1” wide (2.54mm). 16A will result in a temperature rise of just under 21C."

In other words it will work just fine.

The consumer sockets are all the classic 4mm banana plug type sockets, the input will be rated at 80 Amps, so it must be accordingly stable. For the power distribution or ground collective system I took solid copper bars.

The interface plug towards the Raspberry USB cable is a Neutrik USB convertible socket. I love these Neutrik sockets. Since I have found them, I have been using them in all of my metal cases, they are really extremely reliable.

The integration into our environment at DB0MHB will be through a CGI web interface, so the CGI code will evaluate the output and reformat everything.

And for matters of completeness, this idea is quite an old one. Already around 10 years back I had this idea: Switching power over IP

1.1 Links

Alternatives:


2 Component List

  • RLY16L: 90 Euro
  • 10x Banana Socket: 10 EURO
  • Hammond 1590FF (7.38"x7.38"), SWATEE Electronics, UK: 46 Euro
  • Neutrik USB Socket: 5 Euro
  • Metal Cable mounts: 2 Euro
  • Fuse holder: 12 Euro
  • 8x 10A Fuse: 10 Euro
  • USB interconnect: 5 Euro
  • Standoffs: 15 Euro
  • Copper bars: 24 Euro
  • Cables: 10 Euro
  • Cable Shoes: 10 Euro
  • Spiral plastic cable mount: 2 Euro
  • Isolation PCB for positive Connect: 2 Euro

Total: 243 Euro

3 Programming

Unfortunately there is no real sample code for Linux on the web. I have found some C#/Mono code, but I didn't want to use it, since all that Visual Studiu stuff is far overcomplicated for that little purpose.

For the sake of completeness, here is the link to the C# code: http://www.robot-electronics.co.uk/files/usb-rly16-linux-mono.zip

Referring to the full set of commands (90,91,92,93) I must confess that I decided to support only the command Nr. 91 ("get me the state of the relays"). I just omitted the others.

Please consider the following page for some details: http://www.robot-electronics.co.uk/htm/usb_rly16tech.htm

And here is my Python code. Use it at your own risk.

#!/usr/bin/env python

# (c) 2017 Markus Heller, M.A. DL8RDS, relix GmbH

import time
import serial
import sys

interface = {
    "1"   : "Get value of Relay 1",
    "2"   : "Get value of Relay 2",
    "3"   : "Get value of Relay 3",
    "4"   : "Get value of Relay 4",
    "5"   : "Get value of Relay 5",
    "6"   : "Get value of Relay 6",
    "7"   : "Get value of Relay 7",
    "8"   : "Get value of Relay 8",
    "91"  : "Get relay states - sends a single byte back to the controller, bit high meaning the corresponding relay is powered",
    "100" : "100 - All relays on",
    "101" : "101 - Turn relay 1 on",
    "102" : "102 - Turn relay 2 on",
    "103" : "103 - Turn relay 3 on",
    "104" : "104 - Turn relay 4 on",
    "105" : "105 - Turn relay 5 on",
    "106" : "106 - Turn relay 6 on",
    "107" : "107 - Turn relay 7 on",
    "108" : "108 - Turn relay 8 on",
    "110" : "110 - All relays off",
    "111" : "111 - Turn relay 1 off",
    "112" : "112 - Turn relay 2 off",
    "113" : "113 - Turn relay 3 off",
    "114" : "114 - Turn relay 4 off",
    "115" : "115 - Turn relay 5 off",
    "116" : "116 - Turn relay 6 off",
    "117" : "117 - Turn relay 7 off",
    "118" : "118 - Turn relay 8 off"
}

# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
    port='/dev/ttyACM0',
    baudrate=19200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_TWO,
    bytesize=serial.SEVENBITS
)

ser.isOpen()

def interaction(input):
    if interface.has_key(input):
        index = "0"
        if int(input) in range(1, 8):
            index = input
            input = "91"
        ser.write(unichr(int(input)))
        time.sleep(1)
        out = ''
        while ser.inWaiting() > 0:
            out += ser.read(1)

        if int(index) in range(1, 8):
            print format(ord(out), '#010b')[2:][::-1][int(index)-1]
        elif out != '':
            print format(ord(out), '#010b')[2:][::-1]
    else:
        print "Input not recognized"

def singlecommand():
    input = sys.argv[1]
    interaction(input)

def loop():
    print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
    input=1

    while 1 :
        # get keyboard input
        input = raw_input(">> ")
        # Python 3 users:
        # input = input(">> ")

        if input == "h":
            print "Help:"
            for k in sorted(interface.keys()):
                print interface[k]
        elif input in ['exit', 'e', 'q']:
            ser.close()
            exit()
        else:
            interaction(input) 

if __name__ == "__main__":
    if len(sys.argv) > 1:
        # command from commandline
        singlecommand()
    else:
        # interactive loop
        loop()

4 Images

2017-04-14-RLY16-1.jpg 2017-04-14-RLY16-2.jpg

2017-04-14-RLY16-3.jpg 2017-04-14-RLY16-5.jpg

2017-04-14-RLY16-6.jpg 2017-04-14-RLY16-7.jpg

2017-04-14-RLY16-4.jpg 2017-04-14-RLY16-8.jpg