change to array

master
ademant 2019-05-27 10:17:57 +02:00
parent 78a8a6739a
commit 9e68851042
1 changed files with 51 additions and 54 deletions

105
tri.py
View File

@ -26,80 +26,79 @@ GAIN = 1
# declare vars
channel_names=["time","CPU_temp","a0_0","a0_1","a0_2","a0_3","a1_0","a1_1","a1_2","a1_3","volt_scale","amp_scale",
"volt_batt_term","volt_batt_sense","volt_array","current_batt","current_array","temp_heatsink","temp_batt","ah_reset",
"ah_total","kwh_reset","kwh_total","power_in","power_out","hour_total","state","volt_sweep_mp","volt_sweep_oc"]
"volt_bat_term","volt_bat_sens","volt_arr","amp_bat","amp_arr","temp_heatsink","temp_bat","ah_res",
"ah_tot","kwh_res","kwh_tot","watt_in","watt_out","hour_tot","state","volt_sweep_mp","volt_sweep_oc"]
ch_val=np.zeros(len(channel_names))
ch_old=ch_val
while a > 1:
# copy channel values to backup
ch_old=ch_val
# set actual time
ch_val[channel_names.index("time")]=time.time()
# get cpu temperature
tempC=0
tFile = open('/sys/class/thermal/thermal_zone0/temp')
ch_val[channel_names.index("CPU_temp")] = float(tFile.read())
tFile.close()
timefile=round(timestamp/3600)
f1=open("/home/pi/log/data_{:d}.txt".format(int(timefile)),"a")
f1.write("{:f};".format(timestamp))
if temp > 0:
f1.write("CPU_temp:{:d};".format(temp))
# 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))
ch_val[channel_names.index("a0_{0}".format(i))]=adc.read_adc(i,gain=GAIN)
ch_val[channel_names.index("a1_{0}".format(i))]=adcc.read_adc(i,gain=GAIN)
#read the registers from logical address 0 to 30.
rr = client.read_holding_registers(0,90,unit=1)
#scaling
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))
ch_val[channel_names.index("volt_scaling")]=rr.registers[0]*65536+rr.registers[1]
ch_val[channel_names.index("amp_scaling")]= rr.registers[2]*65536+rr.registers[3]
#the stuff we want
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
ch_val[channel_names.index("volt_bat_term")]=rr.registers[25]
ch_val[channel_names.index("volt_bat_sense")]=rr.registers[26]
ch_val[channel_names.index("volt_arr")]=rr.registers[27] # Array voltage
ch_val[channel_names.index("amp_bat")]=rr.registers[28] # Battery current
ch_val[channel_names.index("amp_arr")]=rr.registers[29] # Array current
ch_val[channel_names.index("temp_heatsink")]=rr.registers[35] # Temperature heatsink
ch_val[channel_names.index("temp_bat")]=rr.registers[36] # Temperature battery
ch_val[channel_names.index("ah_res")]=rr.registers[52] * 65536 + rr.registers[53] # Ah resetable
ch_val[channel_names.index("ah_tot")]=rr.registers[54] * 65536 + rr.registers[55] # Ah total
ch_val[channel_names.index("kwh_res")]=rr.registers[56] # kwh resetable
ch_val[channel_names.index("kwh_tot")]=rr.registers[57] # kwh total
ch_val[channel_names.index("watt_in")]=rr.registers[58] # Power in
ch_val[channel_names.index("watt_out")]=rr.registers[59] # Power out
ch_val[channel_names.index("hour_tot")]=rr.registers[42] * 65536 + rr.registers[43] # hour total
ch_val[channel_names.index("state")]=rr.registers[50] # State
ch_val[channel_names.index("volt_sweep_mp")]=rr.registers[61] # Array voltage
ch_val[channel_names.index("volt_sweep_oc")]=rr.registers[62] # Array voltage
timefile=round(timestamp/3600)
f1=open("/home/pi/log/data_{:d}.txt".format(int(timefile)),"a")
for i in len(ch_val):
if ch_val[i]<> ch_old[i]:
f1.write(channel_names[i] + ":{0};".format(ch_val[i]))
f1.write("\n")
f1.close()
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]
# 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" % volt_batt
# print "Battery Voltage_t: %.2f" % volt_batt_t
@ -119,8 +118,6 @@ while a > 1:
time.sleep(5)
f1.write("\n")
f1.close()
# close the client