Sunday, March 21, 2010

The turner off-er

Unconventionally, this is an entry about a hardware hack I perpetrated, and not a script as such. There is a script at the end, but it's trivial.

For a long, long time I wanted to make this device. It started such a long time ago that I don't even remember what I wanted to use it for... The idea is that you have some sort of an appliance that you plug into the wall, like a refrigerator or a lamp or something, and you want to control when it is turned off and on from your computer.

I approached the subject several times now. Each of the previous times my lousy electronic and electronics-related skills had caused me either (a) to get too frustrated too continue at the planning stage, (b) not to be able to obtain the proper components, or (c) to almost annihilate the world due to a short-circuit. And each time I put it off until I got more drive.

And finally, this week (mostly due to the confidence that playing with the Arduino has given me) I've done it! It works, and I am not unhappy.

Þiſ herewiþ iſ þe chronicle of my victory.

Bright was the morning and high ovr hearts when I proceeded (again) to think about the problem of turning things on and off with my computer. I started out by searching for relevant information (because I know there are schematics available out there) and found somebody's blog post about how this can be done with an Arduino. This was an excellent point to start, because I own an Arduino, and those are easier to come to grips with than hooking up a bunch of wires to the back of the computer. And it's basically a ready solution.

Except that it isn't for me. That there is done for 120V AC and I live in a place where we have 220V AC in the wall, so I had to redesign.

So I had to find out about relays. In a nutshell these are like switches which use electromagnetic magic to turn a switch on and off. That's all I need to know. I require the relay to be turned on when a 5V DC signal appears on one of its magic electromagnetic coils, and allows 220V AC to pass through it. A longer while spent searching and a couple of questions this guy one I know allowed me to figure out that the relay I need is a JQX115F 005. A further 30 minutes with a datasheet helped me figure out that I want something like a JQX115F 005/2ZS2... which they didn't have at the store, so I finally acquired the JQX115F 005/2ZS4 instead.

Here's a sketch of what I came up with:


I used my breadboard to put the thing together and test it out, first with my Arduino.


When that worked, I soldered all the bits together and put them in a socket-like box, and then tested it out. It stopped working so well, because I soldered a few elements the wrong way (specifically, the diode was upside down)... and then I tried again and it worked fine again.


The thing left to do now was to interface with a computer. Happily, I several of those things. Having visited electronics stores, I also obtained the necessary parts to make a cable connection between the computer and the turner off-er.

There is a snag, however. I decided to use the parallel port to control the device. My main reason behind this is that using any (e.g., USB) serial port would require an additional chip between the computer and the device that would translate the values from the serial protocol to some usable form. This is expensive to put together. So I stuck to the parallel port, since I have plenty of old computers that sport it. The snag is though, that the parallel port is not designed to provide power, and may fail to give enough current to work the relay (it's not powerful enough to turn the switch on), which would not have been a problem with the USB.

I decided to do a nasty hack then, and use the USB to get power and simultaneously use the printer port to control the device. This is not pretty, I know, but it's easier, and it's enough for the moment. I'll probably try to use only USB in the future, if I ever get round to redesigning the device.

So, the cable has two endings that go into the computer. In the USB cable I tap into the Vcc 5V (usually red) and the Ground (usually black). In the parallel port, I tap into the Ground pin and the first input pin - Data0 (see image below). Then I connect ground from the USB cable and the ground the parallel port together.


As for the device, the Vcc goes into the Vcc of the turner off-er (red socket), the grounds go into the Ground of the turner off-er (black socket), and the Data0 goes into the Input of the turner off-er (yellow socket). And all is well. Although the actual cable I made looks kinda manky.


Last thing was a computer program to control the thing. I decided to go with the pyParallel library, so the script to turn the thing on or off would be something like:

 
1 #!/usr/bin/python
2 from parallel import Parallel
3 from sys import argv
4
5 def usage():
6 print "Usage: %s on|off" % argv[0]
7
8 if len(argv) != 2:
9 usage()
10 elif not argv[1] in ['on', 'off']:
11 usage()
12 else:
13 port = Parallel()
14 data = 1 if argv[1] == 'on' else 0
15 port.setData(data)


And I had to remove lp and install the ppdev kernel module on my Debian Sid box where I was testing it:

1 rmmod lp
2 modprobe ppdev

And that's it.

Update (April 19th): I later made another unit. It came out slightly neater (and much quicker) now that I knew exactly what I was doing. Thus, I give you, Mk. II:

And with the Mk. II cable:

No comments: