esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
292 stars 36 forks source link

Issue with with ADC and ESP32-C6 #5363

Open lucas2lab opened 10 months ago

lucas2lab commented 10 months ago

The problem

Impossible to launch the build using the ADC component on an ESP32-C6. The exact same pin using a digital read is compiled, but using the ADC module I get the error as displayed in the log. It seems the ADC for the C6 is wrongly declared in esphome or I may setup it wrongly. Could you please assist me ?

Which version of ESPHome has the issue?

2023.12.5

What type of installation are you using?

pip

Which version of Home Assistant has the issue?

No response

What platform are you using?

ESP32-IDF

Board

esp32-c6

Component causing the issue

adc

Example YAML snippet

esphome:
  name: esp32-c6-adc-test 

esp32:
  board: esp32-c6-devkitc-1
  variant: esp32c6
  framework:
    type: esp-idf
    version: 5.1.2
    platform_version: 6.5.0

# Enable logging
logger:

ota:
  password: "123456"

#binary_sensor:
#  - name: "Binary 1"
#    platform: gpio
#    pin: GPIO0

sensor:
  - name: "ADC 1"
    platform: adc
    pin: GPIO0
    update_interval: 5s

Anything in the logs that might be useful for us?

PS C:\Users\User\Projects\ESPHomeTest> esphome compile .\helloworld.yaml
INFO ESPHome 2023.12.5
INFO Reading configuration .\helloworld.yaml...
WARNING The selected ESP-IDF framework version is not the recommended one. If there are connectivity or build issues please remove the manual version.
WARNING The selected ESP-IDF framework version is not the recommended one. If there are connectivity or build issues please remove the manual version.
ERROR Unexpected exception while reading configuration:
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\User\anaconda3\envs\ESPHome\Scripts\esphome.exe\__main__.py", line 7, in <module>
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\__main__.py", line 1041, in main
    return run_esphome(sys.argv)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\__main__.py", line 1019, in run_esphome
    config = read_config(dict(args.substitution) if args.substitution else {})
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\config.py", line 1066, in read_config
    res = load_config(command_line_substitutions)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\config.py", line 920, in load_config
    return _load_config(command_line_substitutions)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\config.py", line 908, in _load_config
    result = validate_config(config, command_line_substitutions)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\config.py", line 836, in validate_config
    result.run_validation_steps()
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\config.py", line 143, in run_validation_steps
    task.step.run(self)
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\config.py", line 717, in run
    self.comp.final_validate_schema(conf)
  File "C:\Users\User\anaconda3\envs\ESPHome\Lib\site-packages\esphome\components\adc\sensor.py", line 41, in final_validate_config
    in ESP32_VARIANT_ADC2_PIN_TO_CHANNEL[variant]
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
KeyError: 'ESP32C6'
PS C:\Users\User\Projects\ESPHomeTest>

Additional information

No response

ssieb commented 10 months ago

This is fixed in dev, but even after you get past this, it still won't compile due to other issues. Support for the C6 hasn't really been added yet.

lucas2lab commented 10 months ago

Thank you very much. Indeed the ADC compilation is not passing.

ssieb commented 10 months ago

I was going to leave this open since it does need to be fixed at some point.

rgnyldz commented 6 months ago

I get a similar error only about the adc2_channel_t . I'm new and guessing that it wants us to use adc2 ?

|-- noise-c @ 0.1.4
|-- ArduinoJson @ 6.18.5
Compiling .pioenvs/multisensor-c6/src/main.o
In file included from src/esphome/components/adc/adc_sensor.h:10,
                 from src/esphome.h:3,
                 from src/main.cpp:3:
/data/cache/platformio/packages/framework-espidf/components/driver/deprecated/driver/adc.h:19:2: warning: #warning "legacy adc driver is deprecated, please migrate to use esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode drivers respectively" [-Wcpp]
   19 | #warning "legacy adc driver is deprecated, please migrate to use esp_adc/adc_oneshot.h and esp_adc/adc_continuous.h for oneshot mode and continuous mode drivers respectively"
      |  ^~~~~~~
In file included from src/esphome/components/adc/adc_sensor.h:11:
/data/cache/platformio/packages/framework-espidf/components/esp_adc/deprecated/include/esp_adc_cal.h:17:2: warning: #warning "legacy adc calibration driver is deprecated, please migrate to use esp_adc/adc_cali.h and esp_adc/adc_cali_scheme.h" [-Wcpp]
   17 | #warning "legacy adc calibration driver is deprecated, please migrate to use esp_adc/adc_cali.h and esp_adc/adc_cali_scheme.h"
      |  ^~~~~~~
src/esphome/components/adc/adc_sensor.h:26:21: error: 'adc2_channel_t' has not been declared
   26 |   void set_channel2(adc2_channel_t channel) {
      |                     ^~~~~~~~~~~~~~
src/esphome/components/adc/adc_sensor.h:63:3: error: 'adc2_channel_t' does not name a type; did you mean 'adc_channel_t'?
   63 |   adc2_channel_t channel2_{ADC2_CHANNEL_MAX};
      |   ^~~~~~~~~~~~~~
      |   adc_channel_t
src/esphome/components/adc/adc_sensor.h:66:3: error: 'esp_adc_cal_characteristics_t' does not name a type
   66 |   esp_adc_cal_characteristics_t cal_characteristics_[SOC_ADC_ATTEN_NUM] = {};
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/esphome/components/adc/adc_sensor.h: In member function 'void esphome::adc::ADCSensor::set_channel1(adc1_channel_t)':
src/esphome/components/adc/adc_sensor.h:24:5: error: 'channel2_' was not declared in this scope; did you mean 'channel1_'?
   24 |     channel2_ = ADC2_CHANNEL_MAX;
      |     ^~~~~~~~~
      |     channel1_
src/esphome/components/adc/adc_sensor.h:24:17: error: 'ADC2_CHANNEL_MAX' was not declared in this scope; did you mean 'ADC1_CHANNEL_MAX'?
   24 |     channel2_ = ADC2_CHANNEL_MAX;
      |                 ^~~~~~~~~~~~~~~~
      |                 ADC1_CHANNEL_MAX
src/esphome/components/adc/adc_sensor.h: In member function 'void esphome::adc::ADCSensor::set_channel2(int)':
src/esphome/components/adc/adc_sensor.h:27:5: error: 'channel2_' was not declared in this scope; did you mean 'channel1_'?
   27 |     channel2_ = channel;
      |     ^~~~~~~~~
      |     channel1_
*** [.pioenvs/multisensor-c6/src/main.o] Error 1
========================== [FAILED] Took 3.88 seconds ==========================

Here is my yaml file (sorry its long duo to LD2411s);

substitutions:
  name: multisensor-c6
  friendly_name: Multisensor C6
  uart_tx_pin: GPIO17
  uart_rx_pin: GPIO16

esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  name_add_mac_suffix: false
  includes:
    - LD2411s.h
  project:
    name: esphome.web
    version: '1.0'
  on_boot:
    priority: 250
    then:
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xFF, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0x73, 0x00, 0x04, 0x03, 0x02, 0x01]
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xFE, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]

esp32:
  board: esp32-c6-devkitm-1
  variant: esp32c6
  flash_size: 4MB
  framework:
    type: esp-idf
    version: 5.1.2
    platform_version: 6.5.0
    sdkconfig_options:
      CONFIG_OPENTHREAD_ENABLED: n
      CONFIG_ENABLE_WIFI_STATION: y
      CONFIG_USE_MINIMAL_MDNS: y

# Enable logging
logger:
  logs:
    sensor: INFO # reduced logging to minimize web_server target overload..

# Enable Home Assistant API
api:
  encryption:
    key: "XXXXXX"

ota:
  password: "XXXXXX"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Multisensor C6 Fallback Hotspot"
    password: "XXXXXX"

captive_portal:

dashboard_import:
  package_import_url: github://esphome/example-configs/esphome-web/esp32.yaml@main
  import_full_config: true

web_server:

uart:
  id: uart_bus
  tx_pin: ${uart_tx_pin}
  rx_pin: ${uart_rx_pin}
  baud_rate: 256000
  parity: NONE
  stop_bits: 1

number:
  - platform: template
    name: "${name} Min Motion Distance"
    id: minmotion
    optimistic: true
    unit_of_measurement: cm
    min_value: 30
    max_value: 717
    step: 1
  - platform: template
    name: "${name} Max Motion Distance"
    id: maxmotion
    optimistic: true
    unit_of_measurement: cm
    min_value: 30
    max_value: 717
    step: 1
  - platform: template
    name: "${name} Min Presence Distance"
    id: minpresence
    optimistic: true
    unit_of_measurement: cm
    min_value: 30
    max_value: 425
    step: 1
  - platform: template
    name: "${name} Max Presence Distance"
    id: maxpresence
    optimistic: true
    unit_of_measurement: cm
    min_value: 30
    max_value: 425
    step: 1
  - platform: template
    name: "${name} Unoccupied Time"
    id: unocc_time
    optimistic: true
    unit_of_measurement: sec
    min_value: 0
    max_value: 6553
    step: 1

sensor:
  - platform: wifi_signal
    name: "WiFi Signal"
    update_interval: 3s
  - platform: adc
    pin: 2
    name: "Temperature Sensor"
    update_interval: 5s
    unit_of_measurement: "°C"
    filters:
      - lambda: return (x - 0.5) * 100;
  - platform: custom
    lambda: |-
      auto my_sensor = new UARTSensor(id(uart_bus)); 
      App.register_component(my_sensor);
      return {my_sensor->distance_sensor, my_sensor->presence_sensor, my_sensor->motion_sensor, my_sensor->max_motion_sensor, my_sensor->min_motion_sensor, my_sensor->max_presence_sensor, my_sensor->min_presence_sensor, my_sensor->unocc_time_sensor};
    sensors:
      - name: "${name} Distance"
        state_class: measurement
        id: distance_sensor
        unit_of_measurement: cm
      - id: presence_sensor
        name: "internal_presense_sensor_to_binary_template"
        internal: true
        on_value:
          - binary_sensor.template.publish:
              id: presence_template
              state: !lambda return x > 0;
      - id: motion_sensor
        name: "internal_motion_sensor_to_binary_template"
        internal: true
        on_value:
          - binary_sensor.template.publish:
              id: motion_template
              state: !lambda return x > 0;
      - name: "internal_max_motion"
        id: max_motion_sensor
        internal: true
        on_value:
          - number.set:
              id: maxmotion
              value: !lambda return x;
      - name: "internal_min_motion"
        id: min_motion_sensor
        internal: true
        on_value:
          - number.set:
              id: minmotion
              value: !lambda return x;
      - name: "internal_max_presence"
        id: max_presence_sensor
        internal: true
        on_value:
          - number.set:
              id: maxpresence
              value: !lambda return x;
      - name: "internal_min_presence"
        id: min_presence_sensor
        internal: true
        on_value:
          - number.set:
              id: minpresence
              value: !lambda return x;
      - name: "internal_unocc"
        id: unocc_time_sensor
        internal: true
        on_value:
          - number.set:
              id: unocc_time
              value: !lambda return x;

binary_sensor:
  - platform: template
    id: presence_template
    name: "${name} Presence"
    device_class: occupancy
    filters:
      - delayed_off: 1s
  - platform: template
    id: motion_template
    name: "${name} Motion"
    device_class: motion
    filters:
      - delayed_off: 1s

script:
  - id: statepublish
    then:
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xFF, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]
      - uart.write:
          id: uart_bus 
          data: !lambda
                  int minmot = id(minmotion).state;
                  int maxmot = id(maxmotion).state;
                  int minpr = id(minpresence).state;
                  int maxpr = id(maxpresence).state;
                  int unocctm = id(unocc_time).state;
                  uint16_t minmvalue = static_cast<uint16_t>(minmot);
                  uint8_t minmhighByte = static_cast<uint8_t>(minmvalue >> 8);
                  uint8_t minmlowByte = static_cast<uint8_t>(minmvalue & 0xFF);
                  uint16_t maxmvalue = static_cast<uint16_t>(maxmot);
                  uint8_t maxmhighByte = static_cast<uint8_t>(maxmvalue >> 8);
                  uint8_t maxmlowByte = static_cast<uint8_t>(maxmvalue & 0xFF);
                  uint16_t minpvalue = static_cast<uint16_t>(minpr);
                  uint8_t minphighByte = static_cast<uint8_t>(minpvalue >> 8);
                  uint8_t minplowByte = static_cast<uint8_t>(minpvalue & 0xFF);
                  uint16_t maxpvalue = static_cast<uint16_t>(maxpr);
                  uint8_t maxphighByte = static_cast<uint8_t>(maxpvalue >> 8);
                  uint8_t maxplowByte = static_cast<uint8_t>(maxpvalue & 0xFF);
                  uint16_t unoccvalue = static_cast<uint16_t>(unocctm * 10);
                  uint8_t unocchighByte = static_cast<uint8_t>(unoccvalue >> 8);
                  uint8_t unocclowByte = static_cast<uint8_t>(unoccvalue & 0xFF);
                  std::vector<uint8_t> data = {0xFD, 0xFC, 0xFB, 0xFA, 0x20, 0x00, 0x67, 0x00, 0x00, 0x00, maxmlowByte, maxmhighByte, 0x00, 0x00, 0x01, 0x00, minmlowByte, minmhighByte, 0x00, 0x00, 0x02, 0x00, maxplowByte, maxphighByte, 0x00, 0x00, 0x03, 0x00, minplowByte, minphighByte, 0x00, 0x00, 0x04, 0x00, unocclowByte, unocchighByte, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01};
                  return data;
      - uart.write: [0xFD, 0xFC, 0xFB, 0xFA, 0x04, 0x00, 0xFE, 0x00, 0x01, 0x00, 0x04, 0x03, 0x02, 0x01]
QuickJack commented 5 months ago

I am facing the same issue.

The datasheet of ESP32-C6 (https://www.espressif.com/sites/default/files/documentation/esp32-c6_datasheet_en.pdf) shows in chapter 2.9.9 that it has only one ADC. However, the original ESP32 (https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf) shows in chapter 2.2 that it has two.

There is an issue when using WiFi together with ADC2 on the original ESP32 (s. https://www.bitfoic.com/components/how-to-resolve-the-wifi-and-adc2-sharing-dilemma?id=206). This is also described in https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf in chapter 29.3.1.

The tools are somehow mixing features of the ESP32 with those of the ESP32-C6.

jharris4 commented 5 months ago

I’m consistently getting the adc2_channel_t error as well if I try to add a sensor with platform adc. Compiles and works fine if I don’t use adc.

WouterJN commented 3 months ago

When will this be solved? Are there any work arrounds that still work with 2024.7?

My current code;

  name: livingroom-plant-watering-unit
  friendly_name: LivingRoom Plant Watering Unit

esp32:
  board: esp32-c6-devkitc-1
  variant: esp32c6
  flash_size: 8MB
  framework:
    type: esp-idf
    version: 5.2.2
    platform_version: 6.6.0
    sdkconfig_options:
      CONFIG_ESPTOOLPY_FLASHSIZE_8MB: y
      CONFIG_OPENTHREAD_ENABLED: n
      CONFIG_ENABLE_WIFI_STATION: y
      CONFIG_USE_MINIMAL_MDNS: y

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "xxxx"

ota:
  - platform: esphome
    password: "xxxx"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Livingroom-Plant-Watering-Unit"
    password: "xxxx"

captive_portal:

sensor:
  - platform: adc
    pin: GPIO01
    name: Soil Moisture Value
    attenuation: 11db
    id:  soil_moisture
    accuracy_decimals: 0
    unit_of_measurement: "%"
    update_interval: 10s
    icon: mdi:flower
    filters:
      - calibrate_linear:
        - 1.795 -> 0.0
        - 1.43 -> 100.0
      - sliding_window_moving_average:
          window_size: 15
          send_every: 15
      - median:
          window_size: 15
          send_every: 7
          send_first_at: 3

I also tried by adding the following code but that also doesnt work anylonger;

  - source:
      type: git
      url: https://github.com/ssieb/esphome
      ref: adc
    components: [ adc ]
    refresh: 1min

It would be great if you could help @ssieb

jkremlacek commented 3 months ago

I quickly checked sources and if I understand the issue correctly, current adc support in esphome is based on deprecated solution provided by esp-idf which is missing support for C6 and as it is deprecated it will most likely never be added.

@ssieb Is there any rough time range when migration to ADC oneshot would be done on esphome side?

(normally I would offer my help, but I haven't touched C++ seriously for 6+ years and I lack knowledge in both deprecated and new solution of esp-idf - I tried bruteforce fixing it, but after 2hrs I got into deadend)

Source (deprecation info): https://docs.espressif.com/projects/esp-idf/en/v5.3/esp32/migration-guides/release-5.x/5.0/peripherals.html#adc

lboue commented 3 months ago

I quickly checked sources and if I understand the issue correctly, current adc support in esphome is based on deprecated solution provided by esp-idf which is missing support for C6 and as it is deprecated it will most likely never be added.

@ssieb Is there any rough time range when migration to ADC oneshot would be done on esphome side?

(normally I would offer my help, but I haven't touched C++ seriously for 6+ years and I lack knowledge in both deprecated and new solution of esp-idf - I tried bruteforce fixing it, but after 2hrs I got into deadend)

Source (deprecation info): https://docs.espressif.com/projects/esp-idf/en/v5.3/esp32/migration-guides/release-5.x/5.0/peripherals.html#adc

For ADC Oneshot Mode Driver:

Doc ESP-IDF Programming Guide: Analog to Digital Converter (ADC) Oneshot Mode Driver

Examples

Here is an example for the ADC module with ESP-IDF v5.3

lboue commented 3 months ago

@lucas2lab @jkremlacek I've spent several hours coding and I have a static example that works but is limited to GPIO0. Could you test it with the following configuration?

ESPHome config

external_components:
  - source:
      # https://github.com/lboue/esphome/blob/adc_oneshot/esphome/components/adc
      type: git
      url: https://github.com/lboue/esphome
      ref: adc_oneshot
    components: [ adc ]
    refresh: 0s

sensor:
  - name: "ADC1_CH0 GPIO0"
    platform: adc
    pin: GPIO0
    update_interval: 15s

image

ADC channels

image

jkremlacek commented 3 months ago

Worked like a charm for me.

I tried it with a voltage divider using 2x 47k resistors on 5v input and ignoring the innacuracy of the ADC within the ESP (about 0.1 V) it measured correctly.


Note for others, in case you would be replicating my usecase you have to change the sensor definition in order to adjust the measurement for the voltage divider using filters and multiply:

sensor:
  - name: "ADC1_CH0 GPIO0"
    platform: adc
    pin: GPIO0
    update_interval: 15s
    filters:
      - multiply: 2
dcolens commented 3 months ago

@lboue thanks for the fix, I tried it too with a FireBeetle 2 ESP32-C6 and it is working fine, any chance to get a patch that works for other GPIOs :-D ?

naknut commented 2 months ago

@lboue I could not get you fix to work for me. This is my current configuration:

esphome:
  name: soil-mosture-lab
  friendly_name: Soil Mosture Lab

esp32:
  board: esp32-c6-devkitc-1
  flash_size: 8MB
  variant: esp32c6
  framework:
    type: esp-idf
    version: "5.2.1"
    platform_version: 6.6.0
    sdkconfig_options:
      CONFIG_OPENTHREAD_ENABLED: n
      CONFIG_ENABLE_WIFI_STATION: y
      CONFIG_USE_MINIMAL_MDNS: y
      CONFIG_ESPTOOLPY_FLASHSIZE_8MB: y

external_components:
  - source:
      # https://github.com/lboue/esphome/blob/adc_oneshot/esphome/components/adc
      type: git
      url: https://github.com/lboue/esphome
      ref: adc_oneshot
    components: [ adc ]
    refresh: 0s

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "msi5g7riSsnTaf19fahduGUr9q8CzdIBaLA2K2CpB/w="

ota:
  - platform: esphome
    password: "515825743d9cdf6e99109b287a490fa1"

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Soil-Mosture-Lab"
    password: "yewabOCCJh7q"

captive_portal:

sensor:
  - name: "ADC1_CH0 GPIO0"
    platform: adc
    pin: GPIO0
    update_interval: 15s

and this is the output I get when flashing:

INFO ESPHome 2024.8.0
INFO Reading configuration /config/esphome/soil-mosture-lab.yaml...
INFO Updating https://github.com/lboue/esphome@adc_oneshot
WARNING The selected ESP-IDF framework version is not the recommended one. If there are connectivity or build issues please remove the manual version.
WARNING This board is unknown. Make sure the chosen chip component is correct.
WARNING The selected ESP-IDF framework version is not the recommended one. If there are connectivity or build issues please remove the manual version.
WARNING This board is unknown. Make sure the chosen chip component is correct.
INFO Generating C++ source...
INFO Updating https://github.com/espressif/esp-protocols.git@mdns-v1.2.5
INFO Compiling app...
Processing soil-mosture-lab (board: esp32-c6-devkitc-1; framework: espidf; platform: platformio/espressif32@6.6.0)
--------------------------------------------------------------------------------
HARDWARE: ESP32C6 160MHz, 512KB RAM, 8MB Flash
 - framework-espidf @ 3.50201.240515 (5.2.1) 
 - tool-cmake @ 3.16.9 
 - tool-ninja @ 1.10.2 
 - tool-riscv32-esp-elf-gdb @ 12.1.0+20221002 
 - tool-xtensa-esp-elf-gdb @ 12.1.0+20221002 
 - toolchain-esp32ulp @ 2.35.0-20220830 
 - toolchain-riscv32-esp @ 13.2.0+20230928
Reading CMake configuration...
Dependency Graph
|-- noise-c @ 0.1.6
Linking .pioenvs/soil-mosture-lab/firmware.elf
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/esphome/components/api/api_connection.o: in function `esphome::api::APIConnection::send_sensor_state(esphome::sensor::Sensor*, float)':
/data/build/soil-mosture-lab/src/esphome/components/api/api_connection.cpp:463:(.text._ZN7esphome3api13APIConnection17send_sensor_stateEPNS_6sensor6SensorEf+0x38): undefined reference to `esphome::sensor::Sensor::has_state() const'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/esphome/components/api/api_connection.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()':
/data/cache/platformio/packages/toolchain-riscv32-esp/riscv32-esp-elf/include/c++/13.2.0/bits/basic_string.h:792:(.text._ZN7esphome3api13APIConnection16send_sensor_infoEPNS_6sensor6SensorE+0x134): undefined reference to `esphome::sensor::Sensor::get_accuracy_decimals()'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: /data/cache/platformio/packages/toolchain-riscv32-esp/riscv32-esp-elf/include/c++/13.2.0/bits/basic_string.h:792:(.text._ZN7esphome3api13APIConnection16send_sensor_infoEPNS_6sensor6SensorE+0x160): undefined reference to `esphome::sensor::Sensor::get_state_class()'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/esphome/core/controller.o: in function `esphome::Controller::setup_controller(bool)':
/data/build/soil-mosture-lab/src/esphome/core/controller.cpp:29:(.text._ZN7esphome10Controller16setup_controllerEb+0x60): undefined reference to `esphome::sensor::Sensor::add_on_state_callback(std::function<void (float)>&&)'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o: in function `esphome::Component::set_component_source(char const*)':
/data/build/soil-mosture-lab/src/esphome/core/component.h:149:(.text._Z5setupv+0x510): undefined reference to `esphome::sensor::Sensor::Sensor()'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o: in function `esphome::api::APIServer* esphome::Application::register_component<esphome::api::APIServer>(esphome::api::APIServer*)':
/data/build/soil-mosture-lab/src/esphome/core/application.h:191:(.text._Z5setupv+0x524): undefined reference to `vtable for esphome::adc::ADCSensor'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: /data/build/soil-mosture-lab/src/esphome/core/application.h:191:(.text._Z5setupv+0x528): undefined reference to `vtable for esphome::adc::ADCSensor'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o: in function `setup()':
/data/build/soil-mosture-lab/src/main.cpp:144:(.text._Z5setupv+0x530): undefined reference to `vtable for esphome::adc::ADCSensor'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: /data/build/soil-mosture-lab/src/main.cpp:144:(.text._Z5setupv+0x534): undefined reference to `vtable for esphome::adc::ADCSensor'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: /data/build/soil-mosture-lab/src/main.cpp:145:(.text._Z5setupv+0x53c): undefined reference to `vtable for esphome::adc::ADCSensor'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o:/data/build/soil-mosture-lab/src/main.cpp:145: more undefined references to `vtable for esphome::adc::ADCSensor' follow
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o: in function `setup()':
/data/build/soil-mosture-lab/src/main.cpp:168:(.text._Z5setupv+0x5de): undefined reference to `esphome::sensor::Sensor::set_state_class(esphome::sensor::StateClass)'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o: in function `esphome::preferences::IntervalSyncer* esphome::Application::register_component<esphome::preferences::IntervalSyncer>(esphome::preferences::IntervalSyncer*)':
/data/build/soil-mosture-lab/src/esphome/core/application.h:191:(.text._Z5setupv+0x600): undefined reference to `esphome::sensor::Sensor::set_accuracy_decimals(signed char)'
/data/cache/platformio/packages/toolchain-riscv32-esp/bin/../lib/gcc/riscv32-esp-elf/13.2.0/../../../../riscv32-esp-elf/bin/ld: .pioenvs/soil-mosture-lab/src/main.o: in function `esphome::adc::ADCSensor::ADCSensor()':
/data/build/soil-mosture-lab/src/esphome/components/adc/adc_sensor.h:36:(.text._Z5setupv+0x648): undefined reference to `esphome::adc::ADCSensor::set_sample_count(unsigned char)'
collect2: error: ld returned 1 exit status
*** [.pioenvs/soil-mosture-lab/firmware.elf] Error 1
========================= [FAILED] Took 23.75 seconds =========================
eugene-polyakov commented 2 months ago

Hi folks - based upon @lboue work, added GPIO selection and re-implemented as a custom component, fits my purposes better.

Still pretty raw, didn't touch ADC2 -- it's not available with WiFi on anyways I believe so please stick to ADC1 pins for now.

source here working example

Luis-Sarabando commented 1 month ago

Can confirm the above solution worked for me on a M5nanoC6 (esp32-c6-devkitc-1), so adc is working!

  board: esp32-c6-devkitc-1
  flash_size: 8MB
  variant: esp32c6
  framework:
    type: esp-idf
#Can be dev, latest, recommended, or check https://github.com/espressif/esp-idf/releases
    version: "5.3.1"
    #version: "5.3.0"
#check platform_version @ https://github.com/platformio/platform-espressif32/releases/
    platform_version: 6.9.0
    #platform_version: 6.8.1
    sdkconfig_options:
      CONFIG_ESPTOOLPY_FLASHSIZE_8MB: y

#This is to make sure adc compiles or else it will throw an error
external_components:
  - source:
      # https://github.com/lboue/esphome/blob/adc_oneshot/esphome/components/adc
      type: git
      url: https://github.com/lboue/esphome
      ref: adc_oneshot
    components: [ adc ]
    refresh: 0s

    sensor:
  - name: "ADC 1"
    platform: adc
    pin: GPIO5
    update_interval: 5s
kdpuvvadi commented 1 month ago

I'm using FireBeetle 2 ESP32-C6 from dfrobot. With https://github.com/lboue/esphome component, value seems to be stuck at 2080 (Raw value, Calibrated Voltage: 2074 mV). From their documentation added x2 multiplication factor.

[21:52:38][I][adc:200]: calibration scheme version is Curve Fitting
[21:52:38][I][adc:231]: Calibration Success
[21:52:38][I][adc:157]: ADC1 Channel[0] Raw Data: 2080
[21:52:38][I][adc:161]: ADC1 Channel[0] Calibrated Voltage: 2073 mV
[21:52:38][I][adc:244]: deregister Curve Fitting calibration scheme
[21:52:38][D][sensor:093]: 'Battery Level': Sending state 4.14600 V with 2 decimals of accuracy

Even at battery empty status, value seems to be the same.

I've another board (same sku), it same on that too. I don't think it's issue with the hardware. I've also tried flashing tasmota and it is reading properly. i might have to dig into this.

Luis-Sarabando commented 1 month ago

Update to my post above, adc works but the values don't match up, they dont change, which leads me to belive its not actually working @kdpuvvadi

As ill inject a voltage and the measured in esp doesnt even change.

Tag me if you managed to fix it.

PhracturedBlue commented 1 month ago

I put together a working ADC for the ESP32-C6 here: https://github.com/PhracturedBlue/c6_adc

It is just a stop-gap, and it doesn't support features like auto-attenuation or averaging multiple samples per iteration. but it seems to work for me on all chanels (0-6). The default update rate is 60sec, but that can be changed in the usual manner.

The config would look something like:

esp32:
  board: esp32-c6-devkitc-1
  flash_size: 4MB
  variant: esp32c6
  framework:
    type: esp-idf
    version: "5.3.0"
    platform_version: 6.8.1
    sdkconfig_options:
      CONFIG_ESPTOOLPY_FLASHSIZE_8MB: y

esphome:
  name: test

external_components:
  - source: github://PhracturedBlue/c6_adc
    # refresh: 0s   # uncomment to force refresh of repo

sensor:
  - platform: c6_adc
    name: adc5
    pin: GPIO5
    attenuation: 0dB  # optional.  defaults to 12dB

@Luis-Sarabando FYI

edit: added the sample() method so it can be used with other sensors like the ct_clamp.

kdpuvvadi commented 1 month ago

There seems to be issue with your component @PhracturedBlue

In file included from src/esphome/components/c6_adc/c6_adc.cpp:2:
src/esphome/components/c6_adc/c6_adc.h:3:10: fatal error: esphome/components/voltage_sampler/voltage_sampler.h: No such file or directory
    3 | #include "esphome/components/voltage_sampler/voltage_sampler.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
*** [.pioenvs\test\src\esphome\components\c6_adc\c6_adc.cpp.o] Error 1
PhracturedBlue commented 1 month ago

what version of esphome are you using? I used 2024.8.3 with the exact configuration above, and it builds fine for me.

Edit: tested on 2024.9.2 and it works fine there too

kdpuvvadi commented 1 month ago

I was on 2024.9.2. Downgraded to 2024.8.3. Getting the same error.

Removed the esphome completetly and installed 9.2 and still same issue. Just for the sake of it tried on windows workstation (also wsl2), debian 12 box and macos Sequoia. Getting same error on all.

PhracturedBlue commented 1 month ago

Sorry about that. it was due to a change I made when adding support for the ct_clamp. it didn't show up when the ct_clamp was also included. The issue should be fixed now. you need to either remove your external_components directory, or add refresh: 0s to the c6_adc external-component in your yaml (see exmaple above) to force a git-pull. I don't recommend leaving refresh: 0s in your production yaml file as it slows down the validator.