嗨,我试图通过树莓派、继电器和外部电源来控制一些电磁阀。我在下面的 python 中编写了一个运行良好的脚本,用于更改物理按钮的值,但也希望能够从 Web 浏览器控制它。我一直在使用 webiopi 并且可以正常工作,因为我现在可以打开和关闭继电器。我的问题是,当我启动 python 脚本时,我设置为输出的按钮工作正常,但是当我按下 javascript 上的开/关按钮时,物理按钮不再工作。我认为问题是我需要让 python 脚本与 javascript“对话”?按下时更新输出的值?但是我不确定该怎么做?下面是我的两个单独的代码。
Python。
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.IN) ## Setup GPIO Pin 10 to IN
GPIO.setup(22, GPIO.IN) ## Setup GPIO Pin 22 to IN
GPIO.setup(13, GPIO.OUT) ## Setup GPIO Pin 13 to OUT
#############################################
# All buttons wired to provide High input when pressed.
#############################################
# Cleanroom = 0 #sets a variable that will never be meet so while loop will continue for ever or if power is interupted.
Cleanroom = 0
while True:
#########################################################################
# below is code for opening cryo, it includes button debouncing
cryo_open = 0
while True:
#take a reading
on = GPIO.input(22)
GPIO.output(13, False)
#if the last reading was low and this one high, print
if ((not cryo_open) and on):
print("Cryo Open")
#update previous input
cryo_open = on
#slight pause to debounce
time.sleep(0.05)
break
#########################################################################
# below is code for closing cryo, it includes button debouce
cryo_close = 0
while True:
#take a reading
on = GPIO.input(10)
GPIO.output(13, True)
#if the last reading was low and this one high, print
if ((not cryo_close) and on):
print("Cryo Close")
#update previous input
cryo_close = on
#slight pause to debounce
time.sleep(0.05)
break
GPIO.cleanup(13)
GPIO.cleanup(22)
GPIO.cleanup(10)
Java 脚本
<!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" />
<title>WebIOPi | Demo</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
webiopi().setFunction(25,"out");
webiopi().setFunction(15,"out");
var content, button;
content = $("#content");
// create a "SWITCH" labeled cryo for GPIO 25
button = webiopi().createGPIOButton(25, "Cryo");
content.append(button); // append button to content div
// create a button that output a single pulse
button = webiopi().createPulseButton("pulse", "Cryo Open 2", 25);
content.append(button); // append button to content div
// create a button that output a single pulse
button = webiopi().createPulseButton("pulse", "Cryo Close 2", 15);
content.append(button); // append button to content div
});
</script>
<h1 style = "text-align:center; font-family: Arial; font-size:15px">Clean Room Pneumatic Valve Control</h1>
<style type="text/css">
button {
display: block;
background-color: rgb(202, 60, 60);
margin: 15px 5px 5px 5px;
width: 300px;
height: 100px;
color: rgb(202, 60, 60);
color: rgb(202, 60, 60);
font-size: 15pt;
font-weight: block;
color: white;
border-radius: 20px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
outline: none;
}
//input[type="range"] {
color: rgb(202, 60, 60);
display: block;
width: 160px;
height: 45px;//
}
#gpio25.HIGH {
background-color: rgb(28, 184, 65); /* this is a green */
}
#gpio15.LOW {
background-color: rgb(202, 60, 60); /* this is a maroon */
}
</style>
</head>
<body>
<div id="content" align="center"></div>
</body>
</html>