Integrate humidity sensor in ESPHome

Hi there,
I have a little project where I try to collect himidity data from a custom made sensor. The heart of this sensor is an 74hc14d and it’s a capacitive humidity sensor. The controller is a Wemos D1 mini and I already have a working arduino based script which I now try to port to the yml.

I’ve tried several settings, the initial one was the following:

  - platform: pulse_counter
    name: "Feuchtigkeitssensor"
    update_interval: 600s
    pin: D8
    filters:
      - median:
          window_size: 11
          send_every: 4
          send_first_at: 1
    accuracy_decimals: 0

But I also tried this simple one:

  - platform: pulse_counter
    pin: D5
    name: "Feuchtigkeitssensor"
    accuracy_decimals: 0

If I use one of these or other constalations it’s seems like the pulse counter is blocking the Wemos when I plug in the sensor. Wifi doesn’t connect at all so I used a cable to connect to debug. When I connect the sensor there is no output written to the console. If I disconnect it, the Wemos seems to continue and prints out the collected values. Some times it gets soft resetted by watch dog with cause 4.

This is my working arduino test example:


static const int HUMIDITY_MEASURING_INTERVALL = 9;
static const int HUMIDITY_MEASURING_TIME = 100;

void ICACHE_RAM_ATTR pulseCounter();
volatile int pulse;

void pulseCounter() {
  pulse++;
}

unsigned long interruptForCountingPulse(int interruptPin) {
  unsigned long dataArray[HUMIDITY_MEASURING_INTERVALL];

  for (int i = 0; i < HUMIDITY_MEASURING_INTERVALL; i++) {
    pulse = 0;
    attachInterrupt(interruptPin, pulseCounter, RISING);
    delay(HUMIDITY_MEASURING_TIME);
    detachInterrupt(interruptPin);

    dataArray[i] = pulse;

    delay(100);
  }

  return median(dataArray, HUMIDITY_MEASURING_INTERVALL);
}

unsigned long median(unsigned long *values, size_t arraySize) {
  unsigned long tmp = 0;     // set to 0, make the compiler happy
  const size_t relVal = 2;   // +- 2 Werte + 1 für die Mittelwertberechnung

  for (size_t i = 0; i < arraySize - 2; i++) {
    for (size_t j = arraySize - 1; j > i; j--) {
      if (values[j] < values[j - 1]) {
        tmp = values[j];
        values[j] = values[j - 1];
        values[j - 1] = tmp;
      }
    }
  }

  tmp = 0;
  for (size_t i = arraySize / 2 - relVal; i < arraySize / 2 + relVal + 1;
       tmp += values[i++]) {
  }

  return tmp / (relVal * 2 + 1) * 1000 / HUMIDITY_MEASURING_TIME;
}

void setup() {
  Serial.begin(115200);
  Serial.println();
}

void loop() {
  unsigned long humidity = interruptForCountingPulse(14);
  Serial.println(humidity);

  delay(2000);
}

Do you have any idea of how I could port my Arduino code to the yml configuration or should I copy this into custom .h file and include it somehow?

BR
i7i5

The 74hc14d is not a humidity sensor is it?

You’re right, it’s not. But it’s part of a self made sensor: [V] Bausatz für Giess-o-mat Sensor - Mikrocontroller.net. I know the page is in german, but that’s the one I bought back then.

How long are these pulses?

Here are the measurements I did in 2017. Sadly as a new user I can only put one embedded media item in a post. So I merged them:

1: The sensor in air, 4µs high, ~5µs low = ~9µs.
2: The sensor in water, 6µs high, ~8 low = ~14µs.

For the counter not to miss any pulses, the pulse duration should be longer than one APB_CLK cycle (12.5 ns). The pulses are sampled on the edges of the APB_CLK clock and may be missed, if fall between the edges. This applies to counter operation with or without a filer.

From the docs.

Edit: oh that is nanoseconds, 9 micoseconds should be fine!

Here is the log after flashing the following try:

esphome:
  name: giesomat_mk3
  platform: ESP8266
  board: d1_mini
  includes:
    - customLibs/Max44009Sensor.h
  libraries:
    # https://platformio.org/lib/show/1349/Max44009/examples
    - "robtillaart/Max44009 @ ^0.4.3"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: !secret giesomat_fallback_ssid
    password: !secret giesomat_fallback_password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret giesomat_api_password

ota:
  password: !secret giesomat_ota_password


switch:
  - platform: restart
    name: "REBOOT"

  - platform: gpio
    name: "Pumpe 1"
    pin:
      mcp23017: mcp23017_hub
      number: 0
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "Pumpe 2"
    pin:
      mcp23017: mcp23017_hub
      number: 1
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "Pumpe 3"
    pin:
      mcp23017: mcp23017_hub
      number: 2
      mode: OUTPUT
      inverted: true

  - platform: gpio
    name: "Pumpe 4"
    pin:
      mcp23017: mcp23017_hub
      number: 3
      mode: OUTPUT
      inverted: true

display:
  - platform: ssd1306_i2c
    model: "SSD1306 128x64"
    address: 0x3C
    lambda: |-
#      // Draw a line from [0,0] to [100,50]
#      it.line(0, 0, 100, 50);

time:
  - platform: homeassistant
    id: ha_time
    timezone: Europe/Berlin

sensor:
  - platform: bme280
    temperature:
      name: "Temperatur"
      oversampling: 1x
    pressure:
      name: "Luftdruck"
    humidity:
      name: "Feuchtigkeit"
    address: 0x76
    update_interval: 60s
  
  - platform: custom
    lambda: |-
      auto lux_sensor = new Max44009Sensor();
      App.register_component(lux_sensor);
      return {lux_sensor};
    sensors:
      name: "Umgebungslicht"
      id: max44009
      #requires mqtt configuration
      #state_topic: ${device_name}/feeds/light
      unit_of_measurement: 'lux'
      accuracy_decimals: 1

  - platform: pulse_counter
    pin: D5
    name: "Feuchtigkeitssensor"
#    update_interval: 600s
    accuracy_decimals: 0
#    count_mode:
#      rising_edge: DISABLE
#      falling_edge: INCREMENT
#    unit_of_measurement: "%"
#    filters:
#      - median:
#          window_size: 7
#          send_every: 4
#          send_first_at: 1
#      - calibrate_linear:
#        - 1.25 -> 100.00
#        - 2.8 -> 0.00
#      - lambda: |
#          if (x < 0) return 0; 
#          else if (x > 100) return 100;
#          else return (x);
    
mcp23017:
  - id: 'mcp23017_hub'
    address: 0x20
    
i2c:
  sda: D2
  scl: D1
  scan: true

Done! Flashing is complete!

Showing logs:
[11:49:05][I][logger:166]: Log initialized
[11:49:05][C][ota:366]: There have been 7 suspected unsuccessful boot attempts.
[11:49:05][I][app:029]: Running through setup()...
[11:49:05][C][mcp23017:010]: Setting up MCP23017...
[11:49:05][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 1'...
[11:49:05][D][switch:025]: 'Pumpe 1' Turning OFF.
[11:49:05][D][switch:045]: 'Pumpe 1': Sending state OFF
[11:49:05][D][switch:025]: 'Pumpe 1' Turning OFF.
[11:49:05][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 2'...
[11:49:05][D][switch:025]: 'Pumpe 2' Turning OFF.
[11:49:05][D][switch:045]: 'Pumpe 2': Sending state OFF
[11:49:05][D][switch:025]: 'Pumpe 2' Turning OFF.
[11:49:05][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 3'...
[11:49:05][D][switch:025]: 'Pumpe 3' Turning OFF.
[11:49:05][D][switch:045]: 'Pumpe 3': Sending state OFF
[11:49:05][D][switch:025]: 'Pumpe 3' Turning OFF.
[11:49:05][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 4'...
[11:49:05][D][switch:025]: 'Pumpe 4' Turning OFF.
[11:49:05][D][switch:045]: 'Pumpe 4': Sending state OFF
[11:49:05][D][switch:025]: 'Pumpe 4' Turning OFF.
[11:49:05][C][bme280.sensor:081]: Setting up BME280...
[11:49:05][C][pulse_counter:139]: Setting up pulse counter 'Feuchtigkeitssensor'...
[11:49:06][C][ssd1306_i2c:010]: Setting up I2C SSD1306...
[11:49:30]
[11:49:30]--------------- CUT HERE FOR EXCEPTION DECODER ---------------
[11:49:30]
[11:49:30]Soft WDT reset
[11:49:30]
[11:49:30]>>>stack>>>
[11:49:30]
[11:49:30]ctx: cont
[11:49:30]sp: 3ffffb40 end: 3fffffc0 offset: 01a0
[11:49:30]3ffffce0:  3fff130c 00000001 3fff130c 4021f8f8  
[11:49:30]3ffffcf0:  3fff130c 00000000 00000006 4021f9bc  
[11:49:30]3ffffd00:  00000001 3fff102c 3fff130c 4021fabe  
[11:49:30]3ffffd10:  3fff1023 0000003c 00000002 3fff1023  
[11:49:30]3ffffd20:  00000000 00ffffff 00000000 00000010  
[11:49:30]3ffffd30:  3ffffdb0 0000003c 3fff1021 4021fd3c  
[11:49:30]3ffffd40:  3ffffdb0 3fff2a14 00000010 402182d8  
[11:49:30]3ffffd50:  3ffffdc0 3fff2a14 0000003c 402070d8  
[11:49:30]3ffffd60:  3ffffdb0 3fff2a14 0000003c 402072ba  
[11:49:30]3ffffd70:  3ffffd40 3fff2a14 0000003c 402072ba  
[11:49:30]3ffffd80:  3ffffd00 3fff2a14 0000003c 00000030  
[11:49:30]3ffffd90:  3ffffdc1 00000010 3fff2c6c 40207428  
[11:49:30]3ffffda0:  3fff2c6c 00000000 3fff2c6c 402097f3  
[11:49:30]3ffffdb0:  00000000 00000000 00000000 00000000  
[11:49:30]3ffffdc0:  3fff2c2e 0000000c 3fff2c7c 4020744a  
[11:49:30]3ffffdd0:  3fff2c6c 0000003c 00000400 00000030  
[11:49:30]3ffffde0:  3fff2c6c 402096f8 3fff2c6c 40209220  
[11:49:30]3ffffdf0:  3fff2c6c 402096f8 3fff2c6c 40209427  
[11:49:30]3ffffe00:  00000000 0000000c 3fff2c6c 4020988e  
[11:49:30]3ffffe10:  00000000 0000ea60 3fff2c6c 4020e30c  
[11:49:30]3ffffe20:  00000000 3fff3464 3fff0e34 4020f56d  
[11:49:30]3ffffe30:  00000000 00000000 3fff2944 40209e7a  
[11:49:30]3ffffe40:  3fff341c 0000000c 3fff0e34 402221f9  
[11:49:30]3ffffe50:  3fff300c 0000000b 3fff0e34 4020e099  
[11:49:30]3ffffe60:  3fff341c 00000012 3fff3400 3fff326c  
[11:49:30]3ffffe70:  3fff337c 3fffff6c 3fff337c 4022e4fe  
[11:49:30]3ffffe80:  3fffff01 3fff1570 3fff337c 3fff326c  
[11:49:30]3ffffe90:  3fff337c 3fff0eb8 00000000 402107e0  
[11:49:30]3ffffea0:  3fff2850 00000000 00000000 3fff2880  
[11:49:30]3ffffeb0:  00000000 00000000 00000000 40223d88  
[11:49:30]3ffffec0:  00000000 40223d88 00000000 40223d88  
[11:49:30]3ffffed0:  00000000 40223d88 00000000 40223d88  
[11:49:30]3ffffee0:  00000000 00000000 3fff27a8 00000000  
[11:49:30]3ffffef0:  00000000 3fff27d0 00000000 00000000  
[11:49:30]3fffff00:  00000000 40223d88 00000000 40223d88  
[11:49:30]3fffff10:  00000000 40223d88 00000000 40223d88  
[11:49:30]3fffff20:  00000000 40223d88 00000000 00000000  
[11:49:30]3fffff30:  3fff337c feefeffe 4020f8a8 40222320  
[11:49:30]3fffff40:  3fff326c 3fff2e68 3fff2e68 3fff3114  
[11:49:30]3fffff50:  3fff2d54 3fff301c 3fff2c04 3fff2b9c  
[11:49:30]3fffff60:  3fff2b34 3fff2a84 3fff2a34 3fff3400  
[11:49:30]3fffff70:  feefeffe feefeffe feefeffe feefeffe  
[11:49:30]3fffff80:  3fff2c04 3fff31c4 3fff2e64 00000005  
[11:49:30]3fffff90:  feefeffe feefeffe feefeffe 3fff12c8  
[11:49:30]3fffffa0:  3fffdad0 00000000 3fff1288 4021f214  
[11:49:30]3fffffb0:  feefeffe feefeffe 3ffe8588 401006b9  
[11:49:30]<<<stack<<<
[11:49:30]
[11:49:30]--------------- CUT HERE FOR EXCEPTION DECODER ---------------
[11:49:30]
[11:49:30] ets Jan  8 2013,rst cause:2, boot mode:(3,6)
[11:49:30]
[11:49:30]load 0x4010f000, len 3584, room 16 
[11:49:31]tail 0
[11:49:31]chksum 0xb0
[11:49:31]csum 0xb0
[11:49:31]v2843a5ac
[11:49:31]~ld

For the moment comment out the display section, which is where it is failing.

Hm, sending the measured values seems to work but than it crashed o.O Without the humidity sensors all other configured sensors are working flawless :confused:

Done! Flashing is complete!

Showing logs:
[21:41:44][I][logger:166]: Log initialized
[21:41:44][C][ota:366]: There have been 2 suspected unsuccessful boot attempts.
[21:41:44][I][app:029]: Running through setup()...
[21:41:44][C][mcp23017:010]: Setting up MCP23017...
[21:41:44][W][i2c:076]: Unknown transmit error 4 for address 0x20
[21:41:44][E][component:092]: Component was marked as failed.
[21:41:44][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 1'...
[21:41:44][D][switch:025]: 'Pumpe 1' Turning OFF.
[21:41:44][D][switch:045]: 'Pumpe 1': Sending state OFF
[21:41:44][D][switch:025]: 'Pumpe 1' Turning OFF.
[21:41:44][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 2'...
[21:41:44][D][switch:025]: 'Pumpe 2' Turning OFF.
[21:41:44][D][switch:045]: 'Pumpe 2': Sending state OFF
[21:41:44][D][switch:025]: 'Pumpe 2' Turning OFF.
[21:41:44][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 3'...
[21:41:44][D][switch:025]: 'Pumpe 3' Turning OFF.
[21:41:44][D][switch:045]: 'Pumpe 3': Sending state OFF
[21:41:44][D][switch:025]: 'Pumpe 3' Turning OFF.
[21:41:44][C][switch.gpio:011]: Setting up GPIO Switch 'Pumpe 4'...
[21:41:44][D][switch:025]: 'Pumpe 4' Turning OFF.
[21:41:44][D][switch:045]: 'Pumpe 4': Sending state OFF
[21:41:44][D][switch:025]: 'Pumpe 4' Turning OFF.
[21:41:44][C][bme280.sensor:081]: Setting up BME280...
[21:41:44][W][i2c:076]: Unknown transmit error 4 for address 0x76
[21:41:44][E][component:092]: Component was marked as failed.
[21:41:44][C][pulse_counter:139]: Setting up pulse counter 'Feuchtigkeitssensor'...
[21:41:45][C][wifi:033]: Setting up WiFi...
[21:41:46][D][wifi:324]: Starting scan...
[21:41:47][D][sensor:092]: 'Umgebungslicht': Sending state -30.00000 lux with 1 decimals of accuracy
[21:41:47][D][pulse_counter:159]: 'Feuchtigkeitssensor': Retrieved counter: 3.00 pulses/min
[21:41:48][D][sensor:092]: 'Feuchtigkeitssensor': Sending state 3.00000 pulses/min with 0 decimals of accuracy
[21:41:51]
[21:41:51]--------------- CUT HERE FOR EXCEPTION DECODER ---------------
[21:41:51]
[21:41:51]Soft WDT reset
[21:41:51]
[21:41:51]>>>stack>>>
[21:41:51]
[21:41:51]ctx: sys
[21:41:51]sp: 3fffed60 end: 3fffffb0 offset: 01a0
[21:41:51]3fffef00:  00000000 000000fb 00000000 0000000c  
[21:41:51]3fffef10:  00000145 3ffec85e 00000018 40247535  
[21:41:51]3fffef20:  3ffeed30 3ffece8c 3fffdcc0 3ffeb340  
[21:41:51]3fffef30:  00000080 3ffeed30 3fff1a0c 3ffe8588  
[21:41:51]3fffef40:  40246e87 3fffdab0 00000000 4021e66c  
[21:41:51]3fffef50:  3ffeb340 40000f49 3fffdab0 40000f49  
[21:41:51]3fffef60:  40000e19 00000005 00069728 00000000  
[21:41:51]3fffef70:  00000000 ffffffff ffffffff 401052e1  
[21:41:51]3fffef80:  401052e7 00069728 00000000 65746147  
[21:41:51]3fffef90:  4010000d 76726553 20656369 76616e55  
[21:41:51]3fffefa0:  40245d64 3fffef3c 40245d15 3ffffe38  
[21:41:51]3fffefb0:  3fffffc0 00000000 00000000 feefeffe  
[21:41:51]3fffefc0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3fffefd0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3fffefe0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3fffeff0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff000:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff010:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff020:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff030:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff040:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff050:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff060:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff070:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff080:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff090:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff0a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff0b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff0c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff0d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff0e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff0f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff100:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff110:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff120:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff130:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff140:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff150:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff160:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff170:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff180:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff190:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff1a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff1b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff1c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff1d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff1e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff1f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff200:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff210:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff220:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff230:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff240:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff250:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff260:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff270:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff280:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff290:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff2a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff2b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff2c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff2d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff2e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff2f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff300:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff310:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff320:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff330:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff340:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff350:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff360:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff370:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff380:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff390:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff3a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff3b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff3c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff3d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff3e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff3f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff400:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff410:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff420:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff430:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff440:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff450:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff460:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff470:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff480:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff490:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff4a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff4b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff4c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff4d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff4e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff4f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff500:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff510:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff520:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff530:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff540:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff550:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff560:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff570:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff580:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff590:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff5a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff5b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff5c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff5d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff5e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff5f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff600:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff610:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff620:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff630:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff640:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff650:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff660:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff670:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff680:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff690:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff6a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff6b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff6c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff6d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff6e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff6f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff700:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff710:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff720:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff730:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff740:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff750:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff760:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff770:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff780:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff790:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff7a0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff7b0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff7c0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff7d0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff7e0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff7f0:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff800:  feefeffe feefeffe feefeffe feefeffe  
[21:41:51]3ffff810:  feefeffe feefeffe feefeffe 401001fb  
[21:41:51]3ffff820:  feefeffe feefeffe 00000000 4010735a  
[21:41:51]3ffff830:  40100cb5 feefeffe feefeffe 401001fb  
[21:41:51]3ffff840:  c0030025 feefeffe 00000000 401001fb  
[21:41:51]3ffff850:  40100cb5 feefeffe 00000000 401001fb  
[21:41:51]3ffff860:  40100cb5 40100bdc 00000000 4010735a  
[21:41:51]3ffff870:  40100cb5 00000030 00000000 401001fb  
[21:41:51]3ffff880:  c0030025 40100bdc 00000000 4010735a  
[21:41:51]3ffff890:  40100cb5 40100bdc 00000000 401001fb  
[21:41:51]3ffff8a0:  c0030025 40100bdc 00000000 4010735a  
[21:41:51]3ffff8b0:  40100cb5 00000030 00000020 40100c9d  
[21:41:51]3ffff8c0:  c0030025 40100bdc 3fffc258 4000050c  
[21:41:51]3ffff8d0:  4000c8a9 00000030 00000002 401001fb  
[21:41:51]3ffff8e0:  3fffc200 40100bdc 00000000 4010735a  
[21:41:51]3ffff8f0:  40100cb5 00000030 00000000 40100c9d  
[21:41:51]3ffff900:  c0030025 00000001 00000000 40100c9d  
[21:41:51]3ffff910:  c0030025 7ff00000 00000000 00000022  
[21:41:51]3ffff920:  3fffc200 40100bdc 3fffc258 401001fb  
[21:41:51]3ffff930:  4000e268 00000030 00000000 4010735a  
[21:41:51]3ffff940:  40100cb5 00000000 00000000 40100c9d  
[21:41:51]3ffff950:  c0030025 00000000 00000000 401001fb  
[21:41:51]3ffff960:  c0030025 4025c361 00000000 4010735a  
[21:41:52]3ffff970:  40100cb5 40100bdc 3fffc258 40100c9d  
[21:41:52]3ffff980:  c0030025 00000030 0000001f 401001fb  
[21:41:52]3ffff990:  40000e86 00000023 00000000 4010735a  
[21:41:52]3ffff9a0:  40100cb5 40100bdc 3fffc258 40100c9d  
[21:41:52]3ffff9b0:  c0030025 00000030 0000001f 401001fb  
[21:41:52]3ffff9c0:  40101872 00000000 00000000 4010735a  
[21:41:52]3ffff9d0:  40100cb5 40100bdc 3fffc258 40100c9d  
[21:41:52]3ffff9e0:  c0030025 00000030 0000001f fffffffe  
[21:41:52]3ffff9f0:  3ffeb9c0 00000000 00000003 00000022  
[21:41:52]3ffffa00:  3fffc200 40100bdc 3fffc258 4000050c  
[21:41:52]3ffffa10:  4000050c 00000030 0000001f fffffffe  
[21:41:52]3ffffa20:  4000050c 00000000 00000003 00000005  
[21:41:52]3ffffa30:  2c9f0300 3ff20a00 3fffdbc0 3ffeb350  
[21:41:52]3ffffa40:  0000000c 4025c361 00000003 3fffc278  
[21:41:52]3ffffa50:  40102e84 3fffc200 00000022 00000030  
[21:41:52]3ffffa60:  00000015 00000000 00000000 00000015  
[21:41:52]3ffffa70:  0000000a 4025c361 00000001 3ffffbe0  
[21:41:52]3ffffa80:  0000000c 00000000 00000020 40100738  
[21:41:52]3ffffa90:  0000a000 3ffffb83 0000000c 40101f28  
[21:41:52]3ffffaa0:  3ffeb2fc 40101872 04000000 401001fb  
[21:41:52]3ffffab0:  00000001 00000004 00000000 4010735a  
[21:41:52]3ffffac0:  40100cb5 00000000 00000000 40100c9d  
[21:41:52]3ffffad0:  c0030025 40100bdc 00000000 40100c9d  
[21:41:52]3ffffae0:  c0030025 00000030 00000018 401001fb  
[21:41:52]3ffffaf0:  3fffc200 40100bdc 00000000 4010735a  
[21:41:52]3ffffb00:  40100cb5 00000030 00000000 40100c9d  
[21:41:52]3ffffb10:  c0030025 00000000 6000001c 3ffea851  
[21:41:52]3ffffb20:  00000039 3ffe9dba 000000a8 401001fb  
[21:41:52]3ffffb30:  3fffc200 40100bdc 00000000 4010735a  
[21:41:52]3ffffb40:  40100cb5 00000030 00000000 40100c9d  
[21:41:52]3ffffb50:  c0030025 00000002 6000001c 3ffea851  
[21:41:52]3ffffb60:  00000039 3ffe9dba 000000a8 00000022  
[21:41:52]3ffffb70:  3fffc200 40100bdc 3fffc258 4000050c  
[21:41:52]3ffffb80:  40207563 00000030 00000000 401001fb  
[21:41:52]3ffffb90:  402076ac 00000071 00000000 4010735a  
[21:41:52]3ffffba0:  40100cb5 00000000 000000a8 401001fb  
[21:41:52]3ffffbb0:  c0030025 4025c361 00000000 4010735a  
[21:41:52]3ffffbc0:  40100cb5 00000005 3ffe99ab 40100c9d  
[21:41:52]3ffffbd0:  c0030025 40100bdc 3fffc258 401001fb  
[21:41:52]3ffffbe0:  4000dd09 00000030 00000000 4010735a  
[21:41:52]3ffffbf0:  40100cb5 40100bdc 3fffc258 40100c9d  
[21:41:52]3ffffc00:  c0030025 00000030 00000010 401001fb  
[21:41:52]3ffffc10:  00000000 00000000 00000000 4010735a  
[21:41:52]3ffffc20:  40100cb5 40100bdc 3fffc258 40100c9d  
[21:41:52]3ffffc30:  c0030025 00000030 00000010 ffffffff  
[21:41:52]3ffffc40:  40100abc b22d0e56 00000000 00000022  
[21:41:52]3ffffc50:  3fffc200 40100bdc 3fffc258 4000050c  
[21:41:52]3ffffc60:  4020b298 00000030 00000010 ffffffff  
[21:41:52]3ffffc70:  4020b28c 0000052e 00007530 00000000  
[21:41:52]3ffffc80:  00004bc6 00000000 3fff348c 0000001e  
[21:41:52]3ffffc90:  00000000 4025c361 00000000 3fff259c  
[21:41:52]3ffffca0:  00000e15 3fff259c 00000100 00000030  
[21:41:52]3ffffcb0:  4000dd24 00000030 00000000 4010735a  
[21:41:52]3ffffcc0:  40100cb5 40100bdc 3fffc258 401001fb  
[21:41:52]3ffffcd0:  c0030025 00000030 00000000 4010735a  
[21:41:52]3ffffce0:  40100cb5 00000000 00000000 40100c9d  
[21:41:52]3ffffcf0:  c0030025 4bc6a7f0 00000000 401001fb  
[21:41:52]3ffffd00:  40100cb5 00000000 00000000 4010735a  
[21:41:52]3ffffd10:  40100cb5 40100bdc 3fffc258 40100c9d  
[21:41:52]3ffffd20:  c0030025 00000030 00000010 ffffffff  
[21:41:52]3ffffd30:  40000ea3 00000023 00000001 00000022  
[21:41:52]3ffffd40:  3fffc200 40100bdc 3fffc258 4000050c  
[21:41:52]3ffffd50:  4021e720 00000030 00000010 ffffffff  
[21:41:52]3ffffd60:  4021e71a 3fffefa0 00000001 c0000001  
[21:41:52]3ffffd70:  3fffdab0 c0000000 3fffd9d0 3fff11e8  
[21:41:52]3ffffd80:  00000000 00000000 00000001 3fff0d54  
[21:41:52]3ffffd90:  0000000e 3fff259c 00000303 00000030  
[21:41:52]3ffffda0:  0000000e 3fff259c 00000303 00000030  
[21:41:52]3ffffdb0:  00000000 4020e6d8 3fff3484 00000008  
[21:41:52]3ffffdc0:  00000000 4bc6a7f0 0189374b 00000000  
[21:41:52]3ffffdd0:  00000000 00000000 4bc6a7f0 00000000  
[21:41:52]3ffffde0:  00000000 00000000 40100abc 00000e15  
[21:41:52]3ffffdf0:  00000000 fffef237 3fff0d54 00000000  
[21:41:52]3ffffe00:  00104849 3fff3484 3fff0d54 401004fa  
[21:41:52]3ffffe10:  3fff2ef8 3fff1ab4 3fff2edc 00000100  
[21:41:52]3ffffe20:  00000000 00000000 00000001 40100738  
[21:41:52]3ffffe30:  00000a6d 0000000e 3fff0d54 0000000e  
[21:41:52]3ffffe40:  3fff259c 00000303 4021e723 3fffefa0  
[21:41:52]3ffffe50:  3fff259c 0000000e 3fff0d54 4020d8a0  
[21:41:52]3ffffe60:  3fff1ab4 0000000f 3fff32c8 00000001  
[21:41:52]3ffffe70:  00000100 00000038 0000000e 4022d8a6  
[21:41:52]3ffffe80:  3fffff01 3fff1490 3fff3244 00000001  
[21:41:52]3ffffe90:  3fff3244 3fff0dd8 00000000 4020fdd4  
[21:41:52]3ffffea0:  3fff2770 00000000 00000000 3fff27a0  
[21:41:52]3ffffeb0:  00000000 00000000 00000000 40223130  
[21:41:52]3ffffec0:  00000000 40223130 00000000 40223130  
[21:41:52]3ffffed0:  00000000 40223130 00000000 40223130  
[21:41:52]3ffffee0:  00000000 00000000 3fff26c8 00000000  
[21:41:52]3ffffef0:  00000000 3fff26f0 00000000 00000000  
[21:41:52]3fffff00:  00000000 40223130 00000000 40223130  
[21:41:52]3fffff10:  00000000 40223130 00000000 40223130  
[21:41:52]3fffff20:  00000000 40223130 00000000 00000000  
[21:41:52]3fffff30:  3fff3244 feefeffe 4020ef9c 4020efdc  
[21:41:52]3fffff40:  3fff3134 3fff2d30 3fff2d30 3fff2fdc  
[21:41:52]3fffff50:  3fff2c1c 3fff2e5c 3fff2b24 3fff2abc  
[21:41:52]3fffff60:  3fff2a54 3fff29a4 3fff2954 3fff32c8  
[21:41:52]3fffff70:  feefeffe feefeffe feefeffe feefeffe  
[21:41:52]3fffff80:  3fff311c 3fff2b24 3fff0df0 3fff2d2c  
[21:41:52]3fffff90:  feefeffe feefeffe feefeffe 3fff11e8  
[21:41:52]3fffffa0:  3fffdad0 00000000 3fff11a8 4021e808  
[21:41:52]<<<stack<<<
[21:41:52]
[21:41:52]--------------- CUT HERE FOR EXCEPTION DECODER ---------------
[21:41:52]
[21:41:52] ets Jan  8 2013,rst cause:2, boot mode:(3,6)
[21:41:52]
[21:41:52]load 0x4010f000, len 3584, room 16 
[21:41:52]tail 0
[21:41:52]chksum 0xb0
[21:41:52]csum 0xb0
[21:41:52]v2843a5ac
[21:41:52]~ld

I tried to write a custom sensor based on my ino file - just for testing first:

#include "esphome.h"

volatile int pulse;
void ICACHE_RAM_ATTR pulseCounter() {
	pulse++;
}

class HumiditySensor : public PollingComponent, public Sensor  {
	static const int HUMIDITY_MEASURING_INTERVALL = 9;
	static const int HUMIDITY_MEASURING_TIME = 100;
	int interruptPin = 14;

    public:
		HumiditySensor() : PollingComponent(600000) {} // 5 mins update interval
        void setup() override;
        void update() override {
        	unsigned long result = interruptForCountingPulse();
        	publish_state(result); 
        }
    private:
    	unsigned long median(unsigned long *values, size_t arraySize) {
        	unsigned long tmp = 0;     // set to 0, make the compiler happy
        	const size_t relVal = 2;   // +- 2 Werte + 1 für die Mittelwertberechnung
        
        	for (size_t i = 0; i < arraySize - 2; i++) {
        		for (size_t j = arraySize - 1; j > i; j--) {
        			if (values[j] < values[j - 1]) {
        				tmp = values[j];
        				values[j] = values[j - 1];  
        				values[j - 1] = tmp;
        			}
        		}
        	}
        
        	tmp = 0;
        	for (size_t i = arraySize / 2 - relVal; i < arraySize / 2 + relVal + 1;
        	   tmp += values[i++]) {
        	}
        
        	return tmp / (relVal * 2 + 1) * 1000 / HUMIDITY_MEASURING_TIME;
        }
    
	    unsigned long interruptForCountingPulse() {
        	unsigned long dataArray[HUMIDITY_MEASURING_INTERVALL];
        
        	for (int i = 0; i < HUMIDITY_MEASURING_INTERVALL; i++) {
        		pulse = 0;
        		attachInterrupt(interruptPin, pulseCounter, RISING);
        		delay(HUMIDITY_MEASURING_TIME);
        		detachInterrupt(interruptPin);
        
        		dataArray[i] = pulse;
        
        		delay(100);
        	}
        
        	return median(dataArray, HUMIDITY_MEASURING_INTERVALL);
        }
};

Then I configured it in my yml:

  - platform: custom
    lambda: |-
      auto a = new HumiditySensor();
      App.register_component(a);
      return {a};
    sensors:
      name: "Feuchtigkeitssensor"
      id: humiditySensor
      accuracy_decimals: 1

This thing crash on compilation at auto a = new HumiditySensor(); with the following error:

/root/.platformio/packages/[email protected]/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: /data/giesomat_mk3/.pioenvs/giesomat_mk3/src/main.cpp.o:(.text._ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE0_E9_M_invokeERKSt9_Any_data+0x4): undefined reference to `vtable for HumiditySensor'
/root/.platformio/packages/[email protected]/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: /data/giesomat_mk3/.pioenvs/giesomat_mk3/src/main.cpp.o:(.text._ZNSt17_Function_handlerIFSt6vectorIPN7esphome6sensor6SensorESaIS4_EEvEZ5setupEUlvE0_E9_M_invokeERKSt9_Any_data+0x8): undefined reference to `vtable for HumiditySensor'
collect2: error: ld returned 1 exit status
*** [/data/giesomat_mk3/.pioenvs/giesomat_mk3/firmware.elf] Error 1

What the hack did I miss >.>

Any ideas? :slight_smile:

You are out of my league now but did you include an include line in the yaml file?

Yep :confused:

esphome:
  name: giesomat_mk3
  platform: ESP8266
  board: d1_mini
  includes:
    - customLibs/Max44009Sensor.h
    - customLibs/HumiditySensor.h
  libraries:
    # https://platformio.org/lib/show/1349/Max44009/examples
    - "robtillaart/Max44009 @ ^0.4.3"

I also commented out lines from the lambda, this error is somehow related to the “new” initialization auf my class HumiditySensor(). Mb there are any coders here who can help.

So noone here has a clew how to get it work? :confused:
Meanwhile i tried to implement my code like the pulse_counter one. Compiles but doesn’t work -.-

#include "esphome.h"


static const char *TAG = "humidity_sensor";
struct PulseCounterStorage {
	volatile int counter{0};
	GPIOPin *pin;
	ISRInternalGPIOPin *isr_pin;
	static const int HUMIDITY_MEASURING_INTERVALL = 9;
	static const int HUMIDITY_MEASURING_TIME = 100;

	static void ICACHE_RAM_ATTR gpio_intr(PulseCounterStorage *arg) {
       arg->counter++;
	}

	bool pulse_counter_setup(GPIOPin *pin) {
		this->pin = pin;
		this->pin->setup();
		this->isr_pin = this->pin->to_isr();
		return true;
	}
	
	unsigned long median(unsigned long *values, size_t arraySize) {
		unsigned long tmp = 0;     // set to 0, make the compiler happy
		const size_t relVal = 2;   // +- 2 Werte + 1 für die Mittelwertberechnung
	
		for (size_t i = 0; i < arraySize - 2; i++) {
			for (size_t j = arraySize - 1; j > i; j--) {
				if (values[j] < values[j - 1]) {
					tmp = values[j];
					values[j] = values[j - 1];  
					values[j - 1] = tmp;
				}
			}
		}
	
		tmp = 0;
		for (size_t i = arraySize / 2 - relVal; i < arraySize / 2 + relVal + 1;
		   tmp += values[i++]) {
		}
	
		return tmp / (relVal * 2 + 1) * 1000 / HUMIDITY_MEASURING_TIME;
	}
    
	unsigned long interruptForCountingPulse() {
		unsigned long dataArray[HUMIDITY_MEASURING_INTERVALL];
	
		for (int i = 0; i < HUMIDITY_MEASURING_INTERVALL; i++) {
			this->counter = 0;
			this->pin->attach_interrupt(gpio_intr, this, CHANGE);
			delay(HUMIDITY_MEASURING_TIME);
			this->pin->detach_interrupt();
	
			dataArray[i] = this->counter;
	
			delay(100);
		}
	
		return median(dataArray, HUMIDITY_MEASURING_INTERVALL);
	}
};

class HumiditySensor : public sensor::Sensor, public PollingComponent {
	public:
        HumiditySensor() {};
		void set_pin(GPIOPin *pin) { pin_ = pin; }
		void set_total_sensor(sensor::Sensor *total_sensor) { total_sensor_ = total_sensor; }
		
		void setup() override {
		   ESP_LOGCONFIG(TAG, "Setting up pulse counter '%s'...", this->name_.c_str());
			if (!this->storage_.pulse_counter_setup(this->pin_)) {
				this->mark_failed();
				return;
			}
		}
		void update() override {			
			unsigned long result = this->storage_.interruptForCountingPulse();
			ESP_LOGD(TAG, "'%s': Retrieved counter: %d", this->get_name().c_str(), result);
            this->publish_state(result);
			if (this->total_sensor_ != nullptr) {
				this->total_sensor_->publish_state(result);
		   }
		}
		float get_setup_priority() const override { return setup_priority::DATA; }
		void dump_config() override {
		   LOG_SENSOR("", "Pulse Counter", this);
		   LOG_PIN("  Pin: ", this->pin_);
		}
	protected:
		GPIOPin *pin_;
		PulseCounterStorage storage_;
		sensor::Sensor *total_sensor_;
};

Maybe I implemented it wrong or smth. I don’t know what to try next, no clew.

Have you tried to ask at Discord? Maybe there are some more people with deeper knowledge about ESPHome…

I’d say just ask, no harm in that. :slight_smile:

1 Like

Thank you for that advice! I joined the discord und was helped very fast. No solution yet, but there are some ideas to check :slight_smile:

1 Like

Did you end up getting this working?