documentation + convention typographique
This commit is contained in:
parent
ae7c1ef751
commit
a8f2c7ebf3
|
@ -1,3 +1,10 @@
|
||||||
|
/**
|
||||||
|
* @file main.h
|
||||||
|
* @brief projet thermo-bibli, Équiper la réserve de l'UB de capteurs de température et hygrométrie autonomes et connectés
|
||||||
|
* @author Bastien Boineau, Lucas Pallaro, Loris Galland
|
||||||
|
* @version 1.0
|
||||||
|
* @date 28/06/2021
|
||||||
|
*/
|
||||||
#ifndef HEADER_H
|
#ifndef HEADER_H
|
||||||
#define HEADER_H
|
#define HEADER_H
|
||||||
#include <ESPAsyncWebServer.h>
|
#include <ESPAsyncWebServer.h>
|
||||||
|
@ -9,15 +16,14 @@
|
||||||
#include <secret.h>
|
#include <secret.h>
|
||||||
|
|
||||||
#define TOPIC "test1"
|
#define TOPIC "test1"
|
||||||
#define MQTT_ADRESS "192.168.0.242"
|
#define MQTT_ADDRESS "192.168.0.242"
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
#define DELAI 600000 //en ms
|
|
||||||
#define ESPNAME "esp32-bastien"
|
#define ESPNAME "esp32-bastien"
|
||||||
#define GRAPPE "grappe2"
|
#define CLUSTER "grappe1"
|
||||||
#define NOMBRE_CAPTEUR 5
|
#define SENSORS_NUMBER 5
|
||||||
#define DHT22 22
|
#define DHT22 22
|
||||||
#define TIME_TO_SLEEP 598
|
#define TIME_TO_SLEEP 598
|
||||||
#define uS_TO_S_FACTOR 1000000
|
#define US_TO_S_FACTOR 1000000
|
||||||
#define R2 100
|
#define R2 100
|
||||||
#define R3 10
|
#define R3 10
|
||||||
#define VOLTAGE_OUT(Vin) (((Vin)*R3) / (R2 + R3))
|
#define VOLTAGE_OUT(Vin) (((Vin)*R3) / (R2 + R3))
|
||||||
|
@ -31,12 +37,14 @@
|
||||||
#define CONV_FACTOR 2.92
|
#define CONV_FACTOR 2.92
|
||||||
#define READS 20
|
#define READS 20
|
||||||
|
|
||||||
void setupMQTT(const char *adresse, int port);
|
void setupMQTT(const char *address, int port);
|
||||||
void setupWIFI(const char *wifiName, const char *password);
|
void setupWIFI(const char *wifi_name, const char *password);
|
||||||
void reconnect(void);
|
void reconnect(void);
|
||||||
void initCapteurs(DHT *capteurs, int L);
|
void initSensors(DHT *sensors, int number);
|
||||||
void lireCapteurs(DHT capteurs[], float temp[], float hum[], int L);
|
void readSensors(DHT sensors[], float temp[], float hum[], int number);
|
||||||
void ecrireMessage(char *txt, float *temp, float *hum, int L);
|
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
|
//timeClient.getEpochTime().toCharArray(date, 50); = convertir un string en char
|
||||||
|
|
106
src/main.cpp
106
src/main.cpp
|
@ -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);
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue