Add LocalSave and SendLocalData
This commit is contained in:
parent
cce2fb3629
commit
f164212c74
|
@ -41,12 +41,15 @@
|
|||
#define READS 20
|
||||
const long gmtOffset_sec = 3600;
|
||||
|
||||
int getAverageChargeLevel();
|
||||
void setupMQTT(const char *address, int port);
|
||||
void setupWIFI(const char *wifi_name, const char *password);
|
||||
void reconnect(void);
|
||||
void initSensors(DHT *sensors, int number);
|
||||
void readSensors(DHT sensors[], float temp[], float hum[], int number);
|
||||
void sleep();
|
||||
void LocalSave();
|
||||
void SendLocalData();
|
||||
std::tuple<int, int, int> getDate();
|
||||
|
||||
/**
|
||||
|
|
144
src/main.cpp
144
src/main.cpp
|
@ -17,7 +17,6 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
// Variable pour accéder et gérer les préférences de l'application
|
||||
Preferences preferences;
|
||||
|
||||
// Array of DHT sensors with their respective pins and types
|
||||
|
@ -133,8 +132,6 @@ void setupWIFI(const char *wifi_name, const char *password)
|
|||
cpttry--;
|
||||
if (cpttry == 0)
|
||||
{
|
||||
Serial.print("\n Connexion impossible au Wi-Fi, les données serons enregistrées en local et envoyées une fois le connexion rétablie.\n");
|
||||
|
||||
Serial.print("wifi: 10 try go to sleep");
|
||||
sleep();
|
||||
}
|
||||
|
@ -147,7 +144,7 @@ void setupWIFI(const char *wifi_name, const char *password)
|
|||
|
||||
void reconnect(void)
|
||||
{
|
||||
Serial.println("Connecting to MQTT Broker...");
|
||||
Serial.println("\n Connecting to MQTT Broker...");
|
||||
int cpttry = 10;
|
||||
while (!MqttClient.connected())
|
||||
{
|
||||
|
@ -155,9 +152,8 @@ void reconnect(void)
|
|||
cpttry--;
|
||||
if (cpttry == 0)
|
||||
{
|
||||
Serial.print("\n Connexion impossible au MQTT, les données serons enregistrées en local et envoyées une fois le connexion rétablie.\n");
|
||||
|
||||
Serial.print("Mqtt: 10 try go to sleep");
|
||||
LocalSave();
|
||||
sleep();
|
||||
}
|
||||
|
||||
|
@ -250,7 +246,7 @@ unsigned long getTime()
|
|||
synctime.tv_usec = 0;
|
||||
if (settimeofday(&synctime, NULL) != 0)
|
||||
std::cout << "error\n";
|
||||
// TimeClient.forceUpdate(); // cette ligne crée un bug avec l'heure quand on est connecté au réseau du fablab.
|
||||
// TimeClient.forceUpdate(); //cette ligne crée un bug avec l'heure quand on est connecté au réseau du fablab
|
||||
vTaskDelay(1000);
|
||||
if (!getLocalTime(&timeinfo))
|
||||
{
|
||||
|
@ -262,6 +258,22 @@ unsigned long getTime()
|
|||
return now;
|
||||
}
|
||||
|
||||
int getAverageChargeLevel()
|
||||
{
|
||||
const int moyenne = 10;
|
||||
int charge[moyenne];
|
||||
int total = 0;
|
||||
|
||||
for (int i = 0; i < moyenne; i++)
|
||||
{
|
||||
charge[i] = Battery.getBatteryChargeLevel(); // Assurez-vous que Battery.getBatteryChargeLevel() est défini
|
||||
total += charge[i];
|
||||
}
|
||||
|
||||
int chargelvl = total / moyenne;
|
||||
return chargelvl;
|
||||
}
|
||||
|
||||
//-------------------- Création de trames --------------------//
|
||||
|
||||
/**
|
||||
|
@ -274,47 +286,7 @@ unsigned long getTime()
|
|||
*/
|
||||
void writeMessage(char *txt, float *temp, float *hum, int number)
|
||||
{
|
||||
// time_t rawtime = TimeClient.getEpochTime();
|
||||
int chargelvl1 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl2 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl3 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl4 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl5 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl6 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl7 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl8 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl9 = Battery.getBatteryChargeLevel();
|
||||
int chargelvl10 = Battery.getBatteryChargeLevel();
|
||||
|
||||
//-------------------- Début de l'enregistrement des données --------------------//
|
||||
|
||||
preferences.begin("chargelvl-hum-temp", false);
|
||||
int chargelvl = preferences.getInt("chargelvl", 1);
|
||||
|
||||
// Boucle pour récupérer les valeurs dans le tableau puis les enregistrer.
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
|
||||
char Temp[5];
|
||||
char Hum[5];
|
||||
float newTemp = preferences.getFloat(Temp, temp[i]);
|
||||
float newHum = preferences.getFloat(Hum, hum[i]);
|
||||
|
||||
preferences.putFloat("temp", newTemp);
|
||||
preferences.putFloat("hum", newHum);
|
||||
|
||||
Serial.printf("temp: %0.2f\n", newTemp); // print pour vérifier si les valeurs enregistrées sont les bonnes.
|
||||
Serial.printf("hum: %0.2f\n", newHum);
|
||||
}
|
||||
|
||||
chargelvl = (chargelvl1 + chargelvl2 + chargelvl3 + chargelvl4 + chargelvl5 + chargelvl6 + chargelvl7 + chargelvl8 + chargelvl9 + chargelvl10) / 10; // Calcul de la moyenne du niveau de charge de la batterie.
|
||||
|
||||
Serial.printf("chargelvl: %d\n", chargelvl); // print pour vérifier si les valeurs enregistrées sont les bonnes.
|
||||
preferences.putInt("chargelvl", chargelvl);
|
||||
preferences.end();
|
||||
|
||||
//-------------------- Fin de l'enregistrement des données sur le micro-controleur --------------------//
|
||||
|
||||
int chargelvl = getAverageChargeLevel();
|
||||
delay(2000);
|
||||
|
||||
switch (number)
|
||||
|
@ -443,7 +415,7 @@ bool TestBoot(bool resOrRfsh)
|
|||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println("-----------------Start---------------------");
|
||||
Serial.println("\n-----------------Start---------------------");
|
||||
|
||||
setupWIFI(SSID, PWD);
|
||||
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
||||
|
@ -486,6 +458,60 @@ void setup()
|
|||
// Serial.println('\n');
|
||||
}
|
||||
|
||||
// Fonction pour sauvegarder les données localement
|
||||
void LocalSave()
|
||||
{
|
||||
int chargelvl = getAverageChargeLevel(); // get de la batterie.
|
||||
|
||||
preferences.begin("capteurs", false);
|
||||
time_t rawtime;
|
||||
rawtime = preferences.getInt("time", 1);
|
||||
rawtime = TimeClient.getEpochTime();
|
||||
|
||||
Serial.print("\n-----------------Save---------------------\n");
|
||||
|
||||
for (int i = 0; i < 5; i++) // debut de la boucle pour récuperer les valeur dans le tableau puis les enregistrer
|
||||
{
|
||||
char Temp[5];
|
||||
char Humi[5];
|
||||
float newTemp = preferences.getFloat(Temp, temp[i]);
|
||||
float newHum = preferences.getFloat(Humi, hum[i]);
|
||||
|
||||
preferences.putFloat("temp", newTemp);
|
||||
preferences.putFloat("hum", newHum);
|
||||
|
||||
Serial.printf("temp: %0.2f\n", newTemp); // print pour vérifier si les valeurs enregistrées sont les bonnes
|
||||
Serial.printf("hum: %0.2f\n", newHum);
|
||||
}
|
||||
|
||||
Serial.printf("time: %d \n", rawtime);
|
||||
Serial.printf("chargelvl: %d\n", chargelvl); // print pour vérifier si les valeurs enregistrées sont les bonnes
|
||||
preferences.putInt("time", rawtime);
|
||||
preferences.putInt("chargelvl", chargelvl);
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
// Fonction pour envoyer les données locales
|
||||
void SendLocalData()
|
||||
{
|
||||
preferences.begin("capteurs", true);
|
||||
time_t rawtime = preferences.getInt("time", 0);
|
||||
int chargelvl = preferences.getInt("chargelvl", 0);
|
||||
|
||||
temp[4] = preferences.getFloat("temp", 0);
|
||||
hum[4] = preferences.getFloat("hum", 0);
|
||||
|
||||
char msgsld[150];
|
||||
sprintf(msgsld, "%lu|%s|%0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f|%d", rawtime, CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], hum[0], hum[1], hum[2], hum[3], hum[4], chargelvl);
|
||||
|
||||
MqttClient.publish(TOPIC, msgsld);
|
||||
Serial.println("msgsld : ");
|
||||
Serial.println(msgsld);
|
||||
|
||||
preferences.clear();
|
||||
preferences.end();
|
||||
}
|
||||
|
||||
//-------------------- Boucle principale --------------------//
|
||||
|
||||
/**
|
||||
|
@ -501,15 +527,16 @@ void loop()
|
|||
char msg[70];
|
||||
bool horoIssue = false;
|
||||
|
||||
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
||||
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
||||
|
||||
MqttClient.loop();
|
||||
if (!MqttClient.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
||||
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
||||
Serial.printf(msg);
|
||||
// MqttClient.publish(TOPIC, msg, date);
|
||||
|
||||
// TimeClient.update();
|
||||
// TimeClient.getFormattedTime().toCharArray(time, 30);
|
||||
|
@ -524,15 +551,20 @@ void loop()
|
|||
// Serial.println(day,DEC);
|
||||
// Serial.println('\n');
|
||||
|
||||
now = getTime(); // affiche le temps
|
||||
|
||||
now = getTime();
|
||||
lenght = sprintf(date, "%s", ultoa(now, date, 10) /* "|%d-%d-%d ", year, month, day */);
|
||||
sprintf(date + lenght, time);
|
||||
// Serial.println("msg1 : "); //debug print
|
||||
// Serial.println(msg); //debug print
|
||||
sprintf(date + strlen(date), msg);
|
||||
// Serial.println("msg2 : "); //debug print
|
||||
// Serial.println(msg); //debug print
|
||||
// Serial.println("date : "); //debug print
|
||||
// Serial.println(date); //debug print
|
||||
SendLocalData();
|
||||
MqttClient.publish(TOPIC, date);
|
||||
preferences.clear();
|
||||
|
||||
Serial.println("-----------------DeepSleep---------------------");
|
||||
Serial.println("----------------- DeepSleep ---------------------");
|
||||
delay(2000);
|
||||
|
||||
sleep();
|
||||
|
|
Loading…
Reference in a new issue