Skip to content
This repository was archived by the owner on Jun 28, 2023. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adapted Log Script
  • Loading branch information
akrlfix committed Jan 5, 2022
commit 729f8a5a3db4b444ff56fdf8cee196098851d44f
93 changes: 41 additions & 52 deletions thingset_log.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@

import json
import time
from datetime import datetime

from thingset.cansocket import CANsocket
sock = CANsocket('can0') # or other interface
from thingset.thingset_can import ThingSet_CAN

mppt_id = 0x14
bms_id = 0xa
Expand Down Expand Up @@ -32,7 +30,7 @@
0x71: 'Bat_V',
0x72: 'Bat_A',
0x73: 'Bat_degC',
0x74: 'IC_degC',
0x76: 'MOSFETs_degC',
0x7C: 'SOC_pct',
0x7E: 'ErrorFlags',
0x7F: 'BmsState',
Expand All @@ -42,22 +40,20 @@
0x9D: 'BalancingStatus',
}

mppt_data = {}
mppt_updated = False
mppt_file = open("data/%s_mppt.csv" % datetime.now().strftime("%Y%m%d_%H%M%S"), "a")
mppt_file = open("data/%s_mppt.csv" %
datetime.now().strftime("%Y%m%d_%H%M%S"), "a")

bms_data = {}
bms_updated = False
bms_file = open("data/%s_bms.csv" % datetime.now().strftime("%Y%m%d_%H%M%S"), "a")
bms_file = open("data/%s_bms.csv" %
datetime.now().strftime("%Y%m%d_%H%M%S"), "a")

last_update = int(time.time())

def csv_header(file, id_map):
names = ['Timestamp_s']
for key in id_map:
names.append(id_map[key])
file.write(','.join(names) + '\n')


def csv_data(file, id_map, timestamp, data):
values = [str(timestamp)]
for key in id_map:
Expand All @@ -68,55 +64,48 @@ def csv_data(file, id_map, timestamp, data):
file.write(','.join(values) + '\n')
file.flush()


ts = ThingSet_CAN(if_name='can0')
ts.subscribe(bms_id)
ts.subscribe(mppt_id)
ts.start()

csv_header(mppt_file, mppt_map)
csv_header(bms_file, bms_map)

last_update = int(time.time())

try:
while True:
frame = sock.receive()

data_name = str(frame.dataobjectID)
if frame.source == mppt_id and frame.dataobjectID in mppt_map:
data_name = mppt_map[frame.dataobjectID]
mppt_data[data_name] = frame.cbor
mppt_updated = True
elif frame.source == bms_id and frame.dataobjectID in bms_map:
data_name = bms_map[frame.dataobjectID]
bms_data[data_name] = frame.cbor
bms_updated = True

time.sleep(1)
now = int(time.time())

# store data in CSV file
if now > last_update:
try:
print("BMS: Bat %.2fV %.2fA, Cells %.2fV < %.2fV < %.2fV, Err %d " % ( \
bms_data['Bat_V'], bms_data['Bat_A'], bms_data['CellMin_V'], \
bms_data['CellAvg_V'], bms_data['CellMax_V'], bms_data['ErrorFlags']), end='' \
)
except:
pass

try:
print("MPPT: Bat %.2fV %.2fA, Solar %.2fV, Load %.2fA, Err %d" % ( \
mppt_data['Bat_V'], mppt_data['Bat_A'], mppt_data['Solar_V'], \
mppt_data['Load_A'], mppt_data['ErrorFlags']) \
)
except:
print("")
#pass

if mppt_updated:
#print(json.dumps(mppt_data))
csv_data(mppt_file, mppt_map, now, mppt_data)
mppt_data = {}
mppt_updated = False
if bms_updated:
#print(json.dumps(bms_data))
csv_data(bms_file, bms_map, now, bms_data)
bms_data = {}
bms_updated = False
last_update = now
try:
bms_data = ThingSet_CAN.translate(ts.data[bms_id], bms_map)
csv_data(bms_file, bms_map, now, bms_data)
#print(json.dumps(bms_data))
print("BMS: Bat %.2fV %.2fA, Cells %.2fV < %.2fV < %.2fV, Err %d" % (
bms_data['Bat_V'], bms_data['Bat_A'], bms_data['CellMin_V'],
bms_data['CellAvg_V'], bms_data['CellMax_V'], bms_data['ErrorFlags']), end=''
)
except KeyError as exc:
#print(exc)
pass

try:
mppt_data = ThingSet_CAN.translate(ts.data[mppt_id], mppt_map)
csv_data(mppt_file, mppt_map, now, mppt_data)
# print(json.dumps(mppt_data))
print("MPPT: Bat %.2fV %.2fA, Solar %.2fV, Load %.2fA, Err %d" % (
mppt_data['Bat_V'], mppt_data['Bat_A'], mppt_data['Solar_V'],
mppt_data['Load_A'], mppt_data['ErrorFlags'])
)
except KeyError as exc:
# print(exc)
print("")



except KeyboardInterrupt:
mppt_file.close()
Expand Down