rework
This commit is contained in:
parent
df6f7bd781
commit
c937fc6656
@ -4,6 +4,7 @@
|
|||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_mac.h"
|
#include "esp_mac.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
#include "APlib.h"
|
#include "APlib.h"
|
||||||
|
|
||||||
static const char *TAG = "wifi AP init";
|
static const char *TAG = "wifi AP init";
|
||||||
@ -20,7 +21,7 @@ static void wifi_event_handler(void* arg, esp_event_base_t event_base,int32_t ev
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void wifi_init_softap(wifi_config_t wifi_config){
|
void wifi_init_softap(wifi_config_t* wifi_config){
|
||||||
//ESP_ERROR_CHECK(esp_netif_init());
|
//ESP_ERROR_CHECK(esp_netif_init());
|
||||||
//ESP_ERROR_CHECK(esp_event_loop_create_default());
|
//ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
esp_netif_create_default_wifi_ap();
|
esp_netif_create_default_wifi_ap();
|
||||||
@ -31,7 +32,7 @@ void wifi_init_softap(wifi_config_t wifi_config){
|
|||||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, wifi_config));
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,3 +51,18 @@ wifi_config_t wifi_config_generator(char SSID[32], char pass[32], uint8_t channe
|
|||||||
config.ap.authmode = WIFI_AUTH_OPEN;
|
config.ap.authmode = WIFI_AUTH_OPEN;
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wifi_config_t* get_ap_config(nvs_handle_t nvs){
|
||||||
|
wifi_config_t* config = malloc(sizeof(wifi_config_t));
|
||||||
|
//TODO: proper secure wifi provisionning
|
||||||
|
size_t str_size=32;
|
||||||
|
ESP_ERROR_CHECK(nvs_get_str(nvs, "ap_ssid", (char*)&config->ap.ssid, &str_size));
|
||||||
|
str_size = 64;
|
||||||
|
ESP_ERROR_CHECK(nvs_get_str(nvs, "ap_pass", (char*)&config->ap.password, &str_size));
|
||||||
|
config->ap.ssid_len = strlen((char*)config->ap.ssid);
|
||||||
|
config->ap.channel = 0; //TODO: channel in nvs?
|
||||||
|
config->ap.max_connection = 10;
|
||||||
|
config->ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
|
||||||
|
if(str_size == 0) config->ap.authmode = WIFI_AUTH_OPEN;
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
idf_component_register(SRCS "APlib.c"
|
idf_component_register(SRCS "APlib.c"
|
||||||
INCLUDE_DIRS "include")
|
INCLUDE_DIRS "include"
|
||||||
|
REQUIRES nvs_flash)
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
#ifndef AP_LIB_H
|
#ifndef AP_LIB_H
|
||||||
#define AP_LIB_H
|
#define AP_LIB_H
|
||||||
|
|
||||||
void wifi_init_softap(wifi_config_t wifi_config);
|
#include "nvs_flash.h"
|
||||||
|
#include "esp_wifi.h"
|
||||||
|
|
||||||
|
void wifi_init_softap(wifi_config_t* wifi_config);
|
||||||
wifi_config_t wifi_config_generator(char SSID[32], char pass[32], uint8_t channel);
|
wifi_config_t wifi_config_generator(char SSID[32], char pass[32], uint8_t channel);
|
||||||
|
wifi_config_t* get_ap_config(nvs_handle_t nvs);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -12,7 +12,7 @@ esp_err_t start_server(HTTP_serve_config_t config, configuration_data_t* data){
|
|||||||
|
|
||||||
HTTP_serve_config_t* config_copy = malloc(sizeof(HTTP_serve_config_t));
|
HTTP_serve_config_t* config_copy = malloc(sizeof(HTTP_serve_config_t));
|
||||||
memcpy(config_copy, &config, sizeof(HTTP_serve_config_t));
|
memcpy(config_copy, &config, sizeof(HTTP_serve_config_t));
|
||||||
serv_config.global_user_ctx = &config;
|
serv_config.global_user_ctx = config_copy;
|
||||||
|
|
||||||
httpd_uri_t uri_api_get = {
|
httpd_uri_t uri_api_get = {
|
||||||
.uri = config.apiUri,
|
.uri = config.apiUri,
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
idf_component_register(SRCS "configuration.c"
|
idf_component_register(SRCS "configuration.c"
|
||||||
INCLUDE_DIRS "include"
|
INCLUDE_DIRS "include"
|
||||||
REQUIRES sensirion_i2c_scd4x)
|
REQUIRES sensirion_i2c_scd4x APlib ledController nvs_flash)
|
||||||
|
@ -1,7 +1,13 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
#include "scd4x_data.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
void func(void)
|
void init_conf_from_nvs(configuration_data_t* conf, nvs_handle_t nvs){
|
||||||
{
|
conf->leds = get_led_configuration(nvs);
|
||||||
|
conf->sensor = get_sensor_configuration(nvs);
|
||||||
|
conf->measure = conf->sensor->measure;
|
||||||
|
conf->ap_config = get_ap_config(nvs);
|
||||||
|
size_t str_size;
|
||||||
|
nvs_get_str(nvs, "name", conf->hostname, &str_size);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,22 @@
|
|||||||
#ifndef CONFIGURATION_H
|
#ifndef CONFIGURATION_H
|
||||||
#define CONFIGURATION_H
|
#define CONFIGURATION_H
|
||||||
|
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
#include "scd4x_data.h"
|
#include "scd4x_data.h"
|
||||||
|
#include "APlib.h"
|
||||||
|
#include "ledController.h"
|
||||||
|
|
||||||
struct configuration_data {
|
struct configuration_data {
|
||||||
scd4x_data_t* measure;
|
scd4x_data_t* measure;
|
||||||
|
scd4x_config_t* sensor;
|
||||||
|
led_disp_config_t* leds;
|
||||||
|
wifi_config_t* ap_config;
|
||||||
|
wifi_config_t* sta_config;
|
||||||
|
char hostname[16];
|
||||||
};
|
};
|
||||||
typedef struct configuration_data configuration_data_t;
|
typedef struct configuration_data configuration_data_t;
|
||||||
|
|
||||||
void func(void);
|
void init_conf_from_nvs(configuration_data_t* conf, nvs_handle_t nvs);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
idf_component_register(SRCS "ledController.c"
|
idf_component_register(SRCS "ledController.c"
|
||||||
INCLUDE_DIRS "include"
|
INCLUDE_DIRS "include"
|
||||||
REQUIRES driver)
|
REQUIRES driver nvs_flash)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define LED_CONT_H
|
#define LED_CONT_H
|
||||||
|
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
struct led_disp_config {
|
struct led_disp_config {
|
||||||
uint8_t output;
|
uint8_t output;
|
||||||
@ -16,5 +17,6 @@ typedef struct led_disp_config led_disp_config_t;
|
|||||||
|
|
||||||
esp_err_t init_led_driver(led_disp_config_t configs[], uint8_t config_nb);
|
esp_err_t init_led_driver(led_disp_config_t configs[], uint8_t config_nb);
|
||||||
esp_err_t update_led_status(led_disp_config_t configs[], uint8_t config_nb, uint16_t co2);
|
esp_err_t update_led_status(led_disp_config_t configs[], uint8_t config_nb, uint16_t co2);
|
||||||
|
led_disp_config_t* get_led_configuration(nvs_handle_t nvs);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "nvs_flash.h"
|
||||||
#include "driver/ledc.h"
|
#include "driver/ledc.h"
|
||||||
#include "ledController.h"
|
#include "ledController.h"
|
||||||
|
|
||||||
@ -40,3 +41,17 @@ esp_err_t update_led_status(led_disp_config_t configs[], uint8_t config_nb, uint
|
|||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
led_disp_config_t* get_led_configuration(nvs_handle_t nvs){
|
||||||
|
uint8_t nb = 0;
|
||||||
|
ESP_ERROR_CHECK(nvs_get_u8(nvs, "led_nb", &nb));
|
||||||
|
led_disp_config_t* led_configs = malloc(nb * sizeof(led_disp_config_t));
|
||||||
|
size_t conf_size = sizeof(led_disp_config_t);
|
||||||
|
for(int i = 0; i < nb; i++){
|
||||||
|
char key[16];
|
||||||
|
sprintf(key, "led%i_conf", i+1);
|
||||||
|
ESP_ERROR_CHECK(nvs_get_blob(nvs, key, &led_configs[i], &conf_size));
|
||||||
|
}
|
||||||
|
return led_configs;
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 52f65047e9a926a6c3ad033d63f14530a276110c
|
Subproject commit df58dce808151d1ab7b8d436e4233acf946cb9b1
|
@ -3,7 +3,10 @@ key,type,encoding,value
|
|||||||
main,namespace,,
|
main,namespace,,
|
||||||
name,data,string,CO2Sense
|
name,data,string,CO2Sense
|
||||||
wireless_conf,data,binary,00000001
|
wireless_conf,data,binary,00000001
|
||||||
sensor_conf,data,binary,00000000
|
ap_ssid,data,string,CO2Sense
|
||||||
|
ap_pass,data,string,testtest
|
||||||
|
sensor_conf,data,u16,20
|
||||||
|
led_nb,data,u8,3
|
||||||
led1_conf,data,hex2bin,0400DC051027640001FF
|
led1_conf,data,hex2bin,0400DC051027640001FF
|
||||||
led2_conf,data,hex2bin,0500BC02DB05640001FF
|
led2_conf,data,hex2bin,0500BC02DB05640001FF
|
||||||
led3_conf,data,hex2bin,06000000BB02640001FF
|
led3_conf,data,hex2bin,06000000BB02640001FF
|
||||||
|
|
@ -32,54 +32,30 @@ void app_main(void){
|
|||||||
nvs_handle_t nvs_handle;
|
nvs_handle_t nvs_handle;
|
||||||
ESP_ERROR_CHECK(nvs_open("main", NVS_READWRITE, &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 LEDs driver
|
||||||
led_disp_config_t* led_configs = generate_led_conf(nvs_handle,LED_NUMBER);
|
init_led_driver(conf->leds, LED_NUMBER);
|
||||||
init_led_driver(led_configs, LED_NUMBER);
|
|
||||||
|
|
||||||
// init wifi AP
|
// init wifi AP
|
||||||
wifi_config_t wifi_config = wifi_config_generator("CO2Sense","testtest",1);
|
//wifi_config_t wifi_config = wifi_config_generator("CO2Sense","testtest",1);
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_netif_init());
|
ESP_ERROR_CHECK(esp_netif_init());
|
||||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||||
|
|
||||||
// start avahi
|
// start avahi
|
||||||
init_avahi(nvs_handle);
|
init_avahi(conf->hostname);
|
||||||
|
|
||||||
wifi_init_softap(wifi_config);
|
wifi_init_softap(conf->ap_config);
|
||||||
|
|
||||||
// mount http server fat partition
|
/*scd4x_data_t sensor_data = {
|
||||||
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_err_t err;
|
|
||||||
err = esp_vfs_fat_rawflash_mount("/http", "http", &mount_config);
|
|
||||||
|
|
||||||
if (err != ESP_OK) {
|
|
||||||
ESP_LOGE("MAIN", "Failed to mount FATFS (%s)", esp_err_to_name(err));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// start http server
|
|
||||||
HTTP_serve_config_t serv_config = {
|
|
||||||
.mountpoint = "/http",
|
|
||||||
.getUri = "/*",
|
|
||||||
.postUri = "/*",
|
|
||||||
.apiUri = "/api",
|
|
||||||
};
|
|
||||||
|
|
||||||
scd4x_data_t sensor_data = {
|
|
||||||
.co2 = 0,
|
.co2 = 0,
|
||||||
.temperature = 0,
|
.temperature = 0,
|
||||||
.humidity = 0,
|
.humidity = 0,
|
||||||
};
|
};*/
|
||||||
|
|
||||||
configuration_data_t conf_data = {
|
init_http_server(conf);
|
||||||
.measure = &sensor_data,
|
|
||||||
};
|
|
||||||
|
|
||||||
ESP_ERROR_CHECK(start_server(serv_config, &conf_data));
|
|
||||||
|
|
||||||
// init scd41 sensor
|
// init scd41 sensor
|
||||||
sensirion_i2c_hal_init(0,1);
|
sensirion_i2c_hal_init(0,1);
|
||||||
@ -94,17 +70,17 @@ void app_main(void){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// start fetch sensor task
|
// start fetch sensor task
|
||||||
scd4x_config_t sensor_conf = {
|
/*scd4x_config_t sensor_conf = {
|
||||||
.delay = 5,
|
.delay = 5,
|
||||||
.measure = &sensor_data,
|
.measure = &sensor_data,
|
||||||
};
|
};*/
|
||||||
TaskHandle_t sensor_fetch_handle;
|
TaskHandle_t sensor_fetch_handle;
|
||||||
xTaskCreate(fetch_sensor_task, "FETCH_SENSOR", 4096, &sensor_conf, tskIDLE_PRIORITY, &sensor_fetch_handle);
|
xTaskCreate(fetch_sensor_task, "FETCH_SENSOR", 4096, conf->sensor, tskIDLE_PRIORITY, &sensor_fetch_handle);
|
||||||
|
|
||||||
while(1){
|
while(1){
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||||
ESP_LOGI("MAIN", "co2 : %u ppm, temp : %d m°C, hum : %d mRH", sensor_data.co2, sensor_data.temperature, sensor_data.humidity);
|
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(led_configs, LED_NUMBER, sensor_data.co2);
|
update_led_status(conf->leds, LED_NUMBER, conf->measure->co2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,23 +104,29 @@ void init_nvs(void){
|
|||||||
ESP_ERROR_CHECK(ret);
|
ESP_ERROR_CHECK(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_avahi(nvs_handle_t nvs){
|
void init_avahi(char* hostname){
|
||||||
|
ESP_LOGI("avahi", "hostname : %s", hostname);
|
||||||
ESP_ERROR_CHECK(mdns_init());
|
ESP_ERROR_CHECK(mdns_init());
|
||||||
char hostname[16];
|
|
||||||
size_t buff_size = sizeof(hostname);
|
|
||||||
ESP_ERROR_CHECK(nvs_get_str(nvs, "name", &hostname[0], &buff_size));
|
|
||||||
ESP_LOGI("avahi", "hosname : %s", hostname);
|
|
||||||
ESP_ERROR_CHECK(mdns_hostname_set(hostname));
|
ESP_ERROR_CHECK(mdns_hostname_set(hostname));
|
||||||
ESP_ERROR_CHECK(mdns_instance_name_set(hostname));
|
ESP_ERROR_CHECK(mdns_instance_name_set(hostname));
|
||||||
}
|
}
|
||||||
|
|
||||||
led_disp_config_t* generate_led_conf(nvs_handle_t nvs, unsigned int nb){
|
void init_http_server(configuration_data_t* main_conf){
|
||||||
led_disp_config_t* led_configs = malloc(nb * sizeof(led_disp_config_t));
|
// mount http server fat partition
|
||||||
size_t conf_size = sizeof(led_disp_config_t);
|
const esp_vfs_fat_mount_config_t mount_config = {
|
||||||
for(int i = 0; i < nb; i++){
|
.max_files = 10,
|
||||||
char key[16];
|
.format_if_mount_failed = false,
|
||||||
sprintf(key, "led%i_conf", i+1);
|
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE
|
||||||
ESP_ERROR_CHECK(nvs_get_blob(nvs, key, &led_configs[i], &conf_size));
|
};
|
||||||
}
|
ESP_ERROR_CHECK(esp_vfs_fat_rawflash_mount("/http", "http", &mount_config));
|
||||||
return led_configs;
|
|
||||||
|
// start http server
|
||||||
|
HTTP_serve_config_t serv_config = {
|
||||||
|
.mountpoint = "/http",
|
||||||
|
.getUri = "/*",
|
||||||
|
.postUri = "/*",
|
||||||
|
.apiUri = "/api",
|
||||||
|
};
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(start_server(serv_config, main_conf));
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
#include "ledController.h"
|
#include "ledController.h"
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
|
#include "configuration.h"
|
||||||
|
|
||||||
void fetch_sensor_task(void* pvParameters);
|
void fetch_sensor_task(void* pvParameters);
|
||||||
void init_nvs(void);
|
void init_nvs(void);
|
||||||
void init_avahi(nvs_handle_t nvs);
|
void init_avahi(char* hostname);
|
||||||
|
void init_http_server(configuration_data_t* main_conf);
|
||||||
led_disp_config_t* generate_led_conf(nvs_handle_t nvs, unsigned int nb);
|
led_disp_config_t* generate_led_conf(nvs_handle_t nvs, unsigned int nb);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user