Rev 4872 Rev 4928
Line 6... Line 6...
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 import minimalmodbus
11   12  
12 from pymlab import config 13 from pymlab import config
13 from mlabutils import ejson 14 from mlabutils import ejson
14   15  
15 parser = ejson.Parser() 16 parser = ejson.Parser()
Line 35... Line 36...
35 sys.stderr.write("Invalid sample interval arguments.\n") 36 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.stderr.write("The interval has to be in the range from 5 to 3600 seconds\n")
37 sys.exit(2) 38 sys.exit(2)
38   39  
39 #### Sensor Configuration ########################################### 40 #### Sensor Configuration ###########################################
40   41 # I2C
41 cfg = config.Config( 42 cfg = config.Config(
42 i2c = { 43 i2c = {
43 "port": port, 44 "port": port,
44 }, 45 },
45 bus = [ 46 bus = [
46 { 47 {
47 "name": "current_sensor1", 48 "name": "current_sensor1",
48 "type": "vcai2c01", 49 "type": "vcai2c01",
49 "address": 0x68, 50 "address": 0x68,
50 }, 51 },
51 { -  
52 "name": "current_sensor2", -  
53 "type": "vcai2c01", -  
54 "address": 0x6a, -  
55 }, -  
56 ], 52 ],
57 ) 53 )
58 cfg.initialize() 54 cfg.initialize()
59   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  
60 sys.stdout.write("Current loop sensor example \r\n") 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")
61 sys.stdout.write("Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \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")
62 sensor1 = cfg.get_device("current_sensor1") 71 sensor1 = cfg.get_device("current_sensor1")
63 sensor2 = cfg.get_device("current_sensor2") -  
64 #time.sleep(0.5) 72 #time.sleep(0.5)
65   73  
66 #### Data Logging ################################################### 74 #### Data Logging ###################################################
67   75  
68 try: 76 try:
Line 71... Line 79...
71 79
72 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"0000_"+ stationName + "_data.csv" 80 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"0000_"+ stationName + "_data.csv"
73 now = time.time() 81 now = time.time()
74 82
75 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s 83 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
76 with open(filename, "a") as f: 84 with open(filename, "a") as f:
77   85
-   86 ##Measuremment settings
78 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75); 87 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
-   88
-   89 instrument.address = 0x1E # this is the slave address number (1E-conductivity)
-   90 instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
-   91
-   92 instrument.address = 0x14 # this is the slave address number (14 - pH)
-   93 instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
-   94
79 time.sleep(0.5) 95 time.sleep(0.5)
-   96  
-   97 ##Reading
-   98 ## Read data from analog sensors ##
80 channel1 = sensor1.readCurrent(); 99 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 -  
98   100  
-   101 ## Read data from conductivity sensor ##
-   102 instrument.address = 0x1E # this is the slave address number (1E-conductivity)
-   103  
-   104 temperature1 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
-   105 conductivity = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
-   106 salinity = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
-   107 tds_kcl = instrument.read_float(0x59, 3, 2) # Registernumber, number of decimals
-   108
-   109 ## Read data from pH sensor ##
-   110 instrument.address = 0x14 # this is the slave address number (14 - pH)
-   111
-   112 temperature2 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
-   113 pH = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
-   114 redox = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
-   115  
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)) 116 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))
100   117  
101 f.write("%d;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f\n" % (time.time(), channel1, channel2, channel3, channel4, channel5)) 118 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))
102 f.flush() 119 f.flush()
103   120  
104 sys.stdout.flush() 121 sys.stdout.flush()
105 before = time.time() 122 before = time.time()
106 else: 123 else: