esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 35 forks source link

CAN Bus send appears to corrupt some data #4262

Open PaulSturbo opened 1 year ago

PaulSturbo commented 1 year ago

The problem

Using ESP32dev and MCP2515 with logic level shifting. All working OK. Also ESP8266 & MCP2515 used to monitor CAN messages.

Getting serial feed from Victron Smart Shunt for battery monitoring. All values reported correctly in HA and ESP_LOGD messages.

Test rig running a 12v battery.

CAN data values for battery volts and amps monitored on the CAN bus are incorrect at 12.5volts, they are doubled to give 25v. If I multiply the values by 4, 50v, then the values are exactly 4 times what they should be.

If I reverse the high and low bytes the values are correct at 12.5v, but that would give the wrong values at the recieving end,

As this is due to be used on a 48v system, they should be correct for the intended use,

Which version of ESPHome has the issue?

2023.2.4

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2023.2.5

What platform are you using?

ESP32

Board

Nodemcu

Component causing the issue

Can

Example YAML snippet

globals:
  - id: soh
    type: float
    initial_value: '100'
  - id: soc
    type: float
    initial_value: '100'
  - id: bv
    type: uint16_t
  - id: bc
    type: int16_t

sensor:
  - platform: victron_smart_shunt
    battery_voltage:
      id: bv_vss
      name: Batt Volts Victron
      internal: FALSE
    battery_current:
      id: bc_vss
      name: Batt Amps Victron
      internal: FALSE
    state_of_charge:
      id: soc_vss
      name: SoC Victron
      internal: FALSE  

spi:
  id: McpSpi
  clk_pin: GPIO18
  mosi_pin: GPIO23
  miso_pin: GPIO19

canbus:
  - platform: mcp2515
    id: my_mcp2515
    spi_id: McpSpi
    cs_pin: GPIO05
    can_id: 0x355
    use_extended_id: false
    bit_rate: 500kbps
    clock: 16Mhz

time:
  - platform: sntp
    on_time:
      - seconds: /1
        then:
          - canbus.send: 
              can_id: 0x355
              data: !lambda
                id(soc) = id(soc_vss).state;
                return {uint8_t(id(soc)), 0x00, uint8_t(id(soh)), 0x00};
          - delay: 20ms
          - canbus.send:
              can_id: 0x356
              data: !lambda
                uint8_t bv_low;
                uint8_t bv_high;
                uint8_t bc_low;
                uint8_t bc_high;
                id(bv) = id(bv_vss).state * 100;
                ESP_LOGD("custom", "bv = %d", id(bv));
                bv_low = id(bv) & 0xff;
                ESP_LOGD("custom", "bv_low = %d", bv_low);
                bv_high = (id(bv)>>8) & 0xff;
                ESP_LOGD("custom", "bv_high = %d", bv_high);
                id(bc) = (id(bc_vss).state) * 10;
                bc_low = id(bc) & 0xff;
                bc_high = (id(bc)>>8) & 0xff;
                return {bv_low, bv_high, bc_low, bc_high, 0x14, 0x00,};

external_components:
  - source: github://KinDR007/VictronSmartShunt-ESPHOME@main

uart:
  id: the_uart
  rx_pin: GPIO16
  baud_rate: 19200
  stop_bits: 1
  data_bits: 8
  parity: NONE
  rx_buffer_size: 256

victron_smart_shunt:
  uart_id: the_uart

Anything in the logs that might be useful for us?

Relevant part of the ouput log: [17:06:59][D][canbus:033]: send extended id=0x355 rtr=FALSE size=4 [17:06:59][D][custom:088]: bv = 1242 [17:06:59][D][custom:088]: bv_low = 218 [17:06:59][D][custom:088]: bv_high = 4 [17:06:59][D][canbus:033]: send extended id=0x356 rtr=FALSE size=6

Output from the ESP8266 monitor: [17:09:24][D][canbus:069]: received can message (#1) std can_id=0x355 size=4 [17:09:24][D][custom:051]: can id 0x355 recieved 5D.00.64.00 [17:09:24][D][canbus:069]: received can message (#1) std can_id=0x356 size=6 [17:09:24][D][custom:056]: can id 0x356 recieved B4.09.FF.FE.28.00 (6) [17:09:25][D][canbus:069]: received can message (#1) std can_id=0x355 size=4

Message 0x355 is correct. Message 0x356: High and low bytes are reversed, Values are doubled.

Additional information

Values are correct if battery voltage (bv) value halved or quadrupled. Tests at various settings showed it could be when the bv_high value is 0x04.

PaulSturbo commented 1 year ago

I can confirm that the issue arises when the high byte of the first word is 0x04.

I charged the battery to over 12.8 volts, 1280/0x500 in the CAN string and the numbers were correct.

PaulSturbo commented 1 year ago

Not sure if anyone listening but update:

Data on the bus is corrupted if the value of the first byte in the string is 8 bits long. All other bytes in the string are shifted by 1 bit.

Tried everything in my power to sort this out, hardware and software, changing physical components and umpteen software changes.

Going to ditch ESPHome if I can't get some help.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.