Vielleicht kann mir jemand sagen wo der Fehler ist.
Die Fehlermeldung sagt folgendes:
pi@raspberrypi ~/scripts $ sudo python temp_anzeigen.py
File "temp_anzeigen.py", line 79
webtemp = open('heizung.txt', 'w')
IndentationError: unindent does not match any outer indentation level
Wenn ich den Code für das schreiben der TXT Datei in ein eigenes Script schreibe und ausführe, klappt es. :s
Hier der ganze Code:
Python
#!/usr/bin/python
import time
import sys
import os
import RPi.GPIO as GPIO
# Zuordnung der GPIO Pins (ggf. anpassen)
DISPLAY_RS = 7
DISPLAY_E1 = 8
DISPLAY_E2 = 17
DISPLAY_DATA4 = 25
DISPLAY_DATA5 = 24
DISPLAY_DATA6 = 23
DISPLAY_DATA7 = 18
DISPLAY_WIDTH = 27 # Zeichen je Zeile
DISPLAY_LINE_1 = 0x00 #0x80 # Adresse der ersten Display Zeile
DISPLAY_LINE_2 = 0x40 #0xC0 # Adresse der zweiten Display Zeile
DISPLAY_CHR = True
DISPLAY_CMD = False
E_PULSE = 0.00005
E_DELAY = 0.00005
def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(DISPLAY_E1, GPIO.OUT)
GPIO.setup(DISPLAY_E2, GPIO.OUT)
GPIO.setup(DISPLAY_RS, GPIO.OUT)
GPIO.setup(DISPLAY_DATA4, GPIO.OUT)
GPIO.setup(DISPLAY_DATA5, GPIO.OUT)
GPIO.setup(DISPLAY_DATA6, GPIO.OUT)
GPIO.setup(DISPLAY_DATA7, GPIO.OUT)
display_init()
# 1-Wire Slave-Liste lesen
file = open('/sys/devices/w1_bus_master1/w1_master_slaves')
w1_slaves = file.readlines()
file.close()
# Leere Liste anlegen
temp = []
# Fuer jeden 1-Wire Slave aktuelle Temperatur ausgeben
for line in w1_slaves:
# 1-wire Slave extrahieren
w1_slave = line.split("\n")[0]
# 1-wire Slave Datei lesen
file = open('/sys/bus/w1/devices/' + str(w1_slave) + '/w1_slave')
filecontent = file.read()
file.close()
# Temperaturwerte auslesen und konvertieren und in Liste speichern
stringvalue = filecontent.split("\n")[1].split(" ")[9]
temperature = round(float(stringvalue[2:]) / 1000,0)
# temperature1 = (str(w1_slave) + ': %6.2f C' % temperature)
temperature1 = ('%.0f C' % temperature)
temp.append(temperature1)
time.sleep(1)
lcd_byte1(DISPLAY_LINE_1, DISPLAY_CMD)
lcd_string1("Puffer oben: " + temp[0])
lcd_byte1(DISPLAY_LINE_2, DISPLAY_CMD)
lcd_string1("123456789012345678901234567890123456789012345678901234567890")
time.sleep(1)
lcd_byte2(DISPLAY_LINE_1, DISPLAY_CMD)
lcd_string2("Puffer unten: " + temp[1])
lcd_byte2(DISPLAY_LINE_2, DISPLAY_CMD)
lcd_string2("Zeile22")
time.sleep(5)
# lcd_byte1(0x01, DISPLAY_CMD) Display loeschen
# GPIO.cleanup()
#TXT Datei erstellen
webtemp = open('heizung.txt', 'w')
webtemp.write('Puffer oben\n')
webtemp.write('Puffer unten\n')
webtemp.close()
def display_init():
lcd_byte1(0x33,DISPLAY_CMD)
lcd_byte1(0x32,DISPLAY_CMD)
lcd_byte1(0x28,DISPLAY_CMD)
lcd_byte1(0x0C,DISPLAY_CMD)
lcd_byte1(0x06,DISPLAY_CMD)
lcd_byte1(0x01,DISPLAY_CMD)
lcd_byte2(0x33,DISPLAY_CMD)
lcd_byte2(0x32,DISPLAY_CMD)
lcd_byte2(0x28,DISPLAY_CMD)
lcd_byte2(0x0C,DISPLAY_CMD)
lcd_byte2(0x06,DISPLAY_CMD)
lcd_byte2(0x01,DISPLAY_CMD)
def lcd_string1(message):
message = message.ljust(DISPLAY_WIDTH," ")
for i in range(DISPLAY_WIDTH):
lcd_byte1(ord(message[i]),DISPLAY_CHR)
def lcd_string2(message):
message = message.ljust(DISPLAY_WIDTH," ")
for i in range(DISPLAY_WIDTH):
lcd_byte2(ord(message[i]),DISPLAY_CHR)
def lcd_byte1(bits, mode):
GPIO.output(DISPLAY_RS, mode)
GPIO.output(DISPLAY_DATA4, False)
GPIO.output(DISPLAY_DATA5, False)
GPIO.output(DISPLAY_DATA6, False)
GPIO.output(DISPLAY_DATA7, False)
if bits&0x10==0x10:
GPIO.output(DISPLAY_DATA4, True)
if bits&0x20==0x20:
GPIO.output(DISPLAY_DATA5, True)
if bits&0x40==0x40:
GPIO.output(DISPLAY_DATA6, True)
if bits&0x80==0x80:
GPIO.output(DISPLAY_DATA7, True)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_E1, True)
time.sleep(E_PULSE)
GPIO.output(DISPLAY_E1, False)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_DATA4, False)
GPIO.output(DISPLAY_DATA5, False)
GPIO.output(DISPLAY_DATA6, False)
GPIO.output(DISPLAY_DATA7, False)
if bits&0x01==0x01:
GPIO.output(DISPLAY_DATA4, True)
if bits&0x02==0x02:
GPIO.output(DISPLAY_DATA5, True)
if bits&0x04==0x04:
GPIO.output(DISPLAY_DATA6, True)
if bits&0x08==0x08:
GPIO.output(DISPLAY_DATA7, True)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_E1, True)
time.sleep(E_PULSE)
GPIO.output(DISPLAY_E1, False)
time.sleep(E_DELAY)
def lcd_byte2(bits, mode):
GPIO.output(DISPLAY_RS, mode)
GPIO.output(DISPLAY_DATA4, False)
GPIO.output(DISPLAY_DATA5, False)
GPIO.output(DISPLAY_DATA6, False)
GPIO.output(DISPLAY_DATA7, False)
if bits&0x10==0x10:
GPIO.output(DISPLAY_DATA4, True)
if bits&0x20==0x20:
GPIO.output(DISPLAY_DATA5, True)
if bits&0x40==0x40:
GPIO.output(DISPLAY_DATA6, True)
if bits&0x80==0x80:
GPIO.output(DISPLAY_DATA7, True)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_E2, True)
time.sleep(E_PULSE)
GPIO.output(DISPLAY_E2, False)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_DATA4, False)
GPIO.output(DISPLAY_DATA5, False)
GPIO.output(DISPLAY_DATA6, False)
GPIO.output(DISPLAY_DATA7, False)
if bits&0x01==0x01:
GPIO.output(DISPLAY_DATA4, True)
if bits&0x02==0x02:
GPIO.output(DISPLAY_DATA5, True)
if bits&0x04==0x04:
GPIO.output(DISPLAY_DATA6, True)
if bits&0x08==0x08:
GPIO.output(DISPLAY_DATA7, True)
time.sleep(E_DELAY)
GPIO.output(DISPLAY_E2, True)
time.sleep(E_PULSE)
GPIO.output(DISPLAY_E2, False)
time.sleep(E_DELAY)
if __name__ == '__main__':
main()
Alles anzeigen