Rev 4869 Rev 4872
1 #!/usr/bin/python 1 #!/usr/bin/python
2   2  
3 #uncomment for debbug purposes 3 #uncomment for debbug purposes
4 #import logging 4 #import logging
5 #logging.basicConfig(level=logging.DEBUG) 5 #logging.basicConfig(level=logging.DEBUG)
6   6  
7 import time 7 import time
8 import datetime 8 import datetime
9 import sys 9 import sys
10 import os 10 import os
11   11  
12 from pymlab import config 12 from pymlab import config
13 from mlabutils import ejson 13 from mlabutils import ejson
14   14  
15 parser = ejson.Parser() 15 parser = ejson.Parser()
16   16  
17 #### Script Arguments ############################################### 17 #### Script Arguments ###############################################
18   18  
19 if len(sys.argv) != 3: 19 if len(sys.argv) != 3:
20 sys.stderr.write("Invalid number of arguments.\n") 20 sys.stderr.write("Invalid number of arguments.\n")
21 sys.stderr.write("Usage: %s PORT_ADDRESS CONFIG_FILE\n" % (sys.argv[0], )) 21 sys.stderr.write("Usage: %s PORT_ADDRESS CONFIG_FILE\n" % (sys.argv[0], ))
22 sys.exit(1) 22 sys.exit(1)
23   23  
24   24  
25 port = eval(sys.argv[1]) 25 port = eval(sys.argv[1])
26   26  
27 value = parser.parse_file(sys.argv[2]) 27 value = parser.parse_file(sys.argv[2])
28 path = value['data_path'] 28 path = value['data_path']
29 interval = value['raw_sample_interval'] 29 interval = value['raw_sample_interval']
30 stationName = value['origin'] 30 stationName = value['origin']
31   31  
32   32  
33   33  
34 if (interval<5) or (interval>3600): 34 if (interval<5) or (interval>3600):
35 sys.stderr.write("Invalid sample interval arguments.\n") 35 sys.stderr.write("Invalid sample interval arguments.\n")
36 sys.stderr.write("The interval has to be in the range from 5 to 3600 seconds\n") 36 sys.stderr.write("The interval has to be in the range from 5 to 3600 seconds\n")
37 sys.exit(2) 37 sys.exit(2)
38   38  
39 #### Sensor Configuration ########################################### 39 #### Sensor Configuration ###########################################
40   40  
41 cfg = config.Config( 41 cfg = config.Config(
42 i2c = { 42 i2c = {
43 "port": port, 43 "port": port,
44 }, 44 },
45 bus = [ 45 bus = [
46 { 46 {
47 "name": "current_sensor1", 47 "name": "current_sensor1",
48 "type": "vcai2c01", 48 "type": "vcai2c01",
49 "address": 0x68, 49 "address": 0x68,
50 }, 50 },
51 { 51 {
52 "name": "current_sensor2", 52 "name": "current_sensor2",
53 "type": "vcai2c01", 53 "type": "vcai2c01",
54 "address": 0x6a, 54 "address": 0x6a,
55 }, 55 },
56 ], 56 ],
57 ) 57 )
58 cfg.initialize() 58 cfg.initialize()
59   59  
60 sys.stdout.write("Current loop sensor example \r\n") 60 sys.stdout.write("Current loop sensor example \r\n")
61 sys.stdout.write("Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \r\n") 61 sys.stdout.write("Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \r\n")
62 sensor1 = cfg.get_device("current_sensor1") 62 sensor1 = cfg.get_device("current_sensor1")
63 sensor2 = cfg.get_device("current_sensor2") 63 sensor2 = cfg.get_device("current_sensor2")
64 #time.sleep(0.5) 64 #time.sleep(0.5)
65   65  
66 #### Data Logging ################################################### 66 #### Data Logging ###################################################
67   67  
68 try: 68 try:
69 before = time.time()-interval 69 before = time.time()-interval
70 while True: 70 while True:
71 71
72 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"_"+ stationName + "_data.csv" 72 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"0000_"+ stationName + "_data.csv"
73 now = time.time() 73 now = time.time()
74 74
75 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s 75 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
76 with open(filename, "a") as f: 76 with open(filename, "a") as f:
77   77  
78 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75); 78 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
79 time.sleep(0.5) 79 time.sleep(0.5)
80 channel1 = sensor1.readCurrent(); 80 channel1 = sensor1.readCurrent();
81 81
82 sensor1.setADC(channel = 2, gain = 1, sample_rate = 3.75); 82 sensor1.setADC(channel = 2, gain = 1, sample_rate = 3.75);
83 time.sleep(0.5) 83 time.sleep(0.5)
84 channel2 = sensor1.readCurrent(); 84 channel2 = sensor1.readCurrent();
85 85
86 sensor1.setADC(channel = 3, gain = 1, sample_rate = 3.75); 86 sensor1.setADC(channel = 3, gain = 1, sample_rate = 3.75);
87 time.sleep(0.5) 87 time.sleep(0.5)
88 channel3 = sensor1.readCurrent(); 88 channel3 = sensor1.readCurrent();
89 89
90 sensor1.setADC(channel = 4, gain = 1, sample_rate = 3.75); 90 sensor1.setADC(channel = 4, gain = 1, sample_rate = 3.75);
91 time.sleep(0.5) 91 time.sleep(0.5)
92 channel4 = sensor1.readCurrent(); 92 channel4 = sensor1.readCurrent();
93 93
94 sensor2.setADC(channel = 1, gain = 1, sample_rate = 3.75); 94 sensor2.setADC(channel = 1, gain = 1, sample_rate = 3.75);
95 time.sleep(0.5) 95 time.sleep(0.5)
96 channel5 = sensor2.readCurrent(); 96 channel5 = sensor2.readCurrent();
97 97
98   98  
99 sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \n" % (datetime.datetime.now().isoformat(), channel1, channel2, channel3, channel4, channel5)) 99 sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \n" % (datetime.datetime.now().isoformat(), channel1, channel2, channel3, channel4, channel5))
100   100  
101 f.write("%d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\n" % (time.time(), channel1, channel2, channel3, channel4, channel5)) 101 f.write("%d;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f\n" % (time.time(), channel1, channel2, channel3, channel4, channel5))
102 f.flush() 102 f.flush()
103   103  
104 sys.stdout.flush() 104 sys.stdout.flush()
105 before = time.time() 105 before = time.time()
106 else: 106 else:
107 time.sleep(0.1) 107 time.sleep(0.1)
108   108  
109 except KeyboardInterrupt: 109 except KeyboardInterrupt:
110 f.close() 110 f.close()
111 sys.exit(0) 111 sys.exit(0)
112   112  
113   113