Al contrario di Arduino dove i dati erano salvati su SD card qui vengono salvati sulla memoria interna tramite Littefs. Al primo avvio viene cancellato il file dati poi vengono salvati i dati in append dell'ADC, inoltre
E' stato usato il deep sleep tramite RTC. In questo modo la ESP32 effettua un riavvio completo ogni tot secondi..la variabile conta serve come contatore progressivo dei riavvii in modo che al primo ciclo vengano eseguiti specifici. la variabile conta e' dichiarata come RTC_DATA_ATTR in modo da non essere volatile ed essere preservata nella fase di deep sleep
Anche se non precisssimo quando in deep l'RTC continua a funzionare...si setta quindi la data ed ora al primo riavvio e poi si legge il tempo aggiornato ai successivi riavvii.
Usando lo sketch della sola lettura analogica di un pin ESP32 consuma circa 19 mA
in deep sleep il consumo crolla al livello delle decine di microA tanto che la mia schedina non riesce a rilevarlo
include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include "sdkconfig.h"
#include "soc/soc_caps.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_sleep.h"
#include "esp_log.h"
#include "driver/rtc_io.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "deep_sleep_example.h"
#include <time.h>
#include <sys/time.h>
#include "driver/gpio.h"
#define PIN GPIO_NUM_35
#include "esp_system.h"
#include "spi_flash_mmap.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_littlefs.h"
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "driver/adc.h"
#include "esp_adc_cal.h"
/*
#if SOC_RTC_FAST_MEM_SUPPORTED
static RTC_DATA_ATTR struct timeval sleep_enter_time;
#else
static struct timeval sleep_enter_time;
#endif
*/
RTC_DATA_ATTR int conta =0;
struct tm tm;
static void deep_sleep_task(void *args)
{
esp_deep_sleep_start();
}
static void deep_sleep_register_rtc_timer_wakeup(void)
{
const int wakeup_time_sec = 15;
conta = conta + 1;
gpio_set_direction(PIN, GPIO_MODE_OUTPUT);
gpio_set_level(PIN, 0);
vTaskDelay(100);
gpio_set_level(PIN, 1);
struct timeval tv;
char buffer[30];
time_t curtime;
gettimeofday(&tv, NULL);
curtime=tv.tv_sec;
strftime(buffer,30,"%m-%d-%Y %T.",localtime(&curtime));
printf("Orario %s%ld\n",buffer,tv.tv_usec);
esp_adc_cal_characteristics_t adc1_chars;
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
adc1_config_width(ADC_WIDTH_BIT_DEFAULT);
adc1_config_channel_atten(ADC1_CHANNEL_4, ADC_ATTEN_DB_11);
int adc_value = adc1_get_raw(ADC1_CHANNEL_4);
static const char *TAG = "demo_esp_littlefs";
ESP_LOGI(TAG, "Initializing LittelFS");
esp_vfs_littlefs_conf_t conf = {
.base_path = "/littlefs",
.partition_label = "littlefs",
.format_if_mount_failed = true,
.dont_mount = false,
};
// Use settings defined above to initialize and mount LittleFS filesystem.
// Note: esp_vfs_littlefs_register is an all-in-one convenience function.
esp_err_t ret = esp_vfs_littlefs_register(&conf);
if (ret != ESP_OK)
{
if (ret == ESP_FAIL)
{
ESP_LOGE(TAG, "Failed to mount or format filesystem");
}
else if (ret == ESP_ERR_NOT_FOUND)
{
ESP_LOGE(TAG, "Failed to find LittleFS partition");
}
else
{
ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
}
return;
}
size_t total = 0, used = 0;
ret = esp_littlefs_info(conf.partition_label, &total, &used);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret));
}
else
{
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
}
// Use POSIX and C standard library functions to work with files.
// First create a file.
ESP_LOGI(TAG, "Opening file");
FILE *f = fopen("/littlefs/dati.txt", "a");
if (f == NULL)
{
ESP_LOGE(TAG, "Failed to open file for writing");
return;
}
fprintf(f,"%s%ld, %i\n",buffer,tv.tv_usec,adc_value);
fclose(f);
ESP_LOGI(TAG, "File written");
ESP_LOGI(TAG, "Reading file");
f = fopen("/littlefs/dati.txt", "r");
if (f == NULL)
{
ESP_LOGE(TAG, "Failed to open file for reading");
return;
}
int c;
while ((c = getc(f)) != EOF)
putchar(c);
//char line[64];
//fgets(line, sizeof(line), f);
fclose(f);
// All done, unmount partition and disable LittleFS
esp_vfs_littlefs_unregister(conf.partition_label);
ESP_LOGI(TAG, "LittleFS unmounted");
printf("conteggio %d\n",conta);
//printf("Minuti: %llu\n",tv.tv_sec);
printf("Stato: %d\n",adc_value);
printf("Enabling timer wakeup, %ds\n", wakeup_time_sec);
ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000));
}
void app_main(void)
{
gpio_set_direction(PIN, GPIO_MODE_OUTPUT);
gpio_set_level(PIN, 1);
tm.tm_year = 2024 - 1900;
tm.tm_mon = 11;
tm.tm_mday = 27;
tm.tm_hour = 13;
tm.tm_min = 23;
tm.tm_sec =10;
if (conta < 2) {
time_t t = mktime(&tm);
struct timeval now = { .tv_sec = t };
settimeofday(&now, NULL);
static const char *TAG = "esp_littlefs";
esp_vfs_littlefs_conf_t conf = {
.base_path = "/littlefs",
.partition_label = "littlefs",
.format_if_mount_failed = true,
.dont_mount = false,
};
esp_err_t ret = esp_vfs_littlefs_register(&conf);
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
ESP_LOGE(TAG, "Failed to find LittleFS partition");
} else {
ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
}
return;
}
if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
ESP_LOGE(TAG, "Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
ESP_LOGE(TAG, "Failed to find LittleFS partition");
} else {
ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
}
return;
}
struct stat st;
if (stat("/littlefs/dati.txt", &st) == 0) {
// Delete it if it exists
unlink("/littlefs/dati.txt");
}
else{
ESP_LOGE(TAG, "File non cancellato");
}
esp_vfs_littlefs_unregister(conf.partition_label);
ESP_LOGI(TAG, "LittleFS unmounted");
}
deep_sleep_register_rtc_timer_wakeup();
xTaskCreate(deep_sleep_task, "deep_sleep_task", 4096, NULL, 6, NULL);
}
Nessun commento:
Posta un commento