182 lines
6.3 KiB
C
182 lines
6.3 KiB
C
#include <stdio.h>
|
|
#include "esp_log.h"
|
|
#include "driver/i2c.h"
|
|
#include <esp_system.h>
|
|
#include <bmp280.h>
|
|
#include <dht.h>
|
|
#include <aht.h>
|
|
#include <veml7700.h>
|
|
#include <string.h>
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
#include "freertos/timers.h"
|
|
|
|
#include "http_metrics.h"
|
|
|
|
#define I2C_MASTER_SCL 19
|
|
#define I2C_MASTER_SDA 18
|
|
|
|
//#define BMP280
|
|
//#define DHT11
|
|
//#define AHT10
|
|
//#define VEML7700
|
|
|
|
#ifdef DHT11
|
|
#define CONFIG_EXAMPLE_DATA_GPIO 17
|
|
#define SENSOR_TYPE DHT_TYPE_DHT11
|
|
#endif
|
|
|
|
#ifdef AHT10
|
|
#define AHT_TYPE AHT_TYPE_AHT1x
|
|
#endif
|
|
|
|
static const char *TAG = "smart-oil-heater-control-system";
|
|
|
|
void app_main(void)
|
|
{
|
|
ESP_LOGI(TAG, "starting ...");
|
|
|
|
//Initialize NVS
|
|
esp_err_t ret = nvs_flash_init();
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
|
{
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
ret = nvs_flash_init();
|
|
}
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
connect_wifi(); //will return if successful
|
|
setup_server();
|
|
|
|
sMetric aMetrics[METRIC_MAX_COUNT];
|
|
|
|
/*Sensor Init*/
|
|
ESP_ERROR_CHECK(i2cdev_init());
|
|
|
|
#ifdef BMP280
|
|
bmp280_params_t params;
|
|
bmp280_init_default_params(¶ms);
|
|
bmp280_t devBMP280;
|
|
memset(&devBMP280, 0, sizeof(bmp280_t));
|
|
ESP_ERROR_CHECK(bmp280_init_desc(&devBMP280, BMP280_I2C_ADDRESS_0, 0, I2C_MASTER_SDA, I2C_MASTER_SCL));
|
|
ESP_ERROR_CHECK(bmp280_init(&devBMP280, ¶ms));
|
|
bool bme280p = devBMP280.id == BME280_CHIP_ID;
|
|
ESP_LOGI(TAG, "BMP280: found %s\n", bme280p ? "BME280" : "BMP280");
|
|
#endif
|
|
|
|
#ifdef AHT10
|
|
aht_t devAHT10 = {0};
|
|
devAHT10.mode = AHT_MODE_NORMAL;
|
|
devAHT10.type = AHT_TYPE;
|
|
ESP_ERROR_CHECK(aht_init_desc(&devAHT10, AHT_I2C_ADDRESS_GND, 0, I2C_MASTER_SDA, I2C_MASTER_SCL));
|
|
ESP_ERROR_CHECK(aht_init(&devAHT10));
|
|
#endif
|
|
|
|
#ifdef VEML7700
|
|
i2c_dev_t veml7700_device;
|
|
veml7700_config_t veml7700_configuration;
|
|
|
|
memset(&veml7700_device, 0, sizeof(i2c_dev_t));
|
|
memset(&veml7700_configuration, 0, sizeof(veml7700_config_t));
|
|
|
|
// initialize the device struct
|
|
ESP_ERROR_CHECK(veml7700_init_desc(&veml7700_device, I2C_NUM_0, I2C_MASTER_SDA, I2C_MASTER_SCL));
|
|
|
|
// check if the device is available
|
|
ESP_ERROR_CHECK(veml7700_probe(&veml7700_device));
|
|
|
|
/* set configuration parameters
|
|
* select gain 1/8 coastest resolution but the sensor will most likely not
|
|
* over-saturated
|
|
*/
|
|
veml7700_configuration.gain = VEML7700_GAIN_DIV_8;
|
|
|
|
/* set the time to integrate the light values. more time will lead to a finer
|
|
* resolution but will be over-saturated earlier
|
|
*/
|
|
veml7700_configuration.integration_time = VEML7700_INTEGRATION_TIME_100MS;
|
|
|
|
// interrupt is not used
|
|
veml7700_configuration.persistence_protect = VEML7700_PERSISTENCE_PROTECTION_4;
|
|
veml7700_configuration.interrupt_enable = 1;
|
|
veml7700_configuration.shutdown = 0;
|
|
|
|
// write the configuration to the device
|
|
ESP_ERROR_CHECK(veml7700_set_config(&veml7700_device, &veml7700_configuration));
|
|
|
|
#endif
|
|
|
|
ESP_LOGI(TAG, "running");
|
|
|
|
while (1)
|
|
{
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
|
uint16_t u16MetricCounter = 0U;
|
|
|
|
/*Wifi RSSI*/
|
|
wifi_ap_record_t ap;
|
|
esp_wifi_sta_get_ap_info(&ap);
|
|
printf("WiFi RSSI: %d\n", ap.rssi);
|
|
strcpy(aMetrics[u16MetricCounter].caMetricName, "wifi_rssi");
|
|
aMetrics[0].fMetricValue = ap.rssi;
|
|
u16MetricCounter++;
|
|
|
|
#ifdef BMP280
|
|
if ((bmp280_read_float(&devBMP280, &aMetrics[u16MetricCounter].fMetricValue, &aMetrics[u16MetricCounter+1].fMetricValue, 0) == ESP_OK))
|
|
{
|
|
strcpy(aMetrics[u16MetricCounter].caMetricName, "bmp280_temperature");
|
|
strcpy(aMetrics[u16MetricCounter+1].caMetricName, "bmp280_pressure");
|
|
printf("(BMP280) Temperature: %.2f C, Pressure: %.2f Pa\n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue);
|
|
u16MetricCounter++;
|
|
u16MetricCounter++;
|
|
} else {
|
|
printf("(BMP280) Temperature/pressure reading failed\n");
|
|
}
|
|
#endif
|
|
|
|
#ifdef AHT10
|
|
if ((aht_get_data(&devAHT10, &aMetrics[u16MetricCounter].fMetricValue, &aMetrics[u16MetricCounter+1].fMetricValue) == ESP_OK))
|
|
{
|
|
strcpy(aMetrics[u16MetricCounter].caMetricName, "aht10_temperature");
|
|
strcpy(aMetrics[u16MetricCounter+1].caMetricName, "aht10_humidity");
|
|
printf("(AHT10) Temperature: %.2f C, Humidity: %.2f %% \n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue);
|
|
u16MetricCounter++;
|
|
u16MetricCounter++;
|
|
} else {
|
|
printf("(AHT10) Temperature/Humidity reading failed\n");
|
|
}
|
|
#endif
|
|
|
|
#ifdef DHT11
|
|
if ((dht_read_float_data(SENSOR_TYPE, CONFIG_EXAMPLE_DATA_GPIO, &aMetrics[u16MetricCounter].fMetricValue, &aMetrics[u16MetricCounter+1].fMetricValue) == ESP_OK))
|
|
{
|
|
strcpy(aMetrics[u16MetricCounter].caMetricName, "dht11_humidity");
|
|
strcpy(aMetrics[u16MetricCounter+1].caMetricName, "dht11_temperature");
|
|
printf("(DHT11) Humidity: %.2f %%, Temperature: %.2f C\n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue);
|
|
u16MetricCounter++;
|
|
u16MetricCounter++;
|
|
} else {
|
|
printf("(DHT11) Temperature/pressure reading failed\n");
|
|
}
|
|
#endif
|
|
|
|
#ifdef VEML7700
|
|
uint32_t u32AmbientLight = 0U;
|
|
uint32_t u32WhiteChannel = 0U;
|
|
if ((veml7700_get_ambient_light(&veml7700_device, &veml7700_configuration, &u32AmbientLight) == ESP_OK) && (veml7700_get_white_channel(&veml7700_device, &veml7700_configuration, &u32WhiteChannel) == ESP_OK))
|
|
{
|
|
aMetrics[u16MetricCounter].fMetricValue = u32AmbientLight;
|
|
aMetrics[u16MetricCounter+1].fMetricValue = u32WhiteChannel;
|
|
strcpy(aMetrics[u16MetricCounter].caMetricName, "veml7700_ambient_light");
|
|
strcpy(aMetrics[u16MetricCounter+1].caMetricName, "veml7700_white_channel");
|
|
printf("(VEML7700) Ambient light: %.2f lux, White channel: %.2f lux\n", aMetrics[u16MetricCounter].fMetricValue, aMetrics[u16MetricCounter+1].fMetricValue);
|
|
u16MetricCounter++;
|
|
u16MetricCounter++;
|
|
} else {
|
|
printf("(VEML7700) light reading failed\n");
|
|
}
|
|
#endif
|
|
vSetMetrics(aMetrics, u16MetricCounter);
|
|
}
|
|
}
|