diff --git a/include/main.h b/include/main.h index 4cd4897..7112ae1 100644 --- a/include/main.h +++ b/include/main.h @@ -23,7 +23,8 @@ #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 598 +//#define TIME_TO_SLEEP 28 #define US_TO_S_FACTOR 1000000 #define R2 100 #define R3 10 diff --git a/src/main.cpp b/src/main.cpp index 15a0480..e4eed46 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,10 +36,18 @@ void setupWIFI(const char *wifi_name, const char *password) Serial.print("Connecting to "); Serial.print(wifi_name); + int cpttry = 10; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print('.'); + + /* stop trying wifi connexion */ + cpttry--; + if(cpttry == 0){ + Serial.print("wifi: 10 try go to sleep"); + sleep(); + } } Serial.println('\n'); Serial.println("Connection established!"); @@ -50,8 +58,16 @@ void setupWIFI(const char *wifi_name, const char *password) void reconnect(void) { Serial.println("Connecting to MQTT Broker..."); + int cpttry = 10; while (!MqttClient.connected()) { + /* stop trying mqtt connexion*/ + cpttry--; + if(cpttry == 0){ + Serial.print("Mqtt: 10 try go to sleep"); + sleep(); + } + Serial.print("."); if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe) { @@ -126,7 +142,8 @@ unsigned long getTime() { void writeMessage(char *txt, float *temp, float *hum, int number) { int chargelvl = Battery.getBatteryChargeLevel(); - time_t rawtime = TimeClient.getEpochTime(); + //time_t rawtime = TimeClient.getEpochTime(); + switch (number) { @@ -154,16 +171,50 @@ void writeMessage(char *txt, float *temp, float *hum, int number) } } +//-------------------- Condition --------------------// + +bool ishour(int hours){ + return hours <= (2 * TIME_TO_SLEEP) / 3600; +} + +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 --------------------// void setup() { Serial.begin(9600); + setupWIFI(SSID, PWD); setupMQTT(MQTT_ADDRESS, MQTT_PORT); initSensors(sensors, SENSORS_NUMBER); + + 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)){ + ESP.restart(); + } + /* ######### test time part ######### + Serial.println('\n'); + Serial.println(hours,DEC); + Serial.println(':'); + Serial.println(minutes,DEC); + Serial.println(':'); + Serial.println(secondes,DEC); + Serial.println('\n'); + */ }