from machine import Pin
from time import sleep
import dht 

sensor = dht.DHT22(Pin(14))

while True:
  try:
    sleep(2)
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    temp_f = temp * (9/5) + 32.0
    print('Temperature: %3.1f C' %temp)
    print('Temperature: %3.1f F' %temp_f)
    print('Humidity: %3.1f %%' %hum)
  except OSError as e:
    print('Failed to read sensor.')


import umail
import network


ssid = 'ESP'
password = 'password'


sender_email = '[email protected]'
sender_name = 'ESP32' #sender name
sender_app_password = 'password'
recipient_email ='[email protected]'
email_subject ='Test Email'

def connect_wifi(ssid, password):
 
  station = network.WLAN(network.STA_IF)
  station.active(True)
  station.connect(ssid, password)
  while station.isconnected() == False:
    pass
  print('Connection successful')
  print(station.ifconfig())
    

connect_wifi(ssid, password)


smtp = umail.SMTP('smtp.gmail.com', 465, ssl=True)
smtp.login(sender_email, sender_app_password)
smtp.to(recipient_email)
smtp.write("From:" + sender_name + "<"+ sender_email+">\n")
smtp.write("Subject:" + email_subject + "\n")
smtp.write("Hello from ESP32")
smtp.send()
smtp.quit()