2016-04-30 4 views
0
import threading 
import time 

def cold_temp(): 
    # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. 
    tfile = open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave") 
    # Read all of the text in the file. 
    text = tfile.read() 
    # Close the file now that the text has been read. 
    tfile.close() 
    # Split the text with new lines (\n) and select the second line. 
    secondline = text.split("\n")[1] 
    # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
    temperaturedata = secondline.split(" ")[9] 
    # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
    temperature = float(temperaturedata[2:]) 
    # Put the decimal point in the right place and display it. 
    temperature = temperature/1000 
    return temperature 

output = cold_temp()  
f = open('/var/www/html/coldtemp.html', 'w') 
print >> f, output 
f.close() 
cold_temp() 

Ich habe versucht, die oben und eine einfachePython läuft nicht jede Sekunde

def cold_temp(): 
    while True: 
     # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. 
     tfile = open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave") 
     # Read all of the text in the file. 
     text = tfile.read() 
     # Close the file now that the text has been read. 
     tfile.close() 
     # Split the text with new lines (\n) and select the second line. 
     secondline = text.split("\n")[1] 
     # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
     temperaturedata = secondline.split(" ")[9] 
     # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
     temperature = float(temperaturedata[2:]) 
     # Put the decimal point in the right place and display it. 
     temperature = temperature/1000 
     return temperature 

     output = cold_temp()  
     f = open('/var/www/html/coldtemp.html', 'w') 
     print >> f, output 
     f.close() 
     time.sleep(1) 

Ich wünsche das Skript auszuführen jede Sekunde. Beide oben genannten laufen einmal und dann enden.

+1

was genau ist die Frage? – TehSphinX

+3

Was genau passiert? Stürzt das Skript ab? Läuft es alle 1,1 Sekunden oder nicht alle? –

+0

Zeigen Sie uns 'etwas tun'. –

Antwort

2

Ihre While-Schleife ist an der falschen Stelle. Ich habe mir die Freiheit genommen, Ihren Code zu ändern, um with Klauseln zu verwenden, die der üblichere Weg ist, Dateien zu öffnen, aber es wird brechen, wenn Sie einen wirklich alten Python haben.

import threading 
import time 

def cold_temp(): 
    # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. 
    with open("/sys/bus/w1/devices/28-021571bf69ff/w1_slave") as tfile: 
     # skip first line, keep second line 
     next(tfile) 
     secondline = next(tfile) 
    # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
    temperaturedata = secondline.split(" ")[9] 
    # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
    temperature = float(temperaturedata[2:]) 
    # Put the decimal point in the right place and display it. 
    temperature = temperature/1000 
    return temperature 

while True: 
    output = cold_temp()  
    with open('/var/www/html/coldtemp.html', 'w') as f: 
     print >> f, output 
    time.sleep(1) 
+0

Danke! Das funktioniert perfekt. Entschieden, um zu testen, indem Sie das Thermometer in meinen Mund, dann eiskaltes Wasser. groß –

2

Sie können das Modul sched verwenden, um etwas zu planen, das wiederholt ausgeführt werden soll, sei es alle zwei Stunden oder jede Sekunde.

Wenn Ihre Funktion länger als eine Sekunde zum Ausführen benötigt (unwahrscheinlich, nur die Temp. Zu überprüfen), dann haben Sie eine Verzögerung.

Sie werden auch einige "Drift" in der Zeit aufgrund der Zeit sehen, es dauert Ihre Funktion zu laufen, wie es wiederholt geplant ist. All die winzigen "Bits" von Zeit, die Ihre Funktion benötigt, werden sich schließlich addieren.

z.B. -

import sched, time 

s = sched.scheduler(time.time, time.sleep) 
def some_function(): 

    print time.time() 
    s.enter(1, 0, some_function) 

s.enter(1, 0, some_function) 
s.run() 
+0

Ich kann das nicht funktionieren, aber danke. –

+0

Es sind auch 5 Argumente erforderlich für 's.enter' –

1

Der erste wird einmal ausgeführt, weil Sie ihm keine Möglichkeit gegeben haben, wiederholt zu laufen. Die zweite trifft eine return, die die Funktion (und damit die Schleife) verlässt, bevor sie überhaupt die Möglichkeit hat, etwas auszugeben. Entfernen Sie die return und verwenden Sie einfach temperature für den Wert output.

+0

Es werden keine Fehler ausgegeben, aber die Ausgabe wird nicht in die HTML-Datei geschrieben –

+0

Rufen Sie nicht' output = cold_temp() 'auf. Es ist Rekursion: 'cold_temp' ruft sich für immer selbst auf und kommt nie zum Ausgangsteil. Drucken Sie einfach den Wert von 'temperature'. –

+0

schreibt immer noch nicht .. 'Temperatur = Temperatur/1000 f = open ('/ var/www/html/coldtemp.html', 'w') Druck >> f, Temperatur f.close () time.sleep (1) ' –

Verwandte Themen