admin管理员组文章数量:1332359
Putting the finishing touches on an assignment (ESP-IDF PlatformIO) and my ADC initialization is throwing a warning that a define I'm calling is deprecated. It builds, so this is more in the "it bugs me" category: IOW it's not game-breaking but I'd still like to know how to fix it.
// main function
void app_main() {
esp_event_loop_create_default();
connect_wifi_params_t cbs = {
.on_connected = handle_wifi_connect,
.on_failed = handle_wifi_failed
};
// configure ADC for 12-bit width, 3.3V source
// Photoresistor needs to be connected from 3.3V to pin 34.
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(adc_channel, ADC_ATTEN_DB_11);
// connect wifi
appwifi_connect(cbs);
}
And the call to the ADC if it helps any.
// transmit reading to server. Photoresistor is read here.
static void publish_reading(int temp, int hum) {
char buffer[5];
if (client != NULL && enabled) {
esp_mqtt_client_publish(client, TEMP_TOPIC, itoa(temp, buffer, 10), 0, 1, 0);
esp_mqtt_client_publish(client, HUM_TOPIC, itoa(hum, buffer, 10), 0, 1, 0);
// average ADC value over 32 samples and send it
int adc_val = 0;
for (unsigned char i = 0; i < SAMPLE_CNT; i++) adc_val += adc1_get_raw(adc_channel);
adc_val /= SAMPLE_CNT;
esp_mqtt_client_publish(client, PHOTO_TOPIC, itoa(adc_val, buffer, 10), 0, 1, 0);
}
}
The warning says that ADC_ATTEN_DB_11
is deprecated.
ETA: I should clarify my troubleshooting steps: I already tried ADC_ATTEN_DB_12
. The warning says "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".
Putting the finishing touches on an assignment (ESP-IDF PlatformIO) and my ADC initialization is throwing a warning that a define I'm calling is deprecated. It builds, so this is more in the "it bugs me" category: IOW it's not game-breaking but I'd still like to know how to fix it.
// main function
void app_main() {
esp_event_loop_create_default();
connect_wifi_params_t cbs = {
.on_connected = handle_wifi_connect,
.on_failed = handle_wifi_failed
};
// configure ADC for 12-bit width, 3.3V source
// Photoresistor needs to be connected from 3.3V to pin 34.
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(adc_channel, ADC_ATTEN_DB_11);
// connect wifi
appwifi_connect(cbs);
}
And the call to the ADC if it helps any.
// transmit reading to server. Photoresistor is read here.
static void publish_reading(int temp, int hum) {
char buffer[5];
if (client != NULL && enabled) {
esp_mqtt_client_publish(client, TEMP_TOPIC, itoa(temp, buffer, 10), 0, 1, 0);
esp_mqtt_client_publish(client, HUM_TOPIC, itoa(hum, buffer, 10), 0, 1, 0);
// average ADC value over 32 samples and send it
int adc_val = 0;
for (unsigned char i = 0; i < SAMPLE_CNT; i++) adc_val += adc1_get_raw(adc_channel);
adc_val /= SAMPLE_CNT;
esp_mqtt_client_publish(client, PHOTO_TOPIC, itoa(adc_val, buffer, 10), 0, 1, 0);
}
}
The warning says that ADC_ATTEN_DB_11
is deprecated.
ETA: I should clarify my troubleshooting steps: I already tried ADC_ATTEN_DB_12
. The warning says "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".
- It builds, so this is more in the "it bugs me" category — at the moment, but it is deprecated, so some day it will go away and then your code won't build. Take the time (now!) to update the code to use the replacement before it bites you. – Jonathan Leffler Commented Nov 21, 2024 at 3:52
- I've updated with the troubleshooting steps I already took: slipped my mind last night. – StarSword Commented Nov 21, 2024 at 15:12
2 Answers
Reset to default 1If you look in the manual at ADC Oneshot Mode Driver, if you search for ADC_ATTEN_DB
, you will find:
enumerator
ADC_ATTEN_DB_12
The input voltage of ADC will be attenuated extending the range of measurement by about 12 dB.enumerator
ADC_ATTEN_DB_11
This is deprecated, it behaves the same asADC_ATTEN_DB_12
So replace ADC_ATTEN_DB_11
with ADC_ATTEN_DB_12
.
After a lot of digging, I figured out how to fix it with the purportedly new-and-improved header file. Updated code:
#include "esp_adc/adc_oneshot.h"
// define
#define SAMPLE_CNT 32 // average this number of ADC samples to get reading
// global
adc_oneshot_unit_handle_t adc1_handle;
// returns the average of 32 ADC samples
static int AverageADCSamp() {
int samples = 0;
for (unsigned char i = 0; i < SAMPLE_CNT; i++) {
int rawValue = 0;
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_6, &rawValue));
samples += rawValue;
}
return samples / SAMPLE_CNT; // return the average
}
// transmit reading to server. Photoresistor is read here.
static void publish_reading(int temp, int hum) {
char buffer[5];
if (client != NULL && enabled) {
esp_mqtt_client_publish(client, TEMP_TOPIC, itoa(temp, buffer, 10), 0, 1, 0);
esp_mqtt_client_publish(client, HUM_TOPIC, itoa(hum, buffer, 10), 0, 1, 0);
// average ADC value over 32 samples and send it
esp_mqtt_client_publish(client, PHOTO_TOPIC, itoa(AverageADCSamp(), buffer, 10), 0, 1, 0);
}
}
// main function
void app_main() {
esp_event_loop_create_default();
connect_wifi_params_t cbs = {
.on_connected = handle_wifi_connect,
.on_failed = handle_wifi_failed
};
// configure ADC for 12-bit width, 3.3V source
// Photoresistor needs to be connected from 3.3V to pin 34.
adc_oneshot_unit_init_cfg_t ADC1_C6_config = {
.unit_id = ADC_UNIT_1
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&ADC1_C6_config, &adc1_handle));
adc_oneshot_chan_cfg_t channel_config = {
.bitwidth = ADC_BITWIDTH_12,
.atten = ADC_ATTEN_DB_12
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_6, &channel_config));
// connect wifi
appwifi_connect(cbs);
}
本文标签: cWarning about deprecated define from ESPIDF driveradchStack Overflow
版权声明:本文标题:c - Warning about deprecated define from ESP-IDF driveradc.h - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742317439a2452104.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论