简体   繁体   English

按钮执行jQuery脚本,使用jQuery的?

[英]button executes python script, using jquery?

I'm building a website on a RaspberryPi. 我正在RaspberryPi上建立一个网站。 I have JustGage reading temperatures and other sensors and showing them realtime already working. 我有JustGage读取温度和其他传感器,并显示它们已经实时工作。 But now, I'm trying to create a button, when pressed, executes a python script. 但是现在,我试图创建一个按钮,当按下该按钮时,它会执行python脚本。

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: 现在,当按下此按钮时,我想让它执行python脚本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. 从终端运行时,此python脚本有效。 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? 但是,当在index.php页面上按下按钮时,如何运行此脚本? Preferably with Jquery or PHP (but I don't want the page to reload), but not with Flask. 最好使用Jquery或PHP(但我不希望页面重新加载),但不建议使用Flask。

Or is there an easy way to use Bootstrap Switch or something? 还是有使用Bootstrap Switch或其他方法的简便方法? And, using the current state of the relay, which is saved in "/home/pi/Desktop/UV.txt", as default position of the switch? 并且,使用继电器的当前状态作为开关的默认位置,该状态保存在“ /home/pi/Desktop/UV.txt”中? Is there somewhere a clear tutorial about this? 是否有关于此的清晰教程?

EDIT 1 编辑1

The python script on.py: import os, sys python脚本on.py:导入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: index.php上的按钮:

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

and finally on.php: 最后是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). 在浏览器中打开php文件时,它仅显示内容,没有错误(我在.py和.php文件中都运行了chmod 777和chmod + x)。

As soon as I click the button, chrome shows in the developer-mode that on.php is "GET". 单击按钮后,Chrome在开发人员模式下显示on.php为“ GET”。 So, the button works, but the pythonscript isn't run. 因此,该按钮有效,但是pythonscript没有运行。 When I test it in the terminal with sudo php var/www/on.php it works like a charm. 当我使用sudo php var/www/on.php在终端中对其进行测试时,它就像一种魅力。 What am I still missing? 我还缺少什么? www and www-data are added to the sudo'ers. wwwwww-data被添加到sudo'ers中。 Should apache be in it as well? apache也应该在其中吗?

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 您需要调用一个调用脚本的页面,您可以设置或调用.py脚本并直接将其命中,或者您可以使用以下行创建一个php页面

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 这些是几个选项,并假设服务器在树莓派上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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