Skip to content Skip to sidebar Skip to footer

How To Deal With Indentationerror: Unexpected Indent

I'm currently working with raspberry pi and using DHT11 to read temperature and humidity values every second. I have to save these values into a database in real time. Here's my co

Solution 1:

you had indent error in try except, both should be at same indent

Inshort it should be like:

imports.....
.
.
.
withopen('test.csv', 'w', newline='') as csvfile:
    .
    .
    .
    try:
        .
        .
    except:
    .
    .

(solved)Full code:

import RPi.GPIO as GPIO
import dht11
import time
import datetime
import csv
import os

dht11_pin = 17
limit_sec = 15#15 sec# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

sleep_time = 1
instance = dht11.DHT11(pin=dht11_pin)
previous_temperature = 0
previous_humidity = 0
with open('test.csv', 'w', newline='') as csvfile:
    field_names = ['Date', 'Time', 'Status', 'Temperature', 'Humidity']
    writer = csv.DictWriter(csvfile, fieldnames=field_names)
    writer.writerow({'Date': 'Date', 'Time': 'Time','Status': 'Status', 'Temperature': 'Temperature', 'Humidity': 'Humidity'})

    try:
        print"DHT11 Module Test (CTRL+C to exit)"print"please wait..."
        time.sleep(2)
        now = datetime.datetime.now()
        print("Ready! "+ now.strftime("%d/%m/%Y") + " / " + now.strftime("%H:%M:%S"))
        result = instance.read()
        if result.is_valid():
            now = datetime.datetime.now()
            print("Initial valid input: " + now.strftime("%d/%m/%Y") + " / " + now.strftime("%H:%M:%S"))
            print("Temperature: %d C" % result.temperature)
            print("Humidity: %d %%" % result.humidity)
            print("*******************************************")

            previous_temperature = result.temperature
            previous_humidity = result.humidity
        else:
            print"Invalid result!"
            pass

        counter = 0
        cnt = 0while True:

            cnt += 1if cnt%limit_sec == 0or cnt == 1:

                result = instance.read()
                if result.is_valid():

                    if previous_temperature != result.temperature or previous_humidity != result.humidity:

                        previous_temperature = result.temperature
                        previous_humidity = result.humidity

                        counter += 1
                        rightnow = datetime.datetime.now()

                        if result.humidity>=40:
                            status = 'Good condition.'print(str(counter)+". Last valid input: " )
                            print("Date: " + rightnow.strftime("%d/%m/%Y"))
                            print("Time: " + rightnow.strftime("%H:%M:%S"))
                            print("Status: Your plant is on the good condition.")
                            print("Temperature: %d C" % result.temperature)
                            print("Humidity: %d %%" % result.humidity)
                            print("*******************************************")


                        else:
                            status = 'Bad condition'print(str(counter)+". Last valid input: " )
                            print("Date: " + rightnow.strftime("%d/%m/%Y"))
                            print("Time: " + rightnow.strftime("%H:%M:%S"))
                            print("Status: Your plant is on the bad condition. Please open the water supply.")
                            print("Temperature: %d C" % result.temperature)
                            print("Humidity: %d %%" % result.humidity)
                            print("*******************************************")
                        writer.writerow({'Date': rightnow.strftime("%d/%m/%Y"), 'Time': rightnow.strftime("%H:%M:%S"), 'Status': status, 'Temperature':result.temperature, 'Humidity': result.humidity})   
                else:
                    print"Invalid result!"
                    pass

            time.sleep(sleep_time)

    except KeyboardInterrupt:
            print" Quit"
            GPIO.cleanup()

Post a Comment for "How To Deal With Indentationerror: Unexpected Indent"