简体   繁体   中英

button executes python script, using jquery?

I'm building a website on a RaspberryPi. I have JustGage reading temperatures and other sensors and showing them realtime already working. But now, I'm trying to create a button, when pressed, executes a python script.

So, it is this simple button:

<form method="post">
  <button id="led" name="led" checked data-toggle="toggle">▲</button>
</form>

And now, when this button is pressed, I want to let it execute a python script LED.py:

import os
import smbus
import time
import datetime
import os, sys
rom datetime import datetime
import pytz
import datetime


states = open('/home/pi/Desktop/UV.txt', 'r+')
on=open('/home/pi/Desktop/on.txt', 'r+')
    os.system('sudo /drcontrol/drcontrol.py -d DAE001XA -r 3 -c state | tee /home/pi/Desktop/UV.txt')
state = states.readlines()
on = on.readlines()
state = str(state)
rstate= str(on)
    if (rstate == state):
        print ("Relay on!")
    else:
        print ("Relay Off!")

#--------------------
if (rstate != state):
    os.system('/drcontrol/drcontrol.py -d DAE001XA -r 3 -c on')
    print("on")
else:
    os.system('/drcontrol/drcontrol.py -d DAE001XA -r 3 -c off')
            print("off")

#------------------------------------------------------------------------------------
import os, sys

if os.popen('whoami').readline().strip() == 'root' :
    print( "OK, you're root")
    root = True
else:
    print( "Attention: please run as root with 'sudo'. This scripts GPIO which need root-rights.")
    print( "Now exiting ...")
    sys.exit(1)
    root = False

if __name__ == '__main__':
    main()
print ("Finished")

This python script works when run from the terminal. It will activate the relay, when it is turned off and vice versa. But, how can I run this script, when the button is pressed on index.php page? Preferably with Jquery or PHP (but I don't want the page to reload), but not with Flask.

Or is there an easy way to use Bootstrap Switch or something? And, using the current state of the relay, which is saved in "/home/pi/Desktop/UV.txt", as default position of the switch? Is there somewhere a clear tutorial about this?

EDIT 1

The python script on.py: import os, sys

os.system('/drcontrol/drcontrol.py -d DAE001XA -r 3 -c on')
f=open("/var/www/test.txt", a+)

The button on index.php:

<button id="led">on</button>
<script>
  $("#led").click(function(){
    $.ajax({
      url:"/var/www/on.php",
    });
  });
</script>

and finally on.php:

<?php
  exec("sudo /usr/bin/python3.2 /var/www/on.py")
?>

When the php-file is opened in the browser, it just shows the content, no errors (I have run chmod 777 and chmod +x on both .py and .php files).

As soon as I click the button, chrome shows in the developer-mode that on.php is "GET". So, the button works, but the pythonscript isn't run. When I test it in the terminal with sudo php var/www/on.php it works like a charm. What am I still missing? www and www-data are added to the sudo'ers. Should apache be in it as well?

you would need to call a page that calls the script you can setup or something to call .py scripts and hit it directly or you could create a php page with a line like

exec("python myscript.py arg1 arg2 ..."); 

and on button press just go to that page ...

these are a couple of options and assume the server is running on the raspberry pi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM