Adding custom C to an ESPHome device

Hello all,

Please forgive me if this is covered in documentation that I have yet to read. I’m using multiple esp32’s and I’m able to on-board them to ESPHome. However, I’m not sure how to add the C code that I have written to accept data from my pinouts. For example, Adafruit soil sensors, gps boards, etc…

I found a post within these forums where there is a good code example (or better than mine) but on information on where that custom C is places for access in the configuration.yaml.

Sorry for the noob question and I hope someone out there is kind enough to lead me in the proper direction.

Read about generic custom C components for esphome here…

But heed that big warning at the top. You should probably find another way to do it. Port the code into esphome perhaps?

What are you trying to do? I ask because you’re riding the line on an X/Y problem. (telling us what you want to do without context on what you’re trying to accomplish while your method may not be correct for the platform and leading you down a rabbit hole.)

1 Like

Thanks for the resource. Before I take a read, I’m trying to automate my garden (seems to be a common topic on the internet) but at scale.

I have 6 3x10 beds where I’d like to put a moisture sensor every 2 or so feet. I’m using general esp32s with the Adafruit soil sensor. This sensor is connected to 2 digital outputs and I’m leveraging the wifi capabilities of the esp32 to report into ESPHome. I’ll continue on with remote water control, pump control, etc.

There is a post that was close or had a lot of information that I needed here ( Link errors because I’m somehow compiling duplicates to different folders. Yikes! - ESPHome - Home Assistant Community (home-assistant.io)) but it didn’t explain where to store the c file for inclusion with the ESPHome bin creation to load on the device. Or how to store that information so that I could use the configuration.yaml to include this as a sensor.

Hope this helps, if not, poke me in the nose to try again.

Been reading your post and moved on to contributing code. I think this is the path I want to take. And maybe when I’m done, y’all can include in in the base installation. I also like the option of external code reference with GIt so I’m not dorking around with local files and can share for troubleshooting with the community.

I’m going to start setting up a dev evn as soon as I’m done with my coffee.

1 Like

Thanks for opening a new thread! Much appreciated! :+1:

In addition to what was already said, there might be an interesting project, where you could “steal” some ideas! :slight_smile: It’s not particularly an irrigation control, it’s a growroom control (you might get that from the forum where it’s made :laughing:)

I wanted to introduce that script in a seperate thread, but hadn’t have the time to do so:
Note: This is not my script and I’m not taking any credit for it! I’m not the author!

https://www.grower.ch/forum/threads/brocontrol-open-source-growcontroller.153324/

It’s in german, but you can view the ESPHome code at the end of the first post. Maybe it gives you some ideas. :slight_smile:

1 Like

You want to use external components. Much more versatile and powerful.

1 Like

BroControl LOL!!! Yep, I can pull from this code. I’m reading the thread and hope that they created a git for this resource.

1 Like

Nope, no git whatsoever… That’s the problem, that the author doesn’t want to expose himself in connection with this script… :laughing: I do know, that the author has more than one Github repo, but these are probably connected to his employer…you get the idea :rofl:

The forum thread is the only place to get it, and to look for changes, sorry! I can tag you, when I present the script here in this forum, this thread will be updated. And I already have the permission to do so (you can find it somewhere in that thread) and will try to find the time in the next weeks. :slight_smile:

I wondered what you were saying, but when I look closer, I see the issue LOL.

@FreRange , did you see this component in esphome? Sprinkler Controller — ESPHome

@nickrout thanks for the data, this will help with the watering but not with the moisture sensor. I’m trying to bend my brain around c coding without much success. However, I’m still willing to try and share the repo for hits, tricks, and code addition.

I am pretty sure you place the .h file in the same directory as the esphome yaml files sit, ie /config/esphome.

Well, I’m doing my best to create this add-on for the adafruit sensor (w22515-c stemma soil sensor) but something is not working. I took information and code samples from the b-parasite, and I2C sensor like the one I’m trying to import. I created a fork on the ESPHome dev branch and add my own branch here: esphome/esphome/components/w22515-c at adafruit_soil_sensor · FreRange/esphome · GitHub

General error when I try to create the yaml… grrrr… error is: Platform not found: ‘sensor.w22515-c’.

yaml is:

substitutions:
  name: esphome-web-529e9c
  friendly_name: ESPHome Web 529e9c

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  name_add_mac_suffix: false
  platformio_options:
    board_build.flash_mode: dio
  project:
    name: esphome.web
    version: '1.0'

esp32:
  board: esp32-s3-devkitc-1
  framework:
    type: arduino
.
# Enable logging
logger:

# Enable Home Assistant API
api:

# Allow Over-The-Air updates
ota:
  platform: esphome

# Allow provisioning Wi-Fi via serial
improv_serial:

wifi:
  # Set up a wifi access point
  ap: {}

# In combination with the `ap` this allows the user
# to provision wifi credentials to the device via WiFi AP.
captive_portal:

dashboard_import:
  package_import_url: github://esphome/firmware/esphome-web/esp32s3.yaml@v2
  import_full_config: true

# Sets up Bluetooth LE (Only on ESP32) to allow the user
# to provision wifi credentials to the device.
esp32_improv:
  authorizer: none

# To have a "next url" for improv serial
web_server:

external_components:
  - source:
      type: git
      url: https://github.com/FreRange/esphome
      ref: adafruit_soil_sensor
    components: [ w22515-c ]

i2c:
  sda: GPIO1
  scl: GPIO2

sensor:
  - platform: w22515-c
    new_address: 0x36      # Change the address upon startup, requires a restart.
    accuracy_decimals: 2
    update_interval: 30s   # Number of seconds between polls
    icon: mdi:flower       # Icon on dashboard
    #attenuation: 12db

    moisture:
      name: "Soil Moisture"
      calibration:
        min_capacity: 300  # Optional (default 245)
        max_capacity: 600  # Optional (default 550)
        raw: false         # Use inbuilt conversion.
      filters:
        - skip_initial: 1  # First read is zero, skip it.
        - clamp:           # To address out of range readings
            min_value: 1
            max_value: 3000
            ignore_out_of_range: true

    temperature:
      name: "Soil Temperature"
      filters:
        - skip_initial: 1  # First read is zero, skip it.
        - offset: -0.1     # Calibrate readings.
        - clamp:           # To address out of range readings
            min_value: 1
            max_value: 3000
            ignore_out_of_range: true 

Just an absolute guess - is the hyphen a valid character in a component name?

I’ll update the code to remove the -c but that’s the model number and namespace I created in the c code.

oooo looks like none of these have a -, they use _…

Could not find init.py file for component w22515_c. Please check the component is defined by this source

its there. wth?!