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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started