change structure
parent
f1e3a8ddbc
commit
7a2957391a
57
logger.py
57
logger.py
|
@ -7,24 +7,27 @@ import os, serial,time,socket,sys,json,logging,requests,getopt
|
|||
import vedirect
|
||||
#import upload_osm
|
||||
|
||||
configfile="config.json"
|
||||
configfile="config.json.template"
|
||||
cf=open(configfile,"r")
|
||||
log_conf=json.load(cf)
|
||||
cf.close()
|
||||
channel_names=["time","CPU_temp"]
|
||||
devicename=socket.gethostname()
|
||||
if "device" in log_conf:
|
||||
devicename=log_conf['device']
|
||||
|
||||
mean_count=5
|
||||
if "mean_count" in log_conf:
|
||||
mean_count=int(log_conf['mean_count'])
|
||||
|
||||
channel_names=["time","CPU_temp"]
|
||||
channel_info={"time":{"sensor":"CPU","timestamp":0,"i2c":0},"CPU_temp":{"sensor":"CPU","timestamp":0,"i2c":0}}
|
||||
|
||||
a = 2
|
||||
|
||||
def upload_osm(sensebox_id,sensor_id,value):
|
||||
url="https://ingress.opensensemap.org/boxes/%s/%s" % (sensebox_id,sensor_id)
|
||||
r = requests.post(url,json={'value': value})
|
||||
if r.status_code != requests.codes.ok:
|
||||
if (r.status_code != requests.codes.ok) & (r.status_code != 201):
|
||||
print("Error %d: %s" % (r.status_code,r.text))
|
||||
|
||||
# push options to internet
|
||||
|
@ -84,6 +87,7 @@ if "tsl2591" in log_conf:
|
|||
btls=False
|
||||
else:
|
||||
channel_names.append("lux")
|
||||
channel_info["lux"]={"sensor":"tsl2591","timestamp":0,"i2c":tsl_add}
|
||||
|
||||
# config of bme280 sensor
|
||||
# use pip install RPi.bme280 for library
|
||||
|
@ -110,9 +114,9 @@ if "bme280" in log_conf:
|
|||
bbme=False
|
||||
else:
|
||||
calibration_params=bme280.load_calibration_params(bme_bus,bme_add)
|
||||
channel_names.append("temperature")
|
||||
channel_names.append("humidity")
|
||||
channel_names.append("pressure")
|
||||
for n in ("temperature","humidity","pressure"):
|
||||
channel_names.append(n)
|
||||
channel_info[n]={"sensor":"bme280","timestamp":0,"i2c":bme_add}
|
||||
|
||||
|
||||
# configure the client logging
|
||||
|
@ -147,6 +151,7 @@ if "tristar" in log_conf:
|
|||
triclient.connect()
|
||||
for i in ["volt_scale","amp_scale","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"]:
|
||||
channel_names.append(i)
|
||||
channel_info[i]={"sensor":"tristar","timestamp":0,"i2c":0}
|
||||
|
||||
# declare ve mppt
|
||||
bve=False
|
||||
|
@ -166,6 +171,7 @@ if "vedirect" in log_conf:
|
|||
else:
|
||||
for i in ["volt_bat_ve","volt_arr_ve","amp_ve","watt_ve","days_ve"]:
|
||||
channel_names.append(i)
|
||||
channel_info[i]={"sensor":"victron","timestamp":0,"i2c":0}
|
||||
|
||||
# declare adc
|
||||
badc=False
|
||||
|
@ -211,7 +217,9 @@ if "ads1x15" in log_conf:
|
|||
print("could not assign ADC")
|
||||
if adc_assigned:
|
||||
for j in range(4):
|
||||
channel_names.append("a"+str(adc_count)+"_"+str(j))
|
||||
cnadc="a"+str(adc_count)+"_"+str(j)
|
||||
channel_names.append(cnadc)
|
||||
channel_info[cnadc]={"sensor":"ads"+str(adc_type),"timestamp":0,"i2c":adc_address}
|
||||
adc_count=adc_count+1
|
||||
else:
|
||||
badc=False
|
||||
|
@ -231,6 +239,7 @@ if "mcp9808" in log_conf:
|
|||
else:
|
||||
sens_9808.begin()
|
||||
channel_names.append("temp_9808")
|
||||
channel_info["temp_9808"]={"sensor":"bmcp9808","timestamp":0,"i2c":0x18}
|
||||
|
||||
|
||||
ch_val=np.zeros(len(channel_names))
|
||||
|
@ -326,25 +335,27 @@ while a > 1:
|
|||
f1=open("/home/pi/log/data_{:d}.txt".format(int(timefile)),"a")
|
||||
payload={}
|
||||
for i in range(len(ch_val)):
|
||||
cni=channel_names[i]
|
||||
if ch_val[i] != ch_old[i]:
|
||||
f1.write(channel_names[i] + ":{0};".format(int(ch_val[i])))
|
||||
if channel_names[i] != "time":
|
||||
payload[channel_names[i]]=int(ch_val[i])
|
||||
if bosm:
|
||||
if channel_names[i] in push_vars:
|
||||
if push_counter == push_count:
|
||||
sense_data=push_data[channel_names[i]]/(1000*push_mean_counts[channel_names[i]])
|
||||
if channel_names[i] == "volt_sweep_oc":
|
||||
sense_data=sense_data * ch_val[channel_names.index("volt_scaling")] / 65536 / 32768
|
||||
# upload_osm.upload_osm(sensebox_id,sensebox_sid[channel_names[i]],round(sense_data,2))
|
||||
upload_osm(sensebox_id,sensebox_sid[channel_names[i]],round(sense_data,2))
|
||||
push_data[channel_names[i]]=0
|
||||
push_mean_counts[channel_names[i]]=0
|
||||
else:
|
||||
push_data[channel_names[i]]=push_data[channel_names[i]]+ch_val[i]
|
||||
push_mean_counts[channel_names[i]]=push_mean_counts[channel_names[i]]+1
|
||||
f1.write(cni + ":{0};".format(int(ch_val[i])))
|
||||
if cni != "time":
|
||||
payload[cni]=channel_info[cni]
|
||||
payload[cni]['value']=int(ch_val[i])
|
||||
channel_info[cni]['timestamp']=timestamp
|
||||
f1.write("\n")
|
||||
f1.close()
|
||||
if bosm:
|
||||
for pv in push_vars:
|
||||
if push_counter == push_count:
|
||||
sense_dta=push_data[pv]/(1000*push_mean_counts[pv])
|
||||
if pv == "volt_sweep_oc":
|
||||
sense_data=sense_data * ch_val[channel_names.index("volt_scale")] / 65536 / 32768
|
||||
upload_osm(sensebox_id,sensebox_sid[pv],round(sense_data,2))
|
||||
push_data[pv] = 0
|
||||
push_mean_counts[pv] = 0
|
||||
else:
|
||||
push_data[pv]=push_data[pv]+ch_val[channel_names.index(pv)]
|
||||
push_mean_counts[pv]=push_mean_counts[pv]+1
|
||||
if bsql:
|
||||
json_out={"time": ch_val[channel_names.index("time")],"device": devicename,"payload":payload}
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue