code-grappe/src/main.cpp

160 lines
4 KiB
C++
Raw Normal View History

2021-06-21 11:29:53 +02:00
#include "header.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];
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);
//-------------------- FONCTIONS --------------------//
void setupMQTT(const char *adresse, int port)
{
mqttClient.setServer(adresse, port);
}
void setupWIFI(const char *wifiName, const char *password)
{
Serial.println('\n');
WiFi.begin(wifiName, password);
Serial.print("Connecting to ");
Serial.print(wifiName);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print('.');
}
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...");
while (!mqttClient.connected())
{
Serial.print(".");
if (mqttClient.connect(ESPNAME,MQTT_USER,MQTT_MDP))
{
Serial.println("Connected.");
}
}
}
void initCapteurs(DHT *capteurs, int L)
{
int i;
for (i = 0; i < L; i++)
{
capteurs[i].begin();
}
}
void lireCapteurs(DHT capteurs[], float temp[], float hum[], int L)
{
int i;
for (i = 0; i < L; i++)
{
*(temp + i) = capteurs[i].readTemperature();
*(hum + i) = capteurs[i].readHumidity();
}
}
void sleep()
{
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
std::tuple<int, int, int> getDate()
{
time_t rawtime = timeClient.getEpochTime();
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);
}
void ecrireMessage(char *txt, float *temp, float *hum, int L)
{
int batte = BL.getBatteryChargeLevel();
switch (L)
{
case 1:
sprintf(txt, "|%s|%0.2f|%0.2f", GRAPPE, 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]);
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]);
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]);
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);
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]);
break;
default:
Serial.println("Erreur, temp et hum sont trop longs : trop de capteurs");
break;
}
}
void setup()
{
Serial.begin(9600);
setupWIFI(SSID, PWD);
setupMQTT(MQTT_ADRESS, MQTT_PORT);
initCapteurs(capteurs, NOMBRE_CAPTEUR);
timeClient.begin();
}
void loop()
{
int year, month, day;
int lenght;
char time[30];
char date[30];
char msg[70];
mqttClient.loop();
if (!mqttClient.connected())
{
reconnect();
}
lireCapteurs(capteurs, temp, hum, NOMBRE_CAPTEUR);
ecrireMessage(msg, temp, hum, NOMBRE_CAPTEUR);
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);
sleep();
delay(DELAI);
}