webiopiを使って、ブラウザに複数のボタンを表示させ、ラズベリーパイの複数のGPIOをon,offしたいのですが、うまくいきません。
具体的にはGPIO12,13をそれぞれブラウザのボタンでON,OFFしたいのですが。
下記のようにプログラムを作成すると、ブラウザが「このサイトにアクセスできません」と表示されます。script.pyの7行目「LIGHT2 = 13」と14行目「GPIO.setFunction(LIGHT2, GPIO.OUT)」と最終行「GPIO.digitalWrite(LIGHT2, GPIO.LOW)」を消すと、ブラウザはアクセスできて、GPIO12だけ操作できます。
GPIO13も操作できるようにするにはどうしたらいいでしょうか?
宜しくお願い致します。
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">
<title>WebIOPi | Light Control</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
var content,button;
content=$("#content");
// Create a "Light" labeled button for GPIO 12
button = webiopi().createGPIOButton(12, "ranp1");
// Append button to HTML element with ID="controls" using jQuery
$("#controls").append(button);
button = webiopi().createGPIOButton(13, "Light2");
$("#controls").append(button);
// Refresh GPIO buttons
// pass true to refresh repeatedly of false to refresh once
webiopi().refreshGPIO(true);
});
</script>
<style type="text/css">
button {
display: block;
margin: 5px 5px 5px 5px;
width: 160px;
height: 45px;
font-size: 24pt;
font-weight: bold;
color: white;
}
#gpio12.LOW {
background-color: Black;
}
#gpio12.HIGH {
background-color: Blue;
}
#gpio13.LOW {
background-color: Black;
}
#gpio13.HIGH {
background-color: Blue;
}
</style>
</head>
<body>
<div id="controls" align="center"></div>
</body>
</html>
script.py
#! /usr/bin/env /usr/bin/python3
#_*_ cording:utf-8 _*_
import webiopi
import datetime
GPIO = webiopi.GPIO
LIGHT = 12 # GPIO pin using BCM numbering
LIGHT2 = 13
HOUR_ON = 8 # Turn Light ON at 08:00
HOUR_OFF = 18 # Turn Light OFF at 18:00
# setup function is automatically called at WebIOPi startup
def setup():
# set the GPIO used by the light to output
GPIO.setFunction(LIGHT, GPIO.OUT)
GPIO.setFunction(LIGHT2, GPIO.OUT)
# retrieve current datetime
now = datetime.datetime.now()
# test if we are between ON time and tun the light ON
if ((now.hour >= HOUR_ON) and (now.hour < HOUR_OFF)):
GPIO.digitalWrite(LIGHT, GPIO.HIGH)
# loop function is repeatedly called by WebIOPi
def loop():
# retrieve current datetime
now = datetime.datetime.now()
# toggle light ON all days at the correct time
if ((now.hour == HOUR_ON) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(LIGHT) == GPIO.LOW):
GPIO.digitalWrite(LIGHT, GPIO.HIGH)
# toggle light OFF
if ((now.hour == HOUR_OFF) and (now.minute == 0) and (now.second == 0)):
if (GPIO.digitalRead(LIGHT) == GPIO.HIGH):
GPIO.digitalWrite(LIGHT, GPIO.LOW)
# gives CPU some time before looping again
webiopi.sleep(1)
# destroy function is called at WebIOPi shutdown
def destroy():
GPIO.digitalWrite(LIGHT, GPIO.LOW)
GPIO.digitalWrite(LIGHT2, GPIO.LOW)