ESPHome Custom Sensor - nrf24l01 shunt

Hey all,

Trying to create a custom sensor to transition a sensor from arduino to esphome.
Basically it uses RF to communicate with a battery shunt to gather detail about how much power is being used etc (works in arduino).

Guessing i’m not using the custom sensor properly (e.g. i’m using the update function as if it were the arduino loop function, but at 1000ms loop).

Having issues with the nodemcu 8266 failing to boot, so having trouble diagnosing any further.

I’ve removed all the weird and wonderful radio stuff from the update function, and its still resetting. Seems its not liking the configure radio function.

> [18:45:38][I][logger:166]: Log initialized
> [18:45:38][C][ota:366]: There have been 5 suspected unsuccessful boot attempts.
> [18:45:38][I][app:029]: Running through setup()...
> [18:45:47]
> [18:45:47] ets Jan  8 2013,rst cause:4, boot mode:(3,6)
> [18:45:47]
> [18:45:47]wdt reset
> [18:45:47]load 0x4010f000, len 3584, room 16 
> [18:45:47]tail 0
> [18:45:47]chksum 0xb0
> [18:45:47]csum 0xb0
> [18:45:47]v2843a5ac
> [18:45:47]~ld

my_custom_sensor.h

#include "esphome.h"
#include "RF24.h"
  const int cePin = 2;
  const int csnPin = 8;
  const int mosiPin = 6;
  const int misoPin = 7;
  const int sckPin = 5;
  const int address = 10;
  const char frequency = 'A';
  float battAmpHours = -2.0;
  bool radioNumber = 1;
  int lpCnt = 0;
  RF24 radio(cePin, csnPin);
  uint64_t pipes[2] = { 0x8967452300LL, 0x8967452300LL };
  uint32_t configTimer =  millis();
  unsigned long previousMillis = 0;

class MyCustomSensor : public PollingComponent, public Sensor {
 public:
  // constructor
  Sensor *cabin_shunt_amp_hours_used = new Sensor();
  Sensor *cabin_shunt_amps = new Sensor();
  Sensor *cabin_shunt_watts = new Sensor();
  Sensor *cabin_shunt_voltage = new Sensor();
  Sensor *cabin_shunt_temp = new Sensor();
  Sensor *cabin_shunt_soc = new Sensor();
  Sensor *cabin_shunt_watt_hours_used = new Sensor();
  Sensor *cabin_shunt_amp_hours = new Sensor();

  MyCustomSensor() : PollingComponent(1000) {}

  float get_setup_priority() const override { return esphome::setup_priority::BUS; }

  void setup() override {
    // This will be called by App.setup()
  
  configureRadio();
  }
  
  void update() override {

  }

  void configureRadio() {
  radio.begin();
  radio.setAutoAck(false);
  radio.setDataRate(RF24_2MBPS);
  for (int i = 0; i < 6; i++)
    radio.closeReadingPipe(i);
  // Open a writing and reading pipe
  pipes[0] |= (uint64_t)address;
  pipes[1] |= (uint64_t)address;
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(0, pipes[1]);
  radio.setAutoAck(0, true);
  radio.setChannel((frequency - 'A') * 4);
  radio.setPALevel(RF24_PA_MAX);
  radio.printDetails();  
  }
  
};

ESP home YAML

esphome:
  name: cabin_shunt
  platform: ESP8266
  board: esp01_1m
  includes:
    - my_custom_sensor.h
  libraries:
    - tmrh20/RF24 @ 1.3.11
    
wifi:
  ssid: "xxx"
  password: "xxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "xxx"
    password: "xxx"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "xxx"

ota:
  password: "xxx"

sensor:
- platform: custom
  lambda: |-
    auto my_sensor = new MyCustomSensor();
    App.register_component(my_sensor);
    return {my_sensor->cabin_shunt_amp_hours_used, my_sensor->cabin_shunt_amps, my_sensor->cabin_shunt_watts, my_sensor->cabin_shunt_voltage, my_sensor->cabin_shunt_temp, my_sensor->cabin_shunt_soc, my_sensor->cabin_shunt_watt_hours_used, my_sensor->cabin_shunt_amp_hours};


  sensors:
  - name: "Cabin Amp Hours Used"
    accuracy_decimals: 2
  - name: "Cabin Current Amps"
    accuracy_decimals: 2
  - name: "Cabin Current Watts"
    unit_of_measurement: watts  
    accuracy_decimals: 2
  - name: "Cabin Shunt Voltage"
    unit_of_measurement: volts
    accuracy_decimals: 2
  - name: "Cabin Battery Temperature"
    unit_of_measurement: °C
    accuracy_decimals: 2
  - name: "Cabin State of Charge"
    unit_of_measurement: percent
    accuracy_decimals: 2
  - name: "Cabin Watt Hours Used"
    accuracy_decimals: 2
  - name: "Cabin Total Amp Hours"
    accuracy_decimals: 2

bump? …

Did you ever get anywhere with this?

Unfortunately not. I ended up coding it manually and pumping over to HA with MQTT