Thu, 06 Apr 2023 20:49:52 +0200
Added nvsio utilities to make read/write to nvs namespace easier. Added variables for deep sleep to nvs namespace. In task_wifi, removed some init parameters that might add to instable wifi connect problems.
main/CMakeLists.txt | file | annotate | diff | comparison | revisions | |
main/config.h | file | annotate | diff | comparison | revisions | |
main/iotbalkon.c | file | annotate | diff | comparison | revisions | |
main/nvsio.c | file | annotate | diff | comparison | revisions | |
main/nvsio.h | file | annotate | diff | comparison | revisions | |
main/task_mqtt.c | file | annotate | diff | comparison | revisions | |
main/task_wifi.c | file | annotate | diff | comparison | revisions |
--- a/main/CMakeLists.txt Thu Apr 06 13:22:03 2023 +0200 +++ b/main/CMakeLists.txt Thu Apr 06 20:49:52 2023 +0200 @@ -1,2 +1,2 @@ -idf_component_register(SRCS config.c iotbalkon.c task_bmp280.c task_ina219.c task_apds9930.c task_wifi.c task_mqtt.c task_out.c xutil.c +idf_component_register(SRCS config.c iotbalkon.c task_bmp280.c task_ina219.c task_apds9930.c task_wifi.c task_mqtt.c task_out.c xutil.c nvsio.c INCLUDE_DIRS ".")
--- a/main/config.h Thu Apr 06 13:22:03 2023 +0200 +++ b/main/config.h Thu Apr 06 20:49:52 2023 +0200 @@ -28,6 +28,7 @@ #include "driver/ledc.h" #include "esp_log.h" #include "esp_system.h" +#include "esp_sleep.h" #include "esp_timer.h" #include "esp_app_desc.h" #include "esp_wifi.h" @@ -56,7 +57,7 @@ #include "task_mqtt.h" #include "task_out.h" #include "xutil.h" - +#include "nvsio.h" #endif
--- a/main/iotbalkon.c Thu Apr 06 13:22:03 2023 +0200 +++ b/main/iotbalkon.c Thu Apr 06 20:49:52 2023 +0200 @@ -31,6 +31,8 @@ static TaskHandle_t xTaskWifi = NULL; static TaskHandle_t xTaskOUT = NULL; +#define DS_TIME 60 +#define DS_CURRENT 14.6 #define MAX_LOOPS 32 #define SUB_TIME 1000 @@ -87,7 +89,8 @@ uint8_t Relay2; uint8_t Dimmer3; uint8_t Dimmer4; - +uint8_t DS_Active; +uint32_t DS_Time; // 0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100% @@ -295,7 +298,6 @@ void app_main(void) { uint64_t totalTime, gTimeInMillis; - nvs_handle_t my_handle; #ifdef CONFIG_CODE_PRODUCTION ESP_LOGI(TAG, "Starting production"); @@ -314,48 +316,16 @@ } ESP_ERROR_CHECK(err); - err = nvs_open("balkon", NVS_READWRITE, &my_handle); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Error (%s) opening NVS handle 'balkon'", esp_err_to_name(err)); - } else { - err = nvs_get_u8(my_handle, (char *)"out1", &Relay1); - if (err == ESP_ERR_NVS_NOT_FOUND) { - err = nvs_set_u8(my_handle, (char *)"out1", 0); - Relay1 = 0; - } - if (err != ESP_OK) { - ESP_LOGE(TAG, "Error (%s) opening NVS handle 'balkon', key 'out1'", esp_err_to_name(err)); - } - - err = nvs_get_u8(my_handle, (char *)"out2", &Relay2); - if (err == ESP_ERR_NVS_NOT_FOUND) { - err = nvs_set_u8(my_handle, (char *)"out2", 0); - Relay2 = 0; - } - if (err != ESP_OK) { - ESP_LOGE(TAG, "Error (%s) opening NVS handle 'balkon', key 'out2'", esp_err_to_name(err)); - } - - err = nvs_get_u8(my_handle, (char *)"out3", &Dimmer3); - if (err == ESP_ERR_NVS_NOT_FOUND) { - err = nvs_set_u8(my_handle, (char *)"out3", 0); - Dimmer3 = 0; - } - if (err != ESP_OK) { - ESP_LOGE(TAG, "Error (%s) opening NVS handle 'balkon', key 'out3'", esp_err_to_name(err)); - } - - err = nvs_get_u8(my_handle, (char *)"out4", &Dimmer4); - if (err == ESP_ERR_NVS_NOT_FOUND) { - err = nvs_set_u8(my_handle, (char *)"out4", 0); - Dimmer4 = 0; - } - if (err != ESP_OK) { - ESP_LOGE(TAG, "Error (%s) opening NVS handle 'balkon', key 'out4'", esp_err_to_name(err)); - } - - nvs_commit(my_handle); - nvs_close(my_handle); + nvsio_init(); + Relay1 = nvsio_read_u8((char *)"out1"); + Relay2 = nvsio_read_u8((char *)"out2"); + Dimmer3 = nvsio_read_u8((char *)"out3"); + Dimmer4 = nvsio_read_u8((char *)"out4"); + DS_Active = nvsio_read_u8((char *)"ds_active"); + DS_Time = nvsio_read_u32((char *)"ds_time"); + if (DS_Time == 0) { + DS_Time = DS_TIME; + nvsio_write_u32((char *)"ds_time", DS_Time); } gLastTime = millis(); @@ -428,9 +398,10 @@ */ int State = State_Init; int OldState = State_Init + 1; + uint8_t ds_time = DS_Time; - while (1) { + while (0) { // request_ina219(); request_apds9930(); vTaskDelay(5000 / portTICK_PERIOD_MS); @@ -531,11 +502,11 @@ } } - // if (EEPROM.read(EM_DS_Active)) { - // totalTime += EEPROM.read(EM_DS_Time) * 1000; - // batteryCurrent += DS_CURRENT * EEPROM.read(EM_DS_Time) * 1000; - // // Serial.printf("Added %d totalTime %d\n", EEPROM.read(EM_DS_Time) * 1000, totalTime); - // } + if (DS_Active) { + totalTime += DS_Time * 1000; + batteryCurrent += DS_CURRENT * DS_Time * 1000; + ESP_LOGI(TAG, "Added %ld totalTime %lld", DS_Time * 1000, totalTime); + } // If valid measurements if (loops) { @@ -586,34 +557,34 @@ * If nothing on, do a deep sleep. */ if (Relay1 || Relay2 || Dimmer3 || Dimmer4) { - // if (EEPROM.read(EM_DS_Active)) { - // EEPROM.write(EM_DS_Active, 0); - // EEPROM.commit(); - } + if (DS_Active != 0) { + DS_Active = 0; + nvsio_write_u8((char *)"ds_active", 0); + } - // // Active mode, 60 seconds loop - ST_LOOPS = 6; - gTimeNext = millis() + ST_INTERVAL; - ESP_LOGI(TAG, "Start sleeploops"); - State = State_Wait; - // #if Debug == true - // Serial.println(F("Start sleeploops")); - // #endif - // State = State_Wait; - // } else { - // ds_time = DS_TIME; + // Active mode, 60 seconds loop + ST_LOOPS = 6; + gTimeNext = millis() + ST_INTERVAL; + ESP_LOGI(TAG, "Start sleeploops"); + State = State_Wait; + } else { + ds_time = DS_TIME; // if (solarVolts < 6) { // // At night, increase the deep-sleep time. // ds_time *= 4; // } + nvsio_write_u8((char *)"ds_active", 1); + DS_Active = 1; + nvsio_write_u32((char *)"ds_time", ds_time); + DS_Time = ds_time; // if ((! EEPROM.read(EM_DS_Active)) || (EEPROM.read(EM_DS_Time) != ds_time)) { // EEPROM.write(EM_DS_Active, 1); // EEPROM.write(EM_DS_Time, ds_time); // EEPROM.commit(); // Serial.println("wrote new deep-sleep settings"); // } - // State = State_GoSleep; - // } + State = State_GoSleep; + } break; case State_Wait: if (gTimeInMillis > gTimeNext) { @@ -638,12 +609,9 @@ } break; - case State_GoSleep: ESP_LOGE(TAG, "Entered GoSleep -- not supported"); - // ds_time = EEPROM.read(EM_DS_Time); - // #if Debug == true - // Serial.printf("Going to deep-sleep for %d seconds\n", ds_time); - // #endif - // ESP.deepSleep(ds_time * 1e6); + case State_GoSleep: ESP_LOGI(TAG, "Going to deep-sleep for %ld seconds", DS_Time); + ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(DS_Time * 1e6)); + esp_deep_sleep_start(); break; } vTaskDelay(20 / portTICK_PERIOD_MS);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/nvsio.c Thu Apr 06 20:49:52 2023 +0200 @@ -0,0 +1,139 @@ +/***************************************************************************** + * Copyright (C) 2023 + * + * Michiel Broek <mbroek at mbse dot eu> + * + * This file is part of the ESP32 iotbalkon application. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2, or (at your option) any + * later version. + * + * mbsePi-apps is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ThermFerm; see the file COPYING. If not, write to the Free + * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + *****************************************************************************/ + +#include "config.h" + +#define NVSIO_Namespace "balkon" + +static const char *TAG = "nvsio"; + + +void nvsio_init(void) +{ + esp_err_t err = ESP_OK; + nvs_handle_t my_handle; + + err = nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Error (%s) opening NVS handle '%s'", esp_err_to_name(err), NVSIO_Namespace); + } else { + nvs_commit(my_handle); + nvs_close(my_handle); + } +} + + +uint8_t nvsio_read_u8(char *key) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint8_t val = 0; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u8(my_handle, key, &val); + if (err == ESP_ERR_NVS_NOT_FOUND) { + err = nvs_set_u8(my_handle, key, 0); + val = 0; + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_read_u8(%s) created", key); + } + if (err != ESP_OK) { + ESP_LOGE(TAG, "nvsio_read_u8(%s) Error (%s) opening NVS namespace '%s'", key, esp_err_to_name(err), NVSIO_Namespace); + } + nvs_close(my_handle); + } + + return val; +} + + +void nvsio_write_u8(char *key, uint8_t val) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint8_t tmp; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u8(my_handle, key, &tmp); + if (err == ESP_ERR_NVS_NOT_FOUND) { + nvs_set_u8(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u8(%s) created val %02x", key, val); + } else if (err == ESP_OK && tmp != val) { + nvs_set_u8(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u8(%s) updated val %02x", key, val); + } else { + ESP_LOGI(TAG, "nvsio_write_u8(%s) already val %02x", key, val); + } + nvs_close(my_handle); + } +} + + +uint32_t nvsio_read_u32(char *key) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint32_t val = 0; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u32(my_handle, key, &val); + if (err == ESP_ERR_NVS_NOT_FOUND) { + err = nvs_set_u32(my_handle, key, 0); + val = 0; + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_read_u32(%s) created", key); + } + if (err != ESP_OK) { + ESP_LOGE(TAG, "nvsio_read_u32(%s) Error (%s) opening NVS namespace '%s'", key, esp_err_to_name(err), NVSIO_Namespace); + } + nvs_close(my_handle); + } + + return val; +} + + +void nvsio_write_u32(char *key, uint32_t val) +{ + nvs_handle_t my_handle; + esp_err_t err; + uint32_t tmp; + + if (nvs_open(NVSIO_Namespace, NVS_READWRITE, &my_handle) == ESP_OK) { + err = nvs_get_u32(my_handle, key, &tmp); + if (err == ESP_ERR_NVS_NOT_FOUND) { + nvs_set_u32(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u32(%s) created val %08lx", key, val); + } else if (err == ESP_OK && tmp != val) { + nvs_set_u32(my_handle, key, val); + nvs_commit(my_handle); + ESP_LOGI(TAG, "nvsio_write_u32(%s) updated val %08lx", key, val); + } else { + ESP_LOGI(TAG, "nvsio_write_u32(%s) already val %08lx", key, val); + } + nvs_close(my_handle); + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/nvsio.h Thu Apr 06 20:49:52 2023 +0200 @@ -0,0 +1,43 @@ +/** + * @file nvsio.h + * @brief NVS namespace io utilities. + */ +#ifndef NVSIO_H +#define NVSIO_H + + +/** + * @brief Initilise NVS namespace and create variables if they don't exist. + */ +void nvsio_init(void); + +/** + * @brief Read 8 bit value from NVS. Create variable if new. + * @param key The variable name in the nvs namespace. + * @return The value or 0 if it's a new variable. + */ +uint8_t nvsio_read_u8(char *key); + +/** + * @brief Write 8 bit value to NVS. Create variable if new. + * @param key The variable name in the nvs namespace. + * @param val The value to store. + */ +void nvsio_write_u8(char *key, uint8_t val); + +/** + * @brief Read 32 bit value from NVS. Create variable if new. + * @param key The variable name in the nvs namespace. + * @return The value or 0 if it's a new variable. + */ +uint32_t nvsio_read_u32(char *key); + +/** + * @brief Write 32 bit value to NVS. Create variable if new. + * @param key The variable name in the nvs namespace. + * @param val The value to store. + */ +void nvsio_write_u32(char *key, uint32_t val); + + +#endif
--- a/main/task_mqtt.c Thu Apr 06 13:22:03 2023 +0200 +++ b/main/task_mqtt.c Thu Apr 06 20:49:52 2023 +0200 @@ -243,7 +243,6 @@ static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event) { char *topic = NULL; - nvs_handle_t my_handle; switch (event->event_id) { @@ -287,7 +286,6 @@ case MQTT_EVENT_DATA: ESP_LOGI(TAG, "MQTT_EVENT_DATA len=%d", event->data_len); bool gotit = false; - esp_err_t err; char data[65]; if (event->data_len < 65) snprintf(data, 64, "%.*s", event->data_len, event->data); @@ -299,15 +297,8 @@ if (strncmp(topic, event->topic, event->topic_len) == 0) { ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); gotit = true; - if ((uint8_t)atoi(data) != Relay1) { - Relay1 = (uint8_t)atoi(data); - err = nvs_open("balkon", NVS_READWRITE, &my_handle); - if (err == ESP_OK) { - nvs_set_u8(my_handle, (char *)"out1", Relay1); - nvs_commit(my_handle); - nvs_close(my_handle); - } - } + Relay1 = (uint8_t)atoi(data); + nvsio_write_u8((char *)"out1", Relay1); } free(topic); topic = NULL; @@ -317,15 +308,8 @@ if (strncmp(topic, event->topic, event->topic_len) == 0) { ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); gotit = true; - if ((uint8_t)atoi(data) != Relay2) { - Relay2 = (uint8_t)atoi(data); - err = nvs_open("balkon", NVS_READWRITE, &my_handle); - if (err == ESP_OK) { - nvs_set_u8(my_handle, (char *)"out2", Relay2); - nvs_commit(my_handle); - nvs_close(my_handle); - } - } + Relay2 = (uint8_t)atoi(data); + nvsio_write_u8((char *)"out2", Relay2); } free(topic); topic = NULL; @@ -335,15 +319,8 @@ if (strncmp(topic, event->topic, event->topic_len) == 0) { ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); gotit = true; - if ((uint8_t)atoi(data) != Dimmer3) { - Dimmer3 = (uint8_t)atoi(data); - err = nvs_open("balkon", NVS_READWRITE, &my_handle); - if (err == ESP_OK) { - nvs_set_u8(my_handle, (char *)"out3", Dimmer3); - nvs_commit(my_handle); - nvs_close(my_handle); - } - } + Dimmer3 = (uint8_t)atoi(data); + nvsio_write_u8((char *)"out3", Dimmer3); } free(topic); topic = NULL; @@ -353,15 +330,8 @@ if (strncmp(topic, event->topic, event->topic_len) == 0) { ESP_LOGI(TAG, "Got %s `%s' %d", topic, data, atoi(data)); gotit = true; - if ((uint8_t)atoi(data) != Dimmer4) { - Dimmer4 = (uint8_t)atoi(data); - err = nvs_open("balkon", NVS_READWRITE, &my_handle); - if (err == ESP_OK) { - nvs_set_u8(my_handle, (char *)"out4", Dimmer4); - nvs_commit(my_handle); - nvs_close(my_handle); - } - } + Dimmer4 = (uint8_t)atoi(data); + nvsio_write_u8((char *)"out4", Dimmer4); } free(topic); topic = NULL;
--- a/main/task_wifi.c Thu Apr 06 13:22:03 2023 +0200 +++ b/main/task_wifi.c Thu Apr 06 20:49:52 2023 +0200 @@ -19,9 +19,6 @@ .ssid = ESP_WIFI_SSID, .password = ESP_WIFI_PASS, .scan_method = WIFI_FAST_SCAN, - .sort_method = WIFI_CONNECT_AP_BY_SECURITY, - .threshold.rssi = -127, - .threshold.authmode = WIFI_AUTH_WPA2_PSK, }, }; WIFI_State *wifi_state = NULL; ///< Public state for other tasks. @@ -187,7 +184,7 @@ esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); - ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, /*IP_EVENT_STA_GOT_IP*/ ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_Config) );