try test ntp fix
This commit is contained in:
parent
80dac772f5
commit
367baf5f9a
|
@ -23,7 +23,7 @@
|
||||||
#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
|
||||||
#define TIME_TO_SLEEP 598
|
#define TIME_TO_SLEEP 596
|
||||||
//#define TIME_TO_SLEEP 28
|
//#define TIME_TO_SLEEP 28
|
||||||
#define US_TO_S_FACTOR 1000000
|
#define US_TO_S_FACTOR 1000000
|
||||||
#define R2 100
|
#define R2 100
|
||||||
|
|
118
src/main.cpp
118
src/main.cpp
|
@ -1,7 +1,7 @@
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "NTPClient.h"
|
#include "NTPClient.h"
|
||||||
//#include "Effortless_SPIFFS.h"
|
|
||||||
//#include "driver/"
|
//#include "driver/"
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <nvs_flash.h>
|
#include <nvs_flash.h>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
@ -9,6 +9,7 @@
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
#define STORAGE_NAMESPACE "storage"
|
#define STORAGE_NAMESPACE "storage"
|
||||||
|
#define _OPEN_SYS_ITOA_EXT
|
||||||
#define RESTART 1
|
#define RESTART 1
|
||||||
#define REFRESHMEMBOOT 0
|
#define REFRESHMEMBOOT 0
|
||||||
#define RESTARTTIMES 2
|
#define RESTARTTIMES 2
|
||||||
|
@ -34,7 +35,8 @@ WiFiUDP NtpUDP;
|
||||||
*
|
*
|
||||||
* @param 7200: jet lag europe time from France (2 * 3600 sec)
|
* @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 --------------------//
|
//-------------------- CONDITION --------------------//
|
||||||
|
|
||||||
|
@ -72,9 +74,38 @@ bool testTime (int hours, int minutes, bool restart){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Not needed but carefully habits
|
||||||
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 --------------//
|
||||||
|
@ -171,7 +202,11 @@ std::tuple<int, int, int> getDate()
|
||||||
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) 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();
|
tzset();
|
||||||
|
|
||||||
struct tm *ti;
|
struct tm *ti;
|
||||||
|
@ -184,8 +219,15 @@ 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;
|
time_t now = TimeClient.getEpochTime();;
|
||||||
|
struct timeval synctime;
|
||||||
struct tm timeinfo;
|
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)) {
|
if (!getLocalTime(&timeinfo)) {
|
||||||
Serial.println("Failed to obtain time");
|
Serial.println("Failed to obtain time");
|
||||||
return(0);
|
return(0);
|
||||||
|
@ -296,18 +338,19 @@ bool TestBoot(bool resOrRfsh)
|
||||||
if (err != ESP_OK) return err;
|
if (err != ESP_OK) return err;
|
||||||
// Close
|
// Close
|
||||||
nvs_close(my_handle);
|
nvs_close(my_handle);
|
||||||
Serial.println("not restart");
|
Serial.println("Flag restart = 0 (not restarted)");
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Close
|
// Close
|
||||||
nvs_close(my_handle);
|
nvs_close(my_handle);
|
||||||
Serial.println("already restart");
|
Serial.println("Flag restart = 1 (already restart)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Not needed but finish properly the function */
|
/* Not needed but finish properly the function */
|
||||||
// Close
|
// Close
|
||||||
|
Serial.println("Maybe issue: last return");
|
||||||
nvs_close(my_handle);
|
nvs_close(my_handle);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -318,6 +361,7 @@ bool TestBoot(bool resOrRfsh)
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
Serial.println("-----------------Start---------------------");
|
||||||
|
|
||||||
setupWIFI(SSID, PWD);
|
setupWIFI(SSID, PWD);
|
||||||
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
||||||
|
@ -325,9 +369,10 @@ void setup()
|
||||||
|
|
||||||
|
|
||||||
TimeClient.begin();
|
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;
|
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60 , secondes = rawtime % 60;
|
||||||
|
|
||||||
if (nvs_flash_init() != ESP_OK){
|
if (nvs_flash_init() != ESP_OK){
|
||||||
|
@ -337,12 +382,14 @@ void setup()
|
||||||
* @brief restart the ESP if there is the time restart
|
* @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();
|
ESP.restart();
|
||||||
|
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(':');
|
||||||
|
@ -350,7 +397,7 @@ void setup()
|
||||||
Serial.println(':');
|
Serial.println(':');
|
||||||
Serial.println(secondes,DEC);
|
Serial.println(secondes,DEC);
|
||||||
Serial.println('\n');
|
Serial.println('\n');
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -361,9 +408,11 @@ void loop()
|
||||||
{
|
{
|
||||||
int year, month, day;
|
int year, month, day;
|
||||||
int lenght;
|
int lenght;
|
||||||
|
unsigned long now;
|
||||||
char time[30];
|
char time[30];
|
||||||
char date[30];
|
char date[30];
|
||||||
char msg[70];
|
char msg[70];
|
||||||
|
bool horoIssue = false;
|
||||||
|
|
||||||
MqttClient.loop();
|
MqttClient.loop();
|
||||||
if (!MqttClient.connected())
|
if (!MqttClient.connected())
|
||||||
|
@ -377,13 +426,56 @@ void loop()
|
||||||
TimeClient.update();
|
TimeClient.update();
|
||||||
TimeClient.getFormattedTime().toCharArray(time, 30);
|
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 + lenght, time);
|
||||||
sprintf(date + strlen(date), msg);
|
sprintf(date + strlen(date), msg);
|
||||||
MqttClient.publish(TOPIC, date);
|
MqttClient.publish(TOPIC, date);
|
||||||
|
|
||||||
|
Serial.println("-----------------DeepSleep---------------------");
|
||||||
delay(2000);
|
delay(2000);
|
||||||
|
|
||||||
sleep();
|
sleep();
|
||||||
|
delay(1000);
|
||||||
}
|
}
|
Loading…
Reference in a new issue