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".
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);
}