OpenTLD – Georg Nebehay’s version of OpenTLD in OpenCV 2.3.1 and Towel Day

Happy Towel Day, froods. DON’T PANIC! Coding period has already started and I’m a bit behind my schedule. Here’s a basic task list that I’ll follow. Task list: Get this up and running on your machine Learn how it … Continue reading

Monitoring HDD Temperature using python

I have an HP laptop. And if you have ever used an HP laptop, you must understand my situation. HP heats up a lot. There’s no proper ventilation and heat sink. Despite of using a cooling pad, sometimes my laptop’s core temperature reaches critical temperature value and shuts down. I think it’s quite harmful. To avoid such situation, I always wanted something to measure the core temperature. One of my friends suggested me to use lm-sensors. It’s not bad but it gives me temperature value only when do
$ sensors
It’s kind of irritating to do it constantly to check whether temperature is within range or not.

I wanted to make a python script which would check temperature every minute and warn me if it goes beyond acceptable limit. os.system() would never work. So, I kept looking for something which would give me temperature data. I found this page.
It uses hddtemp. hddtemp sends data via socket 7643(default) to localhost(default).
So, here’s how I have worked it out.

import socket
import time
import os

port = 7634
host = 'localhost'
temp = 45

os.system("notify-send "+"'checking hdd temp'")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
		data = s.recv(4096)
		temp = int(data.split('|')[3])
		if temp > 90:
			notify_string = "'Critical Temp over 90 C."
			os.system('notify-send '+notify_string)
	except KeyboardInterrupt:

hddtempis a requirement. While configuring hddtemp, set it’s port to 7634, which is default, or you can set it to anything you want. change it accordingly. And add it to the daemon. So that it starts at boot.

P.S. Screwed this sem’s GPA.


I can’t believe it that I never came across popen2. I feel so stupid now. Had I known earlier about popen2, I would have used it in many of my scripts. Anyway, better late than never.
So I found out about popen2. It’s a good way to pipe the output of the command. Now, I can easily access the temperature without hddtemp. I pipe the output of lm-sensors to get temperature value.

import popen2
files = popen2.popen2("sensors")
r = files[0].readlines()
temp = int(r.split()[-4][1:3])            # unable to split using "\"
print temp

Read more about popen2

P.S. I am so embarrassed. How could I not know about popen !


@JabbaLaci just told me that popen2 has been deprecated since python 2.6 and suggested me to use subprocess. And also that there are some problems with output of hddtemp. I have changed it.
Using subprocess:

import subprocess

process = subprocess.Popen(["sensors"],shell=False,stdout=subprocess.PIPE)
data = process.communicate()    # returns tuple
temp = int(data[0].split()[5][1:3])
print temp

Read more about subprocess and Popen

P.S. TIL about TIL and subprocess with pipe.

Install OpenCV 2.3.1 and SimpleCV in Ubuntu 12.04 Precise Pangolin, Arch Linux

After screwing up my Ubuntu see previous post and also my Arch Linux, I installed Ubuntu 11.10 and upgraded to 12.04.   Ubuntu 12.04 LTS – Precise Pangolin Install OpenCV 2.3.1 sudo apt-get install libopencv-* sudo apt-get isntall python-opencv sudo … Continue reading