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.
On a raspberry pi, write an assembler program that will calculate the factorial of an integer...
On a raspberry pi, write an assembler program that will calculate the factorial of an integer value inputted by the user. the program should detect if an overflow occurs. If no overflow occurred, then it should print out the value of the factorial. Otherwise print out a message indicating that an overflow occurred.
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.
Write code in Python that does the following : An anagram is when the order of...
Write code in Python that does the following : An anagram is when the order of a word can be changed to create a new word (evil, live, and vile for example). Using the dictionary provided, find all of the anagram groups. Which words contain the most anagrams? How large is this group? Here is another example: Alert, alter, and later have 3 in their group. Use the provided dict.txt as your word list, solution must also finish in less...
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....
Python Problem 4. Estimate pi version 2: write a program that will quickly estimate pi to...
Python Problem 4. Estimate pi version 2: write a program that will quickly estimate pi to a precision of 1e-4 using a monte carlo approach. Your program should employ a loop where each iteration produces a new x,y pair. One will then determine whether the x,y pair lies inside or outside the circle. (see below) Since all the points will land within the square shown above but only a fraction will land within the circle, we can estimate pi by...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT