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