Environment Monitoring

Posted on by Arthur Guy

I have a number of computers which run various things such as email, websites (including this one), databases and a telephone system, these computers are kept in a secure room with very poor ventilation so every year when summer roles around I start to panic as the temperature starts of pass 30 degrees.

This year I have done something about it, a few weeks ago I installed a 12,000 BTU air conditioning unit as well as an environmental monitor, the sensor is a networkable temperature and humidity sensor from a company called Omega. The sensor is monitored using a company called Server Monitoring which I have been using for a couple of years for my main websites, they record the environment conditions every 15 minutes and send me a mice graph each day. Unlike the website monitoring this isn’t as critical so I got wondering why don’t I build a system to monitor it myself, something that records the temperate and humidity every minute and would allow me to process the data myself.

I normally use php for programming websites and the various automated scripts that I have running but this time I decided I would try something different, for some reason I settled on python so below is my simple script that requests the data and stores it in a database.

import socket
import MySQLdb
import time

data = ''

while 1:
        temp = 0
        hum = 0
        HOST = 'sensor'
        PORT = 2000
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST, PORT))
        s.send('SRTC\r')
        data = s.recv(1024)
        s.close()
        #print 'Received', repr(data)
        print 'Waiting for Data ...'

        if data[0:2] == 'TA':
                temp = data[2:7]
    ?
        if data[0:2] == 'HA':
                hum = data[2:7]
    ?
        if temp != 0 and hum != 0:
                print 'Temperature ', temp
                print 'Humidity ', hum
                print 'Saving Data ...'

                # connect
                db = MySQLdb.connect(host="?", user="?", passwd="?", db="?")

                # create a cursor
                cursor = db.cursor()

                #Store Data
                cursor.execute("""INSERT INTO environment (temp, hum, loc)
                         VALUES ("""+temp+""", """+hum+""", "SF1")""")

                print 'Data Save Complete'

                print 'Sleeping for 60 Seconds'
                time.sleep(60)
                print 'Awake'

This is my first attempt at making a script to store this information and my first attempt at programming in python so its not going to be perfect but it does work, I used a couple of other websites to collect several snippets of code to put it together.

The plan now is to build a reporting system into my intranet site that pulls the data of the database and makes some graphs for it

Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • StumbleUpon
  • Twitter
  • email


blog comments powered by Disqus