documentation + convention typographique

This commit is contained in:
bastien 2021-06-28 15:43:37 +02:00
parent ae7c1ef751
commit a8f2c7ebf3
2 changed files with 111 additions and 91 deletions

View file

@ -1,43 +1,51 @@
#ifndef HEADER_H /**
#define HEADER_H * @file main.h
#include <ESPAsyncWebServer.h> * @brief projet thermo-bibli, Équiper la réserve de l'UB de capteurs de température et hygrométrie autonomes et connectés
#include <WiFiUdp.h> * @author Bastien Boineau, Lucas Pallaro, Loris Galland
#include <NTPClient.h> * @version 1.0
#include <DHT.h> * @date 28/06/2021
#include <PubSubClient.h> */
#include <Pangodream_18650_CL.h> #ifndef HEADER_H
#include <secret.h> #define HEADER_H
#include <ESPAsyncWebServer.h>
#define TOPIC "test1" #include <WiFiUdp.h>
#define MQTT_ADRESS "192.168.0.242" #include <NTPClient.h>
#define MQTT_PORT 1883 #include <DHT.h>
#define DELAI 600000 //en ms #include <PubSubClient.h>
#define ESPNAME "esp32-bastien" #include <Pangodream_18650_CL.h>
#define GRAPPE "grappe2" #include <secret.h>
#define NOMBRE_CAPTEUR 5
#define DHT22 22 #define TOPIC "test1"
#define TIME_TO_SLEEP 598 #define MQTT_ADDRESS "192.168.0.242"
#define uS_TO_S_FACTOR 1000000 #define MQTT_PORT 1883
#define R2 100 #define ESPNAME "esp32-bastien"
#define R3 10 #define CLUSTER "grappe1"
#define VOLTAGE_OUT(Vin) (((Vin)*R3) / (R2 + R3)) #define SENSORS_NUMBER 5
#define VOLTAGE_MAX 4200 #define DHT22 22
#define VOLTAGE_MIN 3300 #define TIME_TO_SLEEP 598
#define ADC_REFERENCE 1100 #define US_TO_S_FACTOR 1000000
#define VOLTAGE_TO_ADC(in) ((ADC_REFERENCE * (in)) / 4096) #define R2 100
#define BATTERY_MAX_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MAX)) #define R3 10
#define BATTERY_MIN_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MIN)) #define VOLTAGE_OUT(Vin) (((Vin)*R3) / (R2 + R3))
#define ADC_PIN 34 #define VOLTAGE_MAX 4200
#define CONV_FACTOR 2.92 #define VOLTAGE_MIN 3300
#define READS 20 #define ADC_REFERENCE 1100
#define VOLTAGE_TO_ADC(in) ((ADC_REFERENCE * (in)) / 4096)
void setupMQTT(const char *adresse, int port); #define BATTERY_MAX_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MAX))
void setupWIFI(const char *wifiName, const char *password); #define BATTERY_MIN_ADC VOLTAGE_TO_ADC(VOLTAGE_OUT(VOLTAGE_MIN))
void reconnect(void); #define ADC_PIN 34
void initCapteurs(DHT *capteurs, int L); #define CONV_FACTOR 2.92
void lireCapteurs(DHT capteurs[], float temp[], float hum[], int L); #define READS 20
void ecrireMessage(char *txt, float *temp, float *hum, int L);
void setupMQTT(const char *address, int port);
//timeClient.getEpochTime().toCharArray(date, 50); = convertir un string en char void setupWIFI(const char *wifi_name, const char *password);
void reconnect(void);
#endif void initSensors(DHT *sensors, int number);
void readSensors(DHT sensors[], float temp[], float hum[], int number);
void sleep();
std::tuple<int, int, int> getDate();
void writeMsg(char *txt, float *temp, float *hum, int number);
//timeClient.getEpochTime().toCharArray(date, 50); = convertir un string en char
#endif

View file

@ -1,31 +1,33 @@
#include "header.h" #include "main.h"
using namespace std; using namespace std;
DHT capteurs[NOMBRE_CAPTEUR] = {DHT(23, DHT22), DHT(22, DHT22), DHT(21, DHT22), DHT(17, DHT22), DHT(2, DHT22)}; DHT sensors[SENSORS_NUMBER] = {DHT(23, DHT22), DHT(22, DHT22), DHT(21, DHT22), DHT(17, DHT22), DHT(2, DHT22)};
float temp[NOMBRE_CAPTEUR]; float temp[SENSORS_NUMBER];
float hum[NOMBRE_CAPTEUR]; float hum[SENSORS_NUMBER];
WiFiClient wifiClient; WiFiClient WifiClient;
PubSubClient mqttClient(wifiClient); PubSubClient MqttClient(WifiClient);
WiFiUDP ntpUDP; WiFiUDP NtpUDP;
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 0, 60000); NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 0, 60000);
Pangodream_18650_CL BL(ADC_PIN, CONV_FACTOR, READS); Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
//-------------------- FONCTIONS --------------------// //-------------------- FONCTIONS --------------------//
void setupMQTT(const char *adresse, int port) //-------------- Connexion MQTT --------------//
void setupMQTT(const char *address, int port)
{ {
mqttClient.setServer(adresse, port); MqttClient.setServer(address, port);
} }
void setupWIFI(const char *wifiName, const char *password) void setupWIFI(const char *wifi_name, const char *password)
{ {
Serial.println('\n'); Serial.println('\n');
WiFi.begin(wifiName, password); WiFi.begin(wifi_name, password);
Serial.print("Connecting to "); Serial.print("Connecting to ");
Serial.print(wifiName); Serial.print(wifi_name);
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
@ -41,45 +43,49 @@ void setupWIFI(const char *wifiName, const char *password)
void reconnect(void) void reconnect(void)
{ {
Serial.println("Connecting to MQTT Broker..."); Serial.println("Connecting to MQTT Broker...");
while (!mqttClient.connected()) while (!MqttClient.connected())
{ {
Serial.print("."); Serial.print(".");
if (mqttClient.connect(ESPNAME,MQTT_USER,MQTT_MDP)) if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_PWD))
{ {
Serial.println("Connected."); Serial.println("Connected.");
} }
} }
} }
void initCapteurs(DHT *capteurs, int L) //-------------- Initialisation et lecture des capteurs --------------//
void initSensors(DHT *sensors, int number)
{ {
int i; int i;
for (i = 0; i < L; i++) for (i = 0; i < number; i++)
{ {
capteurs[i].begin(); sensors[i].begin();
} }
} }
void lireCapteurs(DHT capteurs[], float temp[], float hum[], int L) void readSensors(DHT sensors[], float temp[], float hum[], int number)
{ {
int i; int i;
for (i = 0; i < L; i++) for (i = 0; i < number; i++)
{ {
*(temp + i) = capteurs[i].readTemperature(); *(temp + i) = sensors[i].readTemperature();
*(hum + i) = capteurs[i].readHumidity(); *(hum + i) = sensors[i].readHumidity();
} }
} }
//-------------------- Sleep de l'ESP --------------------//
void sleep() void sleep()
{ {
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * US_TO_S_FACTOR);
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
//exemple d'une triple utilisation de valeur pour une fonction //exemple d'une triple utilisation de valeur pour une fonction utilise pour la date
std::tuple<int, int, int> getDate() std::tuple<int, int, int> getDate()
{ {
time_t rawtime = timeClient.getEpochTime(); time_t rawtime = TimeClient.getEpochTime();
struct tm *ti; struct tm *ti;
ti = localtime(&rawtime); ti = localtime(&rawtime);
int year = ti->tm_year + 1900; int year = ti->tm_year + 1900;
@ -89,28 +95,30 @@ std::tuple<int, int, int> getDate()
return std::make_tuple(year, month, day); return std::make_tuple(year, month, day);
} }
void ecrireMessage(char *txt, float *temp, float *hum, int L) //-------------------- Création de trames --------------------//
void writeMessage(char *txt, float *temp, float *hum, int number)
{ {
int batte = BL.getBatteryChargeLevel(); int chargelvl = Battery.getBatteryChargeLevel();
switch (L) switch (number)
{ {
case 1: case 1:
sprintf(txt, "|%s|%0.2f|%0.2f", GRAPPE, temp[0], hum[0]); sprintf(txt, "|%s|%0.2f|%0.2f", CLUSTER, temp[0], hum[0]);
break; break;
case 2: case 2:
sprintf(txt, "|%s|%0.2f %0.2f|%0.2f %0.2f", GRAPPE, temp[0], temp[1], hum[0], hum[1]); sprintf(txt, "|%s|%0.2f %0.2f|%0.2f %0.2f", CLUSTER, temp[0], temp[1], hum[0], hum[1]);
break; break;
case 3: case 3:
sprintf(txt, "|%s|%0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f", GRAPPE, temp[0], temp[1], temp[2], hum[0], hum[1], hum[2]); 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]);
break; break;
case 4: case 4:
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f", GRAPPE, temp[0], temp[1], temp[2], temp[3], hum[0], hum[1], hum[2], hum[3]); 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]);
break; break;
case 5: case 5:
sprintf(txt, "|%s|%0.2f %0.2f %0.2f %0.2f %0.2f|%0.2f %0.2f %0.2f %0.2f %0.2f|%d", GRAPPE, temp[0], temp[1], temp[2], temp[3], temp[4], hum[0], hum[1], hum[2], hum[3], hum[4], batte); 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);
break; break;
case 6: case 6:
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", GRAPPE, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], hum[0], hum[1], hum[2], hum[3], hum[4], hum[5]); 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]);
break; break;
default: default:
Serial.println("Erreur, temp et hum sont trop longs : trop de capteurs"); Serial.println("Erreur, temp et hum sont trop longs : trop de capteurs");
@ -118,15 +126,19 @@ void ecrireMessage(char *txt, float *temp, float *hum, int L)
} }
} }
//-------------------- Initialisation --------------------//
void setup() void setup()
{ {
Serial.begin(9600); Serial.begin(9600);
setupWIFI(SSID, PWD); setupWIFI(SSID, PWD);
setupMQTT(MQTT_ADRESS, MQTT_PORT); setupMQTT(MQTT_ADDRESS, MQTT_PORT);
initCapteurs(capteurs, NOMBRE_CAPTEUR); initSensors(sensors, SENSORS_NUMBER);
timeClient.begin(); TimeClient.begin();
} }
//-------------------- Boucle pricipale --------------------//
void loop() void loop()
{ {
int year, month, day; int year, month, day;
@ -135,25 +147,25 @@ void loop()
char date[30]; char date[30];
char msg[70]; char msg[70];
mqttClient.loop(); MqttClient.loop();
if (!mqttClient.connected()) if (!MqttClient.connected())
{ {
reconnect(); reconnect();
} }
lireCapteurs(capteurs, temp, hum, NOMBRE_CAPTEUR); readSensors(sensors, temp, hum, SENSORS_NUMBER);
ecrireMessage(msg, temp, hum, NOMBRE_CAPTEUR); writeMessage(msg, temp, hum, SENSORS_NUMBER);
timeClient.update(); TimeClient.update();
timeClient.getFormattedTime().toCharArray(time, 30); TimeClient.getFormattedTime().toCharArray(time, 30);
tie(year, month, day) = getDate(); tie(year, month, day) = getDate();
lenght = sprintf(date, "%d-%d-%d ", year, month, day); lenght = sprintf(date, "%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);
delay(2000);
sleep(); sleep();
}
delay(DELAI);
}