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