-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
jk_bms_ble.cpp
1584 lines (1383 loc) · 83.1 KB
/
jk_bms_ble.cpp
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
#include "jk_bms_ble.h"
#include "esphome/core/log.h"
#ifdef USE_ESP32
namespace esphome {
namespace jk_bms_ble {
static const char *const TAG = "jk_bms_ble";
static const uint8_t MAX_NO_RESPONSE_COUNT = 10;
static const uint8_t FRAME_VERSION_JK04 = 0x01;
static const uint8_t FRAME_VERSION_JK02_24S = 0x02;
static const uint8_t FRAME_VERSION_JK02_32S = 0x03;
static const uint16_t JK_BMS_SERVICE_UUID = 0xFFE0;
static const uint16_t JK_BMS_CHARACTERISTIC_UUID = 0xFFE1;
static const uint8_t COMMAND_CELL_INFO = 0x96;
static const uint8_t COMMAND_DEVICE_INFO = 0x97;
static const uint16_t MIN_RESPONSE_SIZE = 300;
static const uint16_t MAX_RESPONSE_SIZE = 320;
static const uint8_t ERRORS_SIZE = 16;
static const char *const ERRORS[ERRORS_SIZE] = {
"Charge Overtemperature", // 0000 0000 0000 0001
"Charge Undertemperature", // 0000 0000 0000 0010
"Coprocessor communication error", // 0000 0000 0000 0100
"Cell Undervoltage", // 0000 0000 0000 1000
"Battery pack undervoltage", // 0000 0000 0001 0000
"Discharge overcurrent", // 0000 0000 0010 0000
"Discharge short circuit", // 0000 0000 0100 0000
"Discharge overtemperature", // 0000 0000 1000 0000
"Wire resistance", // 0000 0001 0000 0000
"Mosfet overtemperature", // 0000 0010 0000 0000
"Cell count is not equal to settings", // 0000 0100 0000 0000
"Current sensor anomaly", // 0000 1000 0000 0000
"Cell Overvoltage", // 0001 0000 0000 0000
"Battery pack overvoltage", // 0010 0000 0000 0000
"Charge overcurrent protection", // 0100 0000 0000 0000
"Charge short circuit", // 1000 0000 0000 0000
};
uint8_t crc(const uint8_t data[], const uint16_t len) {
uint8_t crc = 0;
for (uint16_t i = 0; i < len; i++) {
crc = crc + data[i];
}
return crc;
}
void JkBmsBle::dump_config() { // NOLINT(google-readability-function-size,readability-function-size)
ESP_LOGCONFIG(TAG, "JkBmsBle");
LOG_SENSOR("", "Minimum Cell Voltage", this->min_cell_voltage_sensor_);
LOG_SENSOR("", "Maximum Cell Voltage", this->max_cell_voltage_sensor_);
LOG_SENSOR("", "Minimum Voltage Cell", this->min_voltage_cell_sensor_);
LOG_SENSOR("", "Maximum Voltage Cell", this->max_voltage_cell_sensor_);
LOG_SENSOR("", "Delta Cell Voltage", this->delta_cell_voltage_sensor_);
LOG_SENSOR("", "Average Cell Voltage", this->average_cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 1", this->cells_[0].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 2", this->cells_[1].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 3", this->cells_[2].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 4", this->cells_[3].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 5", this->cells_[4].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 6", this->cells_[5].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 7", this->cells_[6].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 8", this->cells_[7].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 9", this->cells_[8].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 10", this->cells_[9].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 11", this->cells_[10].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 12", this->cells_[11].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 13", this->cells_[12].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 14", this->cells_[13].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 15", this->cells_[14].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 16", this->cells_[15].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 17", this->cells_[16].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 18", this->cells_[17].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 19", this->cells_[18].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 20", this->cells_[19].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 21", this->cells_[20].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 22", this->cells_[21].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 23", this->cells_[22].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 24", this->cells_[23].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Resistance 1", this->cells_[0].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 2", this->cells_[1].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 3", this->cells_[2].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 4", this->cells_[3].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 5", this->cells_[4].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 6", this->cells_[5].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 7", this->cells_[6].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 8", this->cells_[7].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 9", this->cells_[8].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 10", this->cells_[9].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 11", this->cells_[10].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 12", this->cells_[11].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 13", this->cells_[12].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 14", this->cells_[13].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 15", this->cells_[14].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 16", this->cells_[15].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 17", this->cells_[16].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 18", this->cells_[17].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 19", this->cells_[18].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 20", this->cells_[19].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 21", this->cells_[20].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 22", this->cells_[21].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 23", this->cells_[22].cell_resistance_sensor_);
LOG_SENSOR("", "Cell Resistance 24", this->cells_[23].cell_resistance_sensor_);
LOG_SENSOR("", "Total Voltage", this->total_voltage_sensor_);
LOG_SENSOR("", "Current", this->current_sensor_);
LOG_SENSOR("", "Power", this->power_sensor_);
LOG_SENSOR("", "Charging Power", this->charging_power_sensor_);
LOG_SENSOR("", "Discharging Power", this->discharging_power_sensor_);
LOG_SENSOR("", "Power Tube Temperature", this->power_tube_temperature_sensor_);
LOG_SENSOR("", "Temperature Sensor 1", this->temperatures_[0].temperature_sensor_);
LOG_SENSOR("", "Temperature Sensor 2", this->temperatures_[1].temperature_sensor_);
LOG_SENSOR("", "Temperature Sensor 3", this->temperatures_[2].temperature_sensor_);
LOG_SENSOR("", "Temperature Sensor 4", this->temperatures_[3].temperature_sensor_);
LOG_SENSOR("", "Temperature Sensor 5", this->temperatures_[4].temperature_sensor_);
LOG_SENSOR("", "Balacing", this->balancing_sensor_);
LOG_SENSOR("", "State Of Charge", this->state_of_charge_sensor_);
LOG_SENSOR("", "Capacity Remaining", this->capacity_remaining_sensor_);
LOG_SENSOR("", "Total Battery Capacity Setting", this->total_battery_capacity_setting_sensor_);
LOG_SENSOR("", "Charging Cycles", this->charging_cycles_sensor_);
LOG_SENSOR("", "Total Charging Cycle Capacity", this->total_charging_cycle_capacity_sensor_);
LOG_SENSOR("", "Total Runtime", this->total_runtime_sensor_);
LOG_SENSOR("", "Heating Current", this->heating_current_sensor_);
LOG_TEXT_SENSOR("", "Operation Status", this->operation_status_text_sensor_);
LOG_TEXT_SENSOR("", "Total Runtime Formatted", this->total_runtime_formatted_text_sensor_);
LOG_BINARY_SENSOR("", "Balancing", this->balancing_binary_sensor_);
LOG_BINARY_SENSOR("", "Precharging", this->precharging_binary_sensor_);
LOG_BINARY_SENSOR("", "Charging", this->charging_binary_sensor_);
LOG_BINARY_SENSOR("", "Discharging", this->discharging_binary_sensor_);
LOG_BINARY_SENSOR("", "Heating", this->heating_binary_sensor_);
}
void JkBmsBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
esp_ble_gattc_cb_param_t *param) {
switch (event) {
case ESP_GATTC_OPEN_EVT: {
break;
}
case ESP_GATTC_DISCONNECT_EVT: {
this->node_state = espbt::ClientState::IDLE;
this->status_notification_received_ = false;
break;
}
case ESP_GATTC_SEARCH_CMPL_EVT: {
auto *chr = this->parent_->get_characteristic(JK_BMS_SERVICE_UUID, JK_BMS_CHARACTERISTIC_UUID);
if (chr == nullptr) {
ESP_LOGE(TAG, "[%s] No control service found at device, not an JK BMS..?",
this->parent_->address_str().c_str());
break;
}
// Services and characteristic of the old BLE module (C8:47:8C:XX:XX:XX)
//
// Found device at MAC address [C8:47:8C:F2:7E:03]
// Attempting BLE connection to c8:47:8c:f2:7e:03
// Service UUID: 0x1800
// start_handle: 0x1 end_handle: 0x9
// characteristic 0x2A00, handle 0x3, properties 0xa
// characteristic 0x2A01, handle 0x5, properties 0xa
// characteristic 0x2A02, handle 0x7, properties 0x2
// characteristic 0x2A04, handle 0x9, properties 0x2
// Service UUID: 0x1801
// start_handle: 0xa end_handle: 0xd
// characteristic 0x2A05, handle 0xc, properties 0x22
// Service UUID: 0xFFE0
// start_handle: 0xe end_handle: 0x13
// characteristic 0xFFE2, handle 0x10, properties 0x4
// characteristic 0xFFE1, handle 0x12, properties 0x1c
// Service UUID: 0x180A
// start_handle: 0x14 end_handle: 0x26
// characteristic 0x2A29, handle 0x16, properties 0x2
// characteristic 0x2A24, handle 0x18, properties 0x2
// characteristic 0x2A25, handle 0x1a, properties 0x2
// characteristic 0x2A27, handle 0x1c, properties 0x2
// characteristic 0x2A26, handle 0x1e, properties 0x2
// characteristic 0x2A28, handle 0x20, properties 0x2
// characteristic 0x2A23, handle 0x22, properties 0x2
// characteristic 0x2A2A, handle 0x24, properties 0x2
// characteristic 0x2A50, handle 0x26, properties 0x2
// Service UUID: 0x180F
// start_handle: 0x27 end_handle: 0x2a
// characteristic 0x2A19, handle 0x29, properties 0x12
// Service UUID: F000FFC0-0451-4000-B000-000000000000
// start_handle: 0x2b end_handle: 0x33
// characteristic F000FFC1-0451-4000-B000-000000000000, handle 0x2d, properties 0x1c
// characteristic F000FFC2-0451-4000-B000-000000000000, handle 0x31, properties 0x1c
// Services and characteristic of the new BLE module (20:21:11:XX:XX:XX)
//
// Found device at MAC address [20:21:11:28:18:DD]
// Attempting BLE connection to 20:21:11:28:18:dd
// Service UUID: 0xFFE0
// start_handle: 0x1 end_handle: 0xffff
// characteristic 0xFFE1, handle 0x3, properties 0xc
// characteristic 0xFFE1, handle 0x5, properties 0x12
// descriptor 0x2902, handle 0x6
this->char_handle_ = chr->handle;
this->notify_handle_ = (chr->handle == 0x03) ? 0x05 : chr->handle;
auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(), this->parent()->get_remote_bda(),
this->notify_handle_);
if (status) {
ESP_LOGW(TAG, "esp_ble_gattc_register_for_notify failed, status=%d", status);
}
break;
}
case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
this->node_state = espbt::ClientState::ESTABLISHED;
this->status_notification_received_ = false;
ESP_LOGI(TAG, "Request device info");
this->write_register(COMMAND_DEVICE_INFO, 0x00000000, 0x00);
break;
}
case ESP_GATTC_NOTIFY_EVT: {
if (param->notify.handle != this->notify_handle_)
break;
ESP_LOGVV(TAG, "Notification received: %s",
format_hex_pretty(param->notify.value, param->notify.value_len).c_str());
this->assemble(param->notify.value, param->notify.value_len);
break;
}
default:
break;
}
}
void JkBmsBle::update() {
this->track_online_status_();
if (this->node_state != espbt::ClientState::ESTABLISHED) {
ESP_LOGW(TAG, "[%s] Not connected", this->parent_->address_str().c_str());
return;
}
if (!this->status_notification_received_) {
ESP_LOGI(TAG, "Request status notification");
this->write_register(COMMAND_CELL_INFO, 0x00000000, 0x00);
}
}
// TODO: There is no need to assemble frames if the MTU can be increased to > 320 bytes
void JkBmsBle::assemble(const uint8_t *data, uint16_t length) {
if (this->frame_buffer_.size() > MAX_RESPONSE_SIZE) {
ESP_LOGW(TAG, "Frame dropped because of invalid length");
this->frame_buffer_.clear();
}
// Flush buffer on every preamble
if (data[0] == 0x55 && data[1] == 0xAA && data[2] == 0xEB && data[3] == 0x90) {
this->frame_buffer_.clear();
}
this->frame_buffer_.insert(this->frame_buffer_.end(), data, data + length);
if (this->frame_buffer_.size() >= MIN_RESPONSE_SIZE) {
const uint8_t *raw = &this->frame_buffer_[0];
// Even if the frame is 320 bytes long the CRC is at position 300 in front of 0xAA 0x55 0x90 0xEB
const uint16_t frame_size = 300; // this->frame_buffer_.size();
uint8_t computed_crc = crc(raw, frame_size - 1);
uint8_t remote_crc = raw[frame_size - 1];
if (computed_crc != remote_crc) {
ESP_LOGW(TAG, "CRC check failed! 0x%02X != 0x%02X", computed_crc, remote_crc);
this->frame_buffer_.clear();
return;
}
std::vector<uint8_t> data(this->frame_buffer_.begin(), this->frame_buffer_.end());
this->decode_(data);
this->frame_buffer_.clear();
}
}
void JkBmsBle::decode_(const std::vector<uint8_t> &data) {
this->reset_online_status_tracker_();
uint8_t frame_type = data[4];
switch (frame_type) {
case 0x01:
if (this->protocol_version_ == PROTOCOL_VERSION_JK04) {
this->decode_jk04_settings_(data);
} else {
this->decode_jk02_settings_(data);
}
break;
case 0x02:
if (this->protocol_version_ == PROTOCOL_VERSION_JK04) {
this->decode_jk04_cell_info_(data);
} else {
this->decode_jk02_cell_info_(data);
}
break;
case 0x03:
this->decode_device_info_(data);
break;
default:
ESP_LOGW(TAG, "Unsupported message type (0x%02X)", data[4]);
}
}
void JkBmsBle::decode_jk02_cell_info_(const std::vector<uint8_t> &data) {
auto jk_get_16bit = [&](size_t i) -> uint16_t { return (uint16_t(data[i + 1]) << 8) | (uint16_t(data[i + 0]) << 0); };
auto jk_get_32bit = [&](size_t i) -> uint32_t {
return (uint32_t(jk_get_16bit(i + 2)) << 16) | (uint32_t(jk_get_16bit(i + 0)) << 0);
};
const uint32_t now = millis();
if (now - this->last_cell_info_ < this->throttle_) {
return;
}
this->last_cell_info_ = now;
uint8_t frame_version = FRAME_VERSION_JK02_24S;
uint8_t offset = 0;
if (this->protocol_version_ == PROTOCOL_VERSION_JK02_32S) {
frame_version = FRAME_VERSION_JK02_32S;
offset = 16;
}
ESP_LOGI(TAG, "Cell info frame (version %d, %d bytes) received", frame_version, data.size());
ESP_LOGVV(TAG, " %s", format_hex_pretty(&data.front(), 150).c_str());
ESP_LOGVV(TAG, " %s", format_hex_pretty(&data.front() + 150, data.size() - 150).c_str());
// 6 example responses (128+128+44 = 300 bytes per frame)
//
//
// 55.AA.EB.90.02.8C.FF.0C.01.0D.01.0D.FF.0C.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.01.0D.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.FF.FF.00.00.00.0D.00.00.00.00.9D.01.96.01.8C.01.87.01.84.01.84.01.83.01.84.01.85.01.81.01.83.01.86.01.82.01.82.01.83.01.85.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.03.D0.00.00.00.00.00.00.00.00
// 00.00.BE.00.BF.00.D2.00.00.00.00.00.00.54.8E.0B.01.00.68.3C.01.00.00.00.00.00.3D.04.00.00.64.00.79.04.CA.03.10.00.01.01.AA.06.00.00.00.00.00.00.00.00.00.00.00.00.07.00.01.00.00.00.D5.02.00.00.00.00.AE.D6.3B.40.00.00.00.00.58.AA.FD.FF.00.00.00.01.00.02.00.00.EC.E6.4F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
// 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.CD
//
// 55.AA.EB.90.02.8D.FF.0C.01.0D.01.0D.01.0D.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.FF.0C.FF.0C.01.0D.01.0D.01.0D.01.0D.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.FF.FF.00.00.00.0D.00.00.00.00.9D.01.96.01.8C.01.87.01.84.01.84.01.83.01.84.01.85.01.81.01.83.01.86.01.82.01.82.01.83.01.85.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.04.D0.00.00.00.00.00.00.00.00
// 00.00.BE.00.BF.00.D2.00.00.00.00.00.00.54.8E.0B.01.00.68.3C.01.00.00.00.00.00.3D.04.00.00.64.00.79.04.CA.03.10.00.01.01.AA.06.00.00.00.00.00.00.00.00.00.00.00.00.07.00.01.00.00.00.D5.02.00.00.00.00.AE.D6.3B.40.00.00.00.00.58.AA.FD.FF.00.00.00.01.00.02.00.00.F0.E6.4F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
// 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.D3
//
// 55.AA.EB.90.02.8E.FF.0C.01.0D.01.0D.FF.0C.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.FF.0C.FF.0C.01.0D.01.0D.01.0D.01.0D.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.FF.FF.00.00.00.0D.00.00.00.00.9D.01.96.01.8C.01.87.01.84.01.84.01.83.01.84.01.85.01.81.01.83.01.86.01.82.01.82.01.83.01.85.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.04.D0.00.00.00.00.00.00.00.00
// 00.00.BE.00.BF.00.D2.00.00.00.00.00.00.54.8E.0B.01.00.68.3C.01.00.00.00.00.00.3D.04.00.00.64.00.79.04.CA.03.10.00.01.01.AA.06.00.00.00.00.00.00.00.00.00.00.00.00.07.00.01.00.00.00.D5.02.00.00.00.00.AE.D6.3B.40.00.00.00.00.58.AA.FD.FF.00.00.00.01.00.02.00.00.F5.E6.4F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
// 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.D6
//
// 55.AA.EB.90.02.91.FF.0C.FF.0C.01.0D.FF.0C.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.01.0D.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.FF.FF.00.00.00.0D.00.00.00.00.9D.01.96.01.8C.01.87.01.84.01.84.01.83.01.84.01.85.01.81.01.83.01.86.01.82.01.82.01.83.01.85.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.01.D0.00.00.00.00.00.00.00.00
// 00.00.BF.00.C0.00.D2.00.00.00.00.00.00.54.8E.0B.01.00.68.3C.01.00.00.00.00.00.3D.04.00.00.64.00.79.04.CC.03.10.00.01.01.AA.06.00.00.00.00.00.00.00.00.00.00.00.00.07.00.01.00.00.00.D5.02.00.00.00.00.AE.D6.3B.40.00.00.00.00.58.AA.FD.FF.00.00.00.01.00.02.00.00.01.E7.4F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
// 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.E7
//
// 55.AA.EB.90.02.92.01.0D.01.0D.01.0D.01.0D.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.01.0D.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.FF.FF.00.00.00.0D.00.00.00.00.9D.01.96.01.8C.01.87.01.84.01.84.01.83.01.84.01.85.01.81.01.83.01.86.01.82.01.82.01.83.01.85.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.03.D0.00.00.00.00.00.00.00.00
// 00.00.BF.00.C0.00.D2.00.00.00.00.00.00.54.8E.0B.01.00.68.3C.01.00.00.00.00.00.3D.04.00.00.64.00.79.04.CC.03.10.00.01.01.AA.06.00.00.00.00.00.00.00.00.00.00.00.00.07.00.01.00.00.00.D5.02.00.00.00.00.AE.D6.3B.40.00.00.00.00.58.AA.FD.FF.00.00.00.01.00.02.00.00.06.E7.4F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
// 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.F8
//
// 55.AA.EB.90.02.93.FF.0C.01.0D.01.0D.01.0D.01.0D.01.0D.FF.0C.01.0D.01.0D.01.0D.FF.0C.FF.0C.01.0D.01.0D.01.0D.01.0D.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.FF.FF.00.00.00.0D.00.00.00.00.9D.01.96.01.8C.01.87.01.84.01.84.01.83.01.84.01.85.01.81.01.83.01.86.01.82.01.82.01.83.01.85.01.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.04.D0.00.00.00.00.00.00.00.00
// 00.00.BE.00.C0.00.D2.00.00.00.00.00.00.54.8E.0B.01.00.68.3C.01.00.00.00.00.00.3D.04.00.00.64.00.79.04.CD.03.10.00.01.01.AA.06.00.00.00.00.00.00.00.00.00.00.00.00.07.00.01.00.00.00.D5.02.00.00.00.00.AE.D6.3B.40.00.00.00.00.58.AA.FD.FF.00.00.00.01.00.02.00.00.0A.E7.4F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00
// 00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.F8
//
// Byte Len Payload Content Coeff. Unit Example value
// 0 2 0x55 0xAA 0xEB 0x90 Header
// 4 1 0x02 Record type
// 5 1 0x8C Frame counter
// 6 2 0xFF 0x0C Voltage cell 01 0.001 V
// 8 2 0x01 0x0D Voltage cell 02 0.001 V
// 10 2 0x01 0x0D Voltage cell 03 0.001 V
// ...
uint8_t cells = 24 + (offset / 2);
uint8_t cells_enabled = 0;
float min_cell_voltage = 100.0f;
float max_cell_voltage = -100.0f;
float average_cell_voltage = 0.0f;
uint8_t min_voltage_cell = 0;
uint8_t max_voltage_cell = 0;
for (uint8_t i = 0; i < cells; i++) {
float cell_voltage = (float) jk_get_16bit(i * 2 + 6) * 0.001f;
float cell_resistance = (float) jk_get_16bit(i * 2 + 64 + offset) * 0.001f;
if (cell_voltage > 0) {
average_cell_voltage = average_cell_voltage + cell_voltage;
cells_enabled++;
}
if (cell_voltage > 0 && cell_voltage < min_cell_voltage) {
min_cell_voltage = cell_voltage;
min_voltage_cell = i + 1;
}
if (cell_voltage > max_cell_voltage) {
max_cell_voltage = cell_voltage;
max_voltage_cell = i + 1;
}
this->publish_state_(this->cells_[i].cell_voltage_sensor_, cell_voltage);
this->publish_state_(this->cells_[i].cell_resistance_sensor_, cell_resistance);
}
average_cell_voltage = average_cell_voltage / cells_enabled;
this->publish_state_(this->min_cell_voltage_sensor_, min_cell_voltage);
this->publish_state_(this->max_cell_voltage_sensor_, max_cell_voltage);
this->publish_state_(this->max_voltage_cell_sensor_, (float) max_voltage_cell);
this->publish_state_(this->min_voltage_cell_sensor_, (float) min_voltage_cell);
this->publish_state_(this->delta_cell_voltage_sensor_, max_cell_voltage - min_cell_voltage);
this->publish_state_(this->average_cell_voltage_sensor_, average_cell_voltage);
// 54 4 0xFF 0xFF 0x00 0x00 Enabled cells bitmask
// 0x0F 0x00 0x00 0x00 4 cells enabled
// 0xFF 0x00 0x00 0x00 8 cells enabled
// 0xFF 0x0F 0x00 0x00 12 cells enabled
// 0xFF 0x1F 0x00 0x00 13 cells enabled
// 0xFF 0xFF 0x00 0x00 16 cells enabled
// 0xFF 0xFF 0xFF 0x00 24 cells enabled
// 0xFF 0xFF 0xFF 0xFF 32 cells enabled
ESP_LOGV(TAG, "Enabled cells bitmask: 0x%02X 0x%02X 0x%02X 0x%02X", data[54 + offset], data[55 + offset],
data[56 + offset], data[57 + offset]);
// 58 2 0x00 0x0D Average Cell Voltage 0.001 V
// this->publish_state_(this->average_cell_voltage_sensor_, (float) jk_get_16bit(58 + offset) * 0.001f);
// 60 2 0x00 0x00 Delta Cell Voltage 0.001 V
// this->publish_state_(this->delta_cell_voltage_sensor_, (float) jk_get_16bit(60 + offset) * 0.001f);
// 62 1 0x00 Max voltage cell 1
// this->publish_state_(this->max_voltage_cell_sensor_, (float) data[62 + offset] + 1);
// 63 1 0x00 Min voltage cell 1
// this->publish_state_(this->min_voltage_cell_sensor_, (float) data[63 + offset] + 1);
// 64 2 0x9D 0x01 Resistance Cell 01 0.001 Ohm
// 66 2 0x96 0x01 Resistance Cell 02 0.001 Ohm
// 68 2 0x8C 0x01 Resistance Cell 03 0.001 Ohm
// ...
// 110 2 0x00 0x00 Resistance Cell 24 0.001 Ohm
offset = offset * 2;
// 112 2 0x00 0x00 Unknown112
if (frame_version == FRAME_VERSION_JK02_32S) {
this->publish_state_(this->power_tube_temperature_sensor_, (float) ((int16_t) jk_get_16bit(112 + offset)) * 0.1f);
} else {
ESP_LOGD(TAG, "Unknown112: 0x%02X 0x%02X", data[112 + offset], data[113 + offset]);
}
// 114 4 0x00 0x00 0x00 0x00 Wire resistance warning bitmask (each bit indicates a warning per cell / wire)
ESP_LOGD(TAG, "Wire resistance warning bitmask: 0x%02X 0x%02X 0x%02X 0x%02X", data[114 + offset], data[115 + offset],
data[116 + offset], data[117 + offset]);
// 118 4 0x03 0xD0 0x00 0x00 Battery voltage 0.001 V
float total_voltage = (float) jk_get_32bit(118 + offset) * 0.001f;
this->publish_state_(this->total_voltage_sensor_, total_voltage);
// 122 4 0x00 0x00 0x00 0x00 Battery power 0.001 W
// 126 4 0x00 0x00 0x00 0x00 Charge current 0.001 A
float current = (float) ((int32_t) jk_get_32bit(126 + offset)) * 0.001f;
this->publish_state_(this->current_sensor_, (float) ((int32_t) jk_get_32bit(126 + offset)) * 0.001f);
// Don't use byte 122 because it's unsigned
// float power = (float) ((int32_t) jk_get_32bit(122 + offset)) * 0.001f;
float power = total_voltage * current;
this->publish_state_(this->power_sensor_, power);
this->publish_state_(this->charging_power_sensor_, std::max(0.0f, power)); // 500W vs 0W -> 500W
this->publish_state_(this->discharging_power_sensor_, std::abs(std::min(0.0f, power))); // -500W vs 0W -> 500W
// 130 2 0xBE 0x00 Temperature Sensor 1 0.1 °C
this->publish_state_(this->temperatures_[0].temperature_sensor_,
(float) ((int16_t) jk_get_16bit(130 + offset)) * 0.1f);
// 132 2 0xBF 0x00 Temperature Sensor 2 0.1 °C
this->publish_state_(this->temperatures_[1].temperature_sensor_,
(float) ((int16_t) jk_get_16bit(132 + offset)) * 0.1f);
// 134 2 0xD2 0x00 MOS Temperature 0.1 °C
if (frame_version == FRAME_VERSION_JK02_32S) {
uint16_t raw_errors_bitmask = (uint16_t(data[134 + offset]) << 8) | (uint16_t(data[134 + 1 + offset]) << 0);
this->publish_state_(this->errors_bitmask_sensor_, (float) raw_errors_bitmask);
this->publish_state_(this->errors_text_sensor_, this->error_bits_to_string_(raw_errors_bitmask));
} else {
this->publish_state_(this->power_tube_temperature_sensor_, (float) ((int16_t) jk_get_16bit(134 + offset)) * 0.1f);
}
// 136 2 0x00 0x00 System alarms
// 0x00 0x01 Charge overtemperature 0000 0000 0000 0001
// 0x00 0x02 Charge undertemperature 0000 0000 0000 0010
// 0x00 0x04 Coprocessor communication error 0000 0000 0000 0100
// 0x00 0x08 Cell undervoltage 0000 0000 0000 1000
// 0x00 0x10 Battery pack undervoltage 0000 0000 0001 0000
// 0x00 0x20 Discharge overcurrent 0000 0000 0010 0000
// 0x00 0x40 Discharge short circuit 0000 0000 0100 0000
// 0x00 0x80 Discharge overtemperature 0000 0000 1000 0000
// 0x01 0x00 Wire resistance 0000 0001 0000 0000
// 0x02 0x00 Mosfet overtemperature 0000 0010 0000 0000
// 0x04 0x00 Cell count is not equal to settings 0000 0100 0000 0000
// 0x08 0x00 Current sensor anomaly 0000 1000 0000 0000
// 0x10 0x00 Cell Overvoltage 0001 0000 0000 0000
// 0x20 0x00 Battery pack overvoltage 0010 0000 0000 0000
// 0x40 0x00 Charge overcurrent protection 0100 0000 0000 0000
// 0x80 0x00 Charge short circuit 1000 0000 0000 0000
//
// 0x14 0x00 Cell Over Voltage + 0001 0100 0000 0000
// Cell count is not equal to settings
// 0x04 0x08 Cell Undervoltage + 0000 0100 0000 1000
// Cell count is not equal to settings
if (frame_version != FRAME_VERSION_JK02_32S) {
uint16_t raw_errors_bitmask = (uint16_t(data[136 + offset]) << 8) | (uint16_t(data[136 + 1 + offset]) << 0);
this->publish_state_(this->errors_bitmask_sensor_, (float) raw_errors_bitmask);
this->publish_state_(this->errors_text_sensor_, this->error_bits_to_string_(raw_errors_bitmask));
}
// 138 2 0x00 0x00 Balance current 0.001 A
this->publish_state_(this->balancing_current_sensor_, (float) ((int16_t) jk_get_16bit(138 + offset)) * 0.001f);
// 140 1 0x00 Balancing action 0x00: Off
// 0x01: Charging balancer
// 0x02: Discharging balancer
this->publish_state_(this->balancing_sensor_, (data[140 + offset]));
this->publish_state_(this->balancing_binary_sensor_, (data[140 + offset] != 0x00));
ESP_LOGD(TAG, "Balancing indicator (legacy): %s", YESNO(data[140 + offset] != 0x00));
// 141 1 0x54 State of charge in 1.0 %
this->publish_state_(this->state_of_charge_sensor_, (float) data[141 + offset]);
// 142 4 0x8E 0x0B 0x01 0x00 Capacity_Remain 0.001 Ah
this->publish_state_(this->capacity_remaining_sensor_, (float) jk_get_32bit(142 + offset) * 0.001f);
// 146 4 0x68 0x3C 0x01 0x00 Nominal_Capacity 0.001 Ah
this->publish_state_(this->total_battery_capacity_setting_sensor_, (float) jk_get_32bit(146 + offset) * 0.001f);
// 150 4 0x00 0x00 0x00 0x00 Cycle_Count 1.0
this->publish_state_(this->charging_cycles_sensor_, (float) jk_get_32bit(150 + offset));
// 154 4 0x3D 0x04 0x00 0x00 Cycle_Capacity 0.001 Ah
this->publish_state_(this->total_charging_cycle_capacity_sensor_, (float) jk_get_32bit(154 + offset) * 0.001f);
// 158 1 0x64 SOH 1.0 %
ESP_LOGD(TAG, "State of health: %d %%", data[158 + offset]);
// 159 1 0x00 Precharge
ESP_LOGD(TAG, "Precharge: %s", ONOFF(data[159 + offset]));
// 160 2 0x79 0x04 User alarm
ESP_LOGD(TAG, "User alarm: 0x%02X 0x%02X (always 0xC5 0x09?)", data[160 + offset], data[161 + offset]);
// 162 4 0xCA 0x03 0x10 0x00 Total runtime in seconds s
this->publish_state_(this->total_runtime_sensor_, (float) jk_get_32bit(162 + offset));
this->publish_state_(this->total_runtime_formatted_text_sensor_, format_total_runtime_(jk_get_32bit(162 + offset)));
// 166 1 0x01 Charging mosfet enabled 0x00: off, 0x01: on
this->publish_state_(this->charging_binary_sensor_, (bool) data[166 + offset]);
// 167 1 0x01 Discharging mosfet enabled 0x00: off, 0x01: on
this->publish_state_(this->discharging_binary_sensor_, (bool) data[167 + offset]);
// 168 1 0x01 Precharging 0x00: off, 0x01: on
this->publish_state_(this->precharging_binary_sensor_, (bool) data[168 + offset]);
// 169 1 0x01 Balancer working 0x00: off, 0x01: on
// this->publish_state_(this->balancing_binary_sensor_, (bool) data[169 + offset]);
ESP_LOGD(TAG, "Balancing indicator (new): %s", YESNO((bool) data[169 + offset]));
ESP_LOGD(TAG, "Discharge overcurrent protection release timer: %d", jk_get_16bit(170 + offset));
ESP_LOGD(TAG, "Discharge short circuit protection release timer: %d", jk_get_16bit(172 + offset));
ESP_LOGD(TAG, "Charge overcurrent protection release timer: %d", jk_get_16bit(174 + offset));
ESP_LOGD(TAG, "Charge short circuit protection release timer: %d", jk_get_16bit(176 + offset));
ESP_LOGD(TAG, "Undervoltage protection release timer: %d", jk_get_16bit(178 + offset));
ESP_LOGD(TAG, "Overvoltage protection release timer: %d", jk_get_16bit(180 + offset));
ESP_LOGD(TAG, "Temperature sensor absent bitmask: %d", jk_get_16bit(182 + offset));
// bit0: Mosfet temperature sensor
// bit1: Temperature sensor 1
// bit2: Temperature sensor 2
// bit3: Temperature sensor 3
// bit4: Temperature sensor 4
// bit5: Temperature sensor 5
ESP_LOGD(TAG, "Heating: %s", ONOFF((bool) data[183 + offset]));
this->publish_state_(this->heating_binary_sensor_, (bool) data[183 + offset]);
ESP_LOGD(TAG, "Time emergency: %d s", jk_get_16bit(186 + offset));
ESP_LOGD(TAG, "Discharge current correction factor: %d", jk_get_16bit(188 + offset));
ESP_LOGD(TAG, "Charging current sensor voltage: %.3f", jk_get_16bit(190 + offset) * 0.001f);
ESP_LOGD(TAG, "Discharging current sensor voltage: %.3f", jk_get_16bit(192 + offset) * 0.001f);
ESP_LOGD(TAG, "Battery voltage correction factor: %f", (float) jk_get_32bit(194 + offset) * 1.0f);
ESP_LOGD(TAG, "Battery voltage: %.3f", (float) ieee_float_(jk_get_32bit(202 + offset)));
ESP_LOGD(TAG, "Heating current: %.3f A", (float) ((int16_t) jk_get_16bit(204 + offset)) * 0.001f);
this->publish_state_(this->heating_current_sensor_, (float) ((int16_t) jk_get_16bit(204 + offset)) * 0.001f);
ESP_LOGD(TAG, "Charger Plugged: %s", ONOFF((bool) data[213 + offset]));
ESP_LOGD(TAG, "Temperature sensor 3: %.1f °C", (float) jk_get_16bit(222 + offset) * 0.1f);
ESP_LOGD(TAG, "Temperature sensor 4: %.1f °C", (float) jk_get_16bit(224 + offset) * 0.1f);
ESP_LOGD(TAG, "Temperature sensor 5: %.1f °C", (float) jk_get_16bit(226 + offset) * 0.1f);
ESP_LOGD(TAG, "Time enter sleep: %u s", jk_get_32bit(238 + offset));
ESP_LOGD(TAG, "PCL Module State: %s", ONOFF((bool) data[242 + offset]));
if (frame_version == FRAME_VERSION_JK02_32S) {
uint16_t raw_emergency_time_countdown = jk_get_16bit(186 + offset);
ESP_LOGI(TAG, "Emergency switch: %s", ONOFF(raw_emergency_time_countdown > 0));
this->publish_state_(this->emergency_switch_, raw_emergency_time_countdown > 0);
this->publish_state_(this->emergency_time_countdown_sensor_, (float) raw_emergency_time_countdown * 1.0f);
this->publish_state_(this->temperatures_[4].temperature_sensor_,
(float) ((int16_t) jk_get_16bit(222 + offset)) * 0.1f);
this->publish_state_(this->temperatures_[3].temperature_sensor_,
(float) ((int16_t) jk_get_16bit(224 + offset)) * 0.1f);
this->publish_state_(this->temperatures_[2].temperature_sensor_,
(float) ((int16_t) jk_get_16bit(226 + offset)) * 0.1f);
}
// 299 1 0xCD CRC
this->status_notification_received_ = true;
}
void JkBmsBle::decode_jk04_cell_info_(const std::vector<uint8_t> &data) {
auto jk_get_16bit = [&](size_t i) -> uint16_t { return (uint16_t(data[i + 1]) << 8) | (uint16_t(data[i + 0]) << 0); };
auto jk_get_32bit = [&](size_t i) -> uint32_t {
return (uint32_t(jk_get_16bit(i + 2)) << 16) | (uint32_t(jk_get_16bit(i + 0)) << 0);
};
const uint32_t now = millis();
if (now - this->last_cell_info_ < this->throttle_) {
return;
}
this->last_cell_info_ = now;
ESP_LOGI(TAG, "Cell info frame (%d bytes) received", data.size());
ESP_LOGVV(TAG, " %s", format_hex_pretty(&data.front(), 150).c_str());
ESP_LOGVV(TAG, " %s", format_hex_pretty(&data.front() + 150, data.size() - 150).c_str());
// 0x55 0xAA 0xEB 0x90 0x02 0x4B 0xC0 0x61 0x56 0x40 0x1F 0xAA 0x56 0x40 0xFF 0x91 0x56 0x40 0xFF 0x91 0x56 0x40 0x1F
// 0xAA 0x56 0x40 0xFF 0x91 0x56 0x40 0xFF 0x91 0x56 0x40 0xFF 0x91 0x56 0x40 0x1F 0xAA 0x56 0x40 0xFF 0x91 0x56 0x40
// 0xFF 0x91 0x56 0x40 0xFF 0x91 0x56 0x40 0xFF 0x91 0x56 0x40 0x1F 0xAA 0x56 0x40 0xE0 0x79 0x56 0x40 0xE0 0x79 0x56
// 0x40 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x7C 0x1D 0x23 0x3E 0x1B 0xEB 0x08 0x3E 0x56 0xCE 0x14 0x3E 0x4D
// 0x9B 0x15 0x3E 0xE0 0xDB 0xCD 0x3D 0x72 0x33 0xCD 0x3D 0x94 0x88 0x01 0x3E 0x5E 0x1E 0xEA 0x3D 0xE5 0x17 0xCD 0x3D
// 0xE3 0xBB 0xD7 0x3D 0xF5 0x44 0xD2 0x3D 0xBE 0x7C 0x01 0x3E 0x27 0xB6 0x00 0x3E 0xDA 0xB5 0xFC 0x3D 0x6B 0x51 0xF8
// 0x3D 0xA2 0x93 0xF3 0x3D 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x03 0x95 0x56 0x40 0x00
// 0xBE 0x90 0x3B 0x00 0x00 0x00 0x00 0xFF 0xFF 0x00 0x00 0x01 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x66 0xA0 0xD2 0x4A 0x40 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x53 0x96 0x1C 0x00 0x00 0x00 0x00 0x00 0x00 0x48 0x22 0x40 0x00
// 0x13
//
// Byte Len Payload Content Coeff. Unit Example value
// 0 4 0x55 0xAA 0xEB 0x90 Header
// 4 1 0x02 Frame type
// 5 1 0x4B Frame counter
// 6 4 0xC0 0x61 0x56 0x40 Cell voltage 1 V
// 10 4 0x1F 0xAA 0x56 0x40 Cell voltage 2 V
// 14 4 0xFF 0x91 0x56 0x40 Cell voltage 3 V
// 18 4 0xFF 0x91 0x56 0x40 Cell voltage 4 V
// 22 4 0x1F 0xAA 0x56 0x40 Cell voltage 5 V
// 26 4 0xFF 0x91 0x56 0x40 Cell voltage 6 V
// 30 4 0xFF 0x91 0x56 0x40 Cell voltage 7 V
// 34 4 0xFF 0x91 0x56 0x40 Cell voltage 8 V
// 38 4 0x1F 0xAA 0x56 0x40 Cell voltage 9 V
// 42 4 0xFF 0x91 0x56 0x40 Cell voltage 10 V
// 46 4 0xFF 0x91 0x56 0x40 Cell voltage 11 V
// 50 4 0xFF 0x91 0x56 0x40 Cell voltage 12 V
// 54 4 0xFF 0x91 0x56 0x40 Cell voltage 13 V
// 58 4 0x1F 0xAA 0x56 0x40 Cell voltage 14 V
// 62 4 0xE0 0x79 0x56 0x40 Cell voltage 15 V
// 66 4 0xE0 0x79 0x56 0x40 Cell voltage 16 V
// 70 4 0x00 0x00 0x00 0x00 Cell voltage 17 V
// 74 4 0x00 0x00 0x00 0x00 Cell voltage 18 V
// 78 4 0x00 0x00 0x00 0x00 Cell voltage 19 V
// 82 4 0x00 0x00 0x00 0x00 Cell voltage 20 V
// 86 4 0x00 0x00 0x00 0x00 Cell voltage 21 V
// 90 4 0x00 0x00 0x00 0x00 Cell voltage 22 V
// 94 4 0x00 0x00 0x00 0x00 Cell voltage 23 V
// 98 4 0x00 0x00 0x00 0x00 Cell voltage 24 V
// 102 4 0x7C 0x1D 0x23 0x3E Cell resistance 1 Ohm
// 106 4 0x1B 0xEB 0x08 0x3E Cell resistance 2 Ohm
// 110 4 0x56 0xCE 0x14 0x3E Cell resistance 3 Ohm
// 114 4 0x4D 0x9B 0x15 0x3E Cell resistance 4 Ohm
// 118 4 0xE0 0xDB 0xCD 0x3D Cell resistance 5 Ohm
// 122 4 0x72 0x33 0xCD 0x3D Cell resistance 6 Ohm
// 126 4 0x94 0x88 0x01 0x3E Cell resistance 7 Ohm
// 130 4 0x5E 0x1E 0xEA 0x3D Cell resistance 8 Ohm
// 134 4 0xE5 0x17 0xCD 0x3D Cell resistance 9 Ohm
// 138 4 0xE3 0xBB 0xD7 0x3D Cell resistance 10 Ohm
// 142 4 0xF5 0x44 0xD2 0x3D Cell resistance 11 Ohm
// 146 4 0xBE 0x7C 0x01 0x3E Cell resistance 12 Ohm
// 150 4 0x27 0xB6 0x00 0x3E Cell resistance 13 Ohm
// 154 4 0xDA 0xB5 0xFC 0x3D Cell resistance 14 Ohm
// 158 4 0x6B 0x51 0xF8 0x3D Cell resistance 15 Ohm
// 162 4 0xA2 0x93 0xF3 0x3D Cell resistance 16 Ohm
// 166 4 0x00 0x00 0x00 0x00 Cell resistance 17 Ohm
// 170 4 0x00 0x00 0x00 0x00 Cell resistance 18 Ohm
// 174 4 0x00 0x00 0x00 0x00 Cell resistance 19 Ohm
// 178 4 0x00 0x00 0x00 0x00 Cell resistance 20 Ohm
// 182 4 0x00 0x00 0x00 0x00 Cell resistance 21 Ohm
// 186 4 0x00 0x00 0x00 0x00 Cell resistance 22 Ohm
// 190 4 0x00 0x00 0x00 0x00 Cell resistance 23 Ohm
// 194 4 0x00 0x00 0x00 0x00 Cell resistance 24 Ohm
// 198 4 0x00 0x00 0x00 0x00 Cell resistance 25 Ohm
// https://github.com/jblance/mpp-solar/issues/98#issuecomment-823701486
uint8_t cells = 24;
uint8_t cells_enabled = 0;
float min_cell_voltage = 100.0f;
float max_cell_voltage = -100.0f;
float average_cell_voltage = 0.0f;
float total_voltage = 0.0f;
uint8_t min_voltage_cell = 0;
uint8_t max_voltage_cell = 0;
for (uint8_t i = 0; i < cells; i++) {
float cell_voltage = (float) ieee_float_(jk_get_32bit(i * 4 + 6));
float cell_resistance = (float) ieee_float_(jk_get_32bit(i * 4 + 102));
total_voltage = total_voltage + cell_voltage;
if (cell_voltage > 0) {
average_cell_voltage = average_cell_voltage + cell_voltage;
cells_enabled++;
}
if (cell_voltage > 0 && cell_voltage < min_cell_voltage) {
min_cell_voltage = cell_voltage;
min_voltage_cell = i + 1;
}
if (cell_voltage > max_cell_voltage) {
max_cell_voltage = cell_voltage;
max_voltage_cell = i + 1;
}
this->publish_state_(this->cells_[i].cell_voltage_sensor_, cell_voltage);
this->publish_state_(this->cells_[i].cell_resistance_sensor_, cell_resistance);
}
average_cell_voltage = average_cell_voltage / cells_enabled;
this->publish_state_(this->min_cell_voltage_sensor_, min_cell_voltage);
this->publish_state_(this->max_cell_voltage_sensor_, max_cell_voltage);
this->publish_state_(this->min_voltage_cell_sensor_, (float) min_voltage_cell);
this->publish_state_(this->max_voltage_cell_sensor_, (float) max_voltage_cell);
this->publish_state_(this->delta_cell_voltage_sensor_, max_cell_voltage - min_cell_voltage);
this->publish_state_(this->average_cell_voltage_sensor_, average_cell_voltage);
this->publish_state_(this->total_voltage_sensor_, total_voltage);
// 202 4 0x03 0x95 0x56 0x40 Average Cell Voltage V
// this->publish_state_(this->average_cell_voltage_sensor_, (float) ieee_float_(jk_get_32bit(202)));
// 206 4 0x00 0xBE 0x90 0x3B Delta Cell Voltage V
// this->publish_state_(this->delta_cell_voltage_sensor_, (float) ieee_float_(jk_get_32bit(206)));
// 210 4 0x00 0x00 0x00 0x00 Unknown210
ESP_LOGD(TAG, "Unknown210: 0x%02X 0x%02X 0x%02X 0x%02X (always 0x00 0x00 0x00 0x00)", data[210], data[211], data[212],
data[213]);
// 214 4 0xFF 0xFF 0x00 0x00 Unknown214
ESP_LOGD(TAG, "Unknown214: 0x%02X 0x%02X 0x%02X 0x%02X (0xFF 0xFF: 24 cells?)", data[214], data[215], data[216],
data[217]);
// 218 1 0x01 Unknown218
ESP_LOGD(TAG, "Unknown218: 0x%02X", data[218]);
// 219 1 0x00 Unknown219
ESP_LOGD(TAG, "Unknown219: 0x%02X", data[219]);
// 220 1 0x00 Blink cells (0x00: Off, 0x01: Charging balancer, 0x02: Discharging balancer)
bool balancing = (data[220] != 0x00);
this->publish_state_(this->balancing_binary_sensor_, balancing);
this->publish_state_(this->operation_status_text_sensor_, (balancing) ? "Balancing" : "Idle");
// 221 1 0x01 Unknown221
ESP_LOGD(TAG, "Unknown221: 0x%02X", data[221]);
// 222 4 0x00 0x00 0x00 0x00 Balancing current
this->publish_state_(this->balancing_current_sensor_, (float) ieee_float_(jk_get_32bit(222)));
// 226 7 0x00 0x00 0x00 0x00 0x00 0x00 0x00 Unknown226
ESP_LOGD(TAG, "Unknown226: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X (always 0x00...0x00?)", data[226],
data[227], data[228], data[229], data[230], data[231], data[232]);
// 233 4 0x66 0xA0 0xD2 0x4A Unknown233
ESP_LOGD(TAG, "Unknown233: 0x%02X 0x%02X 0x%02X 0x%02X (%f)", data[233], data[234], data[235], data[236],
ieee_float_(jk_get_32bit(233)));
// 237 4 0x40 0x00 0x00 0x00 Unknown237
ESP_LOGD(TAG, "Unknown237: 0x%02X 0x%02X 0x%02X 0x%02X (always 0x40 0x00 0x00 0x00?)", data[237], data[238],
data[239], data[240]);
// 241 45 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x01 0x01 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00
// 286 3 0x53 0x96 0x1C 0x00 Uptime
this->publish_state_(this->total_runtime_sensor_, (float) jk_get_32bit(286));
this->publish_state_(this->total_runtime_formatted_text_sensor_, format_total_runtime_(jk_get_32bit(286)));
// 290 4 0x00 0x00 0x00 0x00 Unknown290
ESP_LOGD(TAG, "Unknown290: 0x%02X 0x%02X 0x%02X 0x%02X (always 0x00 0x00 0x00 0x00?)", data[290], data[291],
data[292], data[293]);
// 294 4 0x00 0x48 0x22 0x40 Unknown294
ESP_LOGD(TAG, "Unknown294: 0x%02X 0x%02X 0x%02X 0x%02X", data[294], data[295], data[296], data[297]);
// 298 1 0x00 Unknown298
ESP_LOGD(TAG, "Unknown298: 0x%02X", data[298]);
// 299 1 0x13 Checksm
status_notification_received_ = true;
}
void JkBmsBle::decode_jk02_settings_(const std::vector<uint8_t> &data) {
auto jk_get_16bit = [&](size_t i) -> uint16_t { return (uint16_t(data[i + 1]) << 8) | (uint16_t(data[i + 0]) << 0); };
auto jk_get_32bit = [&](size_t i) -> uint32_t {
return (uint32_t(jk_get_16bit(i + 2)) << 16) | (uint32_t(jk_get_16bit(i + 0)) << 0);
};
ESP_LOGI(TAG, "Settings frame (%d bytes) received", data.size());
ESP_LOGVV(TAG, " %s", format_hex_pretty(&data.front(), 160).c_str());
ESP_LOGVV(TAG, " %s", format_hex_pretty(&data.front() + 160, data.size() - 160).c_str());
// JK02_24S response example:
//
// 0x55 0xAA 0xEB 0x90 0x01 0x4F 0x58 0x02 0x00 0x00 0x54 0x0B 0x00 0x00 0x80 0x0C 0x00 0x00 0xCC 0x10 0x00 0x00 0x68
// 0x10 0x00 0x00 0x0A 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0xF0 0x0A 0x00 0x00 0xA8 0x61 0x00 0x00 0x1E 0x00 0x00 0x00 0x3C 0x00 0x00 0x00 0xF0 0x49 0x02 0x00 0x2C 0x01 0x00
// 0x00 0x3C 0x00 0x00 0x00 0x3C 0x00 0x00 0x00 0xD0 0x07 0x00 0x00 0xBC 0x02 0x00 0x00 0x58 0x02 0x00 0x00 0xBC 0x02
// 0x00 0x00 0x58 0x02 0x00 0x00 0x38 0xFF 0xFF 0xFF 0x9C 0xFF 0xFF 0xFF 0x84 0x03 0x00 0x00 0xBC 0x02 0x00 0x00 0x0D
// 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x88 0x13 0x00 0x00 0xDC 0x05 0x00 0x00
// 0xE4 0x0C 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
// 0x40
// Byte Len Payload Content Coeff. Unit Example value
// 0 4 0x55 0xAA 0xEB 0x90 Header
// 4 1 0x01 Frame type
// 5 1 0x4F Frame counter
// 6 4 0x58 0x02 0x00 0x00 ** [JK-PB2A16S-20P v14] Smart sleep voltage
ESP_LOGV(TAG, " Smart sleep voltage: %f", (float) jk_get_32bit(6) * 0.001f);
this->publish_state_(this->smart_sleep_voltage_number_, (float) jk_get_32bit(6) * 0.001f);
// 10 4 0x54 0x0B 0x00 0x00 Cell UVP
ESP_LOGV(TAG, " Cell UVP: %f V", (float) jk_get_32bit(10) * 0.001f);
this->publish_state_(this->cell_voltage_undervoltage_protection_number_, (float) jk_get_32bit(10) * 0.001f);
// 14 4 0x80 0x0C 0x00 0x00 Cell UVP recovery
ESP_LOGV(TAG, " Cell UVPR: %f V", (float) jk_get_32bit(14) * 0.001f);
this->publish_state_(this->cell_voltage_undervoltage_recovery_number_, (float) jk_get_32bit(14) * 0.001f);
// 18 4 0xCC 0x10 0x00 0x00 Cell OVP
ESP_LOGV(TAG, " Cell OVP: %f V", (float) jk_get_32bit(18) * 0.001f);
this->publish_state_(this->cell_voltage_overvoltage_protection_number_, (float) jk_get_32bit(18) * 0.001f);
// 22 4 0x68 0x10 0x00 0x00 Cell OVP recovery
ESP_LOGV(TAG, " Cell OVPR: %f V", (float) jk_get_32bit(22) * 0.001f);
this->publish_state_(this->cell_voltage_overvoltage_recovery_number_, (float) jk_get_32bit(22) * 0.001f);
// 26 4 0x0A 0x00 0x00 0x00 Balance trigger voltage
ESP_LOGV(TAG, " Balance trigger voltage: %f V", (float) jk_get_32bit(26) * 0.001f);
this->publish_state_(this->balance_trigger_voltage_number_, (float) jk_get_32bit(26) * 0.001f);
// 30 4 0x00 0x00 0x00 0x00 ** [JK-PB2A16S-20P v14] SOC 100% voltage
ESP_LOGV(TAG, " SOC 100%% voltage: %f V", (float) jk_get_32bit(30) * 0.001f);
this->publish_state_(this->cell_soc100_voltage_number_, (float) jk_get_32bit(30) * 0.001f);
// 34 4 0x00 0x00 0x00 0x00 ** [JK-PB2A16S-20P v14] SOC 0% voltage
ESP_LOGV(TAG, " SOC 0%% voltage: %f V", (float) jk_get_32bit(34) * 0.001f);
this->publish_state_(this->cell_soc0_voltage_number_, (float) jk_get_32bit(34) * 0.001f);
// 38 4 0x00 0x00 0x00 0x00 ** [JK-PB2A16S-20P v14] Voltage cell request charge voltage
ESP_LOGV(TAG, " Voltage cell request charge voltage [RCV]: %f V", (float) jk_get_32bit(38) * 0.001f);
this->publish_state_(this->cell_request_charge_voltage_number_, (float) jk_get_32bit(38) * 0.001f);
// 42 4 0x00 0x00 0x00 0x00 ** [JK-PB2A16S-20P v14] Voltage cell request float voltage
ESP_LOGV(TAG, " Voltage cell request float voltage [RFV]: %f V", (float) jk_get_32bit(42) * 0.001f);
this->publish_state_(this->cell_request_float_voltage_number_, (float) jk_get_32bit(42) * 0.001f);
// 46 4 0xF0 0x0A 0x00 0x00 Power off voltage
ESP_LOGV(TAG, " Power off voltage: %f V", (float) jk_get_32bit(46) * 0.001f);
this->publish_state_(this->power_off_voltage_number_, (float) jk_get_32bit(46) * 0.001f);
// 50 4 0xA8 0x61 0x00 0x00 Max. charge current
ESP_LOGV(TAG, " Max. charge current: %f A", (float) jk_get_32bit(50) * 0.001f);
this->publish_state_(this->max_charge_current_number_, (float) jk_get_32bit(50) * 0.001f);
// 54 4 0x1E 0x00 0x00 0x00 Charge OCP delay
ESP_LOGV(TAG, " Charge OCP delay: %f s", (float) jk_get_32bit(54));
this->publish_state_(this->charge_overcurrent_protection_delay_number_, (float) jk_get_32bit(54));
// 58 4 0x3C 0x00 0x00 0x00 Charge OCP recovery time
ESP_LOGV(TAG, " Charge OCP recovery time: %f s", (float) jk_get_32bit(58));
this->publish_state_(this->charge_overcurrent_protection_recovery_time_number_, (float) jk_get_32bit(58));
// 62 4 0xF0 0x49 0x02 0x00 Max. discharge current
ESP_LOGV(TAG, " Max. discharge current: %f A", (float) jk_get_32bit(62) * 0.001f);
this->publish_state_(this->max_discharge_current_number_, (float) jk_get_32bit(62) * 0.001f);
// 66 4 0x2C 0x01 0x00 0x00 Discharge OCP delay
ESP_LOGV(TAG, " Discharge OCP delay: %f s", (float) jk_get_32bit(66));
this->publish_state_(this->discharge_overcurrent_protection_delay_number_, (float) jk_get_32bit(66));
// 70 4 0x3C 0x00 0x00 0x00 Discharge OCP recovery time
ESP_LOGV(TAG, " Discharge OCP recovery time: %f s", (float) jk_get_32bit(70));
this->publish_state_(this->discharge_overcurrent_protection_recovery_time_number_, (float) jk_get_32bit(70));
// 74 4 0x3C 0x00 0x00 0x00 SCPR time
ESP_LOGV(TAG, " Short circuit protection recovery time: %f s", (float) jk_get_32bit(74));
this->publish_state_(this->short_circuit_protection_recovery_time_number_, (float) jk_get_32bit(74));
// 78 4 0xD0 0x07 0x00 0x00 Max balance current
ESP_LOGV(TAG, " Max. balance current: %f A", (float) jk_get_32bit(78) * 0.001f);
this->publish_state_(this->max_balance_current_number_, (float) jk_get_32bit(78) * 0.001f);
// 82 4 0xBC 0x02 0x00 0x00 Charge OTP
ESP_LOGV(TAG, " Charge OTP: %f °C", (float) jk_get_32bit(82) * 0.1f);
this->publish_state_(this->charge_overtemperature_protection_number_, (float) jk_get_32bit(82) * 0.1f);
// 86 4 0x58 0x02 0x00 0x00 Charge OTP Recovery
ESP_LOGV(TAG, " Charge OTP recovery: %f °C", (float) jk_get_32bit(86) * 0.1f);
this->publish_state_(this->charge_overtemperature_protection_recovery_number_, (float) jk_get_32bit(86) * 0.1f);
// 90 4 0xBC 0x02 0x00 0x00 Discharge OTP
ESP_LOGV(TAG, " Discharge OTP: %f °C", (float) jk_get_32bit(90) * 0.1f);
this->publish_state_(this->discharge_overtemperature_protection_number_, (float) jk_get_32bit(90) * 0.1f);
// 94 4 0x58 0x02 0x00 0x00 Discharge OTP Recovery
ESP_LOGV(TAG, " Discharge OTP recovery: %f °C", (float) jk_get_32bit(94) * 0.1f);
this->publish_state_(this->discharge_overtemperature_protection_recovery_number_, (float) jk_get_32bit(94) * 0.1f);
// 98 4 0x38 0xFF 0xFF 0xFF Charge UTP
ESP_LOGV(TAG, " Charge UTP: %f °C", (float) ((int32_t) jk_get_32bit(98)) * 0.1f);
this->publish_state_(this->charge_undertemperature_protection_number_, (float) ((int32_t) jk_get_32bit(98)) * 0.1f);
// 102 4 0x9C 0xFF 0xFF 0xFF Charge UTP Recovery
ESP_LOGV(TAG, " Charge UTP recovery: %f °C", (float) ((int32_t) jk_get_32bit(102)) * 0.1f);
this->publish_state_(this->charge_undertemperature_protection_recovery_number_,
(float) ((int32_t) jk_get_32bit(102)) * 0.1f);
// 106 4 0x84 0x03 0x00 0x00 MOS OTP
ESP_LOGV(TAG, " Mosfet OTP: %f °C", (float) ((int32_t) jk_get_32bit(106)) * 0.1f);
this->publish_state_(this->power_tube_overtemperature_protection_number_,
(float) ((int32_t) jk_get_32bit(106)) * 0.1f);
// 110 4 0xBC 0x02 0x00 0x00 MOS OTP Recovery
ESP_LOGV(TAG, " Mosfet OTP recovery: %f °C", (float) ((int32_t) jk_get_32bit(110)) * 0.1f);
this->publish_state_(this->power_tube_overtemperature_protection_recovery_number_,
(float) ((int32_t) jk_get_32bit(110)) * 0.1f);
// 114 4 0x0D 0x00 0x00 0x00 Cell count
ESP_LOGV(TAG, " Cell count: %f", (float) jk_get_32bit(114));
this->publish_state_(this->cell_count_number_, (float) data[114]);
// 118 4 0x01 0x00 0x00 0x00 Charge switch
ESP_LOGV(TAG, " Charge switch: %s", ONOFF((bool) data[118]));
this->publish_state_(this->charging_switch_, (bool) data[118]);
// 122 4 0x01 0x00 0x00 0x00 Discharge switch
ESP_LOGV(TAG, " Discharge switch: %s", ONOFF((bool) data[122]));
this->publish_state_(this->discharging_switch_, (bool) data[122]);
// 126 4 0x01 0x00 0x00 0x00 Balancer switch
ESP_LOGV(TAG, " Balancer switch: %s", ONOFF((bool) data[126]));
this->publish_state_(this->balancer_switch_, (bool) data[126]);
// 130 4 0x88 0x13 0x00 0x00 Nominal battery capacity
ESP_LOGV(TAG, " Nominal battery capacity: %f Ah", (float) jk_get_32bit(130) * 0.001f);
this->publish_state_(this->total_battery_capacity_number_, (float) jk_get_32bit(130) * 0.001f);
// 134 4 0xDC 0x05 0x00 0x00 SCP delay
ESP_LOGV(TAG, " Short circuit protection delay: %f us", (float) jk_get_32bit(134) * 1.0f);
this->publish_state_(this->short_circuit_protection_delay_number_, (float) jk_get_32bit(134) * 1.0f);
// 138 4 0xE4 0x0C 0x00 0x00 Start balance voltage
ESP_LOGV(TAG, " Start balance voltage: %f V", (float) jk_get_32bit(138) * 0.001f);
this->publish_state_(this->balance_starting_voltage_number_, (float) jk_get_32bit(138) * 0.001f);
if (this->protocol_version_ == PROTOCOL_VERSION_JK02_24S) {
ESP_LOGD(TAG, " Unknown142: %f", (float) jk_get_32bit(142) * 0.001f);
ESP_LOGD(TAG, " Unknown146: %f", (float) jk_get_32bit(146) * 0.001f);
ESP_LOGD(TAG, " Unknown150: %f", (float) jk_get_32bit(150) * 0.001f);
ESP_LOGD(TAG, " Unknown154: %f", (float) jk_get_32bit(154) * 0.001f);
// 142 4 0x00 0x00 0x00 0x00
// 146 4 0x00 0x00 0x00 0x00
// 150 4 0x00 0x00 0x00 0x00
// 154 4 0x00 0x00 0x00 0x00
// 158 4 0x00 0x00 0x00 0x00 Con. wire resistance 1
// 162 4 0x00 0x00 0x00 0x00 Con. wire resistance 2
// 166 4 0x00 0x00 0x00 0x00 Con. wire resistance 3
// 170 4 0x00 0x00 0x00 0x00 Con. wire resistance 4
// 174 4 0x00 0x00 0x00 0x00 Con. wire resistance 5
// 178 4 0x00 0x00 0x00 0x00 Con. wire resistance 6
// 182 4 0x00 0x00 0x00 0x00 Con. wire resistance 7
// 186 4 0x00 0x00 0x00 0x00 Con. wire resistance 8
// 190 4 0x00 0x00 0x00 0x00 Con. wire resistance 9
// 194 4 0x00 0x00 0x00 0x00 Con. wire resistance 10
// 198 4 0x00 0x00 0x00 0x00 Con. wire resistance 11
// 202 4 0x00 0x00 0x00 0x00 Con. wire resistance 12
// 206 4 0x00 0x00 0x00 0x00 Con. wire resistance 13
// 210 4 0x00 0x00 0x00 0x00 Con. wire resistance 14
// 214 4 0x00 0x00 0x00 0x00 Con. wire resistance 15
// 218 4 0x00 0x00 0x00 0x00 Con. wire resistance 16
// 222 4 0x00 0x00 0x00 0x00 Con. wire resistance 17
// 226 4 0x00 0x00 0x00 0x00 Con. wire resistance 18
// 230 4 0x00 0x00 0x00 0x00 Con. wire resistance 19
// 234 4 0x00 0x00 0x00 0x00 Con. wire resistance 20
// 238 4 0x00 0x00 0x00 0x00 Con. wire resistance 21