ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Developing Linux SW for relay cards

Tue Jan 27, 2015 3:28 pm

Hi

following the previous announcements in this thread, I am glad to announce a new version of the crelay program.

Most important change is that it now also supports the popular Sainsmart USB 4-channel relay card. :D
As always, get the source code and documentation here:
http://ondrej1024.github.io/crelay/

Have fun.
The Telegea.org project: https://www.telegea.org

winmei
Posts: 3
Joined: Tue Mar 03, 2015 7:51 pm

Re: Developing Linux SW for relay cards

Tue Mar 03, 2015 7:59 pm

hi,

what about Sainsmart USB 8-channel relay card? Is it supported by crelay?

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Wed Mar 04, 2015 10:10 am

Hi winmei,

I guess you are referring to this relay card (the link name is quite misleading):
http://www.sainsmart.com/sainsmart-4-ch ... ics-1.html

I have developed the Sainsmart support only with the 4 channel USB card relay card, but from the specs of the 8 channel version I see that it uses the same USB-to-Serial converter chip FT245RL. So I assume that crelay should work with this card also. It will only control the first 4 relays, though. To get support for all 8 relays the following line in relay_drv.h needs to be changed from

Code: Select all

#define SAINSMART_4CHANNEL_USB_NUM_RELAYS 4
to

Code: Select all

#define SAINSMART_4CHANNEL_USB_NUM_RELAYS 8
In order to properly autodetect the type of relay card used, I would need to find a way of reading some card specific data. Unfortunately I don't have an 8 channel relay here to try.

Hope this helps anyway, Ondrej
The Telegea.org project: https://www.telegea.org

winmei
Posts: 3
Joined: Tue Mar 03, 2015 7:51 pm

Re: Developing Linux SW for relay cards

Wed Mar 04, 2015 5:58 pm

Hi ondrej,
thx for your reply. I'll order 8 channel version and report my experiences here.

cheers

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Tue Mar 17, 2015 10:52 am

Hi all,

I was asked to support also the USB relay cards compatible with the HID API. These cards seem to be around as 1/2/4 and 8 channel versions. They are cheaper than Conrad and Sainsmart: So here is the new version 0.7 of crelay which also controls these type of cards (as always on GitHub).
http://ondrej1024.github.io/crelay/

It would be great if someone with the HID API compatible relays cards could test this as I don't have any of these cards. Some testing was done by Darryl Bond who wrote the usbrelay software, but some more feedback from other people would be nice.

Thanks, Ondrej
The Telegea.org project: https://www.telegea.org

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Thu Apr 09, 2015 2:33 pm

winmei wrote:Hi ondrej,
thx for your reply. I'll order 8 channel version and report my experiences here.
Did you get a chance to try crelay with the Sainsmart 8 channel card? I would like to know if this actually works or if I need to change the code.
Ondrej
The Telegea.org project: https://www.telegea.org

winmei
Posts: 3
Joined: Tue Mar 03, 2015 7:51 pm

Re: Developing Linux SW for relay cards

Fri Apr 24, 2015 7:18 am

oh sorry, I can't test it. I did'nt buy an 8 channel card.

2tirefire
Posts: 12
Joined: Tue May 12, 2015 7:45 pm

Re: Developing Linux SW for relay cards

Tue May 12, 2015 7:53 pm

I have the 8 channel card, but having issues installing c relay. I'm stuck on this part

Build and install :

cd src
make [DRV_CONRAD=n] [DRV_SAINSMART=n] [DRV_HIDAPI=n]
sudo make install
Note: Optionally, you can exclude specific relay card drivers (and their dependencies) from the build, if you don't need them. To do this, specify the driver name as parameter of the "make" command as shown above.


I get this error (copied of terminal)
pi@raspberrypi ~/crelay-master/src $ make [DRV_SAINSMART=n]
[Compile relay_drv_sainsmart.c]
relay_drv_sainsmart.c:73:18: fatal error: ftdi.h: No such file or directory
compilation terminated.
Makefile:65: recipe for target 'relay_drv_sainsmart.o' failed
make: *** [relay_drv_sainsmart.o] Error 1

If anyone could help that would be greatly appreciated. Please be kind I have about 2 hours of experience using terminal and raspberry. OS is rasbian.

Thanks

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Wed May 13, 2015 9:18 am

Hi 2tirefire,

per default, crelay will try to build with all relay cards supported. The make parameters are used to exclude the drivers you don't need/want. DRV_XYZ=n means "driver for XYZ no". The [ ] brackets indicate that these parameters are optional.

Therefore, if you only need the Sainsmart driver, you have to exclude the other two. In this case, the command is this:

Code: Select all

make DRV_CONRAD=n DRV_HIDAPI=n
Please note that you need to install the libftdi library first, since the Sainsmart driver depends on this.

Code: Select all

sudo apt-get install libftdi1 libftdi-dev
Hope this gets you further.
Ondrej
The Telegea.org project: https://www.telegea.org

2tirefire
Posts: 12
Joined: Tue May 12, 2015 7:45 pm

Re: Developing Linux SW for relay cards

Wed May 13, 2015 4:15 pm

ondrej1024 wrote:Hi 2tirefire,

per default, crelay will try to build with all relay cards supported. The make parameters are used to exclude the drivers you don't need/want. DRV_XYZ=n means "driver for XYZ no". The [ ] brackets indicate that these parameters are optional.

Therefore, if you only need the Sainsmart driver, you have to exclude the other two. In this case, the command is this:

Code: Select all

make DRV_CONRAD=n DRV_HIDAPI=n
Please note that you need to install the libftdi library first, since the Sainsmart driver depends on this.

Code: Select all

sudo apt-get install libftdi1 libftdi-dev
Hope this gets you further.
Ondrej
Thank you so much for your time!
That did get me a little farther but it looks like I'm still getting some errors. After following your instructions and then trying to install I'm getting this.

pi@raspberrypi ~/crelay-master/src $ make DRV_CONRAD=n DRV_HIDAPI=n
[Link crelay] with libs -lftdi
relay_drv.o:(.data.rel.ro+0x14): undefined reference to `detect_com_port_conrad_4chan'
relay_drv.o:(.data.rel.ro+0x18): undefined reference to `get_relay_conrad_4chan'
relay_drv.o:(.data.rel.ro+0x1c): undefined reference to `set_relay_conrad_4chan'
relay_drv.o:(.data.rel.ro+0x3c): undefined reference to `detect_com_port_hidapi'
relay_drv.o:(.data.rel.ro+0x40): undefined reference to `get_relay_hidapi'
relay_drv.o:(.data.rel.ro+0x44): undefined reference to `set_relay_hidapi'
collect2: ld returned 1 exit status
Makefile:61: recipe for target 'crelay' failed
make: *** [crelay] Error 1
pi@raspberrypi ~/crelay-master/src $ sudo make install
[Compile relay_drv_hidapi.c]
relay_drv_hidapi.c:89:27: fatal error: hidapi/hidapi.h: No such file or directory
compilation terminated.
Makefile:65: recipe for target 'relay_drv_hidapi.o' failed
make: *** [relay_drv_hidapi.o] Error 1

Any help is greatly appreciated.
thanks,
2tirefire

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Thu May 14, 2015 7:32 am

Just do "make clean" before building to get rid of any old object files or binaries from previous build attempts.
Ondrej
The Telegea.org project: https://www.telegea.org

2tirefire
Posts: 12
Joined: Tue May 12, 2015 7:45 pm

Re: Developing Linux SW for relay cards

Fri May 15, 2015 3:21 am

ondrej1024 wrote:Just do "make clean" before building to get rid of any old object files or binaries from previous build attempts.
Ondrej
It said clean but still getting errors. I decided to wipe the o.s and start over so that it was a fresh installation. I did not get very far. I will list the order in which I applied code.

1: apt-get install libudev-dev libusb-1.0-0-dev
2: git clone git://github.com/signal11/hidapi.git
3: ./bootstrap This command did not work

root@raspberrypi:/home/pi# ./bootstrap
bash: ./bootstrap: No such file or directory

Again I appreciate your time and patience with me.

2tirefire

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Mon May 18, 2015 7:31 am

Hi,
you make this look much more complicated than it is :?
And it looks like you are confusing some things here. So I need to start by asking some essential questions. The first is: what relay card do you have and what cards support do you want to be built into the crelay program?

Ondrej
The Telegea.org project: https://www.telegea.org

2tirefire
Posts: 12
Joined: Tue May 12, 2015 7:45 pm

Re: Developing Linux SW for relay cards

Tue May 19, 2015 5:23 am

ondrej1024 wrote:Hi,
you make this look much more complicated than it is :?
And it looks like you are confusing some things here. So I need to start by asking some essential questions. The first is: what relay card do you have and what cards support do you want to be built into the crelay program?

Ondrej
It is a sainsmart USB 8 CH relay card. I would like to be able to turn on and off individual channels or all on or all off. After reading the very important read me file I was able to get to the end of building but it stops and says that cannot find the library. I can copy and paste the error in a few hours.

I asked some questions in another thread that I started because I don't want to bother you with problems I'm having due to lack of programming experience or failure to rtfm, but any help is greatly appreciated.

Thanks,
2tirefire

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Tue May 19, 2015 8:01 am

OK, I've sent in answer in this thread.
Ondrej
The Telegea.org project: https://www.telegea.org

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Mon Aug 17, 2015 9:49 am

Hi all,

this is an important update for the users of the Conrad 4-channel relay card. This card needed a patched cp210x kernel driver in order to be recognised by crelay and the patch never found its way into the mainline kernel. Now I have completely rewritten the code handling the Conrad card, removing the need for the cp210x driver.

So if you have a Conrad 4-channel relay card, go grab the new version 0.8 of crelay which will make life much easier for you (as always on GitHub).
http://ondrej1024.github.io/crelay/

Ondrej
The Telegea.org project: https://www.telegea.org

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Wed Sep 02, 2015 12:06 pm

And yet another new release. V0.9 of crelay is a big step forward, adding important functionality and configuration options. Now we are approaching the first major 1.0 release.

These are the changes that went into the latest update:
- Added parameter handling in daemon mode via configuration files
- Added new parameters for daemon listen interface and port
- Added new parameters for GPIO pin definitions
- Added new parameter for number of relays on Sainsmart card (4/8)
- Added detection for number of relays on HID API compatible relay card
- Added logging daemon messages to syslog

There is also an official OpenWRT package available now in the development snapshot repositories.
https://downloads.openwrt.org/snapshots/trunk/

It would be nice if someone would like to take up the task of packaging crelay also for Debian/Raspbian.

Have fun, Ondrej
The Telegea.org project: https://www.telegea.org

Spidersbox
Posts: 8
Joined: Thu Sep 10, 2015 5:33 pm

Re: Developing Linux SW for relay cards

Thu Sep 10, 2015 5:51 pm

I've just started programming in linux, and crelay is the first program I'm hacking up :lol:

For my own purposes, I've removed all the web interface stuff and all the drivers except the hid api compatible.
I've added an 'all' command that lists the state of each relay, 'all' 'on | off', and a pulse command.

I need help now, if you don't mind, in using more than one card. I have two of the two channel cards, and about to order more with more relays, but while I'm waiting, I would like to use both cards.

I found how to 'Enumerate and print the HID devices on the system' on the signal11.us site, but in navigating your code, I can't figure where to change to the next card.

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Fri Sep 11, 2015 6:31 am

Hi Spidersbox,

have a look at this project. It does exactly what you want. Supports only HID API relays from command line but can handle multiple cards of these.

Ondrej
The Telegea.org project: https://www.telegea.org

Spidersbox
Posts: 8
Joined: Thu Sep 10, 2015 5:33 pm

Re: Developing Linux SW for relay cards

Fri Sep 11, 2015 4:30 pm

usbrelay was the first one I looked at, but I didn't like the interface - run the app to look for the serial number, then run it again with the commands - your way is much better.
Plus I was only using one card, and didn't notice it could handle multiples.
The coding of usbrelay, with it's use of multiple cards does make it a better candidate for my usage.
I will just have to replace the command line interface.

Thanks for the help!

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Sun Sep 13, 2015 3:25 pm

Hi Spidersbox,

the handling of multiple relay cards has actually been on the ToDo list for crelay for a long time. I just didn't have time to do it yet and it is not a high priority for me. But if you need this feature and you are implementing it for the HID API relays, then it would be nice if you could share the code when you get it working. I am grateful for any contributions.
The Telegea.org project: https://www.telegea.org

Spidersbox
Posts: 8
Joined: Thu Sep 10, 2015 5:33 pm

Re: Developing Linux SW for relay cards

Mon Sep 14, 2015 3:42 am

It was almost a complete rewrite, so you probably won't recognize most of it, but not only would I like to share, but I would like it if someone could go over the code and make sure everything is ok, like memory allocations possible buffer over write , releasing of memory...
Guess I will go to github and see about opening an account ??

I've been programming in windows in c++ (using c++Builder) for the last 17 years, so don't be afraid to make comments that would help!

Oh - I called my program "relay".
Are there lists to check through to see if a program name is used already ??
I tried sudo apt-get install relay and found no packages by that name so I figure the name should be ok..

(have you noticed I'm a complete newbie to linux ??)

Spidersbox
Posts: 8
Joined: Thu Sep 10, 2015 5:33 pm

Re: Developing Linux SW for relay cards

Mon Sep 14, 2015 5:28 am

if you are interested here is the link:
https://github.com/Spidersbox/Relay

ondrej1024
Posts: 171
Joined: Thu Dec 05, 2013 3:09 pm
Contact: Website

Re: Developing Linux SW for relay cards

Tue Sep 15, 2015 12:19 pm

I had only a very quick glance at the code, so cannot really comment on it. But I think the way of numbering continuously the relays of different cards is a nice solution. I had thought about adding another parameter, specifying also the card number. But this would make the command line syntax more complicated. Therefore your solution is quite user friendly.

As for the name of your project, "relay" is maybe a bit too generic. Searching for it on Google will bring up millions of results :shock:

Welcome to the world of Linux programming.
The Telegea.org project: https://www.telegea.org

Spidersbox
Posts: 8
Joined: Thu Sep 10, 2015 5:33 pm

Re: Developing Linux SW for relay cards

Tue Sep 15, 2015 2:40 pm

I thought about "adding another parameter, specifying also the card number."
something like "relay 1:2 on" I suppose it could be added as an optional parameter.

"As for the name of your project, "relay" is maybe a bit too generic."
Yes, I think you are right. I might rename the project, but I was more concerned about the program name.

Return to “Automation, sensing and robotics”