PHP Code to switch a computer On/Off

Forums / NoMachine for Raspberry Pi / PHP Code to switch a computer On/Off

This topic contains 1 reply, has 2 voices, and was last updated by Avatar Britgirl 1 year, 2 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #17822
    Avatar
    backendcoder
    Participant

    I connected my Rasberry Pi server IO to the control pins of my PC: 0V (advise PC switch black wire to go to 0V), power switch pin via 1K resistor (to protect over current), +5V speaker pin (divide the voltage down with 6K8 + 10K resistors in series for powered-up signal to give 3.2V max).

    Then with PHP and wirePi (GPIO control) installed you can run: php pc.php or php pc.php on/off

    Here is the pc.php code:

    <?php

    // Controls the PC via RPi

    if (count($argv) > 1)

    press_button();

    else

    show_state();

    function press_button()

    {

    if (shell_exec(“gpio read 24”) == 0)

    die(“No signal from PC on/off pin!”);

    $before_state = show_state();

    shell_exec(“gpio mode 24 out”);

    shell_exec(“gpio write 24 0”);

    sleep(1);

    shell_exec(“gpio mode 24 in”);

    sleep(5);

    $after_state = show_state();

    if ($before_state == $after_state)

    echo “No change in state!\n”;

    }

    function show_state()

    {

    $state = shell_exec(“gpio read 27”);

    if ($state == 1)

    echo “PC ON\n”;

    else

    echo “PC OFF\n”;

    return $state;

    }

    So with this setup you can power your PC on and off via the SSH command line and use NoMachine with the PC. Alternatively you would need to call somebody to ask them to power up your PC.

    #18555
    Avatar
    Britgirl
    Keymaster

    You should also post this to the Raspberry forums 😉

Viewing 2 posts - 1 through 2 (of 2 total)

This topic was marked as solved, you can't post.