init
This commit is contained in:
commit
005264adc1
4 changed files with 123 additions and 0 deletions
5
CMakeLists.txt
Normal file
5
CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
idf_component_register(SRCS
|
||||
"main/esp32-gp2y.c"
|
||||
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
55
README.md
Normal file
55
README.md
Normal file
|
@ -0,0 +1,55 @@
|
|||
# ESP32 GP2Y Dust Sensor
|
||||
|
||||
## Install
|
||||
|
||||
Designed to be used as an `esp-idf component`. Suggested usage is with `git submodule`
|
||||
|
||||
```
|
||||
git submodule add https://git.oit.cloud/morgan/esp32-gp2y.git components/esp32-gp2y/
|
||||
git submodule update --init
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### `typedef gp2y_cfg_t`
|
||||
`float K` Sensitivity. Default: 0.5
|
||||
`float offset_voltage` Voltage level at 0ug/m3 dust. Defaults to 0.6v, dynamically lowers.
|
||||
`int8_t pin_led` Pin to pulse LED on.
|
||||
`adc1_channel_t adc_channel` ADC channel to read on. See IDF Guide to get pin to channel reference.
|
||||
`adc_unit_t adc_unit` ADC unit to read on.
|
||||
|
||||
### `int32_t gp2y_read(gp2y_cfg_t *cfg, uint8_t samples)`
|
||||
Passing a pointer to `gp2y_cfg_t` and number of samples to take. 100 is a good starting point.
|
||||
|
||||
|
||||
## Example
|
||||
```
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/adc.h"
|
||||
#include "esp32-gp2y.h"
|
||||
|
||||
#define PIN_LED 5
|
||||
|
||||
void app_main() {
|
||||
gp2y_cfg_t gp2y_cfg = GP2Y_DEFAULT_CFG;
|
||||
gp2y_cfg.adc_channel = channel;
|
||||
gp2y_cfg.pin_led = PIN_LED;
|
||||
|
||||
gpio_config_t io_conf;
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
io_conf.pin_bit_mask = (1ULL<<gp2y_cfg.pin_led);
|
||||
io_conf.pull_down_en = 1;
|
||||
io_conf.pull_up_en = 0;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
adc1_config_channel_atten(gp2y_cfg.adc_channel, ADC_ATTEN_DB_11);
|
||||
|
||||
while (1) {
|
||||
printf("aqi: %dug/m3\n", gp2y_read(&gp2y_cfg, 100));
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
```
|
21
include/esp32-gp2y.h
Normal file
21
include/esp32-gp2y.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef _LORA32_H__
|
||||
#define _LORA32_H__
|
||||
|
||||
typedef struct {
|
||||
float K;
|
||||
float offset_voltage;
|
||||
int8_t pin_led;
|
||||
adc1_channel_t adc_channel;
|
||||
adc_unit_t adc_unit;
|
||||
} gp2y_cfg_t;
|
||||
|
||||
#define GP2Y_DEFAULT_CFG {\
|
||||
.K = 0.5,\
|
||||
.offset_voltage = 0.6,\
|
||||
.pin_led = -1,\
|
||||
.adc_channel = -1\
|
||||
}
|
||||
|
||||
int32_t gp2y_read(gp2y_cfg_t *cfg, uint8_t samples);
|
||||
|
||||
#endif // _ESP32_GP2Y_H__
|
42
main/esp32-gp2y.c
Normal file
42
main/esp32-gp2y.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
#include "driver/adc.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp32-gp2y.h"
|
||||
|
||||
int32_t gp2y_read(gp2y_cfg_t *cfg, uint8_t samples) {
|
||||
int32_t accum = 0;
|
||||
|
||||
for(uint8_t i = 0; i < samples; i++) {
|
||||
portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
|
||||
taskENTER_CRITICAL(&myMutex);
|
||||
|
||||
gpio_set_level(cfg->pin_led, 0);
|
||||
usleep(280);
|
||||
|
||||
accum += adc1_get_raw(cfg->adc_channel);
|
||||
|
||||
gpio_set_level(cfg->pin_led, 1);
|
||||
|
||||
taskEXIT_CRITICAL(&myMutex);
|
||||
|
||||
usleep(9620);
|
||||
}
|
||||
|
||||
float avg_adc = (accum / samples);
|
||||
float avg_volt = avg_adc / 4095 * 3.3;
|
||||
float dV = avg_volt - cfg->offset_voltage;
|
||||
|
||||
// recalibrate offset_volt to lowest value seen
|
||||
if(dV < 0) {
|
||||
dV = 0;
|
||||
cfg->offset_voltage = avg_volt;
|
||||
}
|
||||
|
||||
return dV / cfg->K * 100.0;
|
||||
}
|
Loading…
Reference in a new issue