code-grappe/src/main.cpp

187 lines
5 KiB
C++
Raw Normal View History

#include "main.h"
#include <ctime>
#include <iostream>
#include <chrono>
2021-06-21 11:29:53 +02:00
using namespace std;
DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), DHT(17, DHT22), DHT(16, DHT22)};
float temp[SENSORS_NUMBER];
float hum[SENSORS_NUMBER];
2021-06-21 11:29:53 +02:00
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);
2021-06-21 11:29:53 +02:00
//-------------------- FONCTIONS --------------------//
//-------------- Connexion MQTT --------------//
void setupMQTT(const char *address, int port)
2021-06-21 11:29:53 +02:00
{
MqttClient.setServer(address, port);
2021-06-21 11:29:53 +02:00
}
void setupWIFI(const char *wifi_name, const char *password)
2021-06-21 11:29:53 +02:00
{
Serial.println('\n');
WiFi.begin(wifi_name, password);
2021-06-21 11:29:53 +02:00
Serial.print("Connecting to ");
Serial.print(wifi_name);
2021-06-21 11:29:53 +02:00
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())
2021-06-21 11:29:53 +02:00
{
Serial.print(".");
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe)
2021-06-21 11:29:53 +02:00
{
Serial.println("Connected.");
}
}
}
//-------------- Initialisation et lecture des capteurs --------------//
void initSensors(DHT *sensors, int number)
2021-06-21 11:29:53 +02:00
{
int i;
for (i = 0; i < number; i++)
2021-06-21 11:29:53 +02:00
{
sensors[i].begin();
2021-06-21 11:29:53 +02:00
}
}
void readSensors(DHT sensors[], float temp[], float hum[], int number)
2021-06-21 11:29:53 +02:00
{
int i;
for (i = 0; i < number; i++)
2021-06-21 11:29:53 +02:00
{
*(temp + i) = sensors[i].readTemperature();
*(hum + i) = sensors[i].readHumidity();
2021-06-21 11:29:53 +02:00
}
}
//-------------------- Sleep de l'ESP --------------------//
2021-06-21 11:29:53 +02:00
void sleep()
{
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * US_TO_S_FACTOR);
2021-06-21 11:29:53 +02:00
esp_deep_sleep_start();
}
//exemple d'une triple utilisation de valeur pour une fonction utilise pour la date
2021-06-21 11:29:53 +02:00
std::tuple<int, int, int> getDate()
{
time_t rawtime = TimeClient.getEpochTime();
2021-06-21 11:29:53 +02:00
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);
}
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
//Serial.println("Failed to obtain time");
return(0);
}
time(&now);
return now;
}
//-------------------- Création de trames --------------------//
void writeMessage(char *txt, float *temp, float *hum, int number)
2021-06-21 11:29:53 +02:00
{
int chargelvl = Battery.getBatteryChargeLevel();
//float homeValue = 3.3 * analogRead(ADC_PIN) / 2048; for test measurement
//float homeValue1 = 1.7 * analogRead(ADC_PIN) / 1000;
switch (number)
2021-06-21 11:29:53 +02:00
{
case 1:
sprintf(txt, "|%s|%0.2f|%0.2f", CLUSTER, temp[0], hum[0]);
2021-06-21 11:29:53 +02:00
break;
case 2:
sprintf(txt, "|%s|%0.2f %0.2f|%0.2f %0.2f", CLUSTER, temp[0], temp[1], hum[0], hum[1]);
2021-06-21 11:29:53 +02:00
break;
case 3:
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]);
2021-06-21 11:29:53 +02:00
break;
case 4:
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]);
2021-06-21 11:29:53 +02:00
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|%f|%f", CLUSTER, temp[0], temp[1], temp[2], temp[3], temp[4], hum[0], hum[1], hum[2], hum[3], hum[4], chargelvl, homeValue, homeValue1);
2021-06-21 11:29:53 +02:00
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", 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]);
2021-06-21 11:29:53 +02:00
break;
default:
Serial.println("Erreur, temp et hum sont trop longs : trop de capteurs");
break;
}
}
//-------------------- Initialisation --------------------//
2021-06-21 11:29:53 +02:00
void setup()
{
Serial.begin(9600);
setupWIFI(SSID, PWD);
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
initSensors(sensors, SENSORS_NUMBER);
TimeClient.begin();
2021-06-21 11:29:53 +02:00
}
//-------------------- Boucle principale --------------------//
2021-06-21 11:29:53 +02:00
void loop()
{
int year, month, day;
int lenght;
char time[30];
char date[30];
char msg[70];
MqttClient.loop();
if (!MqttClient.connected())
2021-06-21 11:29:53 +02:00
{
reconnect();
}
readSensors(sensors, temp, hum, SENSORS_NUMBER);
writeMessage(msg, temp, hum, SENSORS_NUMBER);
2021-06-21 11:29:53 +02:00
TimeClient.update();
TimeClient.getFormattedTime().toCharArray(time, 30);
2021-06-21 11:29:53 +02:00
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);
delay(2000);
2021-06-21 11:29:53 +02:00
sleep();
}