19f603abe9104a762d0d4a55b39943a3937d975e
[pypid.git] / examples / temp_monitor.py
1 #!/usr/bin/env python
2 # Copyright (C) 2011 W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of pypid.
5 #
6 # pypid is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
10 #
11 # pypid is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Lesser General Public License for more details.
15 #
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with pypid.  If not, see
18 # <http://www.gnu.org/licenses/>.
19
20 """Log control and ambient temperature every 10 seconds.
21
22 usage: python temp_monitor.py
23 """
24
25 import time
26
27 from pypid.backend import get_backend
28
29
30 b = get_backend('melcor')()
31 period = 10
32
33 with open('temp_monitor.log', 'a') as f:
34     last = time.time()
35     last -= last % period
36     next_time = last + period
37     while True:
38         time.sleep(next_time - time.time())
39         tstr = time.strftime('%Y-%m-%d %H:%M:%S')
40         temp = str(b.get_temp())
41         ambient = str(b.get_ambient_temp())
42         f.write('\t'.join([tstr, temp, ambient]) + '\n')
43         f.flush()
44         print('\t'.join([tstr, temp, ambient]))
45         next_time += period