111

I though about using the Raspberry Pi to switch on and off other electrical devices which unnecessary consume power in stand-by mode. In short, I'd like to control an AC socket or multiple sockets. How can one let the computer "push the button":

Image: CC-BY-SA 3.0 by Firstfreddy

The physical solution is a relay, but I don't want to build all on my own and play around with 220V and sparks when switching on and off ;-) By the way the Raspberry Pi requires 2 Watt in idle mode, so only using it as a switch to save energy might not make sense, so it should be usable for other purpose at the same time.

Jakob
  • 2,758
  • 5
  • 22
  • 22

14 Answers14

55

EDIT 2018

Years later and the hobbyist microelectronics community has exploded thanks to the like of cheap and power embedded computers, like Raspberry Pi. This caused mechanical relays that work direct of GPIO on 5/3.3V allot cheaper and easier to get.

You can get them as singles or premade (Bangood, Seeedstudio, Gearbest, eBay, etc) ranging from 4 to 48 "channels" I have even seen. These are much more compact size, very affordable, safe and easy to use.

enter image description here

# * * * WARNING * * * #

Switching mains involves interfacing with potentially lethal voltages. Due care and competence is required. Death is possible. YMMV. This paraphrases Russell McMahon's advice on Electrical Engineering


---Original Answer 2012---

Well you could use a Solid State Relay which is much smaller and easier to control than a mechanical relay (The big 12 volt ones used in automotive industry) , using an MCU or in this case Pi's GPIO pin.

enter image description here

You have to drive the input pin constantly to keep the relay on (just like a mechanical relay). So if something fails with that signal, then the power goes off. To avoid that you have to design another circuit that can sustain itself.

But you can get these pretty cheap on eBay and they are completely safe (isolated), so they won't blow up the Pi and do not require a lot of power to drive them, about 3~10mA. Just check the details before buying one. It is also worth noting they can heat up if you load them heavily (close to the maximum rating)

Piotr Kula
  • 17,307
  • 6
  • 65
  • 104
  • 8
    You don't always have to keep driving the pin for the relay to stay open. There's a special type of relay called bistable relay. It has two stable modes (on and off), you can switch between them and they stay like this until they are switched again. They are not so common as normal relays however. – Krzysztof Adamski Aug 17 '12 at 17:10
  • Thanks! This looks not more difficult or dangerous than a simple screw terminal. The RaspPi could also switch of its own power supply ;-) – Jakob Aug 17 '12 at 18:44
  • From Ebay much cheaper, delivered :) Honk Kong UK – Piotr Kula Aug 20 '12 at 13:07
  • @KrzysztofAdamski Could you possible give a link to a Bistable SSR. Thanks – Piotr Kula Aug 20 '12 at 13:13
  • @ppumkin: I don't think it's possible to build bistable SSR. I was reffering to mechanical relays only. But your answer suggest that in mechanical relays you also have to always drive input pin to keep it's state. And it's not really true. In case you don't want that, bistable relays are better than SSR (if you can ignore their downsides). – Krzysztof Adamski Aug 20 '12 at 13:47
  • Bistable relays are pretty expensive and they need 12v to power to the coil. Not really suitable just to wire into a Pi- as the op has asked. But still- Some people might find it interesting :-) Thanks @KrzysztofAdamski – Piotr Kula Aug 20 '12 at 20:59
  • 1
    @ppumkin: There are 5V bistable relays (zettler AZ850P1-5 for example) and I don't think they are that expensive (< 2$, just like mechanical ones). But if that's actually suitable depends on what you need to do. Sometimes energy savings coming from not having to drive the input pin all the are worth it. You are right that it may not be suitable for the OP as it would be hard to save energy with RPi by sleeping or something like that. – Krzysztof Adamski Aug 21 '12 at 05:53
  • @ppumpkin: A bistable SSR doesn't exist. If you go for a bistable relay, then you're talking about an electromechanical switch, which needs a short pulse to "jump" from one position to another (from open to close, for instance) and needs another short pulse to do the opposite. Once in its stable situation, it doesn't need energy any more. Examples can be found at www.eltako.com: [http://www.eltako.com/en/switchgear-power-units/k-electromechanical-switching-relays-control-relays-coupling-relays-and-installation-contactors.html] – GeertVc Apr 18 '14 at 06:55
  • can I use any SSR according to power rating? or are any types in SSR also? – avi May 27 '14 at 18:00
  • 3
    I have seen catastrophic SSR failures (like, failing ON) from those cheap eBay ones. Run away from them. – Joel Wigton May 10 '16 at 04:24
  • @ppumkin: "Bistable relays are pretty expensive and they need 12v to power to the coil. Not really suitable just to wire into a Pi- as the op has asked." -- I bought dozens of bistable (latching) mechanical relays (years ago), which can be directly driven by a MCU. (I used them with the meagre 3.3V GPIO of an MSP430G2.) And they were not "pretty expensive", but dirt cheap, too. – Sz. Aug 06 '18 at 12:58
  • As comments pointed out the "cheaper" ones seem to have catastrophical break downs. For where I lived the ones NOT from ebay were not cheap.. – Piotr Kula Aug 06 '18 at 13:04
  • Those comments were about SSRs, not mechanical relays. I've followed the subthread started by "In case you don't want that, bistable relays are better than SSR". – Sz. Aug 06 '18 at 13:08
  • Oh right.. this was quite a few years ago. Agreed, I have seen those new relays that you can drive from MCU, like on Seeeeeeed? But I think back then they were not that popular or cheap and mostly used those big 12v mechanical ones, hence the solid state relay answer... probably not "that" relevant today any more – Piotr Kula Aug 06 '18 at 13:22
  • 1
    Edit 2020 :) If you have an home automation system running (zigbee, hue) you can interface with quite cheap Ikea remote wall sockets like this: https://www.ikea.com/gb/en/p/tradfri-wireless-control-outlet-00364477/ I am linking the english version (UK) but they are available in most countries now I think. I personally use it with node-red and domoticz to turn a 3d printer on and off remotely. Works quite well. – Ron Feb 25 '20 at 07:32
31

You could get one of these (RF) remote control power switches (Assuming you can find one suitable for your local power receptacles)

enter image description here

and hardwire the RPi to the remote. This has the advantage of being isolated and not requiring any mains wiring. You'd have to take into consideration the power consumption of the device if the goal is to save power.

John La Rooy
  • 11,947
  • 9
  • 47
  • 75
  • 2
    +1. thats a really good idea, and a lot safer than interacting with the mains as a beginner – AnthonyBlake Aug 21 '12 at 08:28
  • Even better is to add a 433MHz transmitter module to the Pi instead, so you a) don't need to sacrifice the remote, b) can extend it arbitrarily (e.g. to also talk to products not readily compatible with that particular remote; etc.). – Sz. Aug 06 '18 at 13:03
  • @Sz. Sure - but that would require someone to reverse engineer the RF protocol. Might not be too difficult though. – John La Rooy Aug 06 '18 at 22:44
  • True, indeed, I forgot about that added difficulty. My mind was set by reading about a lib recently that supposedly made it very easy. (Never tried though.) I can't seem to find that one now, but e.g. this post appears to make it even simpler than that: https://www.instructables.com/id/Super-Simple-Raspberry-Pi-433MHz-Home-Automation/ – Sz. Aug 07 '18 at 16:39
26

Both Adafruit and Spark fun sell an assembled device exactly for this purpose: The Power Switch Tail exists in several variants, fully assembled or as kit. The kit's assembly instruction (PDF) include schematics.

Jakob
  • 2,758
  • 5
  • 22
  • 22
EricH
  • 261
  • 3
  • 2
16

SainSmart sells Arduino relay modules (shields), they could also be used on Raspberries. There are different models (higher amperage, number of outputs, etc). For example SKU:20-018-100-FBA can be used for "equipment with a large current". And a useful article discussing Using the Raspberry Pi to Control AC Electric Power that mentions the SainSmart.

enter image description here

Sonia Hamilton
  • 261
  • 2
  • 4
10

You can get a certain power suply with a USB-Connection and control it with this http://sispmctl.sourceforge.net/ Debian/Raspian has the package sispmctl by default.

Stefan
  • 101
  • 2
6

Use a Telldus Tellstick!

It has many applications and supports sensors as well.

Code samples in many languages: https://github.com/telldus/telldus

A router which has an HTTP API to control the TellStick if you dont want to have it directly connected to the PI: http://www.dovado.com/index.php?option=com_content&view=article&id=13&Itemid=20

You can also buy a TellStick Net device and send API requests to Telldus Live from the PI (internet access needed).

5

you could also use an existing device. I have interfaced an Aviosys NetPower 8800 switch which connects via USB.

It didn't come with Windows drivers and they weren't willing to nrelease an interface spec so I could build a Linux driver.

But I've written a Python program to control it:

https://skydrive.live.com/#cid=500667A62B4F909A&id=500667A62B4F909A%21294

Lance
  • 59
  • 1
  • Yes, I also though about using an existing electricity usage monitor and switch, but it's difficult to find a device that can easy be controlled without having to install proprietary drivers. – Jakob Aug 23 '12 at 07:08
3

X-10 serial interfaces exist. That would have the advantage of controlling AC switches that aren't right there.

Jakob
  • 2,758
  • 5
  • 22
  • 22
Brian Carlton
  • 313
  • 1
  • 13
2

totally different approach:

as you like your server to be up, you probably like the idea of an UPS. You can use an APC and control is by the raspberry. This will let you powercycle the ups. The smallest UPS can be used, either new or secondhand. The secondhand option will set you back 50euro's/dollar or even less and has extra benefits (and extra joy in makeing a webinterface for even extra monitoring).

This way you can hook up more devices and let the raspberry send shutdown commands during powerfailure etc

My 5-cents.......

user13550
  • 21
  • 1
1

Here is a SAFE way!

I used an off the shelf wireless transmitter, a wireless powerswitch (both 220 or 110volt will work) and a 5v relay.

This high power switching was done using an arduino but raspberry would work just as well (no code required for the transmitter).

http://youtu.be/trZ3y4xCGhA

lenik
  • 11,541
  • 1
  • 30
  • 37
Tony
  • 11
  • 1
1

A more complex but rewarding solution, would be to interface with RF devices directly. Check this out: http://rayshobby.net/?p=3381

gc_i
  • 71
  • 2
1

you just need 2 things:

  1. a transmitter directly interfacing via RXD, TXD of the serial connector of your raspi:

http://www.elv.de/elv-fs20-uart-sender-fs20-us-komplettbausatz.html

  1. a receiver with a relais (toggle switch). This retains its state even during a power outage:

http://www.elv.de/unterputz-funk-wechselschalter-fs20-ws1-komplettbausatz.html

some further info is found here:

Cheapest way to controlling multiple power sockets (lights) through Wi-Fi

sparkie
  • 435
  • 3
  • 9
1

For things that switch relatively occasionally mechanical relays are hard to beat. "solid state relays" have better cycle life but much higher cost and much higher operating losses.

The problem is that a lot of relay boards on the market are badly designed, all too often when I look at such boards I see inadequate creepage and clearance distances. I would not buy such a board for controlling mains without being able to see the layout of the power traces.

It's not helped by the design of the relays themselves, a common design of relay has one of the contact pins in-between the two coil pins. This makes it much harder to maintain creepage/clearance with these relays than it would be with a relay that has the coil pins at one end and the contacts at the other.

https://www.sainsmart.com/collections/internet-of-things/products/4-channel-5v-relay-module appears to be a sensible design. If you look at the picture of the bottom you can see they have slotted the PCB around the common terminal of the relay to control creepage distances.

You should obviously also make sure you mount the board securely in an enclosure that either well insulated or earthed and make sure all wiring is adequately restrained.

Peter Green
  • 6,476
  • 1
  • 19
  • 24
0

Belkin's WeMo Switch + the Ouimeaux Python library = arbitrary control over an outlet without any soldering or mains power exposure/risk.

Downsides: costs $50 US per outlet at list price, only works when your wifi network is up, and may or may not be compatible with the plugs in your part of the world.

Tim
  • 101
  • 1