Question

In: Computer Science

Write a Python code that when the temperature of the CPU in the raspberry pi exceeds...

Write a Python code that when the temperature of the CPU in the raspberry pi exceeds 40 degrees Celsius you receive an email.

Solutions

Expert Solution

# coding=utf-8
import os
import smtplib
from email.mime.text import MIMEText

critical = False
high = 40
too_high = 45

# At First we have to get the current CPU-Temperature with this defined function
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return(res.replace("temp=","").replace("'C\n",""))

# Now we convert our value into a float number
temp = float(getCPUtemperature())

# Check if the temperature is abouve 60°C (you can change this value, but it shouldn't be above 70)
if (temp > high):
    if temp > too_high:
        critical = True
        subject = "Critical warning! The temperature is: {} shutting down!!".format(temp)
        body = "Critical warning! The actual temperature is: {} \n\n Shutting down the pi!".format(temp)
    else:
        subject = "Warning! The temperature is: {} ".format(temp)
        body = "Warning! The actual temperature is: {} ".format(temp)

    # Enter your smtp Server-Connection
    server = smtplib.SMTP('localhost', 25) # if your using *mail: smtp.*mail.com
    server.ehlo()
    server.starttls()
    server.ehlo
    # Login
    # server.login("your email or username", "your Password")
    
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = "Root"
    msg['To'] = "root"

    # Finally send the mail
    server.sendmail("root", "root", msg.as_string())
    server.quit()
    
    # Critical, shut down the pi
    if critical:
        os.popen('sudo halt')

# Don't print anything otherwise. 
# Cron will send you an email for any command that returns "any" output (so you would get another  email)
# else:
#   print "Everything is working fine!"

it will send a mail as soon as it crosses 40'C

I've tweaked it a little bit to also shut down the pi in case the temp is too high (45'C in my case).

To make this as a cronjob running every 30 min do this

mkdir -p ~/scripts
vi ~/scripts/temp.py

paste the code from the script above.

Then crontab -e and add this:

30 * * * * python ~/scripts/temp.py

close crontab editor (ctrl-x if nano or esc, then :wq if vim)

That's it


Related Solutions

Explain the computer components in detail for Raspberry Pi 4B (8GB) (i.e. CPU, Memory, and I/O)....
Explain the computer components in detail for Raspberry Pi 4B (8GB) (i.e. CPU, Memory, and I/O). The explanation should include the following information: i. CPU: CPU speed, types of CPU that are supported (if any), Number of CPU, Thread, CPU Cores, Cache, virtual memory, and any other related information). ii. Memory: Maximum physical memory that can be used, Memory speed and capacity. iii. I/O devices: List example I/O devices that are supported.
The literature review of raspberry pi, also after that write Component that is technically used and...
The literature review of raspberry pi, also after that write Component that is technically used and Drawback of the subject what u choose brief of the subject: A component that is technically used: Drawback:
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at...
Write a C program to blink two LEDs connected to Raspberry pi. One must blink at a rate of 1 Hz and the other at a rate of 2 HZ.
PYTHON COMPUTER CODE PROGRAMMING - DISPLAYING A PLOT OF TEMPERATURE VERSUS DEPTH WHEN GIVEN A LATITUDE....
PYTHON COMPUTER CODE PROGRAMMING - DISPLAYING A PLOT OF TEMPERATURE VERSUS DEPTH WHEN GIVEN A LATITUDE. PYTHON ASSIGNMENT. NOT SURE HOW TO PLOT THIS DATA WITH THE GIVEN INFO BELOW: Just as the atmosphere may be divided into layers characterized by how the temperature changes as altitude increases, the oceans may be divided into zones characterized by how the temperature changes as depth increases. We shall divide the oceans into three zones: the surface zone comprises the water at depths...
Microcomputers and controllers, including the Raspberry Pi and Arduino, are becoming a big part of the...
Microcomputers and controllers, including the Raspberry Pi and Arduino, are becoming a big part of the Internet of Things (IoT). The Raspberry Pi is a computer about the size of a deck of cards that can be used in some amazing projects. It is Linux based and is a good platform for Python. For example, the Cyber Wildcats used it to incorporate a temperature sensor into Room PEO-032 of the Peoples Building, New Castle campus, to monitor the room temperature....
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
Design and implement a Fire Alarm IOT System, using the framework of the Raspberry PI device,...
Design and implement a Fire Alarm IOT System, using the framework of the Raspberry PI device, temperature, C02 and CO sensors. •Define the process specification of the system. The system should collect and analyze the sensor data and email alerts when a fire is detected •Define the domain model for this IOT device •Define the Service specifications
Find the steady state temperature function on a pi x pi rectangular plate, where the temperature...
Find the steady state temperature function on a pi x pi rectangular plate, where the temperature at the top edge is kept at sin(4x)oC and the remaining 3 sides are kept at 0oC
code in python write a code where the initial value is compounded by a multiplier, rounded...
code in python write a code where the initial value is compounded by a multiplier, rounded to the nearest tenth Output: Initial Value: 10 Multiplier: 1.4 Number of compounds: 10 Your Values are: 10 , 14, 19.6 , 27.4, 38.4, 53.8, 75.3, 105.4, 147.6, 206.6
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT