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)]
4141 kakl 22 s2 = [-30 + i for i in range(0, 70)]
4140 kakl 23  
24 plot(t, s, linewidth=3.0)
25 xlabel('time (s)')
26 ylabel('temperature (C)')
4141 kakl 27 title('Temperature external')
4140 kakl 28 f = gcf()
29 ax = f.gca()
30  
4141 kakl 31 f2 = figure()
32 ax2 = f2.gca()
33 ax2.set_xlabel('time (s)')
34 ax2.set_ylabel('temperature (C)')
35 ax2.set_title('Temperature internal')
36 ax2.plot(t, s, linewidth=3.0)
4140 kakl 37  
4141 kakl 38 show(block=False, layout=2, open_plot=True)
39 # show the figure manager but don't block script execution so animation works..
4140 kakl 40  
41 while True:
42 #### Sensor Configuration ###########################################
43 cfg = config.Config(
44 i2c = {
45 "port": 0, # I2C bus number
46 },
47  
48 bus = [
49 {
50 "name": "adc",
51 "type": "i2cadc01",
52 #"channel": 7,
53 },
54 ],
55 )
56  
57  
58 cfg.initialize()
59 adc = cfg.get_device("adc")
60  
61 n = t[-1] + 1
62 try:
63 while True:
64 # Temperature readout
65 temperature = adc.readTemp()
66 print "Internal Temperature =", float("{0:.2f}".format(temperature))
67  
68 time.sleep(0.5)
69  
70 # Voltage readout
71 voltage = adc.readADC()
72 temp = (voltage / 0.0000397) + temperature #temperrature calculation for K type thermocouple
73 print "Voltage =", voltage, ", K-type thermocouple Temperature =", float("{0:.2f}".format(temp))
74  
75 time.sleep(0.5)
76  
77 # refresh graph
78 s = s[1:] + [temp]
4141 kakl 79 s2 = s2[1:] + [temperature]
4140 kakl 80 t = t[1:] + [n]
81 n += 1
82 if (n % 10) == 0:
83 ax.text(n, temp-5, "{0:.1f}".format(temp),
84 verticalalignment='bottom', horizontalalignment='right',
4141 kakl 85 color='red', fontsize=16)
86 ax2.text(n, temperature-5, "{0:.1f}".format(temperature),
87 verticalalignment='bottom', horizontalalignment='right',
4140 kakl 88 color='green', fontsize=16)
89  
90 ax.lines[0].set_xdata(t)
91 ax.lines[0].set_ydata(s)
92 ax.set_xlim(t[0],t[-1])
4141 kakl 93 ax2.lines[0].set_xdata(t)
94 ax2.lines[0].set_ydata(s2)
95 ax2.set_xlim(t[0],t[-1])
4140 kakl 96 f.canvas.draw()
4141 kakl 97 f2.canvas.draw()
4140 kakl 98 time.sleep(0.5)
99  
100  
4141 kakl 101  
102  
4140 kakl 103 except IOError:
104 print "IOError"
105 continue
106  
107 except KeyboardInterrupt:
108 sys.exit(0)
109  
110