Code: Select all
/*
* adc.c
*
* Created on: Jul 28, 2022
* Author: Keith
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
//#include "esp_adc/adc_continuous.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali.h"
#include "MQTT_dialog.h"
#include "mqtt_client.h"
#include "My_Utilities.h"
//ADC Channels
#define ADC1_GPIO1_CHANNEL ADC_CHANNEL_0
#define ADC1_CHANNEL_0_GPIO_NUM 1
//ADC Attenuation
#define ADC_EXAMPLE_ATTEN ADC_ATTEN_DB_11
//ADC Calibration
#define ADC_EXAMPLE_CALI_SCHEME ESP_ADC_CAL_VAL_EFUSE_TP_FIT
extern esp_mqtt_client_handle_t client;
struct
{
int pool_pressure;
int pool_temperature;
}
read_adc_values = {0, 0};
void adc_read( adc_channel_t channel )
{
read_adc_values.pool_pressure = adc_oneshot_read(ADC_CHANNEL_0, ADC1_CHANNEL_0_GPIO_NUM, &adc_raw[0][0]);
myTaskDelay(10);
}
bool payload_process_adc()
{
bool match = false;
static char msg[40];
// char psi_value = 0;
if ( topic_is("/pool/adc/pressure") ) // Topic
{
match = true;
if (payload_is ( "/pool/adc/read1" )) // "/pool/adc/read/gpio1" is the topic
{
printf("got to read adc8\n");
adc_read(ADC1_GPIO1_CHANNEL);
itoa(read_adc_values.pool_pressure, msg, 10);
strcat(msg, " psi");
esp_mqtt_client_enqueue(client, "/pool/adc/pressure", msg, 0, 1, 0, true); // "/pool/adc/pressure" is the topic. Vin Max 2.9V @ 4095 counts
}
else
{
match = false;
}
}
return match;
}