2021-06-28 15:43:37 +02:00
|
|
|
#include "main.h"
|
2022-05-24 10:03:16 +02:00
|
|
|
#include "NTPClient.h"
|
2022-05-24 16:50:46 +02:00
|
|
|
//#include "driver/"
|
2022-05-24 10:03:16 +02:00
|
|
|
#include <sys/time.h>
|
2022-05-20 17:01:50 +02:00
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
|
|
|
#include <chrono>
|
2021-06-21 11:29:53 +02:00
|
|
|
|
2022-05-24 10:03:16 +02:00
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2022-05-20 17:01:50 +02:00
|
|
|
DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), DHT(17, DHT22), DHT(16, DHT22)};
|
2021-06-28 15:43:37 +02:00
|
|
|
float temp[SENSORS_NUMBER];
|
|
|
|
float hum[SENSORS_NUMBER];
|
2021-06-21 11:29:53 +02:00
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
WiFiClient WifiClient;
|
|
|
|
PubSubClient MqttClient(WifiClient);
|
|
|
|
WiFiUDP NtpUDP;
|
2022-05-24 16:50:46 +02:00
|
|
|
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 7200, 60000); // 7200 = 2 * 3600 sec pour le décalage horaire
|
2021-06-28 15:43:37 +02:00
|
|
|
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
|
2021-06-21 11:29:53 +02:00
|
|
|
|
|
|
|
//-------------------- FONCTIONS --------------------//
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
//-------------- Connexion MQTT --------------//
|
|
|
|
|
|
|
|
void setupMQTT(const char *address, int port)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
2021-06-28 15:43:37 +02:00
|
|
|
MqttClient.setServer(address, port);
|
2021-06-21 11:29:53 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
void setupWIFI(const char *wifi_name, const char *password)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
|
|
|
Serial.println('\n');
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
WiFi.begin(wifi_name, password);
|
2021-06-21 11:29:53 +02:00
|
|
|
Serial.print("Connecting to ");
|
2021-06-28 15:43:37 +02:00
|
|
|
Serial.print(wifi_name);
|
2021-06-21 11:29:53 +02:00
|
|
|
|
2022-06-02 00:21:41 +02:00
|
|
|
int cpttry = 10;
|
2021-06-21 11:29:53 +02:00
|
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
delay(500);
|
|
|
|
Serial.print('.');
|
2022-06-02 00:21:41 +02:00
|
|
|
|
|
|
|
/* stop trying wifi connexion */
|
|
|
|
cpttry--;
|
|
|
|
if(cpttry == 0){
|
|
|
|
Serial.print("wifi: 10 try go to sleep");
|
|
|
|
sleep();
|
|
|
|
}
|
2021-06-21 11:29:53 +02:00
|
|
|
}
|
|
|
|
Serial.println('\n');
|
|
|
|
Serial.println("Connection established!");
|
|
|
|
Serial.print("IP address:\t");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
}
|
|
|
|
|
|
|
|
void reconnect(void)
|
|
|
|
{
|
|
|
|
Serial.println("Connecting to MQTT Broker...");
|
2022-06-02 00:21:41 +02:00
|
|
|
int cpttry = 10;
|
2021-06-28 15:43:37 +02:00
|
|
|
while (!MqttClient.connected())
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
2022-06-02 00:21:41 +02:00
|
|
|
/* stop trying mqtt connexion*/
|
|
|
|
cpttry--;
|
|
|
|
if(cpttry == 0){
|
|
|
|
Serial.print("Mqtt: 10 try go to sleep");
|
|
|
|
sleep();
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
Serial.print(".");
|
2022-05-20 17:01:50 +02:00
|
|
|
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
|
|
|
Serial.println("Connected.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
//-------------- Initialisation et lecture des capteurs --------------//
|
|
|
|
|
|
|
|
void initSensors(DHT *sensors, int number)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
|
|
|
int i;
|
2021-06-28 15:43:37 +02:00
|
|
|
for (i = 0; i < number; i++)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
2021-06-28 15:43:37 +02:00
|
|
|
sensors[i].begin();
|
2021-06-21 11:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
void readSensors(DHT sensors[], float temp[], float hum[], int number)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
|
|
|
int i;
|
2021-06-28 15:43:37 +02:00
|
|
|
for (i = 0; i < number; i++)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
2021-06-28 15:43:37 +02:00
|
|
|
*(temp + i) = sensors[i].readTemperature();
|
|
|
|
*(hum + i) = sensors[i].readHumidity();
|
2021-06-21 11:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
//-------------------- Sleep de l'ESP --------------------//
|
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
void sleep()
|
|
|
|
{
|
2021-06-28 15:43:37 +02:00
|
|
|
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * US_TO_S_FACTOR);
|
2021-06-21 11:29:53 +02:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
//exemple d'une triple utilisation de valeur pour une fonction utilise pour la date
|
2021-06-21 11:29:53 +02:00
|
|
|
std::tuple<int, int, int> getDate()
|
|
|
|
{
|
2022-05-24 10:03:16 +02:00
|
|
|
struct timeval synctime;
|
|
|
|
TimeClient.forceUpdate();
|
2021-06-28 15:43:37 +02:00
|
|
|
time_t rawtime = TimeClient.getEpochTime();
|
2022-05-24 10:03:16 +02:00
|
|
|
synctime.tv_sec = rawtime;
|
|
|
|
synctime.tv_usec = 0;
|
|
|
|
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
2022-05-24 16:50:46 +02:00
|
|
|
setenv("TZ", "CEST+2", 1);
|
|
|
|
tzset();
|
2022-05-24 10:03:16 +02:00
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
struct tm *ti;
|
|
|
|
ti = localtime(&rawtime);
|
|
|
|
int year = ti->tm_year + 1900;
|
|
|
|
int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1);
|
|
|
|
int day = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday);
|
|
|
|
return std::make_tuple(year, month, day);
|
|
|
|
}
|
|
|
|
|
2022-05-20 17:01:50 +02:00
|
|
|
// Function that gets current epoch time
|
|
|
|
unsigned long getTime() {
|
|
|
|
time_t now;
|
|
|
|
struct tm timeinfo;
|
|
|
|
if (!getLocalTime(&timeinfo)) {
|
|
|
|
//Serial.println("Failed to obtain time");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
time(&now);
|
2022-05-24 16:50:46 +02:00
|
|
|
Serial.println(now);
|
2022-05-20 17:01:50 +02:00
|
|
|
return now;
|
|
|
|
}
|
2021-06-28 15:43:37 +02:00
|
|
|
//-------------------- Création de trames --------------------//
|
|
|
|
|
|
|
|
void writeMessage(char *txt, float *temp, float *hum, int number)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
2021-06-28 15:43:37 +02:00
|
|
|
int chargelvl = Battery.getBatteryChargeLevel();
|
2022-06-02 00:21:41 +02:00
|
|
|
//time_t rawtime = TimeClient.getEpochTime();
|
|
|
|
|
2022-05-24 10:03:16 +02:00
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
switch (number)
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
|
|
|
case 1:
|
2021-06-28 15:43:37 +02:00
|
|
|
sprintf(txt, "|%s|%0.2f|%0.2f", CLUSTER, temp[0], hum[0]);
|
2021-06-21 11:29:53 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2021-06-28 15:43:37 +02:00
|
|
|
sprintf(txt, "|%s|%0.2f %0.2f|%0.2f %0.2f", CLUSTER, temp[0], temp[1], hum[0], hum[1]);
|
2021-06-21 11:29:53 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
2021-06-28 15:43:37 +02:00
|
|
|
sprintf(txt, "|%s|%0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], hum[0], hum[1], hum[2]);
|
2021-06-21 11:29:53 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2021-06-28 15:43:37 +02:00
|
|
|
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], temp[3], hum[0], hum[1], hum[2], hum[3]);
|
2021-06-21 11:29:53 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
2022-05-24 16:50:46 +02:00
|
|
|
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f|%d", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], hum[0], hum[1], hum[2], hum[3], hum[4], chargelvl);
|
2021-06-21 11:29:53 +02:00
|
|
|
break;
|
|
|
|
case 6:
|
2021-06-28 15:43:37 +02:00
|
|
|
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f %0.2f", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], hum[0], hum[1], hum[2], hum[3], hum[4], hum[5]);
|
2021-06-21 11:29:53 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Serial.println("Erreur, temp et hum sont trop longs : trop de capteurs");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 00:21:41 +02:00
|
|
|
//-------------------- 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;
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
//-------------------- Initialisation --------------------//
|
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
Serial.begin(9600);
|
2022-06-02 00:21:41 +02:00
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
setupWIFI(SSID, PWD);
|
2021-06-28 15:43:37 +02:00
|
|
|
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
|
|
|
initSensors(sensors, SENSORS_NUMBER);
|
2022-06-02 00:21:41 +02:00
|
|
|
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
TimeClient.begin();
|
2022-05-24 10:03:16 +02:00
|
|
|
TimeClient.forceUpdate();
|
2022-06-02 00:21:41 +02:00
|
|
|
|
|
|
|
/* 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');
|
|
|
|
*/
|
2021-06-21 11:29:53 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 10:03:16 +02:00
|
|
|
|
|
|
|
|
2022-05-20 17:01:50 +02:00
|
|
|
//-------------------- Boucle principale --------------------//
|
2021-06-28 15:43:37 +02:00
|
|
|
|
2021-06-21 11:29:53 +02:00
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
int year, month, day;
|
|
|
|
int lenght;
|
|
|
|
char time[30];
|
|
|
|
char date[30];
|
|
|
|
char msg[70];
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
MqttClient.loop();
|
|
|
|
if (!MqttClient.connected())
|
2021-06-21 11:29:53 +02:00
|
|
|
{
|
|
|
|
reconnect();
|
|
|
|
}
|
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
|
|
|
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
2021-06-21 11:29:53 +02:00
|
|
|
|
2021-06-28 15:43:37 +02:00
|
|
|
TimeClient.update();
|
|
|
|
TimeClient.getFormattedTime().toCharArray(time, 30);
|
2021-06-21 11:29:53 +02:00
|
|
|
|
|
|
|
tie(year, month, day) = getDate();
|
|
|
|
lenght = sprintf(date, "%d-%d-%d ", year, month, day);
|
|
|
|
sprintf(date + lenght, time);
|
|
|
|
sprintf(date + strlen(date), msg);
|
2021-06-28 15:43:37 +02:00
|
|
|
MqttClient.publish(TOPIC, date);
|
|
|
|
|
|
|
|
delay(2000);
|
2021-06-21 11:29:53 +02:00
|
|
|
|
|
|
|
sleep();
|
2021-06-28 15:43:37 +02:00
|
|
|
}
|