Skip to content

Commit

Permalink
Firmware: Add sensor logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis960 committed Apr 14, 2024
1 parent 58783f5 commit b4c4684
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ESPlant-Firmware/main/peripherals/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "esp_timer.h"
#include "driver/i2c.h"
#include "aht20.h"
#include "esp_log.h"

#include "peripherals/adc.h"
#include "peripherals/config.h"
Expand Down Expand Up @@ -117,11 +118,13 @@ void sensors_setGreenLedBrightness(float brightness)

void enableLightSensor()
{
ESP_LOGI("SENSORS", "Enabling light sensor");
gpio_set_level(LIGHT_SENSOR_IN, 1);
}

void disableLightSensor()
{
ESP_LOGI("SENSORS", "Disabling light sensor");
gpio_set_level(LIGHT_SENSOR_IN, 0);
}

Expand All @@ -130,6 +133,7 @@ void disableLightSensor()
*/
float sensors_readLightPercentage()
{
ESP_LOGI("SENSORS", "Reading light sensor");
enableLightSensor();
vTaskDelay(20 / portTICK_PERIOD_MS); // Wait for the sensor to stabilize (statistically >10ms is enough)
int light_sensor_value = adc_analogReadAverageRaw(ADC_LIGHT_SENSOR_CHANNEL, 20, 5);
Expand All @@ -140,16 +144,19 @@ float sensors_readLightPercentage()

void enableVoltageMeasurement()
{
ESP_LOGI("SENSORS", "Enabling voltage measurement");
gpio_set_level(VOLTAGE_MEASUREMENT_SELECT, 0);
}

void disableVoltageMeasurement()
{
ESP_LOGI("SENSORS", "Disabling voltage measurement");
gpio_set_level(VOLTAGE_MEASUREMENT_SELECT, 1);
}

float sensors_readVoltage()
{
ESP_LOGI("SENSORS", "Reading voltage");
enableVoltageMeasurement();
int voltage = adc_analogReadAverageVoltage(ADC_VOLTAGE_MEASUREMENT_CHANNEL, 10, 3);
disableVoltageMeasurement();
Expand All @@ -160,13 +167,15 @@ float sensors_readVoltage()

bool sensors_isUsbConnected()
{
ESP_LOGI("SENSORS", "Checking if USB is connected");
return gpio_get_level(POWER_USB_VIN);
}

aht20_dev_handle_t aht_handle = NULL;

void configureI2cBus(int sda, int scl)
{
ESP_LOGI("SENSORS", "Configuring I2C bus");
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = sda,
Expand All @@ -182,6 +191,7 @@ void configureI2cBus(int sda, int scl)

void initAht(int sda, int scl)
{
ESP_LOGI("SENSORS", "Initializing AHT20 sensor");
configureI2cBus(sda, scl);
aht20_i2c_config_t i2c_config = {
.i2c_addr = AHT20_ADDRRES_0,
Expand All @@ -192,6 +202,7 @@ void initAht(int sda, int scl)

void sensors_aht_read_data(sensors_aht_data_t *data)
{
ESP_LOGI("SENSORS", "Reading AHT20 sensor data");
uint32_t temperature_raw, humidity_raw;
float temperature, humidity;
ESP_ERROR_CHECK(aht20_read_temperature_humidity(aht_handle, &temperature_raw, &temperature, &humidity_raw, &humidity));
Expand All @@ -203,13 +214,15 @@ void sensors_aht_read_data(sensors_aht_data_t *data)

void deinitAht()
{
ESP_LOGI("SENSORS", "Deinitializing AHT20 sensor");
ESP_ERROR_CHECK(aht20_del_sensor(aht_handle));
ESP_ERROR_CHECK(i2c_driver_delete(I2C_NUM_0));
}

// Moisture sensor
bool measure_stabilized_output(sensors_moisture_sensor_output_t *output)
{
ESP_LOGI("SENSORS", "Measuring moisture sensor");
const int numberOfMeasurements = 5;
unsigned long measurementStartTime = millis();
unsigned long stabilization_time;
Expand Down Expand Up @@ -255,11 +268,13 @@ bool measure_stabilized_output(sensors_moisture_sensor_output_t *output)
*/
void setupMoistureSensor(long frequency, int dutyCycle)
{
ESP_LOGI("SENSORS", "Setting up moisture sensor");
analogWrite(MOISTURE_SQUARE_WAVE_SIGNAL, frequency, dutyCycle / 255.0, MOISTURE_SQUARE_WAVE_SIGNAL_CHANNEL);
}

void resetToZero()
{
ESP_LOGI("SENSORS", "Resetting moisture sensor to zero");
int strength = 1;
int analogValue = adc_analogReadVoltage(ADC_MOISTURE_SENSOR_CHANNEL);
analogWrite(MOISTURE_SQUARE_WAVE_SIGNAL, 0, 0, MOISTURE_SQUARE_WAVE_SIGNAL_CHANNEL);
Expand All @@ -279,11 +294,13 @@ void resetToZero()

void stopMoistureSensor()
{
ESP_LOGI("SENSORS", "Stopping moisture sensor");
ledc_stop(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_3, 0);
}

bool sensors_read_moisture(sensors_moisture_sensor_output_t *output)
{
ESP_LOGI("SENSORS", "Reading moisture sensor");
resetToZero();
setupMoistureSensor(12800, 2);
bool success = measure_stabilized_output(output);
Expand All @@ -295,6 +312,7 @@ bool sensors_read_moisture(sensors_moisture_sensor_output_t *output)

void sensors_initSensors()
{
ESP_LOGI("SENSORS", "Initializing sensors");
// Set digital output pins
gpio_config_t io_conf = {
.mode = GPIO_MODE_OUTPUT,
Expand Down Expand Up @@ -323,6 +341,7 @@ void sensors_initSensors()

void sensors_deinitSensors()
{
ESP_LOGI("SENSORS", "Deinitializing sensors");
adc_deinitAdc();
deinitAht();
stopMoistureSensor();
Expand All @@ -334,6 +353,7 @@ void sensors_deinitSensors()

void sensors_full_read(sensors_full_data_t *data)
{
ESP_LOGI("SENSORS", "Reading all sensors");
if (!isSensorsInit)
{
sensors_initSensors();
Expand All @@ -355,6 +375,7 @@ void sensors_full_read(sensors_full_data_t *data)

void sensors_playStartupSound()
{
ESP_LOGI("SENSORS", "Playing startup sound");
const int tick = 20;
// d g a d
sensors_playToneSync(587, tick);
Expand All @@ -368,6 +389,7 @@ void sensors_playStartupSound()

void sensors_playShutdownSound()
{
ESP_LOGI("SENSORS", "Playing shutdown sound");
const int tick = 20;
// d a g d
sensors_playToneSync(1175, tick);
Expand Down

0 comments on commit b4c4684

Please sign in to comment.