forked from Tertiush/bmspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bms.py
1154 lines (899 loc) · 48.5 KB
/
bms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import paho.mqtt.client as mqtt
import socket
import time
import yaml
import os
import json
import serial
import io
import json
import atexit
import sys
import constants
print("Starting up...")
config = {}
if os.path.exists('/data/options.json'):
print("Loading options.json")
with open(r'/data/options.json') as file:
config = json.load(file)
print("Config: " + json.dumps(config))
elif os.path.exists('config.yaml'):
print("Loading config.yaml")
with open(r'config.yaml') as file:
config = yaml.load(file, Loader=yaml.FullLoader)['options']
else:
sys.exit("No config file found")
scan_interval = config['scan_interval']
connection_type = config['connection_type']
bms_serial = config['bms_serial']
ha_discovery_enabled = config['mqtt_ha_discovery']
code_running = True
bms_connected = False
mqtt_connected = False
print_initial = True
debug_output = config['debug_output']
disc_payload = {}
repub_discovery = 0
bms_version = ''
bms_sn = ''
pack_sn = ''
packs = 1
cells = 13
temps = 6
print("Connection Type: " + connection_type)
def on_connect(client, userdata, flags, rc):
print("MQTT connected with result code "+str(rc))
client.will_set(config['mqtt_base_topic'] + "/availability","offline", qos=0, retain=False)
global mqtt_connected
mqtt_connected = True
def on_disconnect(client, userdata, rc):
print("MQTT disconnected with result code "+str(rc))
global mqtt_connected
mqtt_connected = False
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
#client.on_message = on_message
client.username_pw_set(username=config['mqtt_user'], password=config['mqtt_password'])
client.connect(config['mqtt_host'], config['mqtt_port'], 60)
client.loop_start()
time.sleep(2)
def exit_handler():
print("Script exiting")
client.publish(config['mqtt_base_topic'] + "/availability","offline")
return
atexit.register(exit_handler)
def bms_connect(address, port):
if connection_type == "Serial":
try:
print("trying to connect %s" % bms_serial)
s = serial.Serial(bms_serial,timeout = 1)
print("BMS serial connected")
return s, True
except IOError as msg:
print("BMS serial error connecting: %s" % msg)
return False, False
else:
try:
print("trying to connect " + address + ":" + str(port))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
s.connect((address, port))
print("BMS socket connected")
return s, True
except OSError as msg:
print("BMS socket error connecting: %s" % msg)
return False, False
def bms_sendData(comms,request=''):
if connection_type == "Serial":
try:
if len(request) > 0:
comms.write(request)
time.sleep(0.25)
return True
except IOError as e:
print("BMS serial error: %s" % e)
# global bms_connected
return False
else:
try:
if len(request) > 0:
comms.send(request)
time.sleep(0.25)
return True
except Exception as e:
print("BMS socket error: %s" % e)
# global bms_connected
return False
def bms_get_data(comms):
try:
if connection_type == "Serial":
inc_data = comms.readline()
else:
temp = comms.recv(4096)
temp2 = temp.split(b'\r')
# Decide which one to take:
for element in range(0,len(temp2)):
SOI = hex(ord(temp2[element][0:1]))
if SOI == '0x7e':
inc_data = temp2[element] + b'\r'
break
if (len(temp2) > 2) & (debug_output > 0):
print("Multiple EOIs detected")
print("...for incoming data: " + str(temp) + " |Hex: " + str(temp.hex(' ')))
return inc_data
except Exception as e:
print("BMS socket receive error: %s" % e)
# global bms_connected
return False
def ha_discovery():
global ha_discovery_enabled
global packs
if ha_discovery_enabled:
print("Publishing HA Discovery topic...")
disc_payload['availability_topic'] = config['mqtt_base_topic'] + "/availability"
device = {}
device['manufacturer'] = "BMS Pace"
device['model'] = "AM-x"
device['identifiers'] = "bmspace_" + bms_sn
device['name'] = "Generic Lithium"
device['sw_version'] = bms_version
disc_payload['device'] = device
for p in range (1,packs+1):
for i in range(0,cells):
disc_payload['name'] = "Pack " + str(p) + " Cell " + str(i+1) + " Voltage"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_v_cell_" + str(i+1)
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/v_cells/cell_" + str(i+1)
disc_payload['unit_of_measurement'] = "mV"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
for i in range(0,temps):
disc_payload['name'] = "Pack " + str(p) + " Temperature " + str(i+1)
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_temp_" + str(i+1)
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/temps/temp_" + str(i+1)
disc_payload['unit_of_measurement'] = "°C"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
# disc_payload['name'] = "MOS_Temp"
# disc_payload['unique_id'] = "bmspace_" + bms_sn + "_t_mos"
# disc_payload['state_topic'] = config['mqtt_base_topic'] + "/t_mos"
# disc_payload['unit_of_measurement'] = "°C"
# client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'] + "/config",json.dumps(disc_payload),qos=0, retain=True)
# disc_payload['name'] = "Environmental_Temp"
# disc_payload['unique_id'] = "bmspace_" + bms_sn + "_t_env"
# disc_payload['state_topic'] = config['mqtt_base_topic'] + "/t_env"
# disc_payload['unit_of_measurement'] = "°C"
# client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'] + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Current"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_i_pack"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_pack"
disc_payload['unit_of_measurement'] = "A"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Voltage"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_v_pack"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/v_pack"
disc_payload['unit_of_measurement'] = "V"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Remaining Capacity"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_i_remain_cap"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_remain_cap"
disc_payload['unit_of_measurement'] = "mAh"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " State of Health"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_soh"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/soh"
disc_payload['unit_of_measurement'] = "%"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Cycles"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_cycles"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/cycles"
disc_payload['unit_of_measurement'] = ""
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Full Capacity"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_i_full_cap"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_full_cap"
disc_payload['unit_of_measurement'] = "mAh"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Design Capacity"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_i_design_cap"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_design_cap"
disc_payload['unit_of_measurement'] = "mAh"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " State of Charge"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_soc"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/soc"
disc_payload['unit_of_measurement'] = "%"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " State of Health"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_soh"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/soh"
disc_payload['unit_of_measurement'] = "%"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload.pop('unit_of_measurement')
disc_payload['name'] = "Pack " + str(p) + " Warnings"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_warnings"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/warnings"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Balancing1"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_balancing1"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/balancing1"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Balancing2"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_balancing2"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/balancing2"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
# Binary Sensors
disc_payload['name'] = "Pack " + str(p) + " Protection Short Circuit"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_prot_short_circuit"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/prot_short_circuit"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Protection Discharge Current"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_prot_discharge_current"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/prot_discharge_current"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Protection Charge Current"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_prot_charge_current"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/prot_charge_current"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Current Limit"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_current_limit"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/current_limit"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Charge FET"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_charge_fet"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/charge_fet"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Discharge FET"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_discharge_fet"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/discharge_fet"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Pack Indicate"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_pack_indicate"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/pack_indicate"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Reverse"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_reverse"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/reverse"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " AC In"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_ac_in"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/ac_in"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Heart"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_heart"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/heart"
disc_payload['payload_on'] = "1"
disc_payload['payload_off'] = "0"
client.publish(config['mqtt_ha_discovery_topic']+"/binary_sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack " + str(p) + " Cell Max Volt Diff"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_" + str(p) + "_cells_max_diff_calc"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_" + str(p) + "/cells_max_diff_calc"
disc_payload['unit_of_measurement'] = "mV"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
# Pack data
disc_payload.pop('payload_on')
disc_payload.pop('payload_off')
disc_payload['name'] = "Pack Remaining Capacity"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_i_remain_cap"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_remain_cap"
disc_payload['unit_of_measurement'] = "mAh"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack Full Capacity"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_i_full_cap"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_full_cap"
disc_payload['unit_of_measurement'] = "mAh"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack Design Capacity"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_i_design_cap"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_design_cap"
disc_payload['unit_of_measurement'] = "mAh"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack State of Charge"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_soc"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_soc"
disc_payload['unit_of_measurement'] = "%"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
disc_payload['name'] = "Pack State of Health"
disc_payload['unique_id'] = "bmspace_" + bms_sn + "_pack_soh"
disc_payload['state_topic'] = config['mqtt_base_topic'] + "/pack_soh"
disc_payload['unit_of_measurement'] = "%"
client.publish(config['mqtt_ha_discovery_topic']+"/sensor/BMS-" + bms_sn + "/" + disc_payload['name'].replace(' ', '_') + "/config",json.dumps(disc_payload),qos=0, retain=True)
else:
print("HA Discovery Disabled")
def chksum_calc(data):
global debug_output
chksum = 0
try:
for element in range(1, len(data)): #-5):
chksum += (data[element])
chksum = chksum % 65536
chksum = '{0:016b}'.format(chksum)
flip_bits = ''
for i in chksum:
if i == '0':
flip_bits += '1'
else:
flip_bits += '0'
chksum = flip_bits
chksum = int(chksum,2)+1
chksum = format(chksum, 'X')
except Exception as e:
if debug_output > 0:
print("Error calculating CHKSUM using data: " + data)
print("Error details: ", str(e))
return(False)
return(chksum)
def cid2_rtn(rtn):
# RTN Reponse codes, looking for errors
if rtn == b'00':
return False, False
elif rtn == b'01':
return True, "RTN Error 01: Undefined RTN error"
elif rtn == b'02':
return True, "RTN Error 02: CHKSUM error"
elif rtn == b'03':
return True, "RTN Error 03: LCHKSUM error"
elif rtn == b'04':
return True, "RTN Error 04: CID2 undefined"
elif rtn == b'05':
return True, "RTN Error 05: Undefined error"
elif rtn == b'06':
return True, "RTN Error 06: Undefined error"
elif rtn == b'09':
return True, "RTN Error 09: Operation or write error"
else:
return False, False
def bms_parse_data(inc_data):
global debug_output
#inc_data = b'~2501460070DC00020D100A0FF40FEA100E10040FF50FFD10010FFD0FE50FF5100A1001060BD20BD20BD40BD40BF80C1AFF7ECFCF258B02271001372710600D0FFA0FFC0FFB0FFC0FFE0FFB0FFA0FFA0FFB0FFD0FFC0FFB0FFB060BEF0BF10BEF0BED0BF70C04FDB1D02E29A9022AF80\r'
try:
SOI = hex(ord(inc_data[0:1]))
if SOI != '0x7e':
return(False,"Incorrect starting byte for incoming data")
if debug_output > 1:
print("SOI: ", SOI)
print("VER: ", inc_data[1:3])
print("ADR: ", inc_data[3:5])
print("CID1 (Type): ", inc_data[5:7])
RTN = inc_data[7:9]
error, info = cid2_rtn(RTN)
if error:
print(error)
raise Exception(error)
LCHKSUM = inc_data[9]
if debug_output > 1:
print("RTN: ", RTN)
print("LENGTH: ", inc_data[9:13])
print(" - LCHKSUM: ", LCHKSUM)
print(" - LENID: ", inc_data[10:13])
LENID = int(inc_data[10:13],16) #amount of bytes, i.e. 2x hex
calc_LCHKSUM = lchksum_calc(inc_data[10:13])
if calc_LCHKSUM == False:
return(False,"Error calculating LCHKSUM for incoming data")
if LCHKSUM != ord(calc_LCHKSUM):
if debug_output > 0:
print("LCHKSUM received: " + str(LCHKSUM) + " does not match calculated: " + str(ord(calc_LCHKSUM)))
return(False,"LCHKSUM received: " + str(LCHKSUM) + " does not match calculated: " + str(ord(calc_LCHKSUM)))
if debug_output > 1:
print(" - LENID (int): ", LENID)
INFO = inc_data[13:13+LENID]
if debug_output > 1:
print("INFO: ", INFO)
CHKSUM = inc_data[13+LENID:13+LENID+4]
if debug_output > 1:
print("CHKSUM: ", CHKSUM)
#print("EOI: ", hex(inc_data[13+LENID+4]))
calc_CHKSUM = chksum_calc(inc_data[:len(inc_data)-5])
if debug_output > 1:
print("Calc CHKSUM: ", calc_CHKSUM)
except Exception as e:
if debug_output > 0:
print("Error1 calculating CHKSUM using data: ", inc_data)
return(False,"Error1 calculating CHKSUM: " + str(e))
if calc_CHKSUM == False:
if debug_output > 0:
print("Error2 calculating CHKSUM using data: ", inc_data)
return(False,"Error2 calculating CHKSUM")
if CHKSUM.decode("ASCII") == calc_CHKSUM:
return(True,INFO)
else:
if debug_output > 0:
print("Received and calculated CHKSUM does not match: Received: " + CHKSUM.decode("ASCII") + ", Calculated: " + calc_CHKSUM)
print("...for incoming data: " + str(inc_data) + " |Hex: " + str(inc_data.hex(' ')))
print("Length of incoming data as measured: " + str(len(inc_data)))
print("SOI: ", SOI)
print("VER: ", inc_data[1:3])
print("ADR: ", inc_data[3:5])
print("CID1 (Type): ", inc_data[5:7])
print("RTN (decode!): ", RTN)
print("LENGTH: ", inc_data[9:13])
print(" - LCHKSUM: ", inc_data[9])
print(" - LENID: ", inc_data[10:13])
print(" - LENID (int): ", int(inc_data[10:13],16))
print("INFO: ", INFO)
print("CHKSUM: ", CHKSUM)
#print("EOI: ", hex(inc_data[13+LENID+4]))
return(False,"Checksum error")
def lchksum_calc(lenid):
chksum = 0
try:
# for element in range(1, len(lenid)): #-5):
# chksum += (lenid[element])
for element in range(0, len(lenid)):
chksum += int(chr(lenid[element]),16)
chksum = chksum % 16
chksum = '{0:04b}'.format(chksum)
flip_bits = ''
for i in chksum:
if i == '0':
flip_bits += '1'
else:
flip_bits += '0'
chksum = flip_bits
chksum = int(chksum,2)
chksum += 1
if chksum > 15:
chksum = 0
chksum = format(chksum, 'X')
except:
print("Error calculating LCHKSUM using LENID: ", lenid)
return(False)
return(chksum)
def bms_request(bms, ver=b"\x32\x35",adr=b"\x30\x31",cid1=b"\x34\x36",cid2=b"\x43\x31",info=b"",LENID=False):
global bms_connected
global debug_output
request = b'\x7e'
request += ver
request += adr
request += cid1
request += cid2
if not(LENID):
LENID = len(info)
#print("Length: ", LENID)
LENID = bytes(format(LENID, '03X'), "ASCII")
#print("LENID: ", LENID)
if LENID == b'000':
LCHKSUM = '0'
else:
LCHKSUM = lchksum_calc(LENID)
if LCHKSUM == False:
return(False,"Error calculating LCHKSUM)")
#print("LCHKSUM: ", LCHKSUM)
request += bytes(LCHKSUM, "ASCII")
request += LENID
request += info
CHKSUM = bytes(chksum_calc(request), "ASCII")
if CHKSUM == False:
return(False,"Error calculating CHKSUM)")
request += CHKSUM
request += b'\x0d'
if debug_output > 2:
print("-> Outgoing Data: ", request)
if not bms_sendData(bms,request):
bms_connected = False
print("Error, connection to BMS lost")
return(False,"Error, connection to BMS lost")
inc_data = bms_get_data(bms)
if inc_data == False:
print("Error retrieving data from BMS")
return(False,"Error retrieving data from BMS")
if debug_output > 2:
print("<- Incoming data: ", inc_data)
success, INFO = bms_parse_data(inc_data)
return(success, INFO)
def bms_getPackNumber(bms):
success, INFO = bms_request(bms,cid2=constants.cid2PackNumber)
if success == False:
return(False,INFO)
try:
packNumber = int(INFO,16)
except:
print("Error extracting total battery count in pack")
return(False,"Error extracting total battery count in pack")
return(success,packNumber)
def bms_getVersion(comms):
global bms_version
success, INFO = bms_request(bms,cid2=constants.cid2SoftwareVersion)
if success == False:
return(False,INFO)
try:
bms_version = bytes.fromhex(INFO.decode("ascii")).decode("ASCII")
client.publish(config['mqtt_base_topic'] + "/bms_version",bms_version)
print("BMS Version: " + bms_version)
except:
return(False,"Error extracting BMS version")
return(success,bms_version)
def bms_getSerial(comms):
global bms_sn
global pack_sn
success, INFO = bms_request(bms,cid2=constants.cid2SerialNumber)
if success == False:
print("Error: " + INFO)
return(False,INFO, False)
try:
bms_sn = bytes.fromhex(INFO[0:30].decode("ascii")).decode("ASCII").replace(" ", "")
pack_sn = bytes.fromhex(INFO[40:68].decode("ascii")).decode("ASCII").replace(" ", "")
client.publish(config['mqtt_base_topic'] + "/bms_sn",bms_sn)
client.publish(config['mqtt_base_topic'] + "/pack_sn",pack_sn)
print("BMS Serial Number: " + bms_sn)
print("Pack Serial Number: " + pack_sn)
except:
return(False,"Error extracting BMS version", False)
return(success,bms_sn,pack_sn)
def bms_getAnalogData(bms,batNumber):
global print_initial
global cells
global temps
global packs
byte_index = 2
i_pack = []
v_pack = []
i_remain_cap = []
i_design_cap = []
cycles = []
i_full_cap = []
soc = []
soh = []
battery = bytes(format(batNumber, '02X'), 'ASCII')
# print("Get analog info for battery: ", battery)
success, inc_data = bms_request(bms,cid2=constants.cid2PackAnalogData,info=battery)
if success == False:
return(False,inc_data)
try:
packs = int(inc_data[byte_index:byte_index+2],16)
if print_initial:
print("Packs: " + str(packs))
byte_index += 2
v_cell = {}
t_cell = {}
for p in range(1,packs+1):
if p > 1:
cells_prev = cells
cells = int(inc_data[byte_index:byte_index+2],16)
#Possible remove this next test as were now testing for the INFOFLAG at the end
if p > 1:
if cells != cells_prev:
byte_index += 2
cells = int(inc_data[byte_index:byte_index+2],16)
if cells != cells_prev:
print("Error parsing BMS analog data: Cannot read multiple packs")
return(False,"Error parsing BMS analog data: Cannot read multiple packs")
if print_initial:
print("Pack " + str(p) + ", Total cells: " + str(cells))
byte_index += 2
cell_min_volt = 0
cell_max_volt = 0
for i in range(0,cells):
v_cell[(p-1,i)] = int(inc_data[byte_index:byte_index+4],16)
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/v_cells/cell_" + str(i+1) ,str(v_cell[(p-1,i)]))
if print_initial:
print("Pack " + str(p) +", V Cell" + str(i+1) + ": " + str(v_cell[(p-1,i)]) + " mV")
#Calculate cell max and min volt
if i == 0:
cell_min_volt = v_cell[(p-1,i)]
cell_max_volt = v_cell[(p-1,i)]
else:
if v_cell[(p-1,i)] < cell_min_volt:
cell_min_volt = v_cell[(p-1,i)]
if v_cell[(p-1,i)] > cell_max_volt:
cell_max_volt = v_cell[(p-1,i)]
#Calculate cells max diff volt
cell_max_diff_volt = cell_max_volt - cell_min_volt
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/cells_max_diff_calc" ,str(cell_max_diff_volt))
if print_initial:
print("Pack " + str(p) +", Cell Max Diff Volt Calc: " + str(cell_max_diff_volt) + " mV")
temps = int(inc_data[byte_index:byte_index + 2],16)
if print_initial:
print("Pack " + str(p) + ", Total temperature sensors: " + str(temps))
byte_index += 2
for i in range(0,temps): #temps-2
t_cell[(p-1,i)] = (int(inc_data[byte_index:byte_index + 4],16)-2730)/10
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/temps/temp_" + str(i+1) ,str(round(t_cell[(p-1,i)],1)))
if print_initial:
print("Pack " + str(p) + ", Temp" + str(i+1) + ": " + str(round(t_cell[(p-1,i)],1)) + " ℃")
# t_mos= (int(inc_data[byte_index:byte_index+4],16))/160-273
# client.publish(config['mqtt_base_topic'] + "/t_mos",str(round(t_mos,1)))
# if print_initial:
# print("T Mos: " + str(t_mos) + " Deg")
# t_env= (int(inc_data[byte_index:byte_index+4],16))/160-273
# client.publish(config['mqtt_base_topic'] + "/t_env",str(round(t_env,1)))
# offset += 7
# if print_initial:
# print("T Env: " + str(t_env) + " Deg")
i_pack.append(int(inc_data[byte_index:byte_index+4],16))
byte_index += 4
if i_pack[p-1] >= 32768:
i_pack[p-1] = -1*(65535 - i_pack[p-1])
i_pack[p-1] = i_pack[p-1]/100
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_pack",str(i_pack[p-1]))
if print_initial:
print("Pack " + str(p) + ", I Pack: " + str(i_pack[p-1]) + " A")
v_pack.append(int(inc_data[byte_index:byte_index+4],16)/1000)
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/v_pack",str(v_pack[p-1]))
if print_initial:
print("Pack " + str(p) + ", V Pack: " + str(v_pack[p-1]) + " V")
i_remain_cap.append(int(inc_data[byte_index:byte_index+4],16)*10)
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_remain_cap",str(i_remain_cap[p-1]))
if print_initial:
print("Pack " + str(p) + ", I Remaining Capacity: " + str(i_remain_cap[p-1]) + " mAh")
byte_index += 2 # Manual: Define number P = 3
i_full_cap.append(int(inc_data[byte_index:byte_index+4],16)*10)
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_full_cap",str(i_full_cap[p-1]))
if print_initial:
print("Pack " + str(p) + ", I Full Capacity: " + str(i_full_cap[p-1]) + " mAh")
soc.append(round(i_remain_cap[p-1]/i_full_cap[p-1]*100,2))
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/soc",str(soc[p-1]))
if print_initial:
print("Pack " + str(p) + ", SOC: " + str(soc[p-1]) + " %")
cycles.append(int(inc_data[byte_index:byte_index+4],16))
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/cycles",str(cycles[p-1]))
if print_initial:
print("Pack " + str(p) + ", Cycles: " + str(cycles[p-1]))
i_design_cap.append(int(inc_data[byte_index:byte_index+4],16)*10)
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/i_design_cap",str(i_design_cap[p-1]))
if print_initial:
print("Pack " + str(p) + ", Design Capacity: " + str(i_design_cap[p-1]) + " mAh")
soh.append(round(i_full_cap[p-1]/i_design_cap[p-1]*100,2))
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/soh",str(soh[p-1]))
if print_initial:
print("Pack " + str(p) + ", SOH: " + str(soh[p-1]) + " %")
byte_index += 2
#Test for non signed value (matching cell count), to skip possible INFOFLAG present in data
if (byte_index < len(inc_data)) and (cells != int(inc_data[byte_index:byte_index+2],16)):
byte_index += 2
except Exception as e:
print("Error parsing BMS analog data: ", str(e))
return(False,"Error parsing BMS analog data: " + str(e))
if print_initial:
print("Script running....")
return True,True
def bms_getPackCapacity(bms):
byte_index = 0
success, inc_data = bms_request(bms,cid2=constants.cid2PackCapacity) # Seem to always reply with pack 1 data, even with ADR= 0 or FF and INFO= '' or FF
if success == False:
return(False,inc_data)
try:
pack_remain_cap = int(inc_data[byte_index:byte_index+4],16)*10
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_remain_cap",str(pack_remain_cap))
if print_initial:
print("Pack Remaining Capacity: " + str(pack_remain_cap) + " mAh")
pack_full_cap = int(inc_data[byte_index:byte_index+4],16)*10
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_full_cap",str(pack_full_cap))
if print_initial:
print("Pack Full Capacity: " + str(pack_full_cap) + " mAh")
pack_design_cap = int(inc_data[byte_index:byte_index+4],16)*10
byte_index += 4
client.publish(config['mqtt_base_topic'] + "/pack_design_cap",str(pack_design_cap))
if print_initial:
print("Pack Design Capacity: " + str(pack_design_cap) + " mAh")
pack_soc = round(pack_remain_cap/pack_full_cap*100,2)
client.publish(config['mqtt_base_topic'] + "/pack_soc",str(pack_soc))
if print_initial:
print("Pack SOC: " + str(pack_soc) + " %")
pack_soh = round(pack_full_cap/pack_design_cap*100,2)
client.publish(config['mqtt_base_topic'] + "/pack_soh",str(pack_soh))
if print_initial:
print("Pack SOH: " + str(pack_soh) + " %")
except Exception as e:
print("Error parsing BMS pack capacity data: ", str(e))
return False, "Error parsing BMS pack capacity data: " + str(e)
return True,True
def bms_getWarnInfo(bms):
byte_index = 2
packsW = 1
warnings = ""
success, inc_data = bms_request(bms,cid2=constants.cid2WarnInfo,info=b'FF')
if success == False:
return(False,inc_data)
#inc_data = b'000210000000000000000000000000000000000600000000000000000000000E0000000000001110000000000000000000000000000000000600000000000000000000000E00000000000000'
try:
packsW = int(inc_data[byte_index:byte_index+2],16)
if print_initial:
print("Packs for warnings: " + str(packs))
byte_index += 2
for p in range(1,packs+1):
cellsW = int(inc_data[byte_index:byte_index+2],16)
byte_index += 2
for c in range(1,cellsW+1):
if inc_data[byte_index:byte_index+2] != b'00':
warn = constants.warningStates[inc_data[byte_index:byte_index+2]]
warnings += "cell " + str(c) + " " + warn + ", "
byte_index += 2
tempsW = int(inc_data[byte_index:byte_index+2],16)
byte_index += 2
for t in range(1,tempsW+1):
if inc_data[byte_index:byte_index+2] != b'00':
warn = constants.warningStates[inc_data[byte_index:byte_index+2]]
warnings += "temp " + str(t) + " " + warn + ", "
byte_index += 2
if inc_data[byte_index:byte_index+2] != b'00':
warn = constants.warningStates[inc_data[byte_index:byte_index+2]]
warnings += "charge current " + warn + ", "
byte_index += 2
if inc_data[byte_index:byte_index+2] != b'00':
warn = constants.warningStates[inc_data[byte_index:byte_index+2]]
warnings += "total voltage " + warn + ", "
byte_index += 2
if inc_data[byte_index:byte_index+2] != b'00':
warn = constants.warningStates[inc_data[byte_index:byte_index+2]]
warnings += "discharge current " + warn + ", "
byte_index += 2
protectState1 = ord(bytes.fromhex(inc_data[byte_index:byte_index+2].decode('ascii')))
if protectState1 > 0:
warnings += "Protection State 1: "
for x in range(0,8):
if (protectState1 & (1<<x)):
warnings += constants.protectState1[x+1] + " | "
warnings = warnings.rstrip("| ")
warnings += ", "
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/prot_short_circuit",str(protectState1>>6 & 1))
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/prot_discharge_current",str(protectState1>>5 & 1))
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/prot_charge_current",str(protectState1>>4 & 1))
byte_index += 2
protectState2 = ord(bytes.fromhex(inc_data[byte_index:byte_index+2].decode('ascii')))
if protectState2 > 0:
warnings += "Protection State 2: "
for x in range(0,8):
if (protectState2 & (1<<x)):
warnings += constants.protectState2[x+1] + " | "
warnings = warnings.rstrip("| ")
warnings += ", "
client.publish(config['mqtt_base_topic'] + "/pack_" + str(p) + "/fully",str(protectState2>>7 & 1))
byte_index += 2
# instructionState = ord(bytes.fromhex(inc_data[byte_index:byte_index+2].decode('ascii')))
# if instructionState > 0:
# warnings += "Instruction State: "
# for x in range(0,8):
# if (instructionState & (1<<x)):
# warnings += constants.instructionState[x+1] + " | "
# warnings = warnings.rstrip("| ")
# warnings += ", "
# byte_index += 2