240 v mains controller


29 posts   Page 1 of 2   1, 2
by Haloric » Mon Jul 23, 2012 12:20 pm
Hi,
I am looking for the simplest way to control a mains power socket from my pi.

I'm happy with any amount of programming, using existing boards and minimal wiring together, but I want the minimum amount of custom hardware.

I have been looking at RF remote control power strips from Maplin (5 sockets for £30!) , to completely isolate the mains from the pi, but am open to using anything that I don't have solder up. I'm not sure however that they are controllable from a pi.

http://www.maplin.co.uk/remote-controll ... ack-348217
I'm planning on making a simple sous-vide cooker.

Has anyone already solved this problem without making their own hardware.

Thanks,
Posts: 2
Joined: Mon Jul 23, 2012 11:35 am
by Hugh Jarse » Tue Jul 24, 2012 8:26 am
If you are going the RF route to control mains, you need to interface to a transmitter. The maplin remote control is one way, but you will need to step the voltage up to make this workable. The Pi GPIO digital lines are 0-3.3v. To drive the maplin kit (R433A transmitter) you need 0-5v minimum, and if you want this to work over a longer distance or noisy environment you should really be looking at 0-12v.

This means that you need something to step the signal voltage up - which usually involves a small amount of soldering. A transistor is a good way and this would even fit inside the remote control on a piece of vero. One slight wrinkle with a single stage transistor is that you should remember to invert the wave form in software so that after the transistor inverts it it is the right polarity.

In principle you could use an opto isolator (which still requires soldering) - however (and I have not checked the data sheets) I would expect that to be able to switch at the speed required (every 400 us or so) would be beyond the capabilities of the integrated opto LED.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Wed Jul 25, 2012 11:01 pm
Just to say that I have put a prototype together which does this and turns 4 sockets on or off from a single GPIO pin (via a transistor). In the end I ditched the remote control because I could not get my signal to coexist with the encoder on the PCB. Instead I went for one of these

http://proto-pic.co.uk/434mhz-rf-link-transmitter/

which works well with the maplin RF sockets. The code I originally wrote to drive the remote control is unchanged. The only thing I had to do was to add an antenna which made a big difference to the number of times I had to repeat the pulse train for each "switch press".

I have nginx (web server) installed on the raspberry pi, and the next step after tidying up the prototype is to drive the code from a web page on the internet to make it truly remote.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Haloric » Thu Jul 26, 2012 8:23 am
Thanks for the information - on the shoulders of giants I shall attempt to stand.

I've ordered one of those transmitters, and I'll see how far I get !.

Thanks
Posts: 2
Joined: Mon Jul 23, 2012 11:35 am
by Hugh Jarse » Thu Jul 26, 2012 10:47 am
For reference, the level changer I used was an open collector NPN ("2N3904) with a 4K7 pull up resistor, and a 470 ohm base resistor (3.3v/470 = 0.7ma, GPIO is limited to 0.8ma). For now, I am using the 12v cell from the remote control to provide the 12v.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by felixfurtak » Tue Jul 31, 2012 5:37 am
Have a look a the Piface. http://pi.cs.man.ac.uk/

This has two relays on it that can switch mains voltage (up to 10A, as long as you are careful!). It also has 8 digital I/Os that could be used for other applications.

It piggybacks on top of the Pi and costs 30 gbp.

There is already some good simple software around to drive it using simple commands to turn relays on and off. https://projects.drogon.net/raspberry-pi/wiringpiface/

I bought one recently and it works pretty well.

Felix
Posts: 47
Joined: Wed Nov 16, 2011 10:41 am
by Hugh Jarse » Tue Jul 31, 2012 8:00 am
I had considered using relays but ruled them out for this application because :

1) direct switching of mains and routing 240VAC via the board could be hazardous especially if the Pi is prone to moving around by its cables and you try to reposition/catch/move it by hand. Probably not suitable for a classroom project.

2) It means that you would have to ensure that the relays were rated at 10's of amps as anything mains could get connected through them.

3) using the relay as an alternative to the transistor above, to generate the waveform is unlikely to work because it needs to switch at over 2KHz. I would expect a relay to be able to switch in the region of 40-80 Hz or so.

That said, that looks like a really nice board. I want one.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by gordon@drogon.net » Tue Jul 31, 2012 6:22 pm
Hugh Jarse wrote:I had considered using relays but ruled them out for this application because :

1) direct switching of mains and routing 240VAC via the board could be hazardous especially if the Pi is prone to moving around by its cables and you try to reposition/catch/move it by hand. Probably not suitable for a classroom project.

2) It means that you would have to ensure that the relays were rated at 10's of amps as anything mains could get connected through them.

3) using the relay as an alternative to the transistor above, to generate the waveform is unlikely to work because it needs to switch at over 2KHz. I would expect a relay to be able to switch in the region of 40-80 Hz or so.

That said, that looks like a really nice board. I want one.


not sure I'd like mains that close to my Pi, however I have had mains that close to arduinos using solid state relays...

But sometimes you just want an easy solution - and there are various main sockets that can be switched via USB - first hit on google gives me: http://www.audon.co.uk/usb_digital/np8800.html but I know there are many others - some with multiple outlets individually switchable - all at the end of the USB cable...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by Hugh Jarse » Wed Aug 01, 2012 7:33 am
gordon@drogon.net wrote:But sometimes you just want an easy solution - and there are various main sockets that can be switched via USB
-Gordon


I agree. If you just want to achieve the end goal then there are products already available for most things. However if you are more interested in the journey, and want to play at faking a remote control in software, the end solution might differ. Even more interesting because one would not normally use a multitasking OS to produce 2Khz waveforms :-).
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Wed Aug 01, 2012 9:03 am
Just to say that I have completed my prototype. I have four sockets which can be switched on or off, over the internet from a web page. Each sockets state is displayed as an LED icon on the web page (green = on, grey = off).

These icons represent the real state of power output, rather than being faked based on the last on/off state pressed. They are fed from the socket outlet under control via a 240vAC-3.3v conversion and fed into a GPIO input pin which is polled.

Whenever the state changes a file dedicated to a socket has its value changed to 0 or 1. The web page in turn reads these files uses ajax/javacript and displays the appropriate icon.

If anyone is interested in any of this I can make it available, including images from the scoping of the maplin remote control encoder which I used to shape the waveforms I sent to the RF transmitter.

Thanks to Mike for his C GPIO library
http://www.open.com.au/mikem/bcm2835/
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by duncanmcg » Wed Aug 01, 2012 12:30 pm
I also have this up and running - viewtopic.php?f=37&t=10667&p=119043#p119043

It's worth noting that I've had some pretty ropey receivers from Maplin - 1 in 4 seem to be duff at the moment.
Posts: 6
Joined: Sun Jul 08, 2012 7:30 am
by Hugh Jarse » Sat Aug 11, 2012 9:42 pm
I have finally managed to put the initial documentation for this project together. It is at:

http://www.mhr.me.uk/projects/socketcontrol.html
Last edited by Hugh Jarse on Tue Oct 29, 2013 11:59 pm, edited 1 time in total.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by DexOS » Sun Aug 12, 2012 2:33 pm
Hugh Jarse wrote:I have finally managed to put the initial documentation for this project together. It is at:

http://www.mhr.me.uk/projects/RemotePowerRpi.html

Great write up, thanks.
Batteries not included, Some assembly required.
User avatar
Posts: 851
Joined: Wed May 16, 2012 6:32 pm
by Hugh Jarse » Thu Oct 18, 2012 8:37 am
I have made a small change to the project weblog to add the construction of a shield from vero to mount the external prototype board inside the Raspberry Pi box.

http://www.mhr.me.uk/projects/socketcontrol.html

However feeding back a DC signal from each socket to indicate the switched state does not seem that nice a solution. So next I will be looking at implementing a SCADA platform to allow analogue inputs/outputs as well as digital outputs to be controlled from the Raspberry Pi. This obviously goes well being controlling 240v mains, but looks relatively straightforward to do with off the shelf components.

I will add further updates when I have developed this further.
Last edited by Hugh Jarse on Tue Oct 29, 2013 11:59 pm, edited 1 time in total.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Sun Jan 13, 2013 10:06 am
Due to recent interest in this project (not sure why this has just happened) I have started adding some of the downloads to the project page. This should reduce requests for these now, but any questions feel free to contact me.

http://www.mhr.me.uk/projects/RPiProjects.html
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by s7mx1 » Sun Jan 13, 2013 11:18 am
Try this
viewtopic.php?f=37&t=26485

and this

https://github.com/s7mx1/pihat

Get status remote control socket from either asda or Morrison. Cost is £5 per socket. No any additional hardware needed.
Posts: 79
Joined: Fri Sep 30, 2011 9:28 am
by Hugh Jarse » Sun Jan 13, 2013 5:35 pm
That is elegant, but a very different project. The trade off for no components is that the the transmitter has 1/80th of the power and there is no state feedback. A very nice bit of engineering nevertheless.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by s7mx1 » Sun Jan 13, 2013 6:50 pm
Hugh Jarse wrote:That is elegant, but a very different project. The trade off for no components is that the the transmitter has 1/80th of the power and there is no state feedback. A very nice bit of engineering nevertheless.


@Hugh

The range as stated in that thread is far better than the original remote comes with the power socket. I get more than 3 meters (with wooden floor in between) and its more than enough for my need. That's without filter added and I can only expect the range to increase with proper filter applied (redhawk has put a simple filter design if you wish to implement).

The feedback could be easily added via other methods like a single chip wireless (wifi/bluetooth/zigbee) client or wireless arduino if you prefer with 5v usb power attached to the power socket. The limitation is only your imagination.

This method is really unique in a such way that no other external transmission chip is involved and the cost of setting the basic system up is extremely low.
Posts: 79
Joined: Fri Sep 30, 2011 9:28 am
by sportsnapper » Sun Jan 13, 2013 7:14 pm
Hugh, I suspect that the increased interest is because of an article in the latest MagPi about remote controlled sockets.

I've yet to get your solution working - not sure why and have been distracted by trying to get a couple of other things working. In your wiring diagram, you are using pin 26 as the output to the RF transmitter. I'm assuming this is header pin26, BCM GPIO pin 7. Also I've ignored the 12v output for the moment, and consequently removed the need for inversion - so my output goes straight to pin2 of the RF transmitter.
Posts: 58
Joined: Wed Sep 05, 2012 11:27 am
by Hugh Jarse » Sun Jan 13, 2013 10:23 pm
Yes it uses GPIO07, the opposite corner pin from 3v3 pin.

Probably the best thing to do is to scope the pin it to see what is happening.

The other thing is that 3.3v is right on the limit of the transmitter - the datasheet quotes 3 - 12v, and the original remote control used 12v too.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Sun Jan 13, 2013 10:29 pm
s7mx1 wrote:The feedback could be easily added via other methods like a single chip wireless (wifi/bluetooth/zigbee) client or wireless arduino if you prefer with 5v usb power attached to the power socket. The limitation is only your imagination.


I am just coming to the end of the project which followed the RF transmitter, and this uses a number of ATMega328s circuits which are all controlled and chatted to from a web page hosted on a central RPi. Similar to SCADA in some ways I guess. This works quite nicely and as well as allowing feedback provides Analogue In/Out and Digital in/Out from each device. Obviously this does not have the elegance in terms of low component count as your project, but I think we have different aims.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Sat Feb 02, 2013 9:56 am
sportsnapper wrote:Hugh, I suspect that the increased interest is because of an article in the latest MagPi about remote controlled sockets.

I've yet to get your solution working - not sure why and have been distracted by trying to get a couple of other things working. In your wiring diagram, you are using pin 26 as the output to the RF transmitter. I'm assuming this is header pin26, BCM GPIO pin 7. Also I've ignored the 12v output for the moment, and consequently removed the need for inversion - so my output goes straight to pin2 of the RF transmitter.

Please note that because this solution works by bit banging and producing waveforms based on a given clockspeed, it is unlikely to work reliably if the RPi has been overclocked. Waveform patterns were based on the recommended 700MHz clock speed settings.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Sat Feb 02, 2013 10:37 am
Hugh Jarse wrote:Please note that because this solution works by bit banging and producing waveforms based on a given clockspeed, it is unlikely to work reliably if the RPi has been overclocked. Waveform patterns were based on the recommended 700MHz clock speed settings.


It addition the original implementation of the mains controller used version 1.5 of Mike McCauley's bcm2835 library (bcm2835-1.5.tar.gz). I have tried building with a later version and the sockets do not switch reliably. This is possibly because version 1.9 of the library the delayMicroseconds() function was reimplemented.

See the version history at : http://www.open.com.au/mikem/bcm2835/index.html

I have just rebuilt the mains controller code using version bcm2835-1.5 and it works perfectly.
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by Hugh Jarse » Wed Oct 30, 2013 12:02 am
I have made some updates since I first posted the remote mains socket controller info.

The link now contains an update to the source code to work with the latest versions of the bcm2835 library, an example of using cron for automatic timed switching of sockets on/off, and update the circuit diagram, and a few corrections.

http://www.mhr.me.uk/projects/socketcontrol.html
Posts: 46
Joined: Mon Mar 05, 2012 5:03 pm
by vectraboyv6 » Tue Nov 12, 2013 9:32 am
Hugh,

i have followed your webpage and i must say your are a lot cleverer than me.

i have brought some remote sockets from clas ohlson and just want to test if they work i have a cheap 433mhz transmitter from ebay and just want to test it.

http://www.clasohlson.com/uk/Remote-Con ... r182035000

i have your sckctrl.c file how would i modify it not to invert the signal and not use the gpio switch on/off line?

i would build the circuit if they test out ok
also being a newbie does this file then need compiling or something to make it run via bash command?

thanks for your help stuart
Posts: 8
Joined: Fri Aug 17, 2012 11:28 am