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
4928 kaklik 11 import minimalmodbus
4868 kaklik 12  
4843 kaklik 13 from pymlab import config
4869 kaklik 14 from mlabutils import ejson
4843 kaklik 15  
4869 kaklik 16 parser = ejson.Parser()
17  
4843 kaklik 18 #### Script Arguments ###############################################
19  
20 if len(sys.argv) != 3:
21 sys.stderr.write("Invalid number of arguments.\n")
4869 kaklik 22 sys.stderr.write("Usage: %s PORT_ADDRESS CONFIG_FILE\n" % (sys.argv[0], ))
4843 kaklik 23 sys.exit(1)
24  
4868 kaklik 25  
4869 kaklik 26 port = eval(sys.argv[1])
4868 kaklik 27  
4869 kaklik 28 value = parser.parse_file(sys.argv[2])
29 path = value['data_path']
30 interval = value['raw_sample_interval']
31 stationName = value['origin']
4843 kaklik 32  
4869 kaklik 33  
34  
4868 kaklik 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  
4843 kaklik 40 #### Sensor Configuration ###########################################
4928 kaklik 41 # I2C
4843 kaklik 42 cfg = config.Config(
43 i2c = {
44 "port": port,
45 },
46 bus = [
47 {
4844 kaklik 48 "name": "current_sensor1",
4843 kaklik 49 "type": "vcai2c01",
4844 kaklik 50 "address": 0x68,
4843 kaklik 51 },
52 ],
53 )
54 cfg.initialize()
55  
4928 kaklik 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")
4844 kaklik 71 sensor1 = cfg.get_device("current_sensor1")
4868 kaklik 72 #time.sleep(0.5)
4843 kaklik 73  
74 #### Data Logging ###################################################
75  
4934 kaklik 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)
4928 kaklik 97  
4934 kaklik 98 ##Reading
99 ## Read data from analog sensors ##
100 channel1 = sensor1.readCurrent();
4843 kaklik 101  
4934 kaklik 102 ## Read data from conductivity sensor ##
103 instrument.address = 0x1E # this is the slave address number (1E-conductivity)
4843 kaklik 104  
4934 kaklik 105 temperature1 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
106 conductivity = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
107 salinity = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
108 tds_kcl = instrument.read_float(0x59, 3, 2) # Registernumber, number of decimals
109  
110 ## Read data from pH sensor ##
111 instrument.address = 0x14 # this is the slave address number (14 - pH)
112  
113 temperature2 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
114 pH = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
115 redox = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
4928 kaklik 116  
4934 kaklik 117 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))
4928 kaklik 118  
4934 kaklik 119 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))
120 f.flush()
4843 kaklik 121  
4934 kaklik 122 sys.stdout.flush()
123 before = time.time()
124 else:
125 time.sleep(0.1)
4843 kaklik 126  
4934 kaklik 127 except KeyboardInterrupt:
128 f.close()
129 sys.exit(0)
130 except Exception as e:
131 sys.stdout.write(e)
132 time.sleep(5)
4843 kaklik 133  
134