How to send mail where dth11 temperature critical raspberry pi
To send an email notification when the DHT11 temperature sensor on your Raspberry Pi reaches a critical temperature, you'll need to follow these steps:
Hardware Requirements:
- Raspberry Pi (any model)
- DHT11 temperature and humidity sensor
- Internet connection
Software Requirements:
- Raspbian OS (the official OS for Raspberry Pi)
- Python 3.x (comes pre-installed with Raspbian)
dht11
library (installable via pip)
Step 1: Install the dht11
library
Open a terminal on your Raspberry Pi and run the following command:
sudo pip3 install dht11
This will install the dht11
library, which allows you to read data from the DHT11 sensor.
Step 2: Write a Python script to read temperature data and send email
Create a new file called temperature_monitor.py
and add the following code:
import dht11
import smtplib
from email.mime.text import MIMEText
# Set up the DHT11 sensor
dht = dht11.DHT11(pin=4) # Pin 4 is the default pin for the DHT11 on Raspberry Pi
# Set up the email settings
SMTP_SERVER = "your_smtp_server.com"
SMTP_PORT = 587
FROM_EMAIL = "[email protected]"
TO_EMAIL = "[email protected]"
PASSWORD = "your_email_password"
# Set the critical temperature threshold
CRITICAL_TEMPERATURE = 30 # Celsius
while True:
# Read the temperature data from the DHT11 sensor
temperature = dht.read_temperature()
if temperature is not None:
# Check if the temperature is critical
if temperature > CRITICAL_TEMPERATURE:
# Send an email notification
subject = "Temperature Critical: {}°C".format(temperature)
body = "The temperature has reached a critical level of {}°C. Please take action to cool down the system.".format(temperature)
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = FROM_EMAIL
msg["To"] = TO_EMAIL
# Send the email using SMTP
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(FROM_EMAIL, PASSWORD)
server.sendmail(FROM_EMAIL, TO_EMAIL, msg.as_string())
server.quit()
time.sleep(60) # Check the temperature every 60 seconds
Replace the placeholders with your own values:
your_smtp_server.com
: your email server's hostname or IP address[email protected]
: the email address you want to send the notification from[email protected]
: the email address you want to send the notification toyour_email_password
: the password for the email accountCRITICAL_TEMPERATURE
: the temperature threshold (in Celsius) that triggers the email notification
Step 3: Run the Python script
Save the temperature_monitor.py
file and run it using the following command:
python3 temperature_monitor.py
The script will run indefinitely, checking the temperature every 60 seconds and sending an email notification if the temperature exceeds the critical threshold.
Note: Make sure to replace the placeholders with your own values and test the script before leaving it running unattended.