Rev 4928 Rev 4934
Line 71... Line 71...
71 sensor1 = cfg.get_device("current_sensor1") 71 sensor1 = cfg.get_device("current_sensor1")
72 #time.sleep(0.5) 72 #time.sleep(0.5)
73   73  
74 #### Data Logging ################################################### 74 #### Data Logging ###################################################
75   75  
-   76 while True:
76 try: 77 try:
77 before = time.time()-interval 78 before = time.time()-interval
78 while True: 79 while True:
79 80
80 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"0000_"+ stationName + "_data.csv" 81 filename = path + time.strftime("%Y%m%d%H", time.gmtime()) +"0000_"+ stationName + "_data.csv"
81 now = time.time() 82 now = time.time()
82 83
83 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s 84 if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
84 with open(filename, "a") as f: 85 with open(filename, "a") as f:
85 86
86 ##Measuremment settings 87 ##Measuremment settings
87 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75); 88 sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
88 89
89 instrument.address = 0x1E # this is the slave address number (1E-conductivity) 90 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 instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
91 92
92 instrument.address = 0x14 # this is the slave address number (14 - pH) 93 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 instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
94 95
95 time.sleep(0.5) 96 time.sleep(0.5)
96   97  
97 ##Reading 98 ##Reading
98 ## Read data from analog sensors ## 99 ## Read data from analog sensors ##
99 channel1 = sensor1.readCurrent(); 100 channel1 = sensor1.readCurrent();
100   101  
101 ## Read data from conductivity sensor ## 102 ## Read data from conductivity sensor ##
102 instrument.address = 0x1E # this is the slave address number (1E-conductivity) 103 instrument.address = 0x1E # this is the slave address number (1E-conductivity)
103   104  
104 temperature1 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals 105 temperature1 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
105 conductivity = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals 106 conductivity = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
106 salinity = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals 107 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 tds_kcl = instrument.read_float(0x59, 3, 2) # Registernumber, number of decimals
108 109
109 ## Read data from pH sensor ## 110 ## Read data from pH sensor ##
110 instrument.address = 0x14 # this is the slave address number (14 - pH) 111 instrument.address = 0x14 # this is the slave address number (14 - pH)
111 112
112 temperature2 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals 113 temperature2 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
113 pH = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals 114 pH = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
114 redox = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals 115 redox = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
115   116  
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)) 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))
117   118  
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)) 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))
119 f.flush() 120 f.flush()
120   121  
121 sys.stdout.flush() 122 sys.stdout.flush()
122 before = time.time() 123 before = time.time()
123 else: 124 else:
124 time.sleep(0.1) 125 time.sleep(0.1)
125   126  
126 except KeyboardInterrupt: 127 except KeyboardInterrupt:
127 f.close() 128 f.close()
128 sys.exit(0) 129 sys.exit(0)
-   130 except Exception as e:
-   131 sys.stdout.write(e)
-   132 time.sleep(5)
129   133  
130   134