celerity
Posts: 20
Joined: Mon Nov 28, 2011 1:50 pm
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 2:10 pm

Hi, I'm don't have any experience in home automation, so apologies if I'm missing an obvious solution.

But... what would the easiest way to control a mains power switch using a Raspberry Pi?

The context is that I have my office room temperature and current output from our solar PV panels on the web, and I'd like to write a script on the RPi to scrape this data and control a fan heater accordingly.

Something like:

if room-temp < 19C and solar-output > 2000W then turn on heater

I'm not comfortable with a soldering iron or Arduino programming, so I'm ideally looking for an off-the-shelf product I can control with the RPi.

Any advice appreciated, thank you,

/\dam

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 2:45 pm

You could look at This on Phidgets. It should connect via USB to the R-Pi, and have minimal wiring requirements.

If you look around on their site, you can find other sensors/controllers to determine other items that may be appropriate for your home automation desires.

Good luck.

celerity
Posts: 20
Joined: Mon Nov 28, 2011 1:50 pm
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 3:48 pm

Thanks wrhii, that page has code samples too, which is great.
It is a bit pricey, but it's interesting to see an example product. Also it has four switches, so maybe there are cheaper, single switches about as well.

Out of interest, how would you safely connect a standard mains appliance to one of those outputs?
Forgive the dumb question, but I really am a novice when it comes to electrical wiring.

/\dam

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automatio

Mon Nov 28, 2011 4:09 pm

Quote from celerity on November 28, 2011, 15:48
Out of interest, how would you safely connect a standard mains appliance to one of those outputs?
Forgive the dumb question, but I really am a novice when it comes to electrical wiring.


No problem, always happy to answer a question asked before the magic smoke is let loose. :D And, it is certainly possible that there are cheaper Relay's available. I have not looked around much, but for what I am going to want Phidgets had close to the right price. I need more than one sensor, so their main board ends up being cheaper to me.

The easiest way to wire this would be to wire the hot leg of the power cord through the Raspberry, and wire the neutral (and ground if present) straight to the device. This would allow the Raspberry to serve as a lightswitch essentially. However, if you have a switch that should control this also, then you will probably want to wire the Raspberry in parallel, not in series.

[moderator] Warning, this is a joke, don't take seriously!

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 4:10 pm

I'm going to give a non-answer, you have been dully warned :P

First purchase or check out or google some basic books on soldering, for circuit design from the ground up I recommend Make - Electronics

Then get yourself a cheap 10-20 USD soldering iron and a pcb with lots of through holes, then learn how to solder :P

In all honesty, soldering is a very daunting prospect, but turns out to be a much easier reality than you fear it to be. It's a great skill and opens a *huge* world to you. Plus you'd only need a very low level of soldering skill in the end to do exactly what you're suggesting. While the set up cost might cost you... lets say 30-40 USD (15 for soldering iron, 5 for solder, 5 for flux, 5 for PCB board + 10 for margin of error of the cost), the amount of money it can save you in the long run is phenomenal! In the end it would probably cost you more to learn and then build the item wrhii links (which is a fairly reasonable price), you would see a return on investment for the next item you made, and the one after that and so on. It's a talent that very quickly pays for itself (and is fun in the process).

Okay. My sales pitch is finished!
Dear forum: Play nice ;-)

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 4:23 pm

Quote from abishur on November 28, 2011, 16:10
I'm going to give a non-answer, you have been dully warned :P

First purchase or check out or google some basic books on soldering, for circuit design from the ground up I recommend

... truncated
Okay. My sales pitch is finished!

Won't disagree with the soldering, or the overall recommendation. If you wanted to do some soldering, you may be looking for something like:
this from digikey

Basically, you need a relay, and it would need to be driven by the 5V or 3.3 V connections on the GPIO segment of the board. You'd also need to build the relevant driver(s) to manipulate it, so it would be a good learning opportunity.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23636
Joined: Sat Jul 30, 2011 7:41 pm

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 4:30 pm

The GPIO's by themselves don't have the grunt to switch a relay I don't think - you may need some stuff inbetween.

See the GertBoard, should do what you need with he right bits and pieces.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

User avatar
Burngate
Posts: 6003
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 4:35 pm

+1 to all above.
Only one caveat: mains can Bite!
Make sure the mains can't reach the Pi - magic smoke - or you - unhappy parents.

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 4:48 pm

@Jamesh - Really, not even a 3-5V DC input relay? Duly noted!

Burngate is absolutely correct! Either use a traditional relay with appropriate protection or a Solid State Relay (which has the proper protection built in by its very nature!)
Dear forum: Play nice ;-)

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 5:16 pm

Quote from abishur on November 28, 2011, 16:48
@Jamesh - Really, not even a 3-5V DC input relay? Duly noted!

Actually, I think he might not be right.
10 ma
25 ma
10 ma
These should all be do-able for the Raspberry.

It is still possible that additional power will be necessary to satisfy everything. And, the warning related to magic smoke is still applicable.

bradburts
Posts: 341
Joined: Sun Oct 02, 2011 7:07 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 5:29 pm

http://www.reuk.co.uk/ have a guide involving modifying the DC side of a digital timer. That keeps you away from the AC. Cannot find the link but its on their site.
The short answer is don't though. At least don't start playing with AC side unless you know what you are doing. Can you rate the relay by yourself? Do you know what to do with regard to grounds and fuses etc. If you do then that's one thing, if you don't make sure you have a teacher other than experience!
Keeping DC side again then there are home plugs such as the x10 which will do the AC job for you. I have seen PIC guides which show you how to drive an x10 network from a PIC. I cannot remember if you have to interface to AC though to communicate. Maybe you could buy this module. May be worth having a look for ideas.

celerity
Posts: 20
Joined: Mon Nov 28, 2011 1:50 pm
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 5:47 pm

All good advice, thanks everyone.
I can actually solder, but would be wary of going near anything with mains current, unless it was a (very) well documented hack.
To give you some idea, I'm still somewhat confused why nobody is talking about fuses with relation to wrhii's proposed solution. Presumably this is obvious to anyone with even hobbyist electrical knowledge, but not to me ;-)

/\dam

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:02 pm

Quote from bradburts on November 28, 2011, 17:29
Keeping DC side again then there are home plugs such as the x10 which will do the AC job for you. I have seen PIC guides which show you how to drive an x10 network from a PIC. I cannot remember if you have to interface to AC though to communicate. Maybe you could buy this module. May be worth having a look for ideas.

Yes, X10 would be fine, but I'm not sure what it's compatibility is with Linux. The only set of X10 module's I have had direct experience with were Serial, and had no known Linux support. Maybe that's changed since in the last several years. I have not needed it, so have not looked.

(ok, did a quick google search for linux x10 and got this for Dummies. Might be a good start if you want to go X10 route. )

Quote from celerity on November 28, 2011, 17:47
I can actually solder, but would be wary of going near anything with mains current, unless it was a (very) well documented hack.
120V is not really that bad to work with, especially since there is no reason to do this 'hot'.

To give you some idea, I'm still somewhat confused why nobody is talking about fuses with relation to wrhii's proposed solution. Presumably this is obvious to anyone with even hobbyist electrical knowledge, but not to me ;-)/\dam
There's no reaon for a fuse when you are using a relay that is rated to support the 120VAC anyway. First of all, the relay will serve as a 'fuse' if more than 120VAC shows up (for some unknown reason), and secondly the relay separates the 5V from the 120V. Most of the relays I've linked to are actually Solid State as far as I could tell. Thus, no reason to fuse.

That's not to say you can't, or that it would be bad to put one in line, but no requirement for it. Also, keep in mind that in all of my comments when I'm saying 'main', what I really am talking about is no different than wiring a light switch. No one puts a fuse on an individual light switch. You have circuit breakers installed in the house for a reason.

celerity
Posts: 20
Joined: Mon Nov 28, 2011 1:50 pm
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:08 pm

We use 240V here in the UK, although I'm sure that doesn't alter what you're saying.

RichC
Posts: 20
Joined: Thu Jul 28, 2011 9:20 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:10 pm

Relays are not always the best tool for the job. Okay, they're simple and can be rated for mains power, but the contacts can degrade and stick when when switching inductive loads. The right kind of contacts and a protection circuit help.

Years ago my ex company designed a small machine with a mains motor and solenoid, both driven using optocouplers and triacs. It had to fire a paper tab into a stack of paper every 10 seconds. So there are alternatives.

celerity
Posts: 20
Joined: Mon Nov 28, 2011 1:50 pm
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:14 pm

Quote from bradburts on November 28, 2011, 17:29
http://www.reuk.co.uk/ have a guide involving modifying the DC side of a digital timer. That keeps you away from the AC. Cannot find the link but its on their site.

It's beyond my skillset to attempt this, but I think this is the page you mean: http://www.reuk.co.uk/Convert-.....oltage.htm

Hope that might help someone,

/\dam

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:37 pm

Quote from celerity on November 28, 2011, 18:08
We use 240V here in the UK, although I'm sure that doesn't alter what you're saying.
It could, though I will say I do not know enough of the specifics of what you will encounter to be able to say for certain. I'd encourage doing some research, and honestly you could just as easily test the relays with an extension cord and a lightbulb as anything else. If a relay is rated for the 240V, then it should be able to handle it. The statements of relays wearing is true after a fashion. They will generally degrade from use, but it is really no different for an x10 unit than it would be others (theoretically). Quality of the relay may differ in the x10, but the principle is the same. They have to use a relay or a switch.

So, if you have a Normally Open (NO) relay, that you have to hold closed to turn on the heater, then if the Raspberry fries the relay will break the circuit.

If, on the other hand, you used a Normally Closed (NC), then the Raspberry has to be able to open it to break the circuit. This is less ideal.

made no check of the relays previously listed for 240V or for NO or NC status.
is not an electrician, and certainly not in the UK
all advice is 'to be used at your own risk, please don't make the nightly news'

kme
Posts: 448
Joined: Sun Sep 04, 2011 9:37 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:46 pm

Quote from celerity on November 28, 2011, 18:08
We use 240V here in the UK, although I'm sure that doesn't alter what you're saying.Well, actually you don't. You run 230V and have done that for 10-15 years when EU harmonized UK 240V with continental Europe's 220V and everything ended up being 230V. It really didn't matter much as the switch was within the normal voltage fluctuation anyway. But it did limit some confusion. EU actually does do make sense some times.

bradburts
Posts: 341
Joined: Sun Oct 02, 2011 7:07 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 6:57 pm

Quote from wrhii on November 28, 2011, 18:02
There's no reaon for a fuse when you are using a relay that is rated to support the 120VAC anyway. First of all, the relay will serve as a 'fuse' if more than 120VAC shows up (for some unknown reason), and secondly the relay separates the 5V from the 120V. Most of the relays I've linked to are actually Solid State as far as I could tell. Thus, no reason to fuse.

That's not to say you can't, or that it would be bad to put one in line, but no requirement for it. Also, keep in mind that in all of my comments when I'm saying 'main', what I really am talking about is no different than wiring a light switch. No one puts a fuse on an individual light switch. You have circuit breakers installed in the house for a reason.

You have to be careful. Not knowing your circuit, your case (if any) your cable routing, the actual relay you will use then I would say use a fuse.
The relay may serve as a fuse if the short is after the relay. What if the short is earlier though?
A lot of thought should go into circuit protection and protection right back towards source is best.
The devil will be in the detail and as such none of us can provide more than background help.

If wrhii has an X10 with a serial interface then you will be good to go with the RPI. Confirm that you can interface with RS232 but if so it should be relatively easy for you to interface with the X10.Grab a USB/232 dongle and print your commands to the /dev/whateveritscalled. Layer 2 drivers are for posh people.

PS
In the UK we use 400V should you go with an SCR.
I hope that that gives you pause ;)

wrhii
Posts: 66
Joined: Fri Nov 18, 2011 1:47 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 7:17 pm

Quote from bradburts on November 28, 2011, 18:57
... truncated...
The devil will be in the detail and as such none of us can provide more than background help.

If wrhii has an X10 with a serial interface then you will be good to go with the RPI. Confirm that you can interface with RS232 but if so it should be relatively easy for you to interface with the X10.Grab a USB/232 dongle and print your commands to the /dev/whateveritscalled. Layer 2 drivers are for posh people.

The actual X10 unit was not serial interfaced, but there is a Serial connector (called a Firecracker) to allow your PC to serve as a remote control. That is what I had. Then the 'base' unit for the X10 plugged into an outlet, and you plugged 'whatever' into it. As I recall, the unit I had was ungrounded, but there may be other choices now than I had available.

In any event, hopefully this has given you a few things to think about and consider so you can research how you want to try to heat your office.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 23636
Joined: Sat Jul 30, 2011 7:41 pm

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 7:22 pm

Quote from wrhii on November 28, 2011, 17:16
Quote from abishur on November 28, 2011, 16:48
@Jamesh - Really, not even a 3-5V DC input relay? Duly noted!

Actually, I think he might not be right.
10 ma
25 ma
10 ma
These should all be do-able for the Raspberry.

It is still possible that additional power will be necessary to satisfy everything. And, the warning related to magic smoke is still applicable.

Those numbers are pushing it a bit - they default to 8ma, although I think they can go up to 16ma.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
"My grief counseller just died, luckily, he was so good, I didn't care."

bradburts
Posts: 341
Joined: Sun Oct 02, 2011 7:07 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 7:32 pm

http://www.uk-automation.co.uk.....;7D12.html
Its Windows only. Wonder if WINE would do it. There may be a linux driver in prep somewhere and there should be libraries around, I have seen PIC examples & source before but cannot remember how they interfaced.
Failing that it should be fairly easy to decode the command structure given that the device allows RS232 communication.
As said, more than a few ideas to try.
No reason for you to touch the AC side anyway.

User avatar
abishur
Posts: 4477
Joined: Thu Jul 28, 2011 4:10 am
Location: USA
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 7:44 pm

Another reason a fuse hasn't really been mentioned is in home automation you have a a fuse box at the root of your power. Relays are fairly analogous to an automatic light switch, when it has power on the input side (a low power DC input usually) it closes the switch on the other side (which in this case is 120V for USA or 240...ish V for Europe). You never worry about putting a fuse on a light switch :)

I wouldn't worry about working with mains power voltages. Just make sure you've turned the power off at the fuse box before doing any work on it and you'll be just fine :)
Dear forum: Play nice ;-)

bradburts
Posts: 341
Joined: Sun Oct 02, 2011 7:07 am

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 7:51 pm

PS
I will be doing a project like yours in a couple of months.
Do you have a Sunnyboy inverter?
If so you could just use the SB multifunction relay.
If you have an SB then its easy, you just need to add a thermostat and a second power relay.

If you don't have an SB then the measurement electronics will get a little involved.
Have a look at http://openenergymonitor.org/emon/ you will find all the circuits etc that you need here. They are a friendly group.

User avatar
glenn66
Posts: 55
Joined: Fri Nov 25, 2011 4:06 pm
Location: Belfast
Contact: Website

Re: Controlling a mains power socket switch? (home automation)

Mon Nov 28, 2011 10:05 pm

Be very, very careful with mains voltages. Keep the mains voltages well away from your logic.

That said, I've used the optocoupler-triac approach to control home heating pumps with PICs in several applications and swear by it. For one thing you avoid problems with start-up currents and can employ a zero-crossing circuit to control motor speed using PWM.

Return to “Other projects”