python4oceanographers

Turning ripples into waves

Monitoring your computer temperature

Again another late post! I am still getting settle here in beautiful Salvador. Unfortunately, the bus strike, the World Cup preparations, and the protests are not helping our apartment search.

Because of that this will be another "script dump post." This script is very useful for those who insist on using Linux on machines designed for windows, and suffer from constant overheating.

The script uses an interesting library to call shell commands as if they were python functions (sh), and Google Translate Speech Service to say out loud the core temperature.

Hope it is useful!

[monitor_temperature] monitor_temperature.py download
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3

import os
import sys
from sh import sensors
from time import sleep
from tempfile import mktemp
from urllib.request import build_opener

# Speak params.
mp3 = mktemp()
opener = build_opener()
google_translate_url = 'http://translate.google.com/translate_tts'
agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)'
opener.addheaders = [('User-agent', agent)]


def parse_temps(line):
    cut = slice(1, 5)
    return float(line.split(':')[1].strip()[cut])


def get_temps():
    res = sensors()
    lines = res.stdout.decode(encoding='UTF-8').split('\n')
    temps = []
    for line in lines:
        if 'Core' in line:
            temps.append(parse_temps(line))
    return max(temps)


def speak(phrase):
    try:
        url = phrase.replace(' ', '%20')
        response = opener.open('%s?q=%s&tl=en' %
                               (google_translate_url, url))
        ofp = open(mp3, 'wb')
        ofp.write(response.read())
        ofp.close()
        os.system('mplayer2 %s > /dev/null 2>&1' % mp3)
    except:
        os.system('espeak "%s"' % phrase)
    return None

def main():
    while True:
        sleep(2)
        os.system('clear && sensors')
        temp = get_temps()
        phrase = None
        if temp >= 94:
            phrase = 'Temperature is %s' % temp
        if temp > 96:
            phrase = 'Turn off the computer before it blows up!'

        if phrase:
            speak(phrase)
            sleep(1)

if __name__ == '__main__':
    sys.exit(main())

In [2]:
HTML(html)
Out[2]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://ocefpaf.github.io/.

Comments