1

Just having a problem on my Raspberry Pi project

I have a web server running on a Raspberry Pi and I write a web interface with some buttons to control the Pi. The Pi will run the python script in a "while True" loop initially. It stands for auto mode.

Here is the question. When I click the button on the webpage, I want to switch to manual mode. That means I want to terminate the "while True" loop and run some other python scripts. How can I achieve it in my webpage? Also considering that I may want to switch back to auto mode.

window.prettyPrint && prettyPrint();
    $('#power-switch').on('switch-change', function (e, data) {
        if(data.value == true){
            var $el = $(data.el)
            , value = data.value;
            //console.log('on',e, $el, value);//for debug

            /*
            add your function here
            */
            js_pw_ON();


            $('#auto-switch').bootstrapSwitch('setActive', true);
            $('#lgt1').prop('disabled', false);
            $('#lgt2').prop('disabled', false);
            $('#lgt3').prop('disabled', false);
            $('#lgt4').prop('disabled', false);
            $('#lgt5').prop('disabled', false);
        }
3
  • You'll need to use some sort of IPC. Commented Dec 3, 2013 at 3:00
  • @JonathonReinhart Can you explain it in details? Actually I don't need to pass information between python and the webpage. All I need is to run different scripts on different buttons. Commented Dec 3, 2013 at 3:10
  • You still need some sort of IPC to tell the process to stop. Is this on Linux? You could send it a signal Commented Dec 3, 2013 at 3:38

1 Answer 1

1

Simplest way to achieve your goal is to use file called lets say "manual_mode" with 0 or 1 inside. Ofcourse both php and python have to have permissions to file.

Php deals pretty well with files. Well if you want manual mode on:

file_put_contents('manual_mode','1');

Off:

file_put_contents('manual_mode','0');

Python deals pretty well with files aswell:

while 1:
    f = open('manual_mode','r')
    val = f.read()
    f.close()
    if val == '1':
        break

EDIT: In case you use jquery, you should first make switch.php file:

<?php
if(!empty($_GET['sw']) && $_GET['sw'] == 1)
{
  file_put_contents('manual_mode','1');
} else
{
  file_put_contents('manual_mode','0');
}

When you call this like switch.php?sw=1 manual_mode will be set to 1 (to 0 otherwise). What you gonna do next is to perform call to php script from your jquery code:

$.get("switch.php?sw=1");

I think that is all you need.

Btw. According to comments, IPC is platform dependent while files are not, so choice is obvious.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer! I use a Bootstrap switch to switch between auto and manual mode. How can I put the "file_put_contents" on this switch buttons? I added the code of the switch in the original question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.