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