PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Safely Power Off RPI 3 From A Webpage Toggle Switch Jquery PHP & Python

Go down

Safely Power Off RPI 3 From A Webpage Toggle Switch Jquery PHP & Python Empty Safely Power Off RPI 3 From A Webpage Toggle Switch Jquery PHP & Python

Post by jamied_uk 28th December 2016, 19:39




Python code


Code:
import os
os.system(sudo poweroff)

PHP poweroff.php


Code:
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Jquery Toggle Switch</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>

        <link href="css/style.css" rel="stylesheet">
<?php
// require('sas.php');
$Sstate1="true";  // false true


if($_POST['action'] == 'poweroff') {
$command = escapeshellcmd('sudo python /home/pi/Documents/scripts/poweroff.py');
$output = shell_exec($command);
echo $output;
}


?>
    </head>

    <body>
            <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
           
            <div id="spacer" style="padding="150px"">
 
            <label for="test3" class="checkbox switch-checkbox" style="cursor:pointer" >
                <input value="1" type="checkbox" id="test3">
               <div id="TextOutput"><?php echo $Ttext1;?></div>
            </label>

        </div>
</div>


        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script>
    // make switch reflect current state!
    $("#test3").prop('checked', <?php echo $Sstate1;?>);
    var $changed='Currently Switched On';
    $('#TextOutput').html($changed);
    $('input[type="checkbox"]').change(function(){
        if($(this).prop("checked") == true){
    // Do something here!
        if(confirm("Are You Sure You Want To Power Off?")) {
    var $changed='Currently Switched On';
    $('#TextOutput').html($changed);
    // do something here!
        alert("Checkbox is checked.");

  }
            }

            else if($(this).prop("checked") == false){
        // Do something here!
                if(confirm("Are You Sure You Want To Power Off?")) {
        var $changed='Currently Switched Off';
    $('#TextOutput').html($changed);
        // do something here!
                alert("Ok Shutting Down, Please Wait.");

  }

            }


    });
          (function($){

              $('.switch-checkbox').each(function(){
                var $input = $('input', this);
        var selectedValue = $(this).val();
        if(selectedValue !== '1')
        selectedValue = '0';
                $input.hide().wrap('<span class="switch">');
                $input.after('<span class="switch-container"><span class="on">On</span><span class="mid"></span><span class="off">Off</span></span>');
        $('#divresult').html($input);
       
              })

          })(jQuery);
        </script>
<div id="divresult">
</div>

    </body>
</html>

Remote Page Code


Code:
 <link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>

        <link href="css/style.css" rel="stylesheet">
<?php

$Sstate1="true";  // false true

function P_Off(){
if($_POST['action'] == 'call_this') {
$command = escapeshellcmd('sudo python /home/pi/Documents/scripts/web_door_open.py');
$output = shell_exec($command);
echo $output;
}}

?>
    </head>

    <body>
            <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
           
            <div id="spacer" style="padding="6px"">
 
            <label for="test3" class="checkbox switch-checkbox" style="cursor:pointer">
                <input value="1" type="checkbox" id="test3">
               <div id="TextOutput"><?php echo $Ttext1;?></div>
            </label>

        </div>
</div>


        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
        <script>
    function myAjax() {
              $.ajax({
               type: "POST",
               url: 'poweroff.php',
               data:{action:'poweroff'},
               success:function(html) {
        }
   
      });
 }
    // make switch reflect current state!
    $("#test3").prop('checked', <?php echo $Sstate1;?>);
    var $changed='Currently Switched On';
    $('#TextOutput').html($changed);
    $('input[type="checkbox"]').change(function(){
        if($(this).prop("checked") == true){
    // Do something here!
        if(confirm("Are You Sure You Want To Power Off?")) {
    myAjax();
    var $changed='Currently Switched On';
    $('#TextOutput').html($changed);
    // do something here!
        alert("Checkbox is checked.");

  }
            }

            else if($(this).prop("checked") == false){
        // Do something here!
                if(confirm("Are You Sure You Want To Power Off?")) {
        myAjax();
        var $changed='Currently Switched Off';
    $('#TextOutput').html($changed);
        myAjax();
        alert("Ok Shutting Down, Please Wait.")
  }

            }


    });
          (function($){

              $('.switch-checkbox').each(function(){
                var $input = $('input', this);
        var selectedValue = $(this).val();
        if(selectedValue !== '1')
        selectedValue = '0';
                $input.hide().wrap('<span class="switch">');
                $input.after('<span class="switch-container"><span class="on">On</span><span class="mid"></span><span class="off">Off</span></span>');
        $('#divresult').html($input);
       
              })

          })(jQuery);
        </script>
<div id="divresult">
</div>

You will need www-data to be a suder for this to work

You should be able update /etc/sudoers to allow www-data to run your script using sudo by adding the following line to /etc/sudoers:

# User alias specification
www-data ALL = NOPASSWD: /path/to/script.sh

raspberrypi.stackexchange.com/questions/14739/allow-apache-to-run-sudo-scripts
jamied_uk
jamied_uk
Admin

Posts : 2951
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum