alphakilo11 / Python

Testing Python Code
0 stars 0 forks source link

Create an automatic temperature alert #13

Open alphakilo11 opened 1 year ago

alphakilo11 commented 1 year ago

intented for use on computers that run while I'm away.

Use this:

import os
import smtplib
import time

# Set the temperature threshold (in degrees Celsius)
threshold = 70

# Set the email credentials
username = "your_email@example.com"
password = "your_email_password"

# Set the recipient's email address
recipient = "recipient@example.com"

# Set the email subject and message
subject = "High CPU Temperature Alert"
message = "The CPU temperature is currently above the threshold of {} degrees Celsius.".format(threshold)

while True:
    # Get the current CPU temperature
    temp = os.popen("vcgencmd measure_temp").readline()

    # Extract the temperature value from the output
    temp = float(temp.replace("temp=", "").replace("'C\n", ""))

    # Check if the temperature exceeds the threshold
    if temp > threshold:
        # Send the email
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(username, password)
        server.sendmail(username, recipient, "Subject: {}\n\n{}".format(subject, message))
        server.quit()

        print("An email has been sent to {}".format(recipient))

    # Wait 10 seconds before checking the temperature again
    time.sleep(10)

and that:

import wmi
w = wmi.WMI(namespace="root\OpenHardwareMonitor")
temperature_infos = w.Sensor()
for sensor in temperature_infos:
    if sensor.SensorType==u'Temperature':
        print(sensor.Name)
        print(sensor.Value)