Mercurial > ~darius > hgwebdir.cgi > beermon
comparison MonitorDev.py @ 16:67a4dc218bbf
- Add commented out entries for ZigBee experiment.
- Send a return and sync up before sending out commands.
author | darius |
---|---|
date | Tue, 29 Jan 2008 11:39:22 +0000 |
parents | 8ada8d5cca15 |
children |
comparison
equal
deleted
inserted
replaced
15:0ec70371cfcf | 16:67a4dc218bbf |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 ############################################################################ | 3 ############################################################################ |
4 # Monitoring/control interface to hardware for beermon | 4 # Monitoring/control interface to hardware for beermon |
5 # | 5 # |
6 # $Id: MonitorDev.py,v 1.3 2007/10/23 01:05:22 darius Exp $ | 6 # $Id: MonitorDev.py,v 1.4 2008/01/29 11:39:22 darius Exp $ |
7 # | 7 # |
8 # Depends on: Python 2.3 (I think) | 8 # Depends on: Python 2.3 (I think) |
9 # | 9 # |
10 ############################################################################ | 10 ############################################################################ |
11 # | 11 # |
89 self.minHeatOvershoot = conf.getfloat('hardware', 'minHeatOvershoot') | 89 self.minHeatOvershoot = conf.getfloat('hardware', 'minHeatOvershoot') |
90 self.minCoolOvershoot = conf.getfloat('hardware', 'minCoolOvershoot') | 90 self.minCoolOvershoot = conf.getfloat('hardware', 'minCoolOvershoot') |
91 | 91 |
92 # Setup locking & spawn SSH | 92 # Setup locking & spawn SSH |
93 self.commsLock = threading.Lock() | 93 self.commsLock = threading.Lock() |
94 #self.p = pexpect.spawn('/usr/bin/cu', ['-l', '/dev/cuaU1', '-s', '38400']) | |
94 self.p = pexpect.spawn('/usr/bin/ssh', ['-xt', '-enone', '-i', '/home/darius/.ssh/id_wrt', 'root@wrt', '(echo logged in; microcom -D/dev/cua/1)']) | 95 self.p = pexpect.spawn('/usr/bin/ssh', ['-xt', '-enone', '-i', '/home/darius/.ssh/id_wrt', 'root@wrt', '(echo logged in; microcom -D/dev/cua/1)']) |
96 | |
97 self.p.timeout = 30 | |
98 #assert(self.p.expect('Connected') == 0) | |
95 assert(self.p.expect('logged in') == 0) | 99 assert(self.p.expect('logged in') == 0) |
96 self.p.timeout = 3 | 100 self.p.timeout = 3 |
97 self.setspeed() | 101 self.setspeed() |
98 | 102 |
99 # Search for 1-wire modules | 103 # Search for 1-wire modules |
108 log.debug("minHeatOnTime - %d, minHeatOffTime - %d" % | 112 log.debug("minHeatOnTime - %d, minHeatOffTime - %d" % |
109 (self.minHeatOnTime, self.minHeatOffTime)) | 113 (self.minHeatOnTime, self.minHeatOffTime)) |
110 log.debug("minHeatOvershoot - %3.2f, minCoolOvershoot - %3.2f" % | 114 log.debug("minHeatOvershoot - %3.2f, minCoolOvershoot - %3.2f" % |
111 (self.minHeatOvershoot, self.minCoolOvershoot)) | 115 (self.minHeatOvershoot, self.minCoolOvershoot)) |
112 self.start() | 116 self.start() |
113 | 117 |
114 def setspeed(self): | 118 def setspeed(self): |
115 """Set the speed microcom talks to the serial port to 38400""" | 119 """Set the speed microcom talks to the serial port to 38400""" |
116 self.commsLock.acquire() | 120 self.commsLock.acquire() |
117 self.p.send('~') | 121 self.p.send('~') |
118 assert(self.p.expect('t - set terminal') == 0) | 122 assert(self.p.expect('t - set terminal') == 0) |
121 self.p.send('p') | 125 self.p.send('p') |
122 assert(self.p.expect('f - 38400') == 0) | 126 assert(self.p.expect('f - 38400') == 0) |
123 self.p.send('f') | 127 self.p.send('f') |
124 assert(self.p.expect('done!') == 0) | 128 assert(self.p.expect('done!') == 0) |
125 self.commsLock.release() | 129 self.commsLock.release() |
126 | 130 |
127 def find1wire(self): | 131 def find1wire(self): |
128 """Scan the bus for 1-wire devices""" | 132 """Scan the bus for 1-wire devices""" |
129 self.commsLock.acquire() | 133 self.commsLock.acquire() |
130 self.p.sendline('') | 134 self.p.sendline('') |
131 assert(self.p.expect('> ') == 0) | 135 assert(self.p.expect('> ') == 0) |
143 # Loop until we get the command prompt (> ) collecting ROM IDs | 147 # Loop until we get the command prompt (> ) collecting ROM IDs |
144 while True: | 148 while True: |
145 idx = self.p.expect([self.romre, self.promptre]) | 149 idx = self.p.expect([self.romre, self.promptre]) |
146 if (idx == 0): | 150 if (idx == 0): |
147 # Matched a ROM | 151 # Matched a ROM |
148 #print "Found ROM " + self.p.match.group() | 152 #log.debug("Found ROM " + self.p.match.group()) |
149 devlist.append(self.p.match.group(0)) | 153 devlist.append(self.p.match.group(0)) |
150 elif (idx == 1): | 154 elif (idx == 1): |
151 # Matched prompt, exit | 155 # Matched prompt, exit |
152 break | 156 break |
153 else: | 157 else: |
174 self.temps[i] = float(self.readTemp(i)) | 178 self.temps[i] = float(self.readTemp(i)) |
175 self.lastUpdate[i] = time.time() | 179 self.lastUpdate[i] = time.time() |
176 except OWReadError: | 180 except OWReadError: |
177 # Ignore this - just results in no update reflected by lastUpdate | 181 # Ignore this - just results in no update reflected by lastUpdate |
178 pass | 182 pass |
179 | 183 |
180 return(self.temps) | 184 return(self.temps) |
181 | 185 |
182 def readTemp(self, id): | 186 def readTemp(self, id): |
183 """Read the temperature of a sensor""" | 187 """Read the temperature of a sensor""" |
184 self.commsLock.acquire() | 188 self.commsLock.acquire() |
186 self.p.sendline(cmd) | 190 self.p.sendline(cmd) |
187 # Echo | 191 # Echo |
188 assert(self.p.expect(cmd) == 0) | 192 assert(self.p.expect(cmd) == 0) |
189 # Eat EOL left from expect | 193 # Eat EOL left from expect |
190 self.p.readline() | 194 self.p.readline() |
191 | |
192 line = self.p.readline().strip() | 195 line = self.p.readline().strip() |
193 self.commsLock.release() | 196 self.commsLock.release() |
194 # 'CRC mismatch' can mean that we picked the wrong ROM.. | 197 # 'CRC mismatch' can mean that we picked the wrong ROM.. |
195 if (re.match('CRC mismatch', line) != None): | 198 if (re.match('CRC mismatch', line) != None): |
196 raise OWReadError | 199 raise OWReadError |
197 | |
198 return(line) | 200 return(line) |
199 | 201 |
200 def setState(self, state): | 202 def setState(self, state): |
201 """Set the heat/cool state, track the on/off time""" | 203 """Set the heat/cool state, track the on/off time""" |
202 if (state == 'cool'): | 204 if (state == 'cool'): |
228 | 230 |
229 self.currState = state | 231 self.currState = state |
230 | 232 |
231 self.commsLock.acquire() | 233 self.commsLock.acquire() |
232 # Need the extra spaces cause the parser in the micro is busted | 234 # Need the extra spaces cause the parser in the micro is busted |
235 self.p.sendline('') | |
236 assert(self.p.expect('> ') == 0) | |
233 cmd = 'out c %02x' % relay | 237 cmd = 'out c %02x' % relay |
234 self.p.sendline(cmd) | 238 self.p.sendline(cmd) |
235 # Echo | 239 # Echo |
236 assert(self.p.expect(cmd) == 0) | 240 assert(self.p.expect(cmd) == 0) |
237 self.commsLock.release() | 241 self.commsLock.release() |
238 | 242 |
239 def run(self): | 243 def run(self): |
240 """Sit in a loop polling temperatures""" | 244 """Sit in a loop polling temperatures""" |