add persistant solution for restart and comment

This commit is contained in:
Aurélien Gauthier 2022-06-02 15:49:34 +02:00
parent d886bce090
commit 44872fc71e
2 changed files with 169 additions and 16 deletions

View file

@ -48,6 +48,15 @@ void initSensors(DHT *sensors, int number);
void readSensors(DHT sensors[], float temp[], float hum[], int number);
void sleep();
std::tuple<int, int, int> getDate();
/**
* @brief hgfhdjskl
*
* @param txt testetest
* @param temp dqsdzfs
* @param hum
* @param number vdsvsvsvs
*/
void writeMsg(char *txt, float *temp, float *hum, int number);
//timeClient.getEpochTime().toCharArray(date, 50); = convertir un string en char

View file

@ -1,11 +1,16 @@
#include "main.h"
#include "NTPClient.h"
//#include "Effortless_SPIFFS.h"
//#include "driver/"
#include <sys/time.h>
#include <nvs_flash.h>
#include <ctime>
#include <iostream>
#include <chrono>
#define STORAGE_NAMESPACE "storage"
#define RESTART 1
#define REFRESHMEMBOOT 0
using namespace std;
@ -13,11 +18,71 @@ DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), D
float temp[SENSORS_NUMBER];
float hum[SENSORS_NUMBER];
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
WiFiClient WifiClient;
PubSubClient MqttClient(WifiClient);
WiFiUDP NtpUDP;
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 7200, 60000); // 7200 = 2 * 3600 sec pour le décalage horaire
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
/**
* @brief create NTP profile for frenchs users
*
* @param NtpUDP: Protocole use
*
* @param "europe.pool.ntp.org": serve target
*
* @param 7200: jet lag europe time from France (2 * 3600 sec)
*/
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 7200, 60000);
//-------------------- CONDITION --------------------//
/**
* @brief Time hour for restart or time for refresh restart persistant mem
*
* @param hours
* @param restart bool at true for restart or at false for resfresh memory
* @return true right hour for restart or refresh
* @return false bad hour for restart or refresh
*/
bool ishour(int hours, bool restart){
if (restart){
return hours <= (2 * TIME_TO_SLEEP) / 3600;
}
// Then refresh time
return (hours > (2 * TIME_TO_SLEEP) / 3600) && (hours <= (4 * TIME_TO_SLEEP) / 3600);
}
/**
* @brief Time minute for restart or time for refresh restart persistant mem
*
* @param minutes
* @param restart bool at true for restart or at false for resfresh memory
* @return true right minute for restart or refresh
* @return false bad minute for restart or refresh
*/
bool isminute(int minutes, bool restart){
if (restart){
return minutes <= ((2 * TIME_TO_SLEEP) % 3600) / 60;
}
// Then refresh time
return (minutes > ((2 * TIME_TO_SLEEP) % 3600)) / 60 && (minutes <= ((4 * TIME_TO_SLEEP) % 3600) / 60);
}
/**
* @brief Time seconde for restart or time for refresh restart persistant mem
*
* @param secondes
* @param restart bool at true for restart or at false for resfresh memory
* @return true right secondes for restart or refresh
* @return false bad secondes for restart or refresh
*/
bool issecond(int secondes, bool restart){
if (restart){
return secondes <= ((2 * TIME_TO_SLEEP) - 1 ) % 60;
}
// Then refresh time
return (secondes > ((2 * TIME_TO_SLEEP) - 1 ) % 60) && (secondes <= ((4 * TIME_TO_SLEEP) - 1 ) % 60);
}
//-------------------- FONCTIONS --------------------//
@ -28,6 +93,7 @@ void setupMQTT(const char *address, int port)
MqttClient.setServer(address, port);
}
void setupWIFI(const char *wifi_name, const char *password)
{
Serial.println('\n');
@ -61,7 +127,7 @@ void reconnect(void)
int cpttry = 10;
while (!MqttClient.connected())
{
/* stop trying mqtt connexion*/
/* stop trying mqtt connexion */
cpttry--;
if(cpttry == 0){
Serial.print("Mqtt: 10 try go to sleep");
@ -130,7 +196,7 @@ unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println("Failed to obtain time");
Serial.println("Failed to obtain time");
return(0);
}
time(&now);
@ -139,6 +205,7 @@ unsigned long getTime() {
}
//-------------------- Création de trames --------------------//
void writeMessage(char *txt, float *temp, float *hum, int number)
{
int chargelvl = Battery.getBatteryChargeLevel();
@ -171,19 +238,88 @@ void writeMessage(char *txt, float *temp, float *hum, int number)
}
}
//-------------------- Condition --------------------//
/**
* @brief read, refresh and return the day boot state
*
* @return bool at true if the esp was restarted or false if error or not restarted
* @inspired of https://github.com/espressif/esp-idf/blob/master/examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c
*/
bool TestBoot(bool resOrRfsh)
{
nvs_handle_t my_handle;
esp_err_t err;
bool booted = false;
bool ishour(int hours){
return hours <= (2 * TIME_TO_SLEEP) / 3600;
// Open
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
if (err != ESP_OK){
Serial.println("Erreur open nvs persistant storage");
return false;
}
/******** Refresh part ********/
if(resOrRfsh == REFRESHMEMBOOT){
int32_t restart = REFRESHMEMBOOT;
err = nvs_set_i32(my_handle, "restart", restart);
if (err != ESP_OK) {
Serial.println("Erreur write nvs persistant storage");
return false;
}
// Commit written value.
// After setting any values, nvs_commit() must be called to ensure changes are written
// to flash storage. Implementations may write to storage at other times,
// but this is not guaranteed.
err = nvs_commit(my_handle);
if (err != ESP_OK) return err;
// Close
nvs_close(my_handle);
Serial.println("Refresh memory restart state ");
return false;
}
/******** ********/
// Read
int32_t restart = 0; // value will default to 0, if not set yet in NVS
err = nvs_get_i32(my_handle, "restart", &restart);
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND){
Serial.println("Erreur read nvs persistant storage");
return false;
}
if(!restart){
// Write
restart = 1;
err = nvs_set_i32(my_handle, "restart", restart);
if (err != ESP_OK) {
Serial.println("Erreur write nvs persistant storage");
return false;
}
// Commit written value.
// After setting any values, nvs_commit() must be called to ensure changes are written
// to flash storage. Implementations may write to storage at other times,
// but this is not guaranteed.
err = nvs_commit(my_handle);
if (err != ESP_OK) return err;
// Close
nvs_close(my_handle);
Serial.println("not restart");
return false;
} else {
// Close
nvs_close(my_handle);
Serial.println("already restart");
return true;
}
/* Not needed but finish properly the function */
// Close
nvs_close(my_handle);
return false;
}
bool isminute(int minutes){
return minutes <= ((2 * TIME_TO_SLEEP) % 3600) / 60;
}
bool issecond(int secondes){
return secondes <= ((2 * TIME_TO_SLEEP) - 1 ) % 60;
}
//-------------------- Initialisation --------------------//
@ -199,12 +335,20 @@ void setup()
TimeClient.begin();
TimeClient.forceUpdate();
/* Reboot one time per day */
int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60 , secondes = rawtime % 60;
if( ishour(hours) && isminute(minutes) && issecond(secondes)){
if (nvs_flash_init() != ESP_OK){
Serial.println("Erreur flash init");
}
/**
* @brief restart the ESP if there is the time restart
*
*/
if(ishour(hours,RESTART) && isminute(minutes,RESTART) && issecond(secondes,RESTART) && TestBoot(RESTART)){
ESP.restart();
} else if(ishour(hours,REFRESHMEMBOOT) && isminute(minutes,REFRESHMEMBOOT) && issecond(secondes,REFRESHMEMBOOT)){
TestBoot(REFRESHMEMBOOT);
}
/* ######### test time part #########
Serial.println('\n');