combine all measures in tri.py
parent
9347a3fe43
commit
1ee4746f29
Binary file not shown.
|
@ -17,7 +17,7 @@ adc = ADS1015()
|
|||
|
||||
# Note you can change the I2C address from its default (0x48), and/or the I2C
|
||||
# bus by passing in these optional parameters:
|
||||
#adc = ADS1015(address=0x49, busnum=1)
|
||||
adcc = ADS1015(address=0x49, busnum=1)
|
||||
|
||||
# Choose a gain of 1 for reading voltages from 0 to 4.09V.
|
||||
# Or pick a different gain to change the range of voltages that are read:
|
||||
|
@ -30,17 +30,18 @@ adc = ADS1015()
|
|||
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
|
||||
GAIN = 1
|
||||
|
||||
print('Reading ADS1x15 values, press Ctrl-C to quit...')
|
||||
f1=open("/home/pi/adc.txt","w+")
|
||||
|
||||
# Print nice channel column headers.
|
||||
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
|
||||
print('-' * 37)
|
||||
# Main loop.
|
||||
while True:
|
||||
# Read all the ADC channel values in a list.
|
||||
values = [0]*4
|
||||
values = [0]*9
|
||||
for i in range(4):
|
||||
# Read the specified ADC channel using the previously set gain value.
|
||||
values[i] = adc.read_adc(i, gain=GAIN)
|
||||
values[0] = time.time()
|
||||
values[1+i] = adc.read_adc(i, gain=GAIN)
|
||||
values[5+i] = adcc.read_adc(i, gain=GAIN)
|
||||
# Note you can also pass in an optional data_rate parameter that controls
|
||||
# the ADC conversion time (in samples/second). Each chip has a different
|
||||
# set of allowed data rate values, see datasheet Table 9 config register
|
||||
|
@ -49,6 +50,7 @@ while True:
|
|||
# Each value will be a 12 or 16 bit signed integer value depending on the
|
||||
# ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
|
||||
# Print the ADC values.
|
||||
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
|
||||
# print("| {0:>6} | {1:>6} | {2:>6} | {3:>6} | {4:>6} | {5:>6} | {6:>6} | {7:>6} | {8:>6}".format(*values))
|
||||
f1.write("| {0:>6} | {1:>6} | {2:>6} | {3:>6} | {4:>6} | {5:>6} | {6:>6} | {7:>6} | {8:>6} | \n".format(*values))
|
||||
# Pause for half a second.
|
||||
time.sleep(0.5)
|
||||
|
|
|
@ -6,6 +6,8 @@ counter = 0
|
|||
|
||||
# import the server implementation
|
||||
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
|
||||
# import ADC
|
||||
from Adafruit_ADS1x15 import ADS1015
|
||||
|
||||
# configure the client logging
|
||||
import logging
|
||||
|
@ -17,58 +19,135 @@ log.setLevel(logging.ERROR)
|
|||
client = ModbusClient(method='rtu', port='/dev/ttyUSB0', baudrate=9600, timeout=1)
|
||||
client.connect()
|
||||
|
||||
# Define the State list
|
||||
state = ['Start', 'Night Check', 'Disconnected', 'Night', 'Fault!', 'BulkCharge', 'Absorption', 'FloatCharge', 'Equalizing']
|
||||
# declare adc
|
||||
adc = ADS1015()
|
||||
adcc = ADS1015(address=0x49, busnum=1)
|
||||
|
||||
GAIN = 1
|
||||
|
||||
#f1=open("/home/pi/tri.txt","a")
|
||||
|
||||
while a > 1:
|
||||
|
||||
timestamp=time.time()
|
||||
|
||||
tempC=0
|
||||
try:
|
||||
tFile = open('/sys/class/thermal/thermal_zone0/temp')
|
||||
temp = float(tFile.read())
|
||||
tempC = temp/1000
|
||||
tFile.close()
|
||||
|
||||
timefile=round(timestamp/3600)
|
||||
f1=open("/home/pi/log/data_{0}.txt".format(timefile),"a")
|
||||
f1.write("{0:>6};".format(timestamp))
|
||||
if tempC > 0:
|
||||
f1.write("CPU_temp:{0}".format(tempC))
|
||||
|
||||
# read adc's
|
||||
for i in range(4):
|
||||
test=adc.read_adc(i,gain=GAIN)
|
||||
print("a{0} ".format(i) + ": {0} ;".format(test))
|
||||
f1.write("a0_{0}".format(i) + ":{0};".format(test))
|
||||
test=adcc.read_adc(i,gain=GAIN)
|
||||
print("a{0} ".format(i) + ": {0} ;".format(test))
|
||||
f1.write("a1_{0}".format(i) + ":{0};".format(test))
|
||||
|
||||
# curr_batt_diff = adcc.read_adc(3, gain=GAIN)
|
||||
# curr_batt_mppt = adcc.read_adc(2, gain=GAIN)
|
||||
# curr_out_1 = adc.read_adc(2,gain=GAIN)
|
||||
# curr_out_2 = adc.read_adc(3,gain=GAIN)
|
||||
# uv_ind=adc.read_adc(1,gain=GAIN)
|
||||
|
||||
# f1.write("{0} ;".format(curr_batt_diff))
|
||||
# f1.write("{0} ;".format(curr_batt_mppt))
|
||||
# f1.write("{0} ;".format(curr_out_1))
|
||||
# f1.write("{0} ;".format(curr_out_2))
|
||||
# f1.write("{0} ;".format(uv_ind))
|
||||
|
||||
#read the registers from logical address 0 to 30.
|
||||
rr = client.read_holding_registers(0,30,1)
|
||||
rr = client.read_holding_registers(0,90,unit=1)
|
||||
|
||||
#scaling
|
||||
batt_scale = 96.667
|
||||
i_scale = 66.667
|
||||
array_scale = 139.15
|
||||
|
||||
volt_scaling= rr.registers[0]*65536+rr.registers[1]
|
||||
amp_scaling= rr.registers[2]*65536+rr.registers[3]
|
||||
f1.write("volt_scale:{0} ;".format(volt_scaling))
|
||||
f1.write("amp_scale:{0} ;".format(amp_scaling))
|
||||
|
||||
#the stuff we want
|
||||
battsV = ( rr.registers[9] * 96.667) / 32768
|
||||
chargeI = ( rr.registers[11] * float(i_scale )) / (2**15)
|
||||
arrayV = ( rr.registers[10] * float(array_scale )) / (2**15)
|
||||
statenum = rr.registers[27]
|
||||
pwmDuty = rr.registers[28] /2.55
|
||||
regTemp = rr.registers[14]
|
||||
powerIn = battsV * chargeI
|
||||
ampH = ( rr.registers[17] + rr.registers[18] ) * 0.1
|
||||
|
||||
|
||||
f1.write("volt_batt_term:{0};".format(rr.registers[25])) # Battery voltage
|
||||
f1.write("volt_batt_sense:{0};".format(rr.registers[26])) # Battery sensing
|
||||
f1.write("volt_array:{0};".format(rr.registers[27])) # Array voltage
|
||||
f1.write("current_batt:{0};".format(rr.registers[28])) # Battery current
|
||||
f1.write("current_array:{0};".format(rr.registers[29])) # Array current
|
||||
f1.write("temp_heatsink:{0};".format(rr.registers[35])) # Temperature heatsink
|
||||
f1.write("temp_batt:{0};".format(rr.registers[36])) # Temperature battery
|
||||
f1.write("ah_reset:{0};".format(rr.registers[52] * 65536 + rr.registers[53])) # Ah resetable
|
||||
f1.write("ah_total:{0};".format(rr.registers[54] * 65536 + rr.registers[55])) # Ah total
|
||||
f1.write("kwh_reset:{0};".format(rr.registers[56])) # kwh resetable
|
||||
f1.write("kwh_total:{0};".format(rr.registers[57])) # kwh total
|
||||
f1.write("power_in:{0};".format(rr.registers[58])) # Power in
|
||||
f1.write("power_out:{0};".format(rr.registers[59])) # Power out
|
||||
f1.write("hour_total:{0};".format(rr.registers[42] * 65536 + rr.registers[43])) # hour total
|
||||
f1.write("state:{0};".format(rr.registers[50])) # State
|
||||
f1.write("volt_sweep_mp:{0};".format(rr.registers[61])) # Array voltage
|
||||
f1.write("volt_sweep_oc:{0};".format(rr.registers[62])) # Array voltage
|
||||
|
||||
|
||||
volt_batt=rr.registers[24] * volt_scaling / 65536 / 32768
|
||||
volt_batt_t=rr.registers[25] * volt_scaling / 65536 / 32768
|
||||
volt_batt_sens=rr.registers[26] * volt_scaling / 65536 / 32768
|
||||
volt_arr=rr.registers[27] * volt_scaling / 65536 / 32768
|
||||
curr_batt=rr.registers[28] * amp_scaling / 65536 / 32768
|
||||
curr_arr=rr.registers[29] * amp_scaling / 65536 / 32768
|
||||
temp_heatsink=rr.registers[35]
|
||||
temp_batt=rr.registers[36]
|
||||
ah_reset = rr.registers[52] * 65536 + rr.registers[53]
|
||||
ah_total = rr.registers[54] * 65536 + rr.registers[55]
|
||||
kwh_reset = rr.registers[56]
|
||||
kwh_total = rr.registers[57]
|
||||
power_in = rr.registers[58] * volt_scaling * amp_scaling / 131072 / 65536 / 65536
|
||||
power_out = rr.registers[59] * volt_scaling * amp_scaling / 131072 / 65536 / 65536
|
||||
hourm = rr.registers[42]*65536+rr.registers[43]
|
||||
charge_state = rr.registers[50]
|
||||
#debug
|
||||
# print "Battery Voltage: %.2f" % battsV
|
||||
# print "Battery Charge Current: %.2f" % chargeI
|
||||
# print "Array Voltage: %.2f" % arrayV
|
||||
# print "PWM Duty: %.2f" % pwmDuty
|
||||
# print "Control State: %.2f" % state
|
||||
# print "Controller Temp: %.2f" % regTemp
|
||||
# print "Power in: %.2f" % powerIn
|
||||
# print "Ah: %.2f" % ampH
|
||||
print "Battery Voltage: %.2f" % volt_batt
|
||||
print "Battery Voltage_t: %.2f" % volt_batt_t
|
||||
print "Battery Voltage Sense: %.2f" % volt_batt_sens
|
||||
print "Array Voltage: %.2f" % volt_arr
|
||||
print "Battery Current: %.2f" % curr_batt
|
||||
print "Array Current: %.2f" % curr_arr
|
||||
print "Power in: %.2f" % power_in
|
||||
print "Power out: %.2f" % power_out
|
||||
|
||||
out = "V:%.2f" % battsV + " A:%.3f" % chargeI + " AV:%.2f" % arrayV + " D:%.2f" % pwmDuty + " S:" + state[statenum] + " CT:%.2f" % regTemp + " P:%.2f" % powerIn + " AH:%.2f" % ampH + "\n"
|
||||
fil = open('/mnt/dumpdata.txt', 'w')
|
||||
fil.write(out)
|
||||
fil.close()
|
||||
print "Controller Temp: %.2f" % temp_heatsink
|
||||
print "Battery Temp: %.2f" % temp_batt
|
||||
print "AH total: %.2f" % ah_total
|
||||
print "kWh total: %.2f" % kwh_total
|
||||
print "hours: %.2f" % hourm
|
||||
print "charge state : %.2f" % charge_state
|
||||
|
||||
# out = "V:%.2f" % battsV + " A:%.3f" % chargeI + " AV:%.2f" % arrayV + " D:%.2f" % pwmDuty + " S:" + state[statenum] + " CT:%.2f" % regTemp + " P:%.2f" % powerIn + " AH:%.2f" % ampH + "\n"
|
||||
# fil = open('/home/pi/dumpdata.txt', 'w')
|
||||
# fil.write(out)
|
||||
# fil.close()
|
||||
counter += 1
|
||||
|
||||
if counter == 24:
|
||||
tid = time.ctime()
|
||||
out = str(tid) + ",%.2f" % battsV + ",%.3f" % chargeI + ",%.2f" % arrayV + ",%.2f" % pwmDuty + "," + state[statenum] + ",%.2f" % regTemp + ",%.2f" % powerIn + ",%.2f" % ampH + "\n"
|
||||
fil = open('/mnt/dumplog.csv', 'a')
|
||||
fil.write(out)
|
||||
fil.close()
|
||||
counter = 0
|
||||
# if counter == 24:
|
||||
# tid = time.ctime()
|
||||
# out = str(tid) + ",%.2f" % battsV + ",%.3f" % chargeI + ",%.2f" % arrayV + ",%.2f" % pwmDuty + "," + state[statenum] + ",%.2f" % regTemp + ",%.2f" % powerIn + ",%.2f" % ampH + "\n"
|
||||
# fil = open('/mnt/dumplog.csv', 'a')
|
||||
# fil.write(out)
|
||||
# fil.close()
|
||||
# counter = 0
|
||||
|
||||
time.sleep(5)
|
||||
f1.write("\n")
|
||||
f1.close()
|
||||
|
||||
|
||||
# close the client
|
||||
client.close()
|
||||
f1.close()
|
||||
|
||||
print "done"
|
||||
|
|
Loading…
Reference in New Issue