amelioration+debug+com
This commit is contained in:
parent
aa83fc58d6
commit
42f7f3331d
|
@ -15,11 +15,11 @@
|
||||||
#include <Pangodream_18650_CL.h>
|
#include <Pangodream_18650_CL.h>
|
||||||
#include <secret.h>
|
#include <secret.h>
|
||||||
|
|
||||||
#define TOPIC "prod"
|
#define TOPIC "test"
|
||||||
#define MQTT_ADDRESS "185.233.103.24"
|
#define MQTT_ADDRESS "185.233.103.24"
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
#define ESPNAME "esp32-bastien"
|
#define ESPNAME "esp32-fablab"
|
||||||
#define CLUSTER "grappe1"
|
#define CLUSTER "grappe3"
|
||||||
#define SENSORS_NUMBER 5
|
#define SENSORS_NUMBER 5
|
||||||
#define DHT22 22
|
#define DHT22 22
|
||||||
//comment for the test part this is the good frequency
|
//comment for the test part this is the good frequency
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#define MQTT_USER "capteurs"
|
#define MQTT_USER "capteurs"
|
||||||
#define MQTT_MDP "Fablab"
|
#define MQTT_MDP "Fablab"
|
||||||
#define SSID "Thermo-Bibli"
|
#define SSID "coh@bit"
|
||||||
#define PWD "motdepass"
|
#define PWD "lewifidecohabit"
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
; Upload options: custom upload port, speed and extra flags
|
; Upload options: custom upload port, speed and extra flags
|
||||||
; Library options: dependencies, extra library storages
|
; Library options: dependencies, extra library storages
|
||||||
; Advanced options: extra scripting
|
; Advanced options: extra scripting
|
||||||
|
|
||||||
;
|
;
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
@ -18,4 +19,4 @@ lib_deps =
|
||||||
knolleary/PubSubClient@^2.8
|
knolleary/PubSubClient@^2.8
|
||||||
ottowinter/ESPAsyncWebServer-esphome@^1.2.7
|
ottowinter/ESPAsyncWebServer-esphome@^1.2.7
|
||||||
ottowinter/AsyncTCP-esphome@^1.2.1
|
ottowinter/AsyncTCP-esphome@^1.2.1
|
||||||
arduino-libraries/NTPClient@^3.1.0
|
arduino-libraries/NTPClient@^3.1.0
|
391
src/main.cpp
391
src/main.cpp
|
@ -1,6 +1,5 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "NTPClient.h"
|
#include "NTPClient.h"
|
||||||
//#include "driver/"
|
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
|
@ -17,58 +16,71 @@
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
// Array of DHT sensors with their respective pins and types
|
||||||
DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), DHT(17, DHT22), DHT(16, DHT22)};
|
DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), DHT(17, DHT22), DHT(16, DHT22)};
|
||||||
|
|
||||||
|
// Arrays to store temperature and humidity readings
|
||||||
float temp[SENSORS_NUMBER];
|
float temp[SENSORS_NUMBER];
|
||||||
float hum[SENSORS_NUMBER];
|
float hum[SENSORS_NUMBER];
|
||||||
|
|
||||||
|
// Pangodream_18650_CL battery object with ADC pin, conversion factor, and number of reads
|
||||||
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
|
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
|
||||||
|
|
||||||
|
// WiFi client object
|
||||||
WiFiClient WifiClient;
|
WiFiClient WifiClient;
|
||||||
|
|
||||||
|
// PubSubClient object for MQTT communication
|
||||||
PubSubClient MqttClient(WifiClient);
|
PubSubClient MqttClient(WifiClient);
|
||||||
|
|
||||||
|
// UDP object for NTP communication
|
||||||
WiFiUDP NtpUDP;
|
WiFiUDP NtpUDP;
|
||||||
|
|
||||||
/**
|
// NTP client object with UDP and NTP server address
|
||||||
* @brief create NTP profile for frenchs users
|
NTPClient TimeClient(NtpUDP, "192.168.23.254"); /*si vous utilisez le Wifi du fablab*/
|
||||||
*
|
// NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org"); /*si vous utilisez un réseau 4g ou autre */
|
||||||
* @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); // ancienne configuration
|
|
||||||
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org");
|
|
||||||
|
|
||||||
//-------------------- CONDITION --------------------//
|
//-------------------- CONDITION --------------------//
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief test if the time is good for restart or refresh memory
|
* @brief Test if the time is good for restart or refresh memory
|
||||||
*
|
*
|
||||||
* @param hours
|
* @param hours
|
||||||
* @param minutes
|
* @param minutes
|
||||||
* @param restart bool to choose which the restart time bool or the resfres time bool will be returned
|
* @param restart bool to choose which the restart time bool or the refresh time bool will be returned
|
||||||
* @return true It's time to refresh or restart
|
* @return true It's time to refresh or restart
|
||||||
* @return false It's not the time to refresh or restart
|
* @return false It's not the time to refresh or restart
|
||||||
*/
|
*/
|
||||||
bool testTime (int hours, int minutes, bool restart){
|
bool testTime(int hours, int minutes, bool restart)
|
||||||
// debug parts
|
{
|
||||||
|
// debug parts
|
||||||
// Serial.println('\n');
|
// Serial.println('\n');
|
||||||
// Serial.println((REFRESHTIMES * TIME_TO_SLEEP) / 3600,DEC);
|
// Serial.println((REFRESHTIMES * TIME_TO_SLEEP) / 3600,DEC);
|
||||||
// Serial.println(((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
|
// Serial.println(((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
|
||||||
// Serial.println('\n');
|
// Serial.println('\n');
|
||||||
|
|
||||||
if (restart){ // restart parts
|
if (restart)
|
||||||
if(hours < (RESTARTTIMES * TIME_TO_SLEEP) / 3600) return true;
|
{ // restart parts
|
||||||
else{
|
if (hours < (RESTARTTIMES * TIME_TO_SLEEP) / 3600)
|
||||||
if((hours = (RESTARTTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
|
return true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ((hours = (RESTARTTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60))
|
||||||
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else { // refresh parts
|
}
|
||||||
if(hours >= (RESTARTTIMES * TIME_TO_SLEEP) / 3600){
|
else
|
||||||
if(hours < (REFRESHTIMES * TIME_TO_SLEEP) / 3600)return true;
|
{ // refresh parts
|
||||||
else{
|
if (hours >= (RESTARTTIMES * TIME_TO_SLEEP) / 3600)
|
||||||
if((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
|
{
|
||||||
else{
|
if (hours < (REFRESHTIMES * TIME_TO_SLEEP) / 3600)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if ((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -78,44 +90,27 @@ bool testTime (int hours, int minutes, bool restart){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool testTime (int hours, int minutes, bool restart){
|
|
||||||
// // debug parts
|
|
||||||
// // Serial.println('\n');
|
|
||||||
// // Serial.println((REFRESHTIMES * TIME_TO_SLEEP) / 3600,DEC);
|
|
||||||
// // Serial.println(((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
|
|
||||||
// // Serial.println('\n');
|
|
||||||
|
|
||||||
// if (restart){ // restart parts
|
|
||||||
// if( hours >= 12 && minutes >= 0 ) return true;
|
|
||||||
// else{
|
|
||||||
// if((hours < 12) /*&& (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60) */)return true;
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// } else { // refresh parts
|
|
||||||
// if(hours >= 12){
|
|
||||||
// if(hours <= 14 && minutes > 20 )return true;
|
|
||||||
// else{
|
|
||||||
// if((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
|
|
||||||
// else{
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// // Not needed but carefully habits
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
//-------------------- FONCTIONS --------------------//
|
//-------------------- FONCTIONS --------------------//
|
||||||
|
|
||||||
//-------------- Connexion MQTT --------------//
|
//-------------- Connexion MQTT --------------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set up MQTT server with address and port
|
||||||
|
*
|
||||||
|
* @param address MQTT server address
|
||||||
|
* @param port MQTT server port
|
||||||
|
*/
|
||||||
void setupMQTT(const char *address, int port)
|
void setupMQTT(const char *address, int port)
|
||||||
{
|
{
|
||||||
MqttClient.setServer(address, port);
|
MqttClient.setServer(address, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set up WiFi connection with SSID and password
|
||||||
|
*
|
||||||
|
* @param wifi_name WiFi SSID
|
||||||
|
* @param password WiFi password
|
||||||
|
*/
|
||||||
void setupWIFI(const char *wifi_name, const char *password)
|
void setupWIFI(const char *wifi_name, const char *password)
|
||||||
{
|
{
|
||||||
Serial.println('\n');
|
Serial.println('\n');
|
||||||
|
@ -132,7 +127,8 @@ void setupWIFI(const char *wifi_name, const char *password)
|
||||||
|
|
||||||
/* stop trying wifi connexion */
|
/* stop trying wifi connexion */
|
||||||
cpttry--;
|
cpttry--;
|
||||||
if(cpttry == 0){
|
if (cpttry == 0)
|
||||||
|
{
|
||||||
Serial.print("wifi: 10 try go to sleep");
|
Serial.print("wifi: 10 try go to sleep");
|
||||||
sleep();
|
sleep();
|
||||||
}
|
}
|
||||||
|
@ -151,12 +147,14 @@ void reconnect(void)
|
||||||
{
|
{
|
||||||
/* stop trying mqtt connexion */
|
/* stop trying mqtt connexion */
|
||||||
cpttry--;
|
cpttry--;
|
||||||
if(cpttry == 0){
|
if (cpttry == 0)
|
||||||
|
{
|
||||||
Serial.print("Mqtt: 10 try go to sleep");
|
Serial.print("Mqtt: 10 try go to sleep");
|
||||||
sleep();
|
sleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
|
|
||||||
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe)
|
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe)
|
||||||
{
|
{
|
||||||
Serial.println("Connected.");
|
Serial.println("Connected.");
|
||||||
|
@ -166,6 +164,12 @@ void reconnect(void)
|
||||||
|
|
||||||
//-------------- Initialisation et lecture des capteurs --------------//
|
//-------------- Initialisation et lecture des capteurs --------------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize DHT sensors
|
||||||
|
*
|
||||||
|
* @param sensors array of DHT sensors
|
||||||
|
* @param number number of sensors
|
||||||
|
*/
|
||||||
void initSensors(DHT *sensors, int number)
|
void initSensors(DHT *sensors, int number)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -175,6 +179,14 @@ void initSensors(DHT *sensors, int number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read temperature and humidity from DHT sensors
|
||||||
|
*
|
||||||
|
* @param sensors array of DHT sensors
|
||||||
|
* @param temp array to store temperature readings
|
||||||
|
* @param hum array to store humidity readings
|
||||||
|
* @param number number of sensors
|
||||||
|
*/
|
||||||
void readSensors(DHT sensors[], float temp[], float hum[], int number)
|
void readSensors(DHT sensors[], float temp[], float hum[], int number)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
@ -187,13 +199,16 @@ void readSensors(DHT sensors[], float temp[], float hum[], int number)
|
||||||
|
|
||||||
//-------------------- Sleep de l'ESP --------------------//
|
//-------------------- Sleep de l'ESP --------------------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Put ESP into deep sleep mode
|
||||||
|
*/
|
||||||
void sleep()
|
void sleep()
|
||||||
{
|
{
|
||||||
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * US_TO_S_FACTOR);
|
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * US_TO_S_FACTOR);
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
//exemple d'une triple utilisation de valeur pour une fonction utilise pour la date
|
// exemple d'une triple utilisation de valeur pour une fonction utilise pour la date
|
||||||
std::tuple<int, int, int> getDate()
|
std::tuple<int, int, int> getDate()
|
||||||
{
|
{
|
||||||
struct timeval synctime;
|
struct timeval synctime;
|
||||||
|
@ -201,11 +216,10 @@ std::tuple<int, int, int> getDate()
|
||||||
time_t rawtime = TimeClient.getEpochTime();
|
time_t rawtime = TimeClient.getEpochTime();
|
||||||
synctime.tv_sec = rawtime;
|
synctime.tv_sec = rawtime;
|
||||||
synctime.tv_usec = 0;
|
synctime.tv_usec = 0;
|
||||||
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
if (settimeofday(&synctime, NULL) != 0)
|
||||||
//setenv("TZ", "CEST+2", 1);
|
std::cout << "error\n";
|
||||||
//tzset();
|
|
||||||
|
|
||||||
// set timezone to France
|
// set timezone to France
|
||||||
setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 2); // set at 2 or 3 (Third parameter)
|
setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 2); // set at 2 or 3 (Third parameter)
|
||||||
tzset();
|
tzset();
|
||||||
|
|
||||||
|
@ -218,32 +232,44 @@ std::tuple<int, int, int> getDate()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function that gets current epoch time
|
// Function that gets current epoch time
|
||||||
unsigned long getTime() {
|
unsigned long getTime()
|
||||||
time_t now = TimeClient.getEpochTime();;
|
{
|
||||||
|
time_t now = TimeClient.getEpochTime();
|
||||||
struct timeval synctime;
|
struct timeval synctime;
|
||||||
struct tm timeinfo;
|
struct tm timeinfo;
|
||||||
|
|
||||||
synctime.tv_sec = now;
|
synctime.tv_sec = now;
|
||||||
synctime.tv_usec = 0;
|
synctime.tv_usec = 0;
|
||||||
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
if (settimeofday(&synctime, NULL) != 0)
|
||||||
TimeClient.forceUpdate();
|
std::cout << "error\n";
|
||||||
|
// TimeClient.forceUpdate(); //cet ligne crée un bug avec l'heure quand on est connecté au réseau du fablab
|
||||||
vTaskDelay(1000);
|
vTaskDelay(1000);
|
||||||
if (!getLocalTime(&timeinfo)) {
|
if (!getLocalTime(&timeinfo))
|
||||||
|
{
|
||||||
Serial.println("Failed to obtain time");
|
Serial.println("Failed to obtain time");
|
||||||
return(0);
|
return (0);
|
||||||
}
|
}
|
||||||
time(&now);
|
time(&now);
|
||||||
Serial.println(now);
|
Serial.println(now);
|
||||||
return now;
|
return now;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-------------------- Création de trames --------------------//
|
//-------------------- Création de trames --------------------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Create message frame with temperature and humidity readings
|
||||||
|
*
|
||||||
|
* @param txt message buffer
|
||||||
|
* @param temp array of temperature readings
|
||||||
|
* @param hum array of humidity readings
|
||||||
|
* @param number number of sensors
|
||||||
|
*/
|
||||||
void writeMessage(char *txt, float *temp, float *hum, int number)
|
void writeMessage(char *txt, float *temp, float *hum, int number)
|
||||||
{
|
{
|
||||||
int chargelvl = Battery.getBatteryChargeLevel();
|
int chargelvl = Battery.getBatteryChargeLevel();
|
||||||
//time_t rawtime = TimeClient.getEpochTime();
|
// time_t rawtime = TimeClient.getEpochTime();
|
||||||
|
|
||||||
|
delay(2000);
|
||||||
|
|
||||||
switch (number)
|
switch (number)
|
||||||
{
|
{
|
||||||
|
@ -271,140 +297,154 @@ void writeMessage(char *txt, float *temp, float *hum, int number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------- read, refresh and return the day boot state --------------------//
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief read, refresh and return the day boot state
|
* @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
|
* @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
|
* @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)
|
bool TestBoot(bool resOrRfsh)
|
||||||
{
|
{
|
||||||
nvs_handle_t my_handle;
|
nvs_handle_t my_handle;
|
||||||
esp_err_t err;
|
esp_err_t err;
|
||||||
bool booted = false;
|
bool booted = false;
|
||||||
//Serial.println("Test");
|
// Serial.println("Test");
|
||||||
|
|
||||||
// Open
|
// Open
|
||||||
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
||||||
if (err != ESP_OK){
|
if (err != ESP_OK)
|
||||||
Serial.println("Erreur open nvs persistant storage");
|
{
|
||||||
return false;
|
Serial.println("Erreur open nvs persistant storage");
|
||||||
}
|
|
||||||
|
|
||||||
/******** 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("Flag restart = 0 (not restarted)");
|
|
||||||
return true;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Close
|
|
||||||
nvs_close(my_handle);
|
|
||||||
Serial.println("Flag restart = 1 (already restart)");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Not needed but finish properly the function */
|
|
||||||
// Close
|
|
||||||
Serial.println("Maybe issue: last return");
|
|
||||||
nvs_close(my_handle);
|
|
||||||
return false;
|
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("Flag restart = 0 (not restarted");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Close
|
||||||
|
nvs_close(my_handle);
|
||||||
|
Serial.println("Flag restart = 1 (already restart)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Not needed but finish properly the function */
|
||||||
|
// Close
|
||||||
|
Serial.println("Maybe issue: last return");
|
||||||
|
nvs_close(my_handle);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//-------------------- Initialisation --------------------//
|
//-------------------- Initialisation --------------------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Setup function for the ESP
|
||||||
|
*/
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("-----------------Start---------------------");
|
Serial.println("-----------------Start---------------------");
|
||||||
|
|
||||||
setupWIFI(SSID, PWD);
|
setupWIFI(SSID, PWD);
|
||||||
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
||||||
initSensors(sensors, SENSORS_NUMBER);
|
initSensors(sensors, SENSORS_NUMBER);
|
||||||
|
|
||||||
|
|
||||||
TimeClient.begin();
|
TimeClient.begin();
|
||||||
TimeClient.update();
|
TimeClient.update();
|
||||||
|
|
||||||
//int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
|
// int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
|
||||||
unsigned long rawtime = getTime();
|
unsigned long rawtime = getTime();
|
||||||
|
|
||||||
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60 , secondes = rawtime % 60;
|
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60, secondes = rawtime % 60;
|
||||||
|
|
||||||
if (nvs_flash_init() != ESP_OK){
|
if (nvs_flash_init() != ESP_OK)
|
||||||
|
{
|
||||||
Serial.println("Erreur flash init");
|
Serial.println("Erreur flash init");
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @brief restart the ESP if there is the time restart
|
* @brief restart the ESP if there is the time restart
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
if(testTime(hours, minutes, RESTART) && TestBoot(RESTART)){
|
if (testTime(hours, minutes, RESTART) && TestBoot(RESTART))
|
||||||
|
{
|
||||||
Serial.println("-----------------RESSSSTTTAAAART---------------------");
|
Serial.println("-----------------RESSSSTTTAAAART---------------------");
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
delay(1000);
|
delay(1000);
|
||||||
} else if(testTime(hours, minutes, REFRESHMEMBOOT)){
|
}
|
||||||
|
else if (testTime(hours, minutes, REFRESHMEMBOOT))
|
||||||
|
{
|
||||||
TestBoot(REFRESHMEMBOOT);
|
TestBoot(REFRESHMEMBOOT);
|
||||||
}
|
}
|
||||||
/* ######### test time part #########
|
// // ######### test time part #########
|
||||||
Serial.println('\n');
|
// Serial.println('\n');
|
||||||
Serial.println(hours,DEC);
|
// Serial.println(hours,DEC);
|
||||||
Serial.println(':');
|
// Serial.println(':');
|
||||||
Serial.println(minutes,DEC);
|
// Serial.println(minutes,DEC);
|
||||||
Serial.println(':');
|
// Serial.println(':');
|
||||||
Serial.println(secondes,DEC);
|
// Serial.println(secondes,DEC);
|
||||||
Serial.println('\n');
|
// Serial.println('\n');
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//-------------------- Boucle principale --------------------//
|
//-------------------- Boucle principale --------------------//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Main loop function for the ESP
|
||||||
|
*/
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
int year, month, day;
|
int year, month, day;
|
||||||
|
@ -423,13 +463,13 @@ void loop()
|
||||||
|
|
||||||
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
||||||
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
||||||
|
Serial.printf(msg);
|
||||||
|
|
||||||
TimeClient.update();
|
// TimeClient.update();
|
||||||
//TimeClient.getFormattedTime().toCharArray(time, 30);
|
// TimeClient.getFormattedTime().toCharArray(time, 30);
|
||||||
|
|
||||||
|
// tie(year, month, day) = getDate();
|
||||||
//tie(year, month, day) = getDate();
|
// /* debug print*/
|
||||||
/* debug print*/
|
|
||||||
// Serial.println('\n');
|
// Serial.println('\n');
|
||||||
// Serial.println(year,DEC);
|
// Serial.println(year,DEC);
|
||||||
// Serial.println(':');
|
// Serial.println(':');
|
||||||
|
@ -438,10 +478,9 @@ void loop()
|
||||||
// Serial.println(day,DEC);
|
// Serial.println(day,DEC);
|
||||||
// Serial.println('\n');
|
// Serial.println('\n');
|
||||||
|
|
||||||
|
|
||||||
now = getTime();
|
now = getTime();
|
||||||
|
|
||||||
lenght = sprintf(date,"%s", ultoa(now, date, 10) /* "|%d-%d-%d ", year, month, day */);
|
lenght = sprintf(date, "%s", ultoa(now, date, 10) /* "|%d-%d-%d ", year, month, day */);
|
||||||
sprintf(date + lenght, time);
|
sprintf(date + lenght, time);
|
||||||
sprintf(date + strlen(date), msg);
|
sprintf(date + strlen(date), msg);
|
||||||
MqttClient.publish(TOPIC, date);
|
MqttClient.publish(TOPIC, date);
|
||||||
|
|
Loading…
Reference in a new issue