Ich habe zahlreiche Beiträge dazu gesucht und gelesen und die Auflösung gelesen. Grundsätzlich sehe ich, dass es auf Einrückung ankommt. Ich habe alle Empfehlungen ausprobiert, erhalte jedoch immer noch die Fehlermeldung, dass 'return' außerhalb der Funktion liegt. at ' return temp_c, temp_f ' Irgendwelche Ideen?

# RasPi Temperature Sensor - Beginning Sensor Networks

#

# For this script, we explore connecting a digital temperature sensor

# to the Raspberry Pi and reading the data. We display the

# temperature

# in Celsius and Fahrenheit.

# Import Python modules (always list in alphabetical order)

import glob

import os

import time

# Issue the modprobe statements to initialize the GPIO and

# temperature sensor modules

os.system('modprobe w1-gpio')

os.system('modprobe w1-therm')

# Use glob to search the file system for directories that match the

# prefix.

# Save the path to the directory.

datadir = glob.glob('/sys/bus/w1/devices/28*')[0]

# Create the full path to the file

datafile = os.path.join(datadir, 'w1_slave')

# Procedure for reading the raw data from the file.

# Open the file and read all of the lines then close it.

def read_data():

    f = open(datafile, 'r')

    lines = f.readlines()

    f.close()

    return lines

# Read the temperature and return the values found.

def get_temp():

# Initialize the variables.

    temp_c = None

    temp_f = None

    lines = read_data()

# If the end of the first line ends with something other than

# 'YES'

# Try reading the file again until 'YES' is found.

while not lines[0].strip().endswith('YES'):

    time.sleep(0.25)

    lines = read_data()

# Search the second line for the data prefixed with 't='

    pos = lines[1].find('t=')

# A return code of -1 means it wasn't found.

if pos != -1:

# Get the raw data located after the 't=' until the end of the

    temp_string = lines[1][ pos+2:]

# Convert the scale for printing

    temp_c = float(temp_string) / 1000.00

# Convert to Farenheit

    temp_f = temp_c * 9.00 / 5.00 + 32.00


return temp_c, temp_f

# Main loop. Read data then sleep 1 second until cancelled with CTRL-
# C.

while True:

    temp_c, temp_f = get_temp()

    print("Temperature is {0} degrees Celsius, "

    "{1} degrees Fahrenheit.".format(temp_c, temp_f))
    time.sleep(1)
-1
user340424 20 Aug. 2015 im 06:51

3 Antworten

Beste Antwort

Sie müssen alles nach lines = read_data() und bis zu return temp_c, temp_f einrücken.

# RasPi Temperature Sensor - Beginning Sensor Networks

#

# For this script, we explore connecting a digital temperature sensor

# to the Raspberry Pi and reading the data. We display the

# temperature

# in Celsius and Fahrenheit.

# Import Python modules (always list in alphabetical order)

import glob

import os

import time

# Issue the modprobe statements to initialize the GPIO and

# temperature sensor modules

os.system('modprobe w1-gpio')

os.system('modprobe w1-therm')

# Use glob to search the file system for directories that match the

# prefix.

# Save the path to the directory.

datadir = glob.glob('/sys/bus/w1/devices/28*')[0]

# Create the full path to the file

datafile = os.path.join(datadir, 'w1_slave')

# Procedure for reading the raw data from the file.

# Open the file and read all of the lines then close it.

def read_data():

    f = open(datafile, 'r')

    lines = f.readlines()

    f.close()

    return lines

# Read the temperature and return the values found.

def get_temp():

# Initialize the variables.

    temp_c = None

    temp_f = None

    lines = read_data()

    # <<<<<<<<<<<<<<< I INDENTED FROM HERE... <<<<<<<<<<<<<<<

    # If the end of the first line ends with something other than

    # 'YES'

    # Try reading the file again until 'YES' is found.

    while not lines[0].strip().endswith('YES'):

        time.sleep(0.25)

        lines = read_data()

    # Search the second line for the data prefixed with 't='

        pos = lines[1].find('t=')

    # A return code of -1 means it wasn't found.

    if pos != -1:

    # Get the raw data located after the 't=' until the end of the

        temp_string = lines[1][ pos+2:]

    # Convert the scale for printing

        temp_c = float(temp_string) / 1000.00

    # Convert to Farenheit

        temp_f = temp_c * 9.00 / 5.00 + 32.00


    return temp_c, temp_f

    # <<<<<<<<<<<<<<< ... TO HERE <<<<<<<<<<<<<<<


# Main loop. Read data then sleep 1 second until cancelled with CTRL-
# C.

while True:

    temp_c, temp_f = get_temp()

    print("Temperature is {0} degrees Celsius, "

    "{1} degrees Fahrenheit.".format(temp_c, temp_f))
    time.sleep(1)
0
Cyphase 20 Aug. 2015 im 03:59
def get_temp():

   temp_c = None

   temp_f = None

   lines = read_data()

   # If the end of the first line ends with something other than

   # 'YES'

   # Try reading the file again until 'YES' is found.

   while not lines[0].strip().endswith('YES'):

      time.sleep(0.25)

      lines = read_data()

      # Search the second line for the data prefixed with 't='

      pos = lines[1].find('t=')

      # A return code of -1 means it wasn't found.

      if pos != -1:

      # Get the raw data located after the 't=' until the end of the

      temp_string = lines[1][ pos+2:]

      # Convert the scale for printing

      temp_c = float(temp_string) / 1000.00

      # Convert to Farenheit

      temp_f = temp_c * 9.00 / 5.00 + 32.00


   return temp_c, temp_f

Einrückung, wenn das hilft

0
Romaan 20 Aug. 2015 im 04:00

Hier ist eine richtig eingerückte Version Ihres Codes. Sie hatten auch eine Mischung aus Tabulatoren und 4 Leerzeichen, die vermieden werden sollte, da dies häufig Probleme verursacht.

# RasPi Temperature Sensor - Beginning Sensor Networks
#
# For this script, we explore connecting a digital temperature sensor
# to the Raspberry Pi and reading the data. We display the
# temperature
# in Celsius and Fahrenheit.

# Import Python modules (always list in alphabetical order)
import glob
import os
import time

# Issue the modprobe statements to initialize the GPIO and
# temperature sensor modules
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

# Use glob to search the file system for directories that match the
# prefix.

# Save the path to the directory.
datadir = glob.glob('/sys/bus/w1/devices/28*')[0]

# Create the full path to the file
datafile = os.path.join(datadir, 'w1_slave')

# Procedure for reading the raw data from the file.
# Open the file and read all of the lines then close it.
def read_data():
    f = open(datafile, 'r')
    lines = f.readlines()
    f.close()
    return lines

# Read the temperature and return the values found.
def get_temp():
    # Initialize the variables.
    temp_c = None
    temp_f = None
    lines = read_data()

    # If the end of the first line ends with something other than
    # 'YES'

    # Try reading the file again until 'YES' is found.
    while not lines[0].strip().endswith('YES'):
        time.sleep(0.25)
        lines = read_data()
        # Search the second line for the data prefixed with 't='
        pos = lines[1].find('t=')

        # A return code of -1 means it wasn't found.
        if pos != -1:
            # Get the raw data located after the 't=' until the end of the
            temp_string = lines[1][ pos+2:]
            # Convert the scale for printing
            temp_c = float(temp_string) / 1000.00
            # Convert to Farenheit
            temp_f = temp_c * 9.00 / 5.00 + 32.00

    return temp_c, temp_f

# Main loop. Read data then sleep 1 second until cancelled with CTRL-
# C.
while True:
    temp_c, temp_f = get_temp()
    print("Temperature is {0} degrees Celsius, "
          "{1} degrees Fahrenheit.".format(temp_c, temp_f))
    time.sleep(1)
0
martineau 20 Aug. 2015 im 04:08