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() |