Rev Author Line No. Line
4140 kakl 1 #!/usr/bin/python
2 """
3 Testbed for the animation.
4  
5 It basically produces series of temperatures that get animated on the client
6 browser side.
7  
8 """
9  
10 import time
11 import sys
12 from pymlab import config
13  
14 import matplotlib
15 matplotlib.use('module://mplh5canvas.backend_h5canvas')
16 from pylab import *
17 import time
18 import numpy as np
19  
20 t = [1 + i for i in range(0, 70)]
21 s = [-30 + i for i in range(0, 70)]
22  
23 plot(t, s, linewidth=3.0)
24 xlabel('time (s)')
25 ylabel('temperature (C)')
26 title('Temperature')
27 f = gcf()
28 ax = f.gca()
29  
30 show(block=False)
31 # show the figure manager but don't block script execution so animation works..
32  
33  
34 while True:
35 #### Sensor Configuration ###########################################
36 cfg = config.Config(
37 i2c = {
38 "port": 0, # I2C bus number
39 },
40  
41 bus = [
42 {
43 "name": "adc",
44 "type": "i2cadc01",
45 #"channel": 7,
46 },
47 ],
48 )
49  
50  
51 cfg.initialize()
52 adc = cfg.get_device("adc")
53  
54 n = t[-1] + 1
55 try:
56 while True:
57 # Temperature readout
58 temperature = adc.readTemp()
59 print "Internal Temperature =", float("{0:.2f}".format(temperature))
60  
61 time.sleep(0.5)
62  
63 # Voltage readout
64 voltage = adc.readADC()
65 temp = (voltage / 0.0000397) + temperature #temperrature calculation for K type thermocouple
66 print "Voltage =", voltage, ", K-type thermocouple Temperature =", float("{0:.2f}".format(temp))
67  
68 time.sleep(0.5)
69  
70 # refresh graph
71 s = s[1:] + [temp]
72 t = t[1:] + [n]
73 n += 1
74 if (n % 10) == 0:
75 ax.text(n, temp-5, "{0:.1f}".format(temp),
76 verticalalignment='bottom', horizontalalignment='right',
77 color='green', fontsize=16)
78  
79 ax.lines[0].set_xdata(t)
80 ax.lines[0].set_ydata(s)
81 ax.set_xlim(t[0],t[-1])
82 f.canvas.draw()
83 time.sleep(0.5)
84  
85  
86 except IOError:
87 print "IOError"
88 continue
89  
90 except KeyboardInterrupt:
91 sys.exit(0)
92  
93