try test ntp fix
This commit is contained in:
parent
80dac772f5
commit
367baf5f9a
|
@ -23,7 +23,7 @@
|
|||
#define SENSORS_NUMBER 5
|
||||
#define DHT22 22
|
||||
//comment for the test part this is the good frequency
|
||||
#define TIME_TO_SLEEP 598
|
||||
#define TIME_TO_SLEEP 596
|
||||
//#define TIME_TO_SLEEP 28
|
||||
#define US_TO_S_FACTOR 1000000
|
||||
#define R2 100
|
||||
|
|
118
src/main.cpp
118
src/main.cpp
|
@ -1,7 +1,7 @@
|
|||
#include "main.h"
|
||||
#include "NTPClient.h"
|
||||
//#include "Effortless_SPIFFS.h"
|
||||
//#include "driver/"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <nvs_flash.h>
|
||||
#include <ctime>
|
||||
|
@ -9,6 +9,7 @@
|
|||
#include <chrono>
|
||||
|
||||
#define STORAGE_NAMESPACE "storage"
|
||||
#define _OPEN_SYS_ITOA_EXT
|
||||
#define RESTART 1
|
||||
#define REFRESHMEMBOOT 0
|
||||
#define RESTARTTIMES 2
|
||||
|
@ -34,7 +35,8 @@ WiFiUDP NtpUDP;
|
|||
*
|
||||
* @param 7200: jet lag europe time from France (2 * 3600 sec)
|
||||
*/
|
||||
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 7200, 60000);
|
||||
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org" , 7200 , 60000);
|
||||
//NTPClient TimeClient(NtpUDP, "pool.ntp.org");
|
||||
|
||||
//-------------------- CONDITION --------------------//
|
||||
|
||||
|
@ -72,9 +74,38 @@ bool testTime (int hours, int minutes, bool restart){
|
|||
}
|
||||
}
|
||||
}
|
||||
// Not needed but carefully habits
|
||||
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 --------------------//
|
||||
|
||||
//-------------- Connexion MQTT --------------//
|
||||
|
@ -171,7 +202,11 @@ std::tuple<int, int, int> getDate()
|
|||
synctime.tv_sec = rawtime;
|
||||
synctime.tv_usec = 0;
|
||||
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
||||
setenv("TZ", "CEST+2", 1);
|
||||
//setenv("TZ", "CEST+2", 1);
|
||||
//tzset();
|
||||
|
||||
// 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)
|
||||
tzset();
|
||||
|
||||
struct tm *ti;
|
||||
|
@ -184,8 +219,15 @@ std::tuple<int, int, int> getDate()
|
|||
|
||||
// Function that gets current epoch time
|
||||
unsigned long getTime() {
|
||||
time_t now;
|
||||
time_t now = TimeClient.getEpochTime();;
|
||||
struct timeval synctime;
|
||||
struct tm timeinfo;
|
||||
|
||||
synctime.tv_sec = now;
|
||||
synctime.tv_usec = 0;
|
||||
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
||||
TimeClient.forceUpdate();
|
||||
vTaskDelay(1000);
|
||||
if (!getLocalTime(&timeinfo)) {
|
||||
Serial.println("Failed to obtain time");
|
||||
return(0);
|
||||
|
@ -296,18 +338,19 @@ bool TestBoot(bool resOrRfsh)
|
|||
if (err != ESP_OK) return err;
|
||||
// Close
|
||||
nvs_close(my_handle);
|
||||
Serial.println("not restart");
|
||||
Serial.println("Flag restart = 0 (not restarted)");
|
||||
return true;
|
||||
|
||||
} else {
|
||||
// Close
|
||||
nvs_close(my_handle);
|
||||
Serial.println("already restart");
|
||||
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;
|
||||
}
|
||||
|
@ -318,6 +361,7 @@ bool TestBoot(bool resOrRfsh)
|
|||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println("-----------------Start---------------------");
|
||||
|
||||
setupWIFI(SSID, PWD);
|
||||
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
||||
|
@ -325,9 +369,10 @@ void setup()
|
|||
|
||||
|
||||
TimeClient.begin();
|
||||
TimeClient.forceUpdate();
|
||||
|
||||
int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
|
||||
//int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
|
||||
unsigned long rawtime = getTime();
|
||||
|
||||
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60 , secondes = rawtime % 60;
|
||||
|
||||
if (nvs_flash_init() != ESP_OK){
|
||||
|
@ -337,12 +382,14 @@ void setup()
|
|||
* @brief restart the ESP if there is the time restart
|
||||
*
|
||||
*/
|
||||
if(TestBoot(RESTART) && testTime(hours, minutes, RESTART)){
|
||||
if(testTime(hours, minutes, RESTART) && TestBoot(RESTART)){
|
||||
Serial.println("-----------------RESSSSTTTAAAART---------------------");
|
||||
ESP.restart();
|
||||
delay(1000);
|
||||
} else if(testTime(hours, minutes, REFRESHMEMBOOT)){
|
||||
TestBoot(REFRESHMEMBOOT);
|
||||
}
|
||||
/* ######### test time part ######### */
|
||||
/* ######### test time part #########
|
||||
Serial.println('\n');
|
||||
Serial.println(hours,DEC);
|
||||
Serial.println(':');
|
||||
|
@ -350,7 +397,7 @@ void setup()
|
|||
Serial.println(':');
|
||||
Serial.println(secondes,DEC);
|
||||
Serial.println('\n');
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
|
@ -361,9 +408,11 @@ void loop()
|
|||
{
|
||||
int year, month, day;
|
||||
int lenght;
|
||||
unsigned long now;
|
||||
char time[30];
|
||||
char date[30];
|
||||
char msg[70];
|
||||
bool horoIssue = false;
|
||||
|
||||
MqttClient.loop();
|
||||
if (!MqttClient.connected())
|
||||
|
@ -377,13 +426,56 @@ void loop()
|
|||
TimeClient.update();
|
||||
TimeClient.getFormattedTime().toCharArray(time, 30);
|
||||
|
||||
tie(year, month, day) = getDate();
|
||||
lenght = sprintf(date, "%d-%d-%d ", year, month, day);
|
||||
|
||||
//tie(year, month, day) = getDate();
|
||||
/* debug print*/
|
||||
// Serial.println('\n');
|
||||
// Serial.println(year,DEC);
|
||||
// Serial.println(':');
|
||||
// Serial.println(month,DEC);
|
||||
// Serial.println(':');
|
||||
// Serial.println(day,DEC);
|
||||
// Serial.println('\n');
|
||||
|
||||
|
||||
now = getTime();
|
||||
//TimeClient.update();
|
||||
//Serial.println(TimeClient.getFormattedTime());
|
||||
|
||||
//Solution to the issue generated by the Ntpclient for the horodatage midnight at two o'clock
|
||||
// if (time[0] == '0' && (time[1] == '0' || time[1] == '1')){
|
||||
// if (day == 31){
|
||||
// if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
|
||||
// day = 1;
|
||||
// month++;
|
||||
// if(month == 13){
|
||||
// month == 1;
|
||||
// year++;
|
||||
// }
|
||||
// }
|
||||
// }else if (day == 30){
|
||||
// if(month == 4 || month == 6 || month == 9 || month == 11){
|
||||
// day = 1;
|
||||
// month++;
|
||||
// }
|
||||
// }else if(day == 29){
|
||||
// if(month == 2){
|
||||
// day = 1;
|
||||
// month++;
|
||||
// }
|
||||
// }else{
|
||||
// day++;
|
||||
// }
|
||||
// }
|
||||
|
||||
lenght = sprintf(date,"%s|", ultoa(now, date, 10) /* "|%d-%d-%d ", year, month, day */);
|
||||
sprintf(date + lenght, time);
|
||||
sprintf(date + strlen(date), msg);
|
||||
MqttClient.publish(TOPIC, date);
|
||||
|
||||
Serial.println("-----------------DeepSleep---------------------");
|
||||
delay(2000);
|
||||
|
||||
sleep();
|
||||
delay(1000);
|
||||
}
|
Loading…
Reference in a new issue