Skip to content

Commit

Permalink
Add fake traffic (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
syssi authored Jul 18, 2022
1 parent 47480ad commit b5cf73f
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 12 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ jobs:
run: 'echo -e "wifi_ssid: ssid\nwifi_password: password\nmqtt_host: host\nmqtt_username: username\nmqtt_password: password" > secrets.yaml'
- run: |
esphome -s external_components_source components config esp32-example.yaml
esphome -s external_components_source components config esp32-example-debug.yaml
esphome -s external_components_source components config esp32-example-faker.yaml
- run: |
esphome -s external_components_source components config esp8266-example.yaml
esphome -s external_components_source components config esp8266-example-debug.yaml
esphome -s external_components_source components config esp8266-example-faker.yaml
esphome-compile:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -201,6 +205,6 @@ jobs:
shell: bash
run: 'echo -e "wifi_ssid: ssid\nwifi_password: password\nmqtt_host: host\nmqtt_username: username\nmqtt_password: password" > secrets.yaml'
- run: |
esphome -s external_components_source components compile esp32-example.yaml
esphome -s external_components_source components compile esp32-example-faker.yaml
- run: |
esphome -s external_components_source components compile esp8266-example.yaml
esphome -s external_components_source components compile esp8266-example-faker.yaml
24 changes: 22 additions & 2 deletions components/jbd_bms/jbd_bms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,26 @@ void JbdBms::loop() {
}
}

void JbdBms::update() { this->send_command_(JBD_CMD_READ, JBD_CMD_HWINFO); }
void JbdBms::update() {
this->send_command_(JBD_CMD_READ, JBD_CMD_HWINFO);

if (this->enable_fake_traffic_) {
// Start: 0xDD 0x03 0x00 0x1D
this->on_jbd_bms_data_(JBD_CMD_HWINFO, {0x06, 0x18, 0x00, 0x00, 0x01, 0xF2, 0x01, 0xF4, 0x00, 0x00, 0x2C,
0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x64, 0x03, 0x04,
0x03, 0x0B, 0x8B, 0x0B, 0x8A, 0x0B, 0x84, 0xFA, 0x8D, 0x77});
// End: 0xFA 0x8D 0x77

// Start: 0xDD 0x04 0x00 0x08
this->on_jbd_bms_data_(JBD_CMD_CELLINFO, {0x0F, 0x45, 0x0F, 0x3D, 0x0F, 0x37, 0x0F, 0x3D, 0xFE, 0xC6, 0x77});
// End: 0xFE 0xC6 0x77

// Start: 0xDD 0x05 0x00 0x19
this->on_jbd_bms_data_(JBD_CMD_HWVER, {0x4A, 0x42, 0x44, 0x2D, 0x53, 0x50, 0x30, 0x34, 0x53, 0x30, 0x33, 0x34, 0x2D,
0x4C, 0x34, 0x53, 0x2D, 0x32, 0x30, 0x30, 0x41, 0x2D, 0x42, 0x2D, 0x55});
// End: 0xFA, 0x08, 0x77
}
}

bool JbdBms::parse_jbd_bms_byte_(uint8_t byte) {
size_t at = this->rx_buffer_.size();
Expand Down Expand Up @@ -289,6 +308,8 @@ void JbdBms::on_hardware_version_data_(const std::vector<uint8_t> &data) {

void JbdBms::dump_config() { // NOLINT(google-readability-function-size,readability-function-size)
ESP_LOGCONFIG(TAG, "JbdBms:");
ESP_LOGCONFIG(TAG, " RX timeout: %d ms", this->rx_timeout_);
ESP_LOGCONFIG(TAG, " Fake traffic enabled: %s", YESNO(this->enable_fake_traffic_));

LOG_SENSOR("", "Total voltage", this->total_voltage_sensor_);
LOG_SENSOR("", "Battery strings", this->battery_strings_sensor_);
Expand Down Expand Up @@ -344,7 +365,6 @@ void JbdBms::dump_config() { // NOLINT(google-readability-function-size,readabi
LOG_SENSOR("", "Cell Voltage 30", this->cells_[29].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 31", this->cells_[30].cell_voltage_sensor_);
LOG_SENSOR("", "Cell Voltage 32", this->cells_[31].cell_voltage_sensor_);
ESP_LOGCONFIG(TAG, " RX timeout: %d ms", this->rx_timeout_);
}
float JbdBms::get_setup_priority() const {
// After UART bus
Expand Down
12 changes: 12 additions & 0 deletions esp32-example-debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<<: !include esp32-example.yaml

logger:
level: DEBUG

uart:
id: uart0
baud_rate: 9600
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
5 changes: 5 additions & 0 deletions esp32-example-faker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<: !include esp32-example-debug.yaml

jbd_bms:
rx_timeout: ${rx_timeout}
enable_fake_traffic: true
5 changes: 2 additions & 3 deletions esp32-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ substitutions:
external_components_source: github://syssi/esphome-jbd-bms@main
tx_pin: GPIO16
rx_pin: GPIO17
rx_timeout: 150ms

esphome:
name: ${name}
Expand Down Expand Up @@ -34,11 +35,9 @@ uart:
baud_rate: 9600
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH

jbd_bms:
rx_timeout: 150ms
rx_timeout: ${rx_timeout}

binary_sensor:
- platform: jbd_bms
Expand Down
12 changes: 12 additions & 0 deletions esp8266-example-debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<<: !include esp8266-example.yaml

logger:
level: DEBUG

uart:
id: uart0
baud_rate: 9600
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH
5 changes: 5 additions & 0 deletions esp8266-example-faker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<<: !include esp8266-example-debug.yaml

jbd_bms:
rx_timeout: ${rx_timeout}
enable_fake_traffic: true
5 changes: 2 additions & 3 deletions esp8266-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ substitutions:
external_components_source: github://syssi/esphome-jbd-bms@main
tx_pin: GPIO4
rx_pin: GPIO5
rx_timeout: 150ms

esphome:
name: ${name}
Expand Down Expand Up @@ -31,11 +32,9 @@ uart:
baud_rate: 9600
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
debug:
direction: BOTH

jbd_bms:
rx_timeout: 150ms
rx_timeout: ${rx_timeout}

binary_sensor:
- platform: jbd_bms
Expand Down
2 changes: 1 addition & 1 deletion test-esp32.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

esphome -s external_components_source components ${1:-run} esp32-example.yaml
esphome -s external_components_source components ${1:-run} ${2:-esp32-example-faker.yaml}
2 changes: 1 addition & 1 deletion test-esp8266.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

esphome -s external_components_source components ${1:-run} ${2:-esp8266-example.yaml}
esphome -s external_components_source components ${1:-run} ${2:-esp8266-example-faker.yaml}

0 comments on commit b5cf73f

Please sign in to comment.