File: home_weather.py

package info (click to toggle)
digitemp 3.7.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 828 kB
  • sloc: ansic: 7,254; perl: 644; makefile: 118; python: 67; sh: 39; sql: 11
file content (175 lines) | stat: -rwxr-xr-x 5,581 bytes parent folder | download | duplicates (7)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
#
# DigiTemp temperature and q-wire home weather station logging
# Copyright 2003 by Brian C. Lane <bcl@brianlane.com>
# http://www.digitemp.com
#
# Requires DigiTemp v3.1.0 or later for counter support
# Requires Python MySQLdb module and Round Robin Database binaries
#
# Logs data to a MySQL database and to a RRDb graph log
#
# Create a MySQL database called weather and give a weather user permission
# to access it.
# GRANT SELECT,INSERT,CREATE ON weather.* TO weather@localhost IDENTIFIED BY 'password';
#
# CREATE TABLE temperature (
#   TemperatureKey bigint UNSIGNED NOT NULL auto_increment,
#   SerialNumber varchar(20) NOT NULL,
#   RecTime timestamp NOT NULL,
#   C float NOT NULL, 
#   PRIMARY KEY(SerialNumber),
#   KEY(TemperatureKey)
# );
#
# CREATE TABLE counter (
#   CounterKey bigint UNSIGNED NOT NULL auto_increment, 
#   SerialNumber varchar(20) NOT NULL,
#   RecTime timestamp NOT NULL,
#   CounterNum tinyint UNSIGNED NOT NULL,
#   CounterValue mediumint UNSIGNED NOT NULL,
#   PRIMARY KEY(SerialNumber),
#   KEY(CounterKey)
# );
# 

import string, os, sys, time
import MySQLdb

timefmt = '%Y-%m-%d %H:%M:%S'

# Connect to the database
try:
  mydb = MySQLdb.Connect(host='localhost',user='weather',passwd='password',db='weather')
except DatabaseError:
  print "Problem connecting to database"
  sys.exit(-1)

cursor=mydb.cursor()

# 1. Read the output of DigiTemp
cmd = '/home/brian/temperature/digitemp -c/home/brian/temperature/digitemp.cfg -a -q'

for outline in os.popen(cmd).readlines():
   outline = outline[:-1]
#   print outline

   if outline[0:1] == 'T':
      # Parse the temperature sensor line
      S = string.split(outline, " ")
#      print S      

      # Add the reading to the database
      sql = "INSERT INTO temperature VALUES(NULL, %s, %s, %s)"
      sqltime = time.strftime( timefmt, time.localtime(int(S[2])))
      cursor.execute( sql, (S[1], sqltime, S[3]) );

   if outline[0:1] == 'C':
      # Parse the counter line
      S = string.split(outline, " ")
#      print S

      # Add the reading to the database
      sql = "INSERT INTO counter VALUES(NULL, %s, %s, %s, %s)"
      sqltime = time.strftime( timefmt, time.localtime(int(S[2])))
      cursor.execute( sql, (S[1], sqltime, S[3], S[4]) );

# -----------------------------------------------------------------------
# Do interesting things with the just-logged information
# =======================================================================
# Brian's Sensor Map:
#
#10E8A00E00000055      Room (grey wire)
#10B95E05000800AA      Attic
#10575A050008008F      Desk
#22B9B20500000049      DS1822
#286D1D2D000000EA      DS18B20 (kermit)
#1092B9330008002E      Drink
#1D9CB900000000B3      Rain Gauge
#1DFA15010000005F      Wind Speed
#1009212E0008004B      DT-1A passive sensor
#

# Dict of serial numbers and what to name them
sensors = {'10E8A00E00000055':'Office',
           '10B95E05000800AA':'Attic',
           '10575A050008008F':'Desk',
           '22B9B20500000049':'DS1822',
           '286D1D2D000000EA':'DS18B20',
           '1092B9330008002E':'Drink',
           '1009212E0008004B':'DT1A'
          }
counters= {'1D9CB900000000B3':'Rain',
           '1DFA15010000005F':'Wind'
          }

rrdtool_path = "/usr/local/rrdtool/bin/rrdtool"
rrd_path     = "/home/brian/temperature/"

def c2f( c ):
  f = 32.0 + ((c * 9.0) / 5.0)
  return f

rrd_sensors = {}

# grab the latest readings for the sensors
for skey in sensors.keys():
  sql = "SELECT UNIX_TIMESTAMP(rectime), C from temperature where serialnumber='"+skey+"' ORDER BY rectime DESC LIMIT 1"
#  print sql
  cursor.execute(sql)
  result = cursor.fetchone()
#  print result
  rrd_sensors[sensors[skey]] = result

# grab the counters
rrd_counters = {}

# grab the latest readings for the sensors
for ckey in counters.keys():
  sql = "SELECT UNIX_TIMESTAMP(rectime), countervalue from counter where serialnumber='"+ckey+"' AND counternum=0 ORDER BY rectime DESC LIMIT 1"
#  print sql
  cursor.execute(sql)
  result = cursor.fetchone()
#  print result
  rrd_counters[counters[ckey]] = result

# Log the temperatures to the RRD
# rrdtool update drink.rrd time:value
rrd = "%s update %s/drink.rrd %ld:%0.2f"  % (rrdtool_path, rrd_path, rrd_sensors['Drink'][0], rrd_sensors['Drink'][1])
os.system(rrd)
#print rrd

# rrdtool update outside.rrd time:value
rrd = "%s update %s/outside.rrd %ld:%0.2f"  % (rrdtool_path, rrd_path, rrd_sensors['Attic'][0], rrd_sensors['Attic'][1])
os.system(rrd)
#print rrd

# rrdtool update room.rrd time:value
rrd = "%s update %s/room.rrd %ld:%0.2f"  % (rrdtool_path, rrd_path, rrd_sensors['Office'][0], rrd_sensors['Office'][1])
os.system(rrd)
#print rrd

# rrdtool update room.rrd time:value
rrd = "%s update %s/kermit.rrd %ld:%0.2f"  % (rrdtool_path, rrd_path, rrd_sensors['DS1822'][0], rrd_sensors['DS1822'][1])
os.system(rrd)
#print rrd

# Log the counter values
# rrdtool update rain.rrd time:value
rrd = "%s update %s/rain.rrd %ld:%ld"  % (rrdtool_path, rrd_path, rrd_counters['Rain'][0], rrd_counters['Rain'][1])
os.system(rrd)
#print rrd

# rrdtool update wind.rrd rime:value
rrd = "%s update %s/wind.rrd %ld:%ld"  % (rrdtool_path, rrd_path, rrd_counters['Wind'][0], rrd_counters['Wind'][1])
os.system(rrd)
#print rrd


# Write a new .signature file
outfile = open('/home/user/.signature','w')
sig = "--[Inside %0.1fF]--[Outside %0.1fF]--[Kermit %0.1fF]--[Coaster %0.1fF]--\n" % (c2f(rrd_sensors['Office'][1]),c2f(rrd_sensors['Attic'][1]),c2f(rrd_sensors['DS1822'][1]),c2f(rrd_sensors['Drink'][1]))
outfile.write(sig)
outfile.close()

mydb.close()