Page 1 of 1

dht22 email python

Posted: Wed Dec 06, 2017 7:40 pm
by sebastiancusis1
hi everyone!

this is my first post so be gentle pls :)
i have an issue with 2 skripts. im trying to send temperature readings (dht22 sensor) via email using python.
1 code:

Code: Select all

import smtplib
import Adafruit_DHT
while True:
    sensor = Adafruit_DHT.DHT22
    pin = 18
    humidity, temperature = Adafruit_DHT.read_retry(sensor,pin)
    if (temperature > 22.5):
        print(temperature)
        
        smtpUser = "email"
        smtpPass = "password"

        toAdd = "email"
        fromAdd = smtpUser

        subject = "the temperature is to high" 
        header = "To: " + toAdd + "\n" + "From:" + fromAdd + "\n" + "Subject:" + subject
        body = ("Warning: temp: {0:0.1f} C".format(temperature))

    
    
        print(header + "\n" + body) 

        s = smtplib.SMTP("smtp.gmail.com", 587)

        s.ehlo()
        s.starttls()
        s.ehlo()

        s.login(smtpUser, smtpPass) 
        s.sendmail(fromAdd, toAdd, header + "\n\n" + body)

        s.quit()
ok so this one works! but the temperature part from "while true till print temperature", is a module that i already created. so want i want to use this module (dht22temp.py)

Code: Select all

import Adafruit_DHT
import time
while True:
        sensor = Adafruit_DHT.DHT22
        pin = 18
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        print("Temp: {1:0.1f} C Humidity: {0:0.1f} %".format(humidity, temperature))
        time.sleep(2)

into the first one. for that i wrote this:

Code: Select all

import smtplib
import dht22temp
if temperature < 22.0:      
    smtpUser = "email"
    smtpPass = "password"
... the rest is the same!... so what i get with the first try (the one that works!: an email with a "warning" + temperature)
but with the second try(the one with module DHT22temp.py imported): i keep getting the temperature readings in the python shell... the if conditional doesnt make anything and i dont get emails...
I dont know if its somethjing to do with the adafruit library that i used for the temperature code, or i have to difine more variables in email code. i just want to import the part that reads the temperature and add it to the email. just like the first try, but there i had to copy paste the code from the dht22temp.py and well yeah it should be easier that that.

srry for the english, its not my first language :p

THanks a lot!

Re: dht22 email python

Posted: Thu Dec 07, 2017 9:29 am
by jbudd
Isn't your code stuck in an infinite loop measuring and printing temperature? It never gets to the IF statement?

Re: dht22 email python

Posted: Thu Dec 07, 2017 5:51 pm
by sebastiancusis1
yes!! it only shows me the temp annd humidity like with the "dht22temp.py" code... but it doesnt run the rest (email part). how could i fix it? :)

Re: dht22 email python

Posted: Thu Dec 07, 2017 6:06 pm
by jbudd
The test for temperature > 22.5 needs to be inside the while True: loop.

I don't know about writing / using Python modules, but it seems odd to me that your module contains both an infinite loop and a hard coded 2 second delay.

If your first code sample works, why not use it?

Re: dht22 email python

Posted: Thu Dec 07, 2017 6:24 pm
by sebastiancusis1
because in the first sample i just copy paste the code. But i want to import the function of the code (read the temperature)use that into the email code. so:
...
import dht22temp

temperature = dht22temp.temperature #So i assume with this i get the temperature readings

while True:
if temperature < 22:
"then the email"

but like u said. it just keeps giving me the temperature readings back and from "while True: ..." doesnt work.