/Designs/Measuring_instruments/ISMS01A/SW/ISMS_read.py
8,6 → 8,7
import datetime
import sys
import os
import minimalmodbus
 
from pymlab import config
from mlabutils import ejson
37,7 → 38,7
sys.exit(2)
 
#### Sensor Configuration ###########################################
 
# I2C
cfg = config.Config(
i2c = {
"port": port,
48,19 → 49,26
"type": "vcai2c01",
"address": 0x68,
},
{
"name": "current_sensor2",
"type": "vcai2c01",
"address": 0x6a,
},
],
)
cfg.initialize()
 
sys.stdout.write("Current loop sensor example \r\n")
sys.stdout.write("Time, channel #1, channel #2, channel #3 , channel #4, channel #5 \r\n")
# modbus
instrument = minimalmodbus.Instrument('/dev/ttyUSB0', 1) # port name, slave address (in decimal)
 
instrument.serial.port # this is the serial port name
instrument.serial.baudrate = 9600 # Baud
instrument.serial.bytesize = 8
instrument.serial.stopbits = 2
instrument.serial.timeout = 0.5 # seconds
 
instrument.mode = minimalmodbus.MODE_RTU # rtu or ascii mode
 
 
sys.stdout.write("Current loop and modbus sensor example \r\n")
sys.stdout.write("Time, water-level, temp1, conduc, salinity, tds_kcl, temp2, pH, redox \r\n")
#sys.stdout.write("Time, channel #1, channel #2, channel #3 , channel #4, channel #5 channel #6 , channel #7, channel #8 \r\n")
sensor1 = cfg.get_device("current_sensor1")
sensor2 = cfg.get_device("current_sensor2")
#time.sleep(0.5)
 
#### Data Logging ###################################################
73,32 → 81,41
now = time.time()
if (now - before >= interval - 2.5): # 0.5*5 channels= 2.5s
with open(filename, "a") as f:
 
with open(filename, "a") as f:
##Measuremment settings
sensor1.setADC(channel = 1, gain = 1, sample_rate = 3.75);
instrument.address = 0x1E # this is the slave address number (1E-conductivity)
instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
instrument.address = 0x14 # this is the slave address number (14 - pH)
instrument.write_register(0x01, 0x1F, 0) # Registernumber, value, number of decimals for storage
time.sleep(0.5)
 
##Reading
## Read data from analog sensors ##
channel1 = sensor1.readCurrent();
sensor1.setADC(channel = 2, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel2 = sensor1.readCurrent();
sensor1.setADC(channel = 3, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel3 = sensor1.readCurrent();
sensor1.setADC(channel = 4, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel4 = sensor1.readCurrent();
sensor2.setADC(channel = 1, gain = 1, sample_rate = 3.75);
time.sleep(0.5)
channel5 = sensor2.readCurrent();
 
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))
## Read data from conductivity sensor ##
instrument.address = 0x1E # this is the slave address number (1E-conductivity)
 
f.write("%d;%0.3f;%0.3f;%0.3f;%0.3f;%0.3f\n" % (time.time(), channel1, channel2, channel3, channel4, channel5))
temperature1 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
conductivity = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
salinity = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
tds_kcl = instrument.read_float(0x59, 3, 2) # Registernumber, number of decimals
## Read data from pH sensor ##
instrument.address = 0x14 # this is the slave address number (14 - pH)
temperature2 = instrument.read_float(0x53, 3, 2) # Registernumber, number of decimals
pH = instrument.read_float(0x55, 3, 2) # Registernumber, number of decimals
redox = instrument.read_float(0x57, 3, 2) # Registernumber, number of decimals
 
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))
 
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))
f.flush()
 
sys.stdout.flush()
/Designs/Measuring_instruments/ISMS01A/SW/average.py
0,0 → 1,80
#!/usr/bin/python
 
import pandas as pd
import os
import time
import datetime
 
 
 
dataSource = "/home/odroid/geozor/station/testingData/"
dataArchive = "/home/odroid/geozor/station/testingData/archive/"
loop = 1
 
 
while True:
try:
print("Start")
## Create sorted list of csv files
listOfDataFiles = list() #empty list
listOfSpecDataFiles = list() #empty list
files = list() #empty list
falg = False # is computation needed
files = sorted(os.listdir(dataSource)) # list of all files and folders in directory
for idx, val in enumerate(files): #goes through files
if val.endswith("data.csv"): # in case of *data.csv
listOfDataFiles.append(val) #add file to listOfFiles
 
## Find the newest and oldest and compare them. If they are from different day, compute the average of all measurement from oldest day
if len(listOfDataFiles)>=2: # if there are more than 2 data files
first = listOfDataFiles[0] # get first of them
last = listOfDataFiles[-1] # get last of them
if time.mktime(datetime.datetime.strptime(last[:8], "%Y%m%d").timetuple()) > time.mktime(datetime.datetime.strptime(first[:8], "%Y%m%d").timetuple()): # if the last is older than first
flag = True # computation needed
print("Computing...")
print(loop)
loop +=1
listOfSpecDataFiles = list() # empty list
for file in listOfDataFiles: # go through data files and create lis of data files measured on same day
# if the day is same like the first one
if time.mktime(datetime.datetime.strptime(first[:8], "%Y%m%d").timetuple()) == time.mktime(datetime.datetime.strptime(file[:8], "%Y%m%d").timetuple()):
listOfSpecDataFiles.append(file)
for file in listOfSpecDataFiles:
df=pd.read_csv(file, sep=';', header=None) # read current csv
dim=df.shape # gets data file dimensions
rowsInd=dim[0] # maximal index of rows
columnsInd=dim[1] # maximal index of columns
values=pd.DataFrame() # empty DataFrame
 
for x in range(0,columnsInd): # for each column
values = values.set_value(0,x,round(df[x].mean(),3),0) #calculates mean value for all cloumns and round it by 3
filename = first[:8]+'000000_VRTY-S1_data_mean.csv'
outfile = open(filename, 'a')
values.to_csv(filename, sep=';', header=None, index=False, mode='a') # save (add) DataFrame to csv
outfile.close()
# move files to archive structure
for file in listOfSpecDataFiles:
year = file[:4]
month = file[4:6]
day = file[6:8]
directory = dataArchive + year + "/" + month + "/" + day + "/"
if not os.path.exists(directory):
os.makedirs(directory)
os.rename(dataSource + file, dataArchive + year + "/" + month + "/" + day + "/" + file) # move file
else:
flag = False # computation is not needed
else:
flag = False # computation is not needed
if flag == False:
time.sleep(10) #long sleep, because is nothing to process
except ValueError:
print ValueError