[newbie] How to call a real python-script

576 views
Skip to first unread message

jeandu...@gmail.com

unread,
Sep 15, 2014, 6:16:14 AM9/15/14
to web...@googlegroups.com
Following the explanations of Dmitri Popov in "Raspberry Geek  Magazine #6", I was able to run a script using webiopi called backup.sh using the following script-file called 'backup.py':
#!/usr/bin/env python
import os
import webiopi
@webiopi.macro
def RunBackup():os.system('/home/pi/backup.sh')

So far so good. backup.sh The problem I'm facing now is that backup.sh is a bash-script, not a Python-script. So I want to substitute the bash-script by a Pythons-script called backuppython.py, I tried a few things but nothing seems to work, could someone here tell what the right syntax is?

thanks in advance
jean


p.s.
I tried things like:
def RunBackup():os.system('sudo python /home/pi/backuppython.py')

Toshi Bass

unread,
Sep 15, 2014, 9:55:15 AM9/15/14
to web...@googlegroups.com
Jean

Your on the right track I think try :

def RunBackup():
    os.system('sudo  /home/pi/backuppython.py')

I didn't try this however it should work its based on the fact that I run a separate python script called cc.py in my set up, it has nothing to do with webiopi but I need to start it and stop it running from within my webiopi python script, as it needs to run in the background I made a start-stop-daemon called cc.sh in the /etc/init.d folder  

I then use the following lines in my webiopi python script :

        os.system('sudo /etc/init.d/cc.sh start')

and 

        os.system('sudo /etc/init.d/cc.sh stop)

although once started my script cc.py needs to run continuously in the back ground until I stop it the command you need should be the same. 

If anyone needs the start-stop-daemon example ask and I will post it.

Toshi

jeandu...@gmail.com

unread,
Sep 15, 2014, 11:10:47 AM9/15/14
to web...@googlegroups.com


Op maandag 15 september 2014 15:55:15 UTC+2 schreef Toshi Bass:

I tried your suggestion but without a positive result:
Here are the three files involved (I renamed backuppython.py to a more appropriate ledblink.py):
In my scripts directory I have 'blink.py':

#!/usr/bin/env python
import os
import webiopi
@webiopi.macro
def RunBlink():
    os.system('sudo ./home/pi/ledblink.py')

In my html directory I have 'index.html':
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
p://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>WebIOPi | LED and Python-script Control</title>
            <script type="text/javascript" src="/webiopi.js"></script>
            <script type="text/javascript">
            webiopi().ready(function() {

                            button = webiopi().createMacroButton("macro", "Backup", "RunBackup");
                            $("#controls").append(button);

                            button = webiopi().createMacroButton("macro", "Blink", "RunBlink");
                            $("#controls").append(button);

                            button = webiopi().createGPIOButton(25, "LED");
                            $("#controls").append(button);

                            webiopi().refreshGPIO(true);
            });
            </script>
            <style type="text/css">
                            button {
                                            display: block;
                                            margin: 5px 5px 5px 5px;
                                            width: 200px;
                                            height: 45px;
                                            font-size: 24pt;
                                            font-weight: bold;
                                            color: white;
                            }

            </style>

</head>
<body>
           <div id="controls" align="left"></div>
</body>
</html>

and in /home/pi I have 'ledblink.py':
#!/usr/bin/env python
import time
import RPi.GPIO as gpio
ledpin=18
gpio.setmode(gpio.BCM)
gpio.setup(ledpin,gpio.OUT)
try:
    while 1:
        gpio.output(ledpin,1)
        time.sleep(0.5)
        gpio.output(ledpin,0)
        time.sleep(0.5)
except KeyboardInterrupt:
    gpio.cleanup()
gpio.cleanup()

Can anyone here help me further?
Thanks in advance
Jean


 

JOHN D

unread,
Sep 17, 2014, 9:03:26 AM9/17/14
to web...@googlegroups.com
Do you get any error messages?  I don't have the time to test run it right now, but I can't really see anything wrong.

Personally I would do away with os.system('sudo ./home/pi/ledblink.py') and just add that script to /etc/webiopi/conf.  Then you can add the contents of ledblink.py to the RunBlink() macro so you can call it right from the button.

Jean Dubois

unread,
Sep 22, 2014, 11:10:30 AM9/22/14
to web...@googlegroups.com


Op woensdag 17 september 2014 15:03:26 UTC+2 schreef JOHN D:
Do you get any error messages?  I don't have the time to test run it right now, but I can't really see anything wrong.

Personally I would do away with os.system('sudo ./home/pi/ledblink.py') and just add that script to /etc/webiopi/conf.  Then you can add the contents of ledblink.py to the RunBlink() macro so you can call it right from the button.
Dear John,
I don't/didn't see any errors, but maybe you could show me what you mean exactly by  "add the contents of ledblink.py to the RunBlink() macro"

thanks in advance
jean

JOHN D

unread,
Sep 23, 2014, 7:34:30 AM9/23/14
to web...@googlegroups.com
Replace


def RunBlink():
    os.system('sudo ./home/pi/ledblink.py')

With

def RunBlink():

    ledpin=18
    gpio.setmode(gpio.BCM)
    gpio.setup(ledpin,gpio.OUT)
    try:
        while 1:
            gpio.output(ledpin,1)
            time.sleep(0.5)
            gpio.output(ledpin,0)
            time.sleep(0.5)
    except KeyboardInterrupt:
        gpio.cleanup()
    gpio.cleanup()

You will also need to add import time and import RPi.GPIO as gpio to the top of this Python script

Toshi Bass

unread,
Sep 25, 2014, 6:00:21 AM9/25/14
to web...@googlegroups.com
Hi Jean

Finally found time to test and slightly modify your ledblink.py code and I can confirm evoking the script with os.system command works fine (ensure permissions are set to 755 for this ledblink.py file):

#!/usr/bin/python

import time;
import RPi.GPIO as GPIO;

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM);

# set GPIO pin 18 as output
ledpin=18

GPIO.setup(ledpin, GPIO.OUT);

try:
    while True:
GPIO.output(ledpin, True);
time.sleep(0.5);
GPIO.output(ledpin, False);
time.sleep(0.5);
except KeyboardInterrupt:
    GPIO.cleanup()
GPIO.cleanup()


Start ledblink.py from your other other python script with     os.system('sudo /home/pi/ledblink.py &')   the & runs the script in the background.

Hope it helps

Toshi

Jean Dubois

unread,
Sep 29, 2014, 8:37:14 AM9/29/14
to web...@googlegroups.com


Op donderdag 25 september 2014 12:00:21 UTC+2 schreef Toshi Bass:

Thank you very much for taking the time to answer my question, I'm going to try out it asap (but I'm a bit too busy at this very moment with other things) and post my feedback in this group

kind regards,
jean 

Jean Dubois

unread,
Sep 29, 2014, 8:38:30 AM9/29/14
to web...@googlegroups.com


Op dinsdag 23 september 2014 13:34:30 UTC+2 schreef JOHN D:
Replace

def RunBlink():
    os.system('sudo ./home/pi/ledblink.py')

With

def RunBlink():
    ledpin=18
    gpio.setmode(gpio.BCM)
    gpio.setup(ledpin,gpio.OUT)
    try:
Also t  you John a big thanks  for taking the time to answer my question, I'm going to try it out asap (but I'm a bit too busy at this very moment with other things) and post my feedback in this group

kind regards,
jean 
 
        while 1:

Jean Dubois

unread,
Oct 16, 2014, 10:40:27 AM10/16/14
to web...@googlegroups.com
Thanks Toshi,
It works like you described

jean


Op donderdag 25 september 2014 12:00:21 UTC+2 schreef Toshi Bass:
Hi Jean

RSV-Tuono

unread,
Jan 4, 2015, 2:39:36 PM1/4/15
to web...@googlegroups.com
I tried this code and it works ...

but don´t know how to stop the script again!

os.exit()
os.system('exit')

will not work.
Do I need a subprocess?

regards
Frank

Toshi Bass

unread,
Jan 4, 2015, 4:33:29 PM1/4/15
to web...@googlegroups.com
I had the feeling webiopi shut down the ledblink script when it closed, but if not try this in your shutdown section :

# Called by WebIOPi at server shutdown
def destroy():

    os.system('kill $(pgrep ledblink.py) > /dev/null 2>&1')

Its a guess if it doesn't work post again and I resolve it.

Toshi 
 

RSV-Tuono

unread,
Jan 5, 2015, 1:00:41 PM1/5/15
to web...@googlegroups.com
Dear Toshi, again: thank you for your help!

The code doesn't work, but I must say, that I need for another script to log the data displayed on screen.
What I did:
#Macro Datalog start
@webiopi.macro
def runLogStart():
 os
.system('sudo python /home/pi/datalog/html/log.py')


#Macro Datalog stop
@webiopi.macro
def runLogStop():
 os
.system('sudo kill $(pgrep /home/pi/datalog/html/log.py) > /dev/null 2>&1')

it is called by following buttons which work properly:
webiopi().ready(function() {
 
var content, button;
 content
= $("#content")


 button
= webiopi().createMacroButton("runLogStart", "START", "runLogStart");
                            $
("#controls").append(button);


 button
= webiopi().createMacroButton("runLogStop", "STOP", "runLogStop");
                            $
("#controls").append(button);
 
 button
= webiopi().createMacroButton("runPlot", "Graph", "runPlot");
                            $
("#controls").append(button);


});

Do I need to add something like an exit code in my log.py?

Frank

Toshi Bass

unread,
Jan 5, 2015, 3:56:41 PM1/5/15
to web...@googlegroups.com

Hi Frank

I checked I am pretty sure if you start a script with os.system('sudo /home/pi/ledblink.py &')  from within webiopi when webiopi stops it shuts down ledblink can you test it, does the led stop blinking when webiopi is stopped ?

With webiopi and ledblink running enter  

ps -A | grep ledblink.py   you should see something like

 5350 pts/0    00:00:00 ledblink.py

Then stop webiopi and enter same command if you still see same result then its still running !
If theirs no result then its stopped

Toshi


RSV-Tuono

unread,
Jan 5, 2015, 4:57:15 PM1/5/15
to web...@googlegroups.com
Hi Toshi, I added a screenshot from what I did:

pi@raspberrypi ~ $ ps aux | grep python  > Check if something is running
pi@raspberrypi ~ $ sudo /etc/init.d/webiopi start > Start Webiopi
pi@raspberrypi ~ $ sudo /etc/init.d/webiopi restart > Restarting > open http://localhost:8000 > push Start button to run log.py
pi@raspberrypi ~ $ ps aux | grep python > check what is running > push Stop button
pi@raspberrypi ~ $ ps aux | grep python > check what is running (log.py still running)
pi@raspberrypi ~ $ sudo /etc/init.d/webiopi stop > Stop Webiopi
pi@raspberrypi ~ $ ps aux | grep python >  check what is running (log.py still running)
pi@raspberrypi ~ $ sudo python /home/pi/datalog/html/log.py > python start from terminal ( got error message) > Stop script by Ctrl-C
^Cpi@raspberrypi ~ ps aux | grep python > check what is running ( log.py still running) [LEDG OFF]
pi@raspberrypi ~ $ sudo kill 4049 > kill process
pi@raspberrypi ~ $ ps aux | grep python > everything is ok

Searching also in other forums for keyword "sudo kill $(pgrep" weren't successful, so I hope you can help again.

(My Christmas holiday ended on Sunday, so I can check the Google Groups only in the evening)

Frank
screenshot.png

RSV-Tuono

unread,
Jan 5, 2015, 5:55:45 PM1/5/15
to web...@googlegroups.com
Hi Toschi,

found by try-and-error that following works for me, how to stop the log.py process:
#Macro Datalog starten
@webiopi.macro
def runLogStart():
os.system('python /home/pi/datalog/html/log.py &')

#Macro Datalog stoppen
@webiopi.macro
def runLogStop():
os.system('sudo pkill -f log.py')

#Macro Datalog stoppen
@webiopi.macro
def runPlot():
os.system('sudo gnuplot /home/pi/datalog/html/log.plt &')

... for sure, not a professional solution, but it still works for at the moment, with only one point left:

My Control LEDG (Green) for log.py script is still lightened, also when I stop main process script.py (Webiopi)

Frank

Reply all
Reply to author
Forward
0 new messages