Hallo Raspi Fans, ich habe mir vor einiger Zeit einen Raspberry Pi zugelegt und beschäftige mich seit kurzem etwas intensiver mit diesem kleinen Wunderding der Technik.
Konkret möchte ich gern eine Steuerung für meine Wärmepume aufbauen, die vor allem komfortabler ist, als das Original. Also habe ich mir Webiopi installiert und versuche nun verzweifelt einige Codeschnipsel, die ich im Web gefunden habe zu einem Programm zusammenzufügen. Konkret komme ich nun aber bei einem Problem nicht weiter:
Die Webseite soll aus meheren Seiten bestehen, die einfach per Link umgeschalten werden können. Die Startseite soll mir live einige Daten von Sensoren anzeigen, mit der zweite nöchte ich gern einen Wochentimer realisieren, der ein Gerät automatisch an drei Schaltzeiten am Tag ein- und auschaltet. Leider klemmt es nun am Wechsel zwischen den beiden Seiten (index.html und index2.html). Es dauert bei mir mindestens 1min, bis die zweite Seite angezeigt wird. Webiopi zeigt beim umschalten jede Menge "Broken Pipe" Fehler an.
Meine Frage nun: gibt es eine Möglichkeit, beim umschalten die laufenden Funktionen, also das Auslesen der Sensoren erst zu stoppen und dann auf die zweite Seite umzuleiten?
Ich beschäftige mich leider erst seit drei Wochen mit Python und Javascript - vergebt mir also bitte meine vielleicht etwas umständliche Ausdrucksweise.
Hier der Code:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<meta name="viewport" content="height = device-height, width = 420, user-scalable = no">
<title>Hennis WP Remote</title>
<script type="text/javascript" src="/webiopi.js"></script>
<script type="text/javascript">
webiopi().ready(function() {
var content, button;
content = $("#content");
w().refreshGPIO(true);
// Following function will process data received from set/getLightHours macro.
var updateLightHours = function(macro, args, response) {
var hours = response.split(";");
// Following lines use jQuery functions
$("#inputOn").val(hours[0]);
$("#inputOff").val(hours[1]);
}
// DS18b20
// create a button which call Temp0
button = webiopi().createButton("macro2", "Temperature", callMacro_Temp);
content.append(button); // append button to content div
webiopi().setClass("macro2", "Temp"); //set color state
// create a button which call Temp1
button = webiopi().createButton("macro3", "Temperature", callMacro_Temp);
content.append(button); // append button to content div
webiopi().setClass("macro3", "Temp"); //set color state
// create a button which call Temp2
button = webiopi().createButton("macro4", "Temperature", callMacro_Temp);
content.append(button); // append button to content div
webiopi().setClass("macro4", "Temp"); //set color state
// create a button which call Temp3
button = webiopi().createButton("macro5", "Delta Temp", callMacro_Temp);
content.append(button); // append button to content div
webiopi().setClass("macro4", "Temp"); //set color state
// BMP
// create a button which call BMPP
button = webiopi().createButton("macro6", "Druck", callMacro_BMP);
content.append(button); // append button to content div
webiopi().setClass("macro5", "BMPP"); //set color state
// create a button which call BMPT
button = webiopi().createButton("macro7", "Server", callMacro_BMP);
content.append(button); // append button to content div
webiopi().setClass("macro6", "BMPT"); //set color state
// Immediately call getLightHours macro to update the UI with current values
// "getLightHours" refers to macro name
// [] is an empty array, because getLightHours macro does not take any argument
// updateLightHours is the callback function, defined above
webiopi().callMacro("getLightHours", [], updateLightHours);
// Create a button to call setLightHours macro
var sendButton = webiopi().createButton("sendButton", "Send", function() {
// Arguments sent to the macro
var hours = [$("#inputOn").val(), $("#inputOff").val()];
// Call the macro
webiopi().callMacro("setLightHours", hours, updateLightHours);
});
// Append the button to the controls box using a jQuery function
$("#controls").append(sendButton);
// Create a "Light" labeled button for GPIO 17
var button = webiopi().createGPIOButton(17, "Light");
// Append the button to the controls box
$("#controls").append(button);
// Refresh GPIO buttons
// pass true to refresh repeatedly of false to refresh once
webiopi().refreshGPIO(true);
});
function callMacro_Temp(){
var args = [0,1,2,3]
webiopi().callMacro("Temp", args, macro_Temp_Callback);}
function macro_Temp_Callback(macro, args, data){
Temp0 = data.split(" ")[0];
Temp1 = data.split(" ")[1];
Temp2 = data.split(" ")[2];
Temp3 = data.split(" ")[3];
webiopi().setLabel("macro2", "Vorlauf: " + Temp0 +" °C");
webiopi().setLabel("macro3", "Rücklauf: " + Temp1 +" °C");
webiopi().setLabel("macro4", "Outdoor: " + Temp2 +" °C");
webiopi().setLabel("macro5", "Delta: " + Temp3 +" °C");
}
setInterval ("callMacro_Temp()", 1000);{
}
function callMacro_BMP(){
var prs = [0,1]
webiopi().callMacro("BMP", prs, macro_BMP_Callback);}
function macro_BMP_Callback(macro, prs, data){
BMPP = data.split(" ")[0];
BMPT = data.split(" ")[1];
webiopi().setLabel("macro6", "Druck: " + BMPP +" hPa");
webiopi().setLabel("macro7", "Sensor: " + BMPT +" °C");
}
setInterval ("callMacro_BMP()", 1000);{
}
</script>
<style type="text/css">
button {
display: block;
margin: 5px 5px 5px 5px;
width: 320px;
height: 45px;
font-size: 20pt;
font-family: times, serif;
font-weight: bold;
color: black;
margin-bottom:5px;
-moz-border-radius:5px 5px 5px 5px; /* Firefox */
-webkit-border-radius:10px 10px 10px 10px; /* Chrome, Safari */
-khtml-border-radius:10px 10px 10px 10px; /* Konqueror */
border-radius:20px 20px 20px 20px; /* CSS3 */
}
input[type="range"] {
display: block;
width: 160px;
height: 45px;
}
#macro5 {
color: #00f;
}
#gpio17.LOW {
background-color: #333;
}
#gpio17.HIGH {
background-color: #66f;
}
</style>
</head>
<body><div align="center">INDEX<div>
<div id="content" align="center"></div>
<div align="center">
Turn On at :<input type="text" id="inputOn" /><br/>
Turn Off at: <input type="text" id="inputOff" /><br/>
<div id="controls"></div>
<div><a href="index2.html" title="index">Timer</a></div>
</body>
</html>
Alles anzeigen
Python:
script.py
# Imports
import webiopi
import time
import datetime
GPIO = webiopi.GPIO
LIGHT = 17 # GPIO pin using BCM numbering
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)
# 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)
#sensors
from webiopi.devices.sensor.onewiretemp import DS18B20
from webiopi.devices.sensor.bmp085 import BMP085
bmp = BMP085()
temp0 = DS18B20(slave="28-00000588f915")
temp1 = DS18B20(slave="28-00000588ec33")
temp2 = DS18B20(slave="28-00000589c43b")
Temp3 = 0
# Enable debug output
webiopi.setDebug()
@webiopi.macro
def getLightHours():
return "%d;%d" % (HOUR_ON, HOUR_OFF)
@webiopi.macro
def setLightHours(on, off):
global HOUR_ON, HOUR_OFF
HOUR_ON = int(on)
HOUR_OFF = int(off)
return getLightHours()
@webiopi.macro
def Temp(arg0, arg1, arg2, arg3):
try:
Temp0 = "%.2f" % (temp0.getCelsius())
except (IOError, TypeError):
Temp0 = 0
pass
try:
Temp1 = "%.2f" % (temp1.getCelsius())
except (IOError, TypeError):
Temp1 = 0
pass
try:
Temp2 = "%.2f" % (temp2.getCelsius())
except (IOError, TypeError):
Temp2 = 0
pass
try:
Temp3 = "%.2f" % (temp0.getCelsius() - (temp1.getCelsius()))
except (IOError, TypeError):
Tem3 = 0
pass
return ("%s %s %s %s" % (Temp0, Temp1, Temp2, Temp3))
@webiopi.macro
def BMP(pr0, pr1):
try:
BMPP = "%.2f" % (bmp.getHectoPascal() + 15.35)
except (IOError, TypeError):
BMPP = 0
pass
try:
BMPT = "%.2f" % (bmp.getCelsius())
except (IOError, TypeError):
BMPT = 0
pass
return ("%s %s" % (BMPP, BMPT))
Alles anzeigen
Da ich meine Seite mittlerweile in CSS, Javascript, Python und HTML aufgeteilt habe, sind dies Scripte, in der ich noch alles beisammen hatte. Das Problem "Broken Pipe" ist aber das hier das Selbe. index2.html kann den gleichen Inhalt wie index.html haben.
Ich würde mich freuen, wenn ich von Euch einen Tipp bekommen würde.
Beste Grüße, Henri