Question: use DHT11 sensor and raspberry pi as a circuit. use Python code. During the code is running, senser will keep detecting and print the information

use DHT11 sensor and raspberry pi as a circuit. use Python code.

During the code is running, senser will keep detecting and print the information in terminal.

When the temperature is higher than 22 degrees Celsius, an email with both the temperature and humidity will be sent to smtp email account.

--------------------------------------------

below is my code

#!/usr/bin/python # -*- coding: utf-8 -*

import RPi.GPIO as GPIO import dht11 import time import smtplib import sys from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText

# initialize GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) #GPIO.cleanup()

# read data using Pin GPIO4 instance = dht11.DHT11(pin=4)

while True: result = instance.read() if result.is_valid(): print("Temp: %d C" % result.temperature +' '+"Humid: %d %%" % result.humidity)

if result.temperature >22: fromaddr = "gogocomunication@Xmail.com" toaddr = "gogocomunication@Xmail.com" msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "Room temperature"

body = "Temperature over 27; " + str(result.temperature) + "C Humidity: " + str(result.humidity) + " % Best, RasPi TempBot" msg.attach(MIMEText(body, 'plain')) server = smtplib.SMTP('smtp.Xmail.com', 587) server.starttls() server.login(fromaddr, "XXXX@XXXX") text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit() print("Warning sent.")

time.sleep(1)

---------------------------

however, I revieve a error:

File "dht11-raspberrypi/dht11_example.py", line 25

if result.temperature >22:

IndentationError: unindent does not match any outer indentation level

how can I set this condition right and fix this code?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!