Rev Author Line No. Line
4941 hanuson1 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
10 import os
11 import minimalmodbus
12  
13 from pymlab import config
14 from mlabutils import ejson
15  
16 parser = ejson.Parser()
17  
18 #### Script Arguments ###############################################
19  
20 if len(sys.argv) != 3:
21 sys.stderr.write("Invalid number of arguments.\n")
22 sys.stderr.write("Usage: %s PORT_ADDRESS CONFIG_FILE\n" % (sys.argv[0], ))
23 sys.exit(1)
24  
25  
26 port = eval(sys.argv[1])
27  
28 value = parser.parse_file(sys.argv[2])
29 path = value['data_path']
30 interval = value['raw_sample_interval']
31 stationName = value['origin']
32  
33  
34  
35 if (interval<5) or (interval>3600):
36 sys.stderr.write("Invalid sample interval arguments.\n")
37 sys.stderr.write("The interval has to be in the range from 5 to 3600 seconds\n")
38 sys.exit(2)
39  
40 #### Sensor Configuration ###########################################
41 # I2C
42 cfg = config.Config(
43 i2c = {
44 "port": port,
45 },
46 bus = [
47 {
48 "name": "current_sensor1",
49 "type": "vcai2c01",
50 "address": 0x68,
51 },
52 ],
53 )
54 cfg.initialize()
55  
56 # modbus
57 instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1) # port name, slave address (in decimal)
58  
59 instrument.serial.port # this is the serial port name
60 instrument.serial.baudrate = 9600 # Baud
61 instrument.serial.bytesize = 8
62 instrument.serial.stopbits = 2
63 instrument.serial.timeout = 0.5 # seconds
64  
65 instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
66  
67  
68 sys.stdout.write("Current loop and modbus sensor example \r\n")
69 sys.stdout.write("Time, water-level, temp1, conduc, salinity, tds_kcl, temp2, pH, redox \r\n")
70 #sys.stdout.write("Time, channel #1, channel #2, channel #3 , channel #4, channel #5 channel #6 , channel #7, channel #8 \r\n")
71 sensor1 = cfg.get_device("current_sensor1")
72 #time.sleep(0.5)
73  
74 #### Data Logging ###################################################
75  
76 while True:
77 try:
78 before = time.time()-interval
79 while True:
80  
81 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"0000_"+ stationName + "_data.csv"
82 now = time.time()
83  
84 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
85 with open(filename, "a") as f:
86  
87 ##Measuremment settings
88 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
89  
90 instrument.address = 0x1E # this is the slave address number (1E-conductivity)
91 instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
92  
93 instrument.address = 0x14 # this is the slave address number (14 - pH)
94 instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
95  
96 time.sleep(0.5)
97  
98 ##Reading
99 ## Read data from analog sensors ##
100 channel1 = sensor1.readCurrent();
101 channel1 = 0.2488*channel1-0.8892; # transformation from mA to meters
102  
103 ## Read data from conductivity sensor ##
104 instrument.address = 0x1E # this is the slave address number (1E-conductivity)
105  
106 temperature1 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
107 conductivity = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
108 salinity = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
109 tds_kcl = instrument.read_float(0x59, 3, 2) # Registernumber, number of decimals
110  
111 ## Read data from pH sensor ##
112 instrument.address = 0x14 # this is the slave address number (14 - pH)
113  
114 temperature2 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
115 pH = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
116 redox = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
117  
118 sys.stdout.write("%s \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t %0.3f \t \n" % (datetime.datetime.now().isoformat(), channel1, temperature1, conductivity, salinity, tds_kcl, temperature2, pH, redox))
119  
120 f.write("%d;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f\n" % (time.time(), channel1, temperature1, conductivity, salinity, tds_kcl, temperature2, pH, redox))
121 f.flush()
122  
123 sys.stdout.flush()
124 before = time.time()
125 else:
126 time.sleep(0.1)
127  
128 except KeyboardInterrupt:
129 f.close()
130 sys.exit(0)
131 except Exception as e:
132 sys.stdout.write(e)
133 time.sleep(5)
134  
135