133 lines
3.5 KiB
C
133 lines
3.5 KiB
C
#include "CO2_Sense.h"
|
|
#include <string.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include <stdio.h>
|
|
#include "nvs_flash.h"
|
|
|
|
#include "lwip/err.h"
|
|
#include "lwip/sys.h"
|
|
|
|
#include "esp_wifi.h"
|
|
#include "esp_log.h"
|
|
#include "esp_vfs_fat.h"
|
|
#include "mdns.h"
|
|
|
|
#include "APlib.h"
|
|
#include "HTTPServe.h"
|
|
#include "ledController.h"
|
|
#include "scd4x_i2c.h"
|
|
#include "sensirion_i2c_hal.h"
|
|
#include "scd4x_data.h"
|
|
|
|
#define LED1_PIN CONFIG_LED_1_PIN
|
|
#define LED2_PIN CONFIG_LED_2_PIN
|
|
#define LED3_PIN CONFIG_LED_3_PIN
|
|
|
|
#define LED_NUMBER 3
|
|
|
|
void app_main(void){
|
|
init_nvs();
|
|
|
|
nvs_handle_t nvs_handle;
|
|
ESP_ERROR_CHECK(nvs_open("main", NVS_READWRITE, &nvs_handle));
|
|
|
|
configuration_data_t* conf = malloc(sizeof(configuration_data_t));
|
|
init_conf_from_nvs(conf, nvs_handle);
|
|
|
|
// init LEDs driver
|
|
init_led_driver(conf->leds, LED_NUMBER);
|
|
|
|
// init wifi AP
|
|
//wifi_config_t wifi_config = wifi_config_generator("CO2Sense","testtest",1);
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
// start avahi
|
|
init_avahi(conf->hostname);
|
|
|
|
wifi_init_softap(conf->ap_config);
|
|
|
|
/*scd4x_data_t sensor_data = {
|
|
.co2 = 0,
|
|
.temperature = 0,
|
|
.humidity = 0,
|
|
};*/
|
|
|
|
init_http_server(conf);
|
|
|
|
// init scd41 sensor
|
|
sensirion_i2c_hal_init(0,1);
|
|
|
|
scd4x_wake_up();
|
|
scd4x_stop_periodic_measurement();
|
|
scd4x_reinit();
|
|
|
|
int16_t error = scd4x_start_periodic_measurement();
|
|
if (error) {
|
|
ESP_LOGE("MAIN", "Error executing scd4x_start_periodic_measurement(): %i\n",error);
|
|
}
|
|
|
|
// start fetch sensor task
|
|
/*scd4x_config_t sensor_conf = {
|
|
.delay = 5,
|
|
.measure = &sensor_data,
|
|
};*/
|
|
TaskHandle_t sensor_fetch_handle;
|
|
xTaskCreate(fetch_sensor_task, "FETCH_SENSOR", 4096, conf->sensor, tskIDLE_PRIORITY, &sensor_fetch_handle);
|
|
|
|
while(1){
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
ESP_LOGI("MAIN", "co2 : %u ppm, temp : %d m°C, hum : %d mRH", conf->measure->co2, conf->measure->temperature, conf->measure->humidity);
|
|
update_led_status(conf->leds, LED_NUMBER, conf->measure->co2);
|
|
}
|
|
}
|
|
|
|
void fetch_sensor_task(void* pvParameters){
|
|
scd4x_config_t* conf = (scd4x_config_t*) pvParameters;
|
|
scd4x_data_t* measures = conf->measure;
|
|
while(1){
|
|
vTaskDelay(conf->delay * 1000 / portTICK_PERIOD_MS);
|
|
int16_t error = scd4x_read_measurement(&measures->co2, &measures->temperature, &measures->humidity);
|
|
if (error)
|
|
ESP_LOGE("sensor fetch", "Error executing scd4x_read_measurement(): %i\n", error);
|
|
}
|
|
}
|
|
|
|
void init_nvs(void){
|
|
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);
|
|
}
|
|
|
|
void init_avahi(char* hostname){
|
|
ESP_LOGI("avahi", "hostname : %s", hostname);
|
|
ESP_ERROR_CHECK(mdns_init());
|
|
ESP_ERROR_CHECK(mdns_hostname_set(hostname));
|
|
ESP_ERROR_CHECK(mdns_instance_name_set(hostname));
|
|
}
|
|
|
|
void init_http_server(configuration_data_t* main_conf){
|
|
// mount http server fat partition
|
|
const esp_vfs_fat_mount_config_t mount_config = {
|
|
.max_files = 10,
|
|
.format_if_mount_failed = false,
|
|
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE
|
|
};
|
|
ESP_ERROR_CHECK(esp_vfs_fat_rawflash_mount("/http", "http", &mount_config));
|
|
|
|
// start http server
|
|
HTTP_serve_config_t serv_config = {
|
|
.mountpoint = "/http",
|
|
.getUri = "/*",
|
|
.postUri = "/*",
|
|
.apiUri = "/api",
|
|
};
|
|
|
|
ESP_ERROR_CHECK(start_server(serv_config, main_conf));
|
|
}
|