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
|
||||
#define HEADER_H
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
@ -9,15 +16,14 @@
|
|||
#include <secret.h>
|
||||
|
||||
#define TOPIC "test1"
|
||||
#define MQTT_ADRESS "192.168.0.242"
|
||||
#define MQTT_ADDRESS "192.168.0.242"
|
||||
#define MQTT_PORT 1883
|
||||
#define DELAI 600000 //en ms
|
||||
#define ESPNAME "esp32-bastien"
|
||||
#define GRAPPE "grappe2"
|
||||
#define NOMBRE_CAPTEUR 5
|
||||
#define CLUSTER "grappe1"
|
||||
#define SENSORS_NUMBER 5
|
||||
#define DHT22 22
|
||||
#define TIME_TO_SLEEP 598
|
||||
#define uS_TO_S_FACTOR 1000000
|
||||
#define US_TO_S_FACTOR 1000000
|
||||
#define R2 100
|
||||
#define R3 10
|
||||
#define VOLTAGE_OUT(Vin) (((Vin)*R3) / (R2 + R3))
|
||||
|
@ -31,12 +37,14 @@
|
|||
#define CONV_FACTOR 2.92
|
||||
#define READS 20
|
||||
|
||||
void setupMQTT(const char *adresse, int port);
|
||||
void setupWIFI(const char *wifiName, const char *password);
|
||||
void setupMQTT(const char *address, int port);
|
||||
void setupWIFI(const char *wifi_name, const char *password);
|
||||
void reconnect(void);
|
||||
void initCapteurs(DHT *capteurs, int L);
|
||||
void lireCapteurs(DHT capteurs[], float temp[], float hum[], int L);
|
||||
void ecrireMessage(char *txt, float *temp, float *hum, int L);
|
||||
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
|
||||
|
106
src/main.cpp
106
src/main.cpp
|
@ -1,31 +1,33 @@
|
|||
#include "header.h"
|
||||
#include "main.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
DHT capteurs[NOMBRE_CAPTEUR] = {DHT(23, DHT22), DHT(22, DHT22), DHT(21, DHT22), DHT(17, DHT22), DHT(2, DHT22)};
|
||||
float temp[NOMBRE_CAPTEUR];
|
||||
float hum[NOMBRE_CAPTEUR];
|
||||
DHT sensors[SENSORS_NUMBER] = {DHT(23, DHT22), DHT(22, DHT22), DHT(21, DHT22), DHT(17, DHT22), DHT(2, DHT22)};
|
||||
float temp[SENSORS_NUMBER];
|
||||
float hum[SENSORS_NUMBER];
|
||||
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient mqttClient(wifiClient);
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 0, 60000);
|
||||
Pangodream_18650_CL BL(ADC_PIN, CONV_FACTOR, READS);
|
||||
WiFiClient WifiClient;
|
||||
PubSubClient MqttClient(WifiClient);
|
||||
WiFiUDP NtpUDP;
|
||||
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 0, 60000);
|
||||
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
|
||||
|
||||
//-------------------- 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');
|
||||
|
||||
WiFi.begin(wifiName, password);
|
||||
WiFi.begin(wifi_name, password);
|
||||
Serial.print("Connecting to ");
|
||||
Serial.print(wifiName);
|
||||
Serial.print(wifi_name);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
|
@ -41,45 +43,49 @@ void setupWIFI(const char *wifiName, const char *password)
|
|||
void reconnect(void)
|
||||
{
|
||||
Serial.println("Connecting to MQTT Broker...");
|
||||
while (!mqttClient.connected())
|
||||
while (!MqttClient.connected())
|
||||
{
|
||||
Serial.print(".");
|
||||
if (mqttClient.connect(ESPNAME,MQTT_USER,MQTT_MDP))
|
||||
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_PWD))
|
||||
{
|
||||
Serial.println("Connected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void initCapteurs(DHT *capteurs, int L)
|
||||
//-------------- Initialisation et lecture des capteurs --------------//
|
||||
|
||||
void initSensors(DHT *sensors, int number)
|
||||
{
|
||||
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;
|
||||
for (i = 0; i < L; i++)
|
||||
for (i = 0; i < number; i++)
|
||||
{
|
||||
*(temp + i) = capteurs[i].readTemperature();
|
||||
*(hum + i) = capteurs[i].readHumidity();
|
||||
*(temp + i) = sensors[i].readTemperature();
|
||||
*(hum + i) = sensors[i].readHumidity();
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------- Sleep de l'ESP --------------------//
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
//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()
|
||||
{
|
||||
time_t rawtime = timeClient.getEpochTime();
|
||||
time_t rawtime = TimeClient.getEpochTime();
|
||||
struct tm *ti;
|
||||
ti = localtime(&rawtime);
|
||||
int year = ti->tm_year + 1900;
|
||||
|
@ -89,28 +95,30 @@ std::tuple<int, int, int> getDate()
|
|||
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();
|
||||
switch (L)
|
||||
int chargelvl = Battery.getBatteryChargeLevel();
|
||||
switch (number)
|
||||
{
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
default:
|
||||
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()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
setupWIFI(SSID, PWD);
|
||||
setupMQTT(MQTT_ADRESS, MQTT_PORT);
|
||||
initCapteurs(capteurs, NOMBRE_CAPTEUR);
|
||||
timeClient.begin();
|
||||
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
||||
initSensors(sensors, SENSORS_NUMBER);
|
||||
TimeClient.begin();
|
||||
}
|
||||
|
||||
//-------------------- Boucle pricipale --------------------//
|
||||
|
||||
void loop()
|
||||
{
|
||||
int year, month, day;
|
||||
|
@ -135,25 +147,25 @@ void loop()
|
|||
char date[30];
|
||||
char msg[70];
|
||||
|
||||
mqttClient.loop();
|
||||
if (!mqttClient.connected())
|
||||
MqttClient.loop();
|
||||
if (!MqttClient.connected())
|
||||
{
|
||||
reconnect();
|
||||
}
|
||||
|
||||
lireCapteurs(capteurs, temp, hum, NOMBRE_CAPTEUR);
|
||||
ecrireMessage(msg, temp, hum, NOMBRE_CAPTEUR);
|
||||
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
||||
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
||||
|
||||
timeClient.update();
|
||||
timeClient.getFormattedTime().toCharArray(time, 30);
|
||||
TimeClient.update();
|
||||
TimeClient.getFormattedTime().toCharArray(time, 30);
|
||||
|
||||
tie(year, month, day) = getDate();
|
||||
lenght = sprintf(date, "%d-%d-%d ", year, month, day);
|
||||
sprintf(date + lenght, time);
|
||||
sprintf(date + strlen(date), msg);
|
||||
mqttClient.publish(TOPIC, date);
|
||||
MqttClient.publish(TOPIC, date);
|
||||
|
||||
delay(2000);
|
||||
|
||||
sleep();
|
||||
|
||||
delay(DELAI);
|
||||
}
|
Loading…
Reference in a new issue