cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Desktop PC Power Control

Fri Oct 11, 2013 4:26 pm

Hey all,

Ive had a Pi since the first batch ever rolled off of the production line, but it generally sits in my cupboard :( Im a Linux noob really and it has given me some great insights but found it generally a pain to use as a desktop OS.

Anyway, I knew one day id need it, and here it is, but I need some help if possible :)

I want to use the GPIO headers to control the power and reset functions on one of my desktop PC's. This is headless and I always remote access it as I've built web based systems to do everything I need to do remotely but occasionally like today it locked up.

I get to work at 9 Am and wanted to access it, I guess something has failed and I cant and im not home for another few hours (I know, first world problems and all...).

I'm thinking I can detach the case buttons as I wont need these, and I can attach the motherboard headers to the GPIO pins on the Pi.

The switches on a case are momentary and are just a + and a - for each button. Obviously id then need to figure out how to create a web page where I can click a button that activates the GPIO's and "pushes" the reset/power buttons, but I have seen examples of this on the web. The part im struggling with is the GPIO section as ive never dealt with these before.

I really want to do this without any extra hardware at all, just a Pi and the necessary connecting wires. Really hope some of the forum experts can lend a hand :)

Thanks in advance for any replies.

pcmanbob
Posts: 6627
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Desktop PC Power Control

Sat Oct 12, 2013 8:38 pm

I don't think you will be able to do this without some extra hardware ( unless some one knows better )
the switches on the front of the pc case are just a simple contact that connects the 2 relevant pins on the mother board you will need to replicate this, the output from the gpio on the Pi are a high or low not a closing contact.
the simple way to do this would to be to use a relay as this can be driven by the gpio on the Pi and will give you a clean contact to replicate the switch.

hope this will give you a starting point if i can give you any more help shout up.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
DavidS
Posts: 4334
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Desktop PC Power Control

Sat Oct 12, 2013 8:42 pm

Slightly OT:

You said that you could not get used to Linux as a desktop OS.

Are you aware that there are other OSes for the RPi?

There is AROS that is almost identical to Amiga OS 3.9, and has the same API as Amiga OS..

There is RISC OS 5.21, that is 100% identical to RISC OS (becaus it is RISC OS :) ) this is the one I use.

There are also a few other non n*x OSes though I think that those two are the most usable alternative OSes for the RPi.
RPi = The best ARM based RISC OS computer around
More than 95% of posts made from RISC OS on RPi 1B/1B+ computers. Most of the rest from RISC OS on RPi 2B/3B/3B+ computers

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Sat Oct 12, 2013 11:17 pm

Thanks for the two good replies guys.

As I say I had my model B right at the start and as such there was only raspbian available. Ive spent a few hours this evening figuring things out and still think its a good distro to use, especially for this project as it includes the GPIO binaries (or whatever they're called...)

Ive found a twin relay on eBay which should be able to accept the GPIO inputs. The relay block ust has a +, Relay 1, Relay 2,- inputs. Obviously the + and - will come from the GPIO and Relay 1 and 2 can be controlled by one of the addressable GPIO pins, the relays will then do the switching.

I think I have the code figured out too, but someone mind verifying it. Il be using a web page and a form as such to call the .py file with the code in it. This is stuff ive done before (not calling python mind, but same principle), im just not confident on the code.

For example, if I connect the relay's control line to GPIO17 and then run this code, what id expect to happen is it will turn on the line for 11 seconds, then turn it off (emulating what happens with a PC power switch, these pass 5 V through when you press them). Im probably using the wrong terminology however...

Code: Select all

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, True)
time.sleep(11)
GPIO.output(17, False)

User avatar
BAStumm
Posts: 134
Joined: Fri Aug 23, 2013 3:37 pm
Location: Loon Lake, WA USA
Contact: Website

Re: Desktop PC Power Control

Sun Oct 13, 2013 1:40 pm

Just make sure the relay can latch using 3.3vdc on the input side. The relay output pins will close the connection on the motherboard. The motherboard is providing it's own power and the relay is like a light switch (open or close the circuit).

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Desktop PC Power Control

Sun Oct 13, 2013 3:16 pm

Hi,

A simple circuit consisting of a general purpose npn transistor e.g. a 2N5088 and a couple of resistors works fine. I have two RPi's and each one can reset the other one using the reset pins. The same approach would apply to controlling the reset or power switches on a PC.

With a transistor based circuit you do not have to worry about 3v or 5v and there is no risk of damaging a gpio from the back emf (reverse voltage spike) that occurs when a relay switches off.

The circuit consists of the gpio pin connected to the base of the transistor through a 2K2 resistor and a 10K resistor connected between the base and ground. The 10K resistor ensures that the transistor is non-conducting, until the gpio is pulled high.

The reset switch is connected across the emitter and collector pins of the transistor. Measure the voltage on the two wires from the reset switch - one is high (3.3v or 5v) and the other is ground (0v).
The 'high' wire is connected to the collector and the ground wire is connected to the emitter of the transistor. (emitter & collector connections corrected - thanks to cosax for pointing this out). A post with a diagram can be found here:http://www.raspberrypi.org/phpBB3/viewt ... 46#p446346

When the gpio pin goes high (3.3v) the transistor conducts and the high side of the reset is pulled down to ground, causing a reset. When the gpio is set low again, the PC restarts.
(You don't need to wait 11 seconds for a reset, although a power off usually requires that the circuit is held low for more than 4 seconds).

You might also consider monitoring your PC by using a ping from the RPi, say every 5 minutes and if there is no response repeat the ping and if still no response, run the reset sequence. Using the crontab in Linux is an easy way to run a script/program at set intervals.

Regards

PS If you decide to use a relay make sure you use a diode across the coil to protect the driving circuit from the back emf spike. You would also need to know that the gpio can provide enough power to drive the relay - typically relays require a transistor based circuit to supply sufficient power from a microprocessor port.
Last edited by anita2r on Wed Nov 06, 2013 1:41 pm, edited 1 time in total.

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Sun Oct 13, 2013 3:56 pm

anita2r wrote:Hi,
When the gpio pin goes high (3.3v) the transistor conducts and the high side of the reset is pulled down to ground, causing a reset. When the gpio is set low again, the PC restarts.
(You don't need to wait 11 seconds for a reset, although a power off usually requires that the circuit is held low for more than 4 seconds).

You might also consider monitoring your PC by using a ping from the RPi, say every 5 minutes and if there is no response repeat the ping and if still no response, run the reset sequence. Using the crontab in Linux is an easy way to run a script/program at set intervals.
Thanks for another helpful reply. The relay im using (http://www.ebay.co.uk/itm/200712189000? ... 1439.l2649) is designed for an Arduino and need 5V input signal, I think I read somewhere that someone gave the relay 5V and a ground, and then used two general purpose GPIO pins to control the latching, we shall see anyway when it arrives, at that price its nothing lost really.

The reason I specified 11 seconds in my (hopefully correct) python script was that I need to hold down my power button for 10 seconds to perform a hard reset, this would be the last options if for some reason the PC didn't respond. Of course, in my other scripts, such as the regular shutdown, I have set the time at 0.5 seconds which should simulate me just pressing the power button.

I never thought of the pinging solution. Il have to work through that in my head and figure out the necessary commands to create a script (I can write that in batch in 30 seconds, but RPI doesn't run windows :lol: ) but it may be useful :)

Parts are all ordered anyway so when I get some time to work on this il keep you all updated.

PS, just to simplify things at the start and so I don't start risking my main PC hardware, im guessing there's no issue with me trying to use the relay board and a 5V LED so I have a visual representation of whether the thing is working (as in, the LED should illuminate when I am sending my button pressing simulations). Will aid in my finding out if my commands, my wiring or the relay circuit is at fault in the event of any issues.

User avatar
BAStumm
Posts: 134
Joined: Fri Aug 23, 2013 3:37 pm
Location: Loon Lake, WA USA
Contact: Website

Re: Desktop PC Power Control

Sun Oct 13, 2013 4:20 pm

RPi is 3v3 on the GPIO so you may need to boost the voltage before it reaches the relay. This would work. https://www.sparkfun.com/products/9118

Just provide 5vdc for the HV side.

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Desktop PC Power Control

Sun Oct 13, 2013 4:52 pm

Hi,

The gpio's on the RPi are 3.3volts, so I don't think that you can run the relay board you mentioned direct from the RPi as it requires 5volts.

If you add a driver circuit to drive the relay board you might as well just use the transistor circuit to directly control the reset or power switch.

My original thought was to use a relay, but the transistor circuit was simple and I already had a few suitable transistors and resistors in my components box.

Regards

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Desktop PC Power Control

Sun Oct 13, 2013 8:52 pm

Hi,

Here is a bash script to test another machine using ping.
If the other machine does not respond to a single ping, then it does a couple of re-tests at 5 second intervals, just to be sure that the other machine has failed.

If it has failed then either add the reset code to this script or call a separate script to do the reset (my preference as it's easier to debug).
This is where the call to the other script would go:
# do a reset using gpio 25
/home/scripts/reset25.sh

This script suppresses the output of the ping command by sending the output to the 'null' device:
ping -c 1 $REMPC >/dev/null

For testing, the script echoes messages to the screen. Once working, a) remove the general "All is well message" and b) redirect the restart message to a log file, for example:
echo "Time for a restart : " `date +"%d/%b/%y at %H:%M"` >>/home/pctest.log

You would have to create the empty log file in the appropriate directory and make it writeable by the owner of the test/reset script (use chown and chmod as necessary). Check the log file to see if your other PC was restarted while you were out!

The time allowed after a reset before retesting is critical because testing too soon would result in another restart - I put 3 minutes into the script:
# startup time for remote PC after reset
STTM=180

Code: Select all

!/bin/bash
#
# script to test if another machine is active
# uses ping as the test
#
# remote machine name or IP address
REMPC=192.168.0.136
# startup time for remote machine after reset
STTM=180
# setup loop parameter for 'today' using today's day number
TODAY=`date +%d`
# set test interval
SLP=300
# set initial status to 'live'
ONOFF=alive
#
# loop only for today - the script is restarted every night
# this script is also started at bootup, so we want it to stop at the end of the day
while [[ `date +%d` == $TODAY ]]
        do
        # wait for 5 minutes at a time
        sleep $SLP
        # test if PC is responding (only do one ping no need to see the verbose result)
                ping -c 1 $REMPC >/dev/null;STATUS=$?
                #status is 0 for success and 1 for failure
                if [[ $STATUS -eq 0 ]]; then
                        # PC has responded
                        # update status
                        ONOFF=alive
                        echo "All is well"
                else
                        # no response so lets re-test a couple of times just to be sure
                        ONOFF=dead
                        for (( N=1; N<=2; N++ ))
                                do
                               # test after 5 seconds
                                sleep 5
                                ping -c 1 $REMPC >/dev/null;STATUS=$?
                                if [[ $STATUS -eq 0 ]]; then
                                        # response received so we're ok
                                        ONOFF=alive
                                        break
                                fi
                        done
                        if [[ $ONOFF == alive ]]; then
                                echo "After a repeat test, all is well : " `date +"%d/%b/%y at %H:%M"`
                                else
                                echo "Time for a restart : " `date +"%d/%b/%y at %H:%M"`
                                # do a reset using gpio 25
                                ## /home/scripts/reset25.sh
                                # wait for PC to restart
                                sleep $STTM
                                # reset status
                                ONOFF=alive
                        fi
                fi
done
# exit at end of today
exit 0
I tested this using two RPi's on my home network and it seemed to work OK in terms of recognizing if the second RPi was live or not and showing the appropriate 'All is well' or 'Time for a restart message'. I wasn't able to test for a single missed ping response followed by a successful ping - but I'm sure you get the idea behind it.

Note the stuff about 'today' is because I based this on a similar script I use for hardware monitoring and the script stops at midnight and a new copy is started just after midnight. Also if the RPi has to be rebooted, the script starts after bootup using @boot in the crontab, and I don't want two copies running when the new copy starts just after midnight.
You could just set it to run an infinite loop, but it would still be advisable to start it from crontab at each reboot. The downside of an endless loop is that as this script will only rarely be required to restart the other machine, you might not know if it locks up.

'Your' user crontab can be accessed from a terminal with:
crontab -e
Any scripts started from this crontab will run as 'you'

If you use sudo crontab -e you will access the root crontab and any scripts started from it will be run as root.

In the script I assumed that any return code from ping, other than 0, was a failure, but you may want to check out ping for other return codes.

Regards

pcmanbob
Posts: 6627
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Desktop PC Power Control

Sun Oct 13, 2013 9:30 pm

I've had a look at the relay board you are buying and it looks like it has opto isolator inputs so it probably will work just fine with the pi.
most of these boards say 5v control signal but in the world of electronics any thing over 2.5v is seen as a high ( ie 5v) as the pi uses 3.3v on the gpio pins it will probably work just fine. just make sure you take the 5v direct from your power supply and not from the pi or you might find the volt drop across the 5v when the relays operate will cause the pi to reboot randomly.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

jsobota
Posts: 42
Joined: Tue Jul 10, 2012 3:24 pm
Location: Plzen, Czech Republic

Re: Desktop PC Power Control

Sun Oct 13, 2013 11:15 pm

The relay board is just fine. Even if it is marketed as Arduino board (which is 5V) the pins controlling the relays are at approx 3V in the default state and you have to pull them low (or connect them to GND) to "fire" the relay (i.e. it has inverted behaviour). There are PC817 optocouplers so it is absolutely safe to connect this relay board directly to the RPi. Moreover, I am able to power my RPi and this relay board (powered from the pins of RPi) from a single USB of my laptop and no unexpected resets occur.

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Mon Oct 14, 2013 9:32 am

Excellent. I did think those two IC's behind the relays were opto isolators but its a new word for me so wasn't 100%. Its also good news the relay will work.

I have ordered appropriate cabled to power the relay board with an external power supply as this did cross my mind, but was doing to try with power direct from the GPIO's first.

Thanks for the bash script, doesn't look a hugely complex language to understand, I think il use this in the end. I plan to control the power states from a web page, I could have this as a button on there to start the CRON job if I wanted to turn it to "auto" mode.

Does my script to do this look OK (now modified to accommodate the backwards mode of the relays, IE theyre pulled low, not high)

Code: Select all

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, False)
time.sleep(11)
GPIO.output(17, True)
Secondary question. Could I somehow monitor the status of a pin, IE, connect the RPI somehow to the motherboards power LED header so I can see remotely whether the :LED is getting power and therefore the IS should be on. I believe the LED again is a 5V one. Could this be done by setting a GPIO pin to IN rather than out, connecting a lead to the LED+ pin on the motherboard and attaching an resistor inline to drop the voltage to a Pi safe level?

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Desktop PC Power Control

Mon Oct 14, 2013 6:05 pm

Hi,

You could put two 5K resistors in series between the LED 5V wire and ground.
The junction of the two resistors will be at 2.5V which will be read as a ON or high by a gpio port set as an input.

If you want to use a web server on your RPi to control the gpio ports you will have to deal with the issue that the gpio controls in sys/class/gpio/ require root access (e.g. through sudo with a password), but the web pages do not (and should not) run as root. Depending on web server, you may find that the owner is www-data.

I have seen a few posts about this but have never tried any of the solutions.

I have used a somewhat complex solution for about a year now. This is what my web page looks like:
Image

My solution involves using a 'despatch script' that is called by a javascript function when web page buttons are clicked.
The 'despatch script' is able to call scripts in a specified folder using sudo without a password (setup in the sudoers configuration file). The scripts in the specified folder and only this folder, can be called using sudo without a password and then run as root and access /sys/class/gpio.

The despatch script also receives the gpio port information and passes it back to the web page as an in-line frame, which loads and triggers a javascript function which reads information in the frame and uses it to update the buttons on the main part of the web page.

The despatch script is executable by the web page owner, but is not writeable by the web page owner. The despatch script isolates the underlying systems that require root access from the web pages. The only scripts that the despatch script accesses are outside the web page area (/var/www) and so are not accessible to web users.

All clear as mud ... :)

Well it works for me!

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Tue Oct 15, 2013 8:21 am

Hah, clear as mud indeed! But your web-page does look very "fully featured" so to speak !

Your solution sounds like what im doing, I think anyway! My page currently just has buttons set up as a form in HTML, one for hard reset, power and reset buttons. The forms are set up as POST and the button press via the POST function calls a second script that performs the action, this is shown in my post above.

Obviously my first step is to install raspbian, get shell access sorted and wire everything up and test if I can control the switching of an LED, im guessing I can just use the same script I have above as this should power it on and off..

Then il connect the relay board, move the LED to the relay and see if I can still control it.

Last, connect the relays to the motherboard power pins, run the command again and hope I don't kill anything!

Then figure out, using your helpful instructions above, how I can get the scripts to perform via the web rather than via shell access.

Its an interesting journey this, learning so many new things all at once!

Sounds like a plan to me!

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Tue Oct 29, 2013 9:04 am

Morning all

Just wanted to update you all as now my parts have arrived.

Firstly, my python script was wrong, but only a little bit, but anyways, I have the Python working 100% now, and I can control the relays either by entering the python manually or by calling

Code: Select all

sudo python /var/www/python/pw_off.py
when SSH'ing to the pi

However I simply could NOT get it to work via the web. Looking at the apche2 logs, I could see it was a permissions issue and have spent most of the day hours trying to resolve it and tart it up a bit, and im finally done :D. Im guessing some may not like my solution and may see it as insecure, but in all honesty, the entire website is protected by a htaccess/htpasswd file and also the most damage someone can do is reset my PC...

I performed a small alteration to my 000-default file in Apache2 as I didn't want to use the standard cgi-bin directory so I ran " sudo nano /etc/apache2/sites-enabled/000-default" and edited the CGI area to point it to where my python files live, I also vastly reduced the lockdowns on the directory.

Code: Select all

ScriptAlias /cgi-bin/ /var/www/python/
        <Directory "/var/www/python">
                AllowOverride All
                Options All
                Order allow,deny
                Allow from All
        </Directory>

So. I access the PHP page which essentially has a form, this is in the page body.

Code: Select all

<FORM NAME="pwon" Method='POST'>  
			<p>  
			<button name="pwon">Power On</button>  
			</p>  
			</FORM> 
This form then calls the script using PHP, this is in the header

Code: Select all

<?php  
   if (isset($_POST['pwon']))  
   {  
    print ('Power On Command Sent...');  
    $result=exec('sudo /var/www/python/pw_on.py');
   }  
 ?>
This then this calls my pw_on script which is ..

Code: Select all

#!/usr/bin/python
print "Content-Type: text/plain;charset=utf-8"
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.output(23, False)
time.sleep(0.5)
GPIO.output(23, True)
GPIO.cleanup()
To accomplish this, the entire www directory is set as CHMOD 777 and www-root is added to the sudoers list, but is restricted to sudo access solely on the directory where the 4 .py files live like this

Code: Select all

www-data ALL = (root) NOPASSWD: /var/www/python/*
Works for me anyway. Ive also sorted out code to monitor the power light pins on the motherboard, however as that is a 5V pin, I am waiting on some of the aforementioned 5k resistors to drop the voltage to GPIO safe levels. The code currently works, just constantly says "Off" as there's nothing connected to the pin voltage is 0v.

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Mon Nov 04, 2013 2:34 pm

OK, managed to sort out the monitoring of the current PC status too. I don't know much about electronics, so forgive me!

Im using the desktop PC's LED + and - headers on the motherboard as this usually connects to an LED which is on when the PC is obbted up, and off when not.

The suggestion above was to use two 5k resistors to drop the voltage to a Pi safe 3.3v from the supplied 5. Now, I probably did something wrong, but when attaching 1,2,3,4,5,6,7,8,9 or 10 resistors, both in series or parallel the voltage didn't drop for 5v when probing with my multimeter, what did change was the amperage, but this wasn't what I wanted.

I rememberes something once about LED's being useable for crude voltage limiters, so I just twisted a quick circuit with

+v5 > 12v LED > Pi
gnd > pi

I measured the voltage before I plugged the lines into the pi and was pleased to see it at 3.2v. The LED wasn't lit up as the voltage was far below the 12v required, but it limits the voltage perfectly.

My next issue (which I now think the two 5k resistors would have fixed!) was inconsistent readings. When the PC was on, the reading was always correct. but when off, it would fluctuate between reading high and low. I suspected interference somewhere so was probing with my meter like mad but everything read correctly. I did some digging and found that the voltage on the pins when low tends to float due to interference on the pi board itself. I ended up using this code in my python to almost forge the reading to be low when the PC is off.

Code: Select all

GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
And that was it :) Final python code looks like this, which is pretty simple tbh.

Code: Select all

#! /usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
if GPIO.input(17) == GPIO.HIGH:
    print("PC Is On")
else:
    print("PC Is Off")
This is called from a PHP Page upon load

Code: Select all

<!DOCTYPE html>
<html>
<head>
<style>
body{margin: 0; padding: 0; #ebebeb; font-family: Arial, Helvetica CE, Sans-Serif;}
</style>
</head>
<body>
<?php  
			echo ('Current Power State - '); 
			$result=exec('sudo /var/www/python/pw_monitor.py'); 
			print ($result); 
?> 
</body>
</html>
and this page in-turn is called from another php page in an iframe which I then use JavaScript to refresh every 60 seconds so I get an accurate representation of the PC's power state. All controllable from this little web-page which the pi is also running using Apache as ive mentioned before.

Image

jazzler
Posts: 31
Joined: Mon Nov 04, 2013 7:43 am

Re: Desktop PC Power Control

Mon Nov 04, 2013 9:10 pm

Have you seen my project? http://pirelay.jasonfindlay.com

You could use this to control your relays connected to the power pins on the motherboard.

Then you could switch it on/off from your phone?

I could make and send you a custom version pointing to a noip DNS so you could access it from anywhere?

Assuming you use Android that is?
PiRelay Project : http://bit.ly/pirelay
PiRelay Play Store App : http://bit.ly/PiRelayApp

jazzler
Posts: 31
Joined: Mon Nov 04, 2013 7:43 am

Re: Desktop PC Power Control

Mon Nov 04, 2013 9:18 pm

Sorry, just read the rest of your thread and noticed you use iphone and have already found a nice solution.

Good work..
PiRelay Project : http://bit.ly/pirelay
PiRelay Play Store App : http://bit.ly/PiRelayApp

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Tue Nov 05, 2013 3:36 pm

No worries and thanks for the comment. I did see your topic the other day and realised it was very similar to what I was trying to accomplish, but all is now done. im just adding in temperature measurements now as well as tidying up the electronics side of things.

cosax
Posts: 13
Joined: Tue Apr 30, 2013 1:18 pm

Re: Desktop PC Power Control

Wed Nov 06, 2013 6:58 am

Just a simple short question to anita2r:

"The reset switch is connected across the emitter and collector pins of the transistor. Measure the voltage on the two wires from the reset switch - one is high (3.3v or 5v) and the other is ground (0v).
The 'high' wire is connected to the emitter and the ground wire is connected to the collector of the transistor."

Why the high is connected to the emitter and not the collector?

Thanks.

anita2r
Posts: 226
Joined: Sun Dec 23, 2012 6:55 pm
Location: Ottawa, Canada

Re: Desktop PC Power Control

Wed Nov 06, 2013 1:33 pm

Hi cosax,

The emitter should be connected to ground.

I posted a longer version with a diagram here: http://www.raspberrypi.org/phpBB3/viewt ... 46#p446346
The text was correct but when making the diagram I reversed E & C.
I wrote this post using the original diagram as my 'guide'

Thanks for noticing my mistake.

I checked one of my own Pi reset circuits and the emitter IS connected to the ground side of the reset header.

Regards

anitaR2

Digitalblueeye
Posts: 10
Joined: Sun Dec 29, 2013 4:06 pm

Re: Desktop PC Power Control

Tue Oct 21, 2014 11:59 pm

If youd like, i could send some code i made for my pi, it is sms via email... all you would need to do is modify a bit and you could control the power functions via text message or email.
I used it last summer to make a textable deadbolt for my room, I can fetch emails with only the actual messages ( no email headers etc) or send them ( also with attachments, ex if you wanted it to execute a certain program remotely you could attach it)

cs2000
Posts: 14
Joined: Fri Oct 11, 2013 4:17 pm

Re: Desktop PC Power Control

Mon Nov 10, 2014 9:12 pm

The content below has now been updated and works on Raspbian Jessie and Apache 2.4.

The page is now also protected by a login screen (default is admin and password) these can be changed in the fiule login_users.php in the login directory

Hey guys.

My old 32 GB SD card finally died in my RPI, meaning my above setup was dead :( Sadly i wasnt able to restore my old .img file using Win32 DiskImager, i suspect due to the size of the new SD not matching the old one exactly so tonight i have re-done it all from scratch and wrote you all a guide of what to do. Here goes...

OK so start with the latest version of Raspbian and then run the following commands

Update the Repo's

Code: Select all

sudo apt-get update
Install any new updates

Code: Select all

sudo apt-get upgrade
Install Apache 2

Code: Select all

sudo apt-get install apache2
Install PHP5

Code: Select all

sudo apt-get install php5
OK, all software installed,

Configuration time...

Enable CGI

Code: Select all

sudo a2enmod cgi
Modify the CGI-BIN Directory location, run the following

Code: Select all

sudo nano /etc/apache2/conf-available/serve-cgi-bin.conf
And edit the ScriptAlias area block to look line mine

Code: Select all

<IfDefine ENABLE_USR_LIB_CGI_BIN>
                ScriptAlias /cgi-bin/ /var/www/python/
                <Directory "/var/www/python/">
                        AllowOverride All
                        Options All
                        Order allow,deny
                        Allow from all
                </Directory>
        </IfDefine>
Call up the Sudoers list

Code: Select all

sudo nano /etc/sudoers
Add this line underneath the root user to add www-data to the list, this allows it to run the shell scripts that actually perform the resets but gives www-data sudo access to ONLY the directory where our scripts live.

Code: Select all

www-data ALL = (root) NOPASSWD: /var/www/html/python/*
Next, lets give permissions to the WWW directory, again, just run.

Code: Select all

sudo chmod 777 /var/www/html
Finally, restart Apache

Code: Select all

sudo service apache2 restart
That should be it.

So lets move onto the files...

The entirity of my "site" is uploaded here. Simply download, extract and upload to your /var/www/html directory.

Code: Select all

http://www.filedropper.com/powercontrolwithauthentication
Once there, CHMOD all the files through your FTP program. Ensure they are all set to 777 otherwise your scripts cannot be ran from the web.


Lastly, the connections/Hardware...

The list of my GPIO pins and there connections is as follows. Mine is an original spec RPI with the original GPIO layout as shown here

Image

2 - Relay Board VCC
4 - Temp Probe VCC (only needed is you have a DS18B20 & wish to monitor temp's)
6 - Relay Board GND
7 - Temp Probe DQ (only needed is you have a DS18B20 & wish to monitor temp's)
9 - Temp Probe GND (only needed is you have a DS18B20 & wish to monitor temp's)
11 - Motherboard Power LED Positive (Plz note, i have limited the current here to 5V to make it GPIO safe and use a Y splitter so i can still see the power light on my case, you dont have to do this and could therefore not use this pin)
16 - Relay Board IN1
18 - Relay Board IN2
20 - Motherboard Power LED Negative

The actual hardware you need is a DS18B20 to monitor temperature (if you so wish)

Code: Select all

http://www.ebay.co.uk/itm/DS18B20-temperature-measurement-sensor-module-Raspberry-Pi-Arduino-/251709621584?pt=UK_BOI_Industrial_Automation_Control_ET&hash=item3a9b100550
And any 2 relay Arduino (5v) board such as

Code: Select all

http://www.ebay.co.uk/itm/5V-2-CHANNEL-BOARD-RELAY-MODULE-SHIELD-FOR-ARDUINO-ARM-PIC-AVR-DSP-ELECTRONIC-UK-/271450520198?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item3f33b63a86
Both links are of the actual items i have bought.

With the relay board, find your own way (i have used single pin Y cables) to connect relay 1 (on the left ot the relays are closest to you) to your reset pins on your motherboard & relay 2 (on the right) to your power pins)

Lets test!

Firstly, disconnect one of the cables for your reset and power switches, this will allow you to do testing without actually resetting your machine!

SSH to your RPI and run the following command.

Code: Select all

sudo python /var/www/python/pw_off.py
You should hear the relay click and a red light next to Relay 2 illuminate for 1 second orso. Great, this confirms your power "switch" works.

So, lets run

Code: Select all

sudo python /var/www/python/reset.py
Again, you should hear the relay click and a red light next to Relay 1 illuminate for 1 second orso. This confirms your Reset "switch" works.

So, next step is testing from the web. On another machine browse to your RPI's IP address. Mine is http://192.168.0.20 but obviously yours will be different. You should see the page.

The page should show you the RPI's core temperature, PC power status and the PC Interior case temperature assuming you followed all elements of my guide If this all works then it proves your connections between the elemants are good. If not, re-check your GPIO connections between the Thermal probe and between your Motherboards reset pins and the RPI.

So, lets click a button and see if you hear the relays click. If you do, top job :D If not, it usually points to a permissions issue. Either you havnt added WWW-DATA to the sudoers list, you havnt changed the CGI-BIN directory or you havnt ran the CHMOD command on the WWW folder. Re-check these and try again. Obviously check the other buttons all work and then give yourself a pat on the back.

If you did decide to add the DS18B20 temp sensor in, there's a few more simple steps.

Code: Select all

sudo nano /boot/config.txt
Scroll to the bottom and type this there, then save and exit the file.

Code: Select all

dtoverlay=w1-gpio
You have now enabled the 1 wire interface that the sensor uses, next just set it up by entering

Code: Select all

sudo modprobe w1-gpio

Code: Select all

sudo modprobe w1-therm
And you're done, the temperature should now show on the page.

Thats essentially it! Im sure theres a ton of improvements that could be made. The page is op[timised for good viewing on an iphone 5 screen so it could be tweeked to run better on a 6 or other brands of phone. In sure my code and my lax permissions could also do with some work, but fundimenmtally it works and thats a start for you all.

Following this I have installed NOIP so I can access the page remotely no matter my IP address and also secured the entire directory with .htaccess rules etc so its fairly secure in my eyes.

If you do have any suggestions for improvements feel free to make them :)

Return to “Automation, sensing and robotics”