I want to use webiopi to display multiple buttons on the browser and turn on / off multiple GPIOs for Raspberry Pi, but it doesn't work. Specifically, I would like to turn GPIO12 and 13 on and off with the buttons on the browser, respectively. When I create the program as shown below, the browser displays "This site cannot be accessed". If you delete the 7th line "LIGHT2 = 13", the 14th line "GPIO.setFunction (LIGHT2, GPIO.OUT)" and the last line "GPIO.digitalWrite (LIGHT2, GPIO.LOW)" in script.py, the browser will access it. Yes, only GPIO12 can be operated. How can I make GPIO13 operational as well? Thank you.
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)
Recommended Posts