嗨,我的 webiopi html 和脚本出现了一些问题,我无法根据 script.py 中的值更改按钮状态,我在 chrome 控制台中收到此错误
这是它自动刷新按钮的时候。
POST http://192.168.0.108:8000/macros/get_DoorStatus/0 404 (get_DoorStatus Not Found)
当我按下它时,我得到了。
Uncaught TypeError: ((n.event.special[e.origType] || (intermediate value)).handle || e.handler).apply is not a function
这是 index.html 代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />
<link rel="stylesheet" type="text/css" href="style.css">
<title>WebIOPi | Demo</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
var content, button;
content = $("#content");
// create a button which call HelloWorld with "User,Name" argument
button = webiopi().createMacroButton("macro", "On", "HelloWorld");
content.append(button); // append button to content div
// the previous button will always call HelloWorld with "User,Name" argument
// you can also create a simple button with your own function
button = webiopi().createMacroButton("macro2", "Off", "HelloWorld2");
content.append(button); // append button to content div
// create a button which call function to get SwitchStatus from script.py (GPIO 27)
button = webiopi().createButton("macro3", "Door", "callMacro_get_DoorStatus");
content.append(button); // append button to content div
});
//webiopi().refreshGPIO(true);
window.onload = function(){
callMacro_get_DoorStatus()
}
setInterval ("callMacro_get_DoorStatus()", 5000);{
}
function callMacro() {
// call HelloWorld(args)
webiopi().callMacro("HelloWorld()");
}
function callMacro() {
// call HelloWorld2(args)
webiopi().callMacro("HelloWorld2()");
}
function callMacro_get_DoorStatus(){
var args = [0]
//Get data form script.py and return it with Callback
webiopi().callMacro("get_DoorStatus", args, macro_get_DoorStatus_Callback);}
function macro_get_DoorStatus_Callback(macro, args, data){
p0 = data.split(" ")[0];
if (p0==1){
webiopi().setClass("macro3", "Open");
webiopi().setLabel("macro3", "Door Open");}
else{
webiopi().setClass("macro3", "Closed");
webiopi().setLabel("macro3", "Door Closed");}
}
</script>
</head>
<body>
<h1 align="center">Switch 1</h1>
<div id="content" align="center"></div>
<style type="text/css">
button {
display: inline-block;
margin: 5px 5px 5px 5px;
width: 160px;
height: 45px;
font-size: 24pt;
font-weight: bold;
color: black;
}
</body>
</html>
这是 script.py 代码
# Imports
import webiopi
import time
import urllib.request
import requests
# Enable debug output
webiopi.setDebug()
# Retrieve GPIO lib
GPIO = webiopi.GPIO
SWITCH = 21
SERVO = 23
LED0 = 24
LED1 = 25
# Called by WebIOPi at script loading
def setup():
webiopi.debug("Script with macros - Setup")
# Setup GPIOs
GPIO.setFunction(SWITCH, GPIO.IN)
#GPIO.setFunction(SERVO, GPIO.PWM)
#GPIO.setFunction(LED0, GPIO.PWM)
#GPIO.setFunction(LED1, GPIO.OUT)
#GPIO.pwmWrite(LED0, 0.5) # set to 50% ratio
#GPIO.pwmWriteAngle(SERVO, 0) # set to 0 (neutral)
#GPIO.digitalWrite(LED1, GPIO.HIGH)
# Looped by WebIOPi
def loop():
# Toggle LED each 5 seconds
#value = not GPIO.digitalRead(LED1)
#GPIO.digitalWrite(LED1, value)
webiopi.sleep(5)
# Called by WebIOPi at server shutdown
def destroy():
webiopi.debug("Script with macros - Destroy")
# Reset GPIO functions
#GPIO.setFunction(SWITCH, GPIO.IN)
#GPIO.setFunction(SERVO, GPIO.IN)
#GPIO.setFunction(LED0, GPIO.IN)
#GPIO.setFunction(LED1, GPIO.IN)
#gpio0 = webiopi.deviceInstance("gpio0")
#gpio0.digitalWrite(0, 1)
# A macro which says hello
@webiopi.macro
def HelloWorld():
urllib.request.urlopen("http://192.168.0.200:9090/?dW5pdDFPTg==").read()
# A macro without args which return nothing
@webiopi.macro
def HelloWorld2():
urllib.request.urlopen("http://192.168.0.200:9090/?dW5pdDFPRkY=").read()
@webiopi.macro
def get_DoorStatus(arg0):
x = urllib.request.urlopen('http://192.168.0.200:9090/')
if 'Switch1Off' in x.read():
print (p0)
return "%d" % p0 # returns "0" or "1"