amelioration+debug+com
This commit is contained in:
parent
aa83fc58d6
commit
42f7f3331d
|
@ -15,11 +15,11 @@
|
|||
#include <Pangodream_18650_CL.h>
|
||||
#include <secret.h>
|
||||
|
||||
#define TOPIC "prod"
|
||||
#define TOPIC "test"
|
||||
#define MQTT_ADDRESS "185.233.103.24"
|
||||
#define MQTT_PORT 1883
|
||||
#define ESPNAME "esp32-bastien"
|
||||
#define CLUSTER "grappe1"
|
||||
#define ESPNAME "esp32-fablab"
|
||||
#define CLUSTER "grappe3"
|
||||
#define SENSORS_NUMBER 5
|
||||
#define DHT22 22
|
||||
//comment for the test part this is the good frequency
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#define MQTT_USER "capteurs"
|
||||
#define MQTT_MDP "Fablab"
|
||||
#define SSID "Thermo-Bibli"
|
||||
#define PWD "motdepass"
|
||||
|
||||
#define SSID "coh@bit"
|
||||
#define PWD "lewifidecohabit"
|
|
@ -4,6 +4,7 @@
|
|||
; Upload options: custom upload port, speed and extra flags
|
||||
; Library options: dependencies, extra library storages
|
||||
; Advanced options: extra scripting
|
||||
|
||||
;
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
|
257
src/main.cpp
257
src/main.cpp
|
@ -1,6 +1,5 @@
|
|||
#include "main.h"
|
||||
#include "NTPClient.h"
|
||||
//#include "driver/"
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <nvs_flash.h>
|
||||
|
@ -17,58 +16,71 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
// Array of DHT sensors with their respective pins and types
|
||||
DHT sensors[SENSORS_NUMBER] = {DHT(04, DHT22), DHT(18, DHT22), DHT(05, DHT22), DHT(17, DHT22), DHT(16, DHT22)};
|
||||
|
||||
// Arrays to store temperature and humidity readings
|
||||
float temp[SENSORS_NUMBER];
|
||||
float hum[SENSORS_NUMBER];
|
||||
|
||||
// Pangodream_18650_CL battery object with ADC pin, conversion factor, and number of reads
|
||||
Pangodream_18650_CL Battery(ADC_PIN, CONV_FACTOR, READS);
|
||||
|
||||
// WiFi client object
|
||||
WiFiClient WifiClient;
|
||||
|
||||
// PubSubClient object for MQTT communication
|
||||
PubSubClient MqttClient(WifiClient);
|
||||
|
||||
// UDP object for NTP communication
|
||||
WiFiUDP NtpUDP;
|
||||
|
||||
/**
|
||||
* @brief create NTP profile for frenchs users
|
||||
*
|
||||
* @param NtpUDP: Protocole use
|
||||
*
|
||||
* @param "europe.pool.ntp.org": serve target
|
||||
*
|
||||
* @param 7200: jet lag europe time from France (2 * 3600 sec)
|
||||
*/
|
||||
//NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org" , 7200 , 60000); // ancienne configuration
|
||||
NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org");
|
||||
// NTP client object with UDP and NTP server address
|
||||
NTPClient TimeClient(NtpUDP, "192.168.23.254"); /*si vous utilisez le Wifi du fablab*/
|
||||
// NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org"); /*si vous utilisez un réseau 4g ou autre */
|
||||
|
||||
//-------------------- CONDITION --------------------//
|
||||
|
||||
|
||||
/**
|
||||
* @brief test if the time is good for restart or refresh memory
|
||||
* @brief Test if the time is good for restart or refresh memory
|
||||
*
|
||||
* @param hours
|
||||
* @param minutes
|
||||
* @param restart bool to choose which the restart time bool or the resfres time bool will be returned
|
||||
* @param restart bool to choose which the restart time bool or the refresh time bool will be returned
|
||||
* @return true It's time to refresh or restart
|
||||
* @return false It's not the time to refresh or restart
|
||||
*/
|
||||
bool testTime (int hours, int minutes, bool restart){
|
||||
bool testTime(int hours, int minutes, bool restart)
|
||||
{
|
||||
// debug parts
|
||||
// Serial.println('\n');
|
||||
// Serial.println((REFRESHTIMES * TIME_TO_SLEEP) / 3600,DEC);
|
||||
// Serial.println(((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
|
||||
// Serial.println('\n');
|
||||
|
||||
if (restart){ // restart parts
|
||||
if(hours < (RESTARTTIMES * TIME_TO_SLEEP) / 3600) return true;
|
||||
else{
|
||||
if((hours = (RESTARTTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
|
||||
if (restart)
|
||||
{ // restart parts
|
||||
if (hours < (RESTARTTIMES * TIME_TO_SLEEP) / 3600)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
if ((hours = (RESTARTTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
} else { // refresh parts
|
||||
if(hours >= (RESTARTTIMES * TIME_TO_SLEEP) / 3600){
|
||||
if(hours < (REFRESHTIMES * TIME_TO_SLEEP) / 3600)return true;
|
||||
else{
|
||||
if((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
|
||||
else{
|
||||
}
|
||||
else
|
||||
{ // refresh parts
|
||||
if (hours >= (RESTARTTIMES * TIME_TO_SLEEP) / 3600)
|
||||
{
|
||||
if (hours < (REFRESHTIMES * TIME_TO_SLEEP) / 3600)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
if ((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -78,44 +90,27 @@ bool testTime (int hours, int minutes, bool restart){
|
|||
return false;
|
||||
}
|
||||
|
||||
// bool testTime (int hours, int minutes, bool restart){
|
||||
// // debug parts
|
||||
// // Serial.println('\n');
|
||||
// // Serial.println((REFRESHTIMES * TIME_TO_SLEEP) / 3600,DEC);
|
||||
// // Serial.println(((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
|
||||
// // Serial.println('\n');
|
||||
|
||||
// if (restart){ // restart parts
|
||||
// if( hours >= 12 && minutes >= 0 ) return true;
|
||||
// else{
|
||||
// if((hours < 12) /*&& (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60) */)return true;
|
||||
// return false;
|
||||
// }
|
||||
// } else { // refresh parts
|
||||
// if(hours >= 12){
|
||||
// if(hours <= 14 && minutes > 20 )return true;
|
||||
// else{
|
||||
// if((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
|
||||
// else{
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // Not needed but carefully habits
|
||||
// return false;
|
||||
// }
|
||||
|
||||
//-------------------- FONCTIONS --------------------//
|
||||
|
||||
//-------------- Connexion MQTT --------------//
|
||||
|
||||
/**
|
||||
* @brief Set up MQTT server with address and port
|
||||
*
|
||||
* @param address MQTT server address
|
||||
* @param port MQTT server port
|
||||
*/
|
||||
void setupMQTT(const char *address, int port)
|
||||
{
|
||||
MqttClient.setServer(address, port);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set up WiFi connection with SSID and password
|
||||
*
|
||||
* @param wifi_name WiFi SSID
|
||||
* @param password WiFi password
|
||||
*/
|
||||
void setupWIFI(const char *wifi_name, const char *password)
|
||||
{
|
||||
Serial.println('\n');
|
||||
|
@ -132,7 +127,8 @@ void setupWIFI(const char *wifi_name, const char *password)
|
|||
|
||||
/* stop trying wifi connexion */
|
||||
cpttry--;
|
||||
if(cpttry == 0){
|
||||
if (cpttry == 0)
|
||||
{
|
||||
Serial.print("wifi: 10 try go to sleep");
|
||||
sleep();
|
||||
}
|
||||
|
@ -151,12 +147,14 @@ void reconnect(void)
|
|||
{
|
||||
/* stop trying mqtt connexion */
|
||||
cpttry--;
|
||||
if(cpttry == 0){
|
||||
if (cpttry == 0)
|
||||
{
|
||||
Serial.print("Mqtt: 10 try go to sleep");
|
||||
sleep();
|
||||
}
|
||||
|
||||
Serial.print(".");
|
||||
|
||||
if (MqttClient.connect(ESPNAME, MQTT_USER, MQTT_MDP)) // MQTT_MDP (mot de passe)
|
||||
{
|
||||
Serial.println("Connected.");
|
||||
|
@ -166,6 +164,12 @@ void reconnect(void)
|
|||
|
||||
//-------------- Initialisation et lecture des capteurs --------------//
|
||||
|
||||
/**
|
||||
* @brief Initialize DHT sensors
|
||||
*
|
||||
* @param sensors array of DHT sensors
|
||||
* @param number number of sensors
|
||||
*/
|
||||
void initSensors(DHT *sensors, int number)
|
||||
{
|
||||
int i;
|
||||
|
@ -175,6 +179,14 @@ void initSensors(DHT *sensors, int number)
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read temperature and humidity from DHT sensors
|
||||
*
|
||||
* @param sensors array of DHT sensors
|
||||
* @param temp array to store temperature readings
|
||||
* @param hum array to store humidity readings
|
||||
* @param number number of sensors
|
||||
*/
|
||||
void readSensors(DHT sensors[], float temp[], float hum[], int number)
|
||||
{
|
||||
int i;
|
||||
|
@ -187,13 +199,16 @@ void readSensors(DHT sensors[], float temp[], float hum[], int number)
|
|||
|
||||
//-------------------- Sleep de l'ESP --------------------//
|
||||
|
||||
/**
|
||||
* @brief Put ESP into deep sleep mode
|
||||
*/
|
||||
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 utilise pour la date
|
||||
// exemple d'une triple utilisation de valeur pour une fonction utilise pour la date
|
||||
std::tuple<int, int, int> getDate()
|
||||
{
|
||||
struct timeval synctime;
|
||||
|
@ -201,9 +216,8 @@ std::tuple<int, int, int> getDate()
|
|||
time_t rawtime = TimeClient.getEpochTime();
|
||||
synctime.tv_sec = rawtime;
|
||||
synctime.tv_usec = 0;
|
||||
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
||||
//setenv("TZ", "CEST+2", 1);
|
||||
//tzset();
|
||||
if (settimeofday(&synctime, NULL) != 0)
|
||||
std::cout << "error\n";
|
||||
|
||||
// set timezone to France
|
||||
setenv("TZ", "CET-1CEST-2,M3.5.0/02:00:00,M10.5.0/03:00:00", 2); // set at 2 or 3 (Third parameter)
|
||||
|
@ -218,32 +232,44 @@ std::tuple<int, int, int> getDate()
|
|||
}
|
||||
|
||||
// Function that gets current epoch time
|
||||
unsigned long getTime() {
|
||||
time_t now = TimeClient.getEpochTime();;
|
||||
unsigned long getTime()
|
||||
{
|
||||
time_t now = TimeClient.getEpochTime();
|
||||
struct timeval synctime;
|
||||
struct tm timeinfo;
|
||||
|
||||
synctime.tv_sec = now;
|
||||
synctime.tv_usec = 0;
|
||||
if(settimeofday(&synctime,NULL) != 0) std::cout << "error\n" ;
|
||||
TimeClient.forceUpdate();
|
||||
if (settimeofday(&synctime, NULL) != 0)
|
||||
std::cout << "error\n";
|
||||
// TimeClient.forceUpdate(); //cet ligne crée un bug avec l'heure quand on est connecté au réseau du fablab
|
||||
vTaskDelay(1000);
|
||||
if (!getLocalTime(&timeinfo)) {
|
||||
if (!getLocalTime(&timeinfo))
|
||||
{
|
||||
Serial.println("Failed to obtain time");
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
time(&now);
|
||||
Serial.println(now);
|
||||
return now;
|
||||
}
|
||||
|
||||
//-------------------- Création de trames --------------------//
|
||||
|
||||
|
||||
/**
|
||||
* @brief Create message frame with temperature and humidity readings
|
||||
*
|
||||
* @param txt message buffer
|
||||
* @param temp array of temperature readings
|
||||
* @param hum array of humidity readings
|
||||
* @param number number of sensors
|
||||
*/
|
||||
void writeMessage(char *txt, float *temp, float *hum, int number)
|
||||
{
|
||||
int chargelvl = Battery.getBatteryChargeLevel();
|
||||
//time_t rawtime = TimeClient.getEpochTime();
|
||||
// time_t rawtime = TimeClient.getEpochTime();
|
||||
|
||||
delay(2000);
|
||||
|
||||
switch (number)
|
||||
{
|
||||
|
@ -271,8 +297,10 @@ void writeMessage(char *txt, float *temp, float *hum, int number)
|
|||
}
|
||||
}
|
||||
|
||||
//-------------------- read, refresh and return the day boot state --------------------//
|
||||
|
||||
/**
|
||||
* @brief read, refresh and return the day boot state
|
||||
* @brief Read, refresh and return the day boot state
|
||||
*
|
||||
* @return bool at true if the esp was restarted or false if error or not restarted
|
||||
* @inspired of https://github.com/espressif/esp-idf/blob/master/examples/storage/nvs_rw_blob/main/nvs_blob_example_main.c
|
||||
|
@ -282,21 +310,24 @@ bool TestBoot(bool resOrRfsh)
|
|||
nvs_handle_t my_handle;
|
||||
esp_err_t err;
|
||||
bool booted = false;
|
||||
//Serial.println("Test");
|
||||
// Serial.println("Test");
|
||||
|
||||
// Open
|
||||
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
|
||||
if (err != ESP_OK){
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
Serial.println("Erreur open nvs persistant storage");
|
||||
return false;
|
||||
}
|
||||
|
||||
/******** Refresh part ********/
|
||||
|
||||
if(resOrRfsh == REFRESHMEMBOOT){
|
||||
if (resOrRfsh == REFRESHMEMBOOT)
|
||||
{
|
||||
int32_t restart = REFRESHMEMBOOT;
|
||||
err = nvs_set_i32(my_handle, "restart", restart);
|
||||
if (err != ESP_OK) {
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
Serial.println("Erreur write nvs persistant storage");
|
||||
return false;
|
||||
}
|
||||
|
@ -305,28 +336,30 @@ bool TestBoot(bool resOrRfsh)
|
|||
// to flash storage. Implementations may write to storage at other times,
|
||||
// but this is not guaranteed.
|
||||
err = nvs_commit(my_handle);
|
||||
if (err != ESP_OK) return err;
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
// Close
|
||||
nvs_close(my_handle);
|
||||
Serial.println("Refresh memory restart state ");
|
||||
return false;
|
||||
}
|
||||
|
||||
/******** ********/
|
||||
|
||||
// Read
|
||||
int32_t restart = 0; // value will default to 0, if not set yet in NVS
|
||||
err = nvs_get_i32(my_handle, "restart", &restart);
|
||||
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND){
|
||||
if (err != ESP_OK && err != ESP_ERR_NVS_NOT_FOUND)
|
||||
{
|
||||
Serial.println("Erreur read nvs persistant storage");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!restart){
|
||||
if (!restart)
|
||||
{
|
||||
// Write
|
||||
restart = 1;
|
||||
err = nvs_set_i32(my_handle, "restart", restart);
|
||||
if (err != ESP_OK) {
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
Serial.println("Erreur write nvs persistant storage");
|
||||
return false;
|
||||
}
|
||||
|
@ -335,13 +368,15 @@ bool TestBoot(bool resOrRfsh)
|
|||
// to flash storage. Implementations may write to storage at other times,
|
||||
// but this is not guaranteed.
|
||||
err = nvs_commit(my_handle);
|
||||
if (err != ESP_OK) return err;
|
||||
if (err != ESP_OK)
|
||||
return err;
|
||||
// Close
|
||||
nvs_close(my_handle);
|
||||
Serial.println("Flag restart = 0 (not restarted)");
|
||||
Serial.println("Flag restart = 0 (not restarted");
|
||||
return true;
|
||||
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
// Close
|
||||
nvs_close(my_handle);
|
||||
Serial.println("Flag restart = 1 (already restart)");
|
||||
|
@ -355,9 +390,11 @@ bool TestBoot(bool resOrRfsh)
|
|||
return false;
|
||||
}
|
||||
|
||||
|
||||
//-------------------- Initialisation --------------------//
|
||||
|
||||
/**
|
||||
* @brief Setup function for the ESP
|
||||
*/
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
@ -367,44 +404,47 @@ void setup()
|
|||
setupMQTT(MQTT_ADDRESS, MQTT_PORT);
|
||||
initSensors(sensors, SENSORS_NUMBER);
|
||||
|
||||
|
||||
TimeClient.begin();
|
||||
TimeClient.update();
|
||||
|
||||
//int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
|
||||
// int rawtime = (TimeClient.getEpochTime()) % 86400; // 86400 = 60sec*60minutes*24heures
|
||||
unsigned long rawtime = getTime();
|
||||
|
||||
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60 , secondes = rawtime % 60;
|
||||
int hours = rawtime / 3600, minutes = (rawtime % 3600) / 60, secondes = rawtime % 60;
|
||||
|
||||
if (nvs_flash_init() != ESP_OK){
|
||||
if (nvs_flash_init() != ESP_OK)
|
||||
{
|
||||
Serial.println("Erreur flash init");
|
||||
}
|
||||
/**
|
||||
* @brief restart the ESP if there is the time restart
|
||||
*
|
||||
*/
|
||||
if(testTime(hours, minutes, RESTART) && TestBoot(RESTART)){
|
||||
if (testTime(hours, minutes, RESTART) && TestBoot(RESTART))
|
||||
{
|
||||
Serial.println("-----------------RESSSSTTTAAAART---------------------");
|
||||
ESP.restart();
|
||||
delay(1000);
|
||||
} else if(testTime(hours, minutes, REFRESHMEMBOOT)){
|
||||
}
|
||||
else if (testTime(hours, minutes, REFRESHMEMBOOT))
|
||||
{
|
||||
TestBoot(REFRESHMEMBOOT);
|
||||
}
|
||||
/* ######### test time part #########
|
||||
Serial.println('\n');
|
||||
Serial.println(hours,DEC);
|
||||
Serial.println(':');
|
||||
Serial.println(minutes,DEC);
|
||||
Serial.println(':');
|
||||
Serial.println(secondes,DEC);
|
||||
Serial.println('\n');
|
||||
*/
|
||||
// // ######### test time part #########
|
||||
// Serial.println('\n');
|
||||
// Serial.println(hours,DEC);
|
||||
// Serial.println(':');
|
||||
// Serial.println(minutes,DEC);
|
||||
// Serial.println(':');
|
||||
// Serial.println(secondes,DEC);
|
||||
// Serial.println('\n');
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-------------------- Boucle principale --------------------//
|
||||
|
||||
/**
|
||||
* @brief Main loop function for the ESP
|
||||
*/
|
||||
void loop()
|
||||
{
|
||||
int year, month, day;
|
||||
|
@ -423,13 +463,13 @@ void loop()
|
|||
|
||||
readSensors(sensors, temp, hum, SENSORS_NUMBER);
|
||||
writeMessage(msg, temp, hum, SENSORS_NUMBER);
|
||||
Serial.printf(msg);
|
||||
|
||||
TimeClient.update();
|
||||
//TimeClient.getFormattedTime().toCharArray(time, 30);
|
||||
// TimeClient.update();
|
||||
// TimeClient.getFormattedTime().toCharArray(time, 30);
|
||||
|
||||
|
||||
//tie(year, month, day) = getDate();
|
||||
/* debug print*/
|
||||
// tie(year, month, day) = getDate();
|
||||
// /* debug print*/
|
||||
// Serial.println('\n');
|
||||
// Serial.println(year,DEC);
|
||||
// Serial.println(':');
|
||||
|
@ -438,10 +478,9 @@ void loop()
|
|||
// Serial.println(day,DEC);
|
||||
// Serial.println('\n');
|
||||
|
||||
|
||||
now = getTime();
|
||||
|
||||
lenght = sprintf(date,"%s", ultoa(now, date, 10) /* "|%d-%d-%d ", year, month, day */);
|
||||
lenght = sprintf(date, "%s", ultoa(now, date, 10) /* "|%d-%d-%d ", year, month, day */);
|
||||
sprintf(date + lenght, time);
|
||||
sprintf(date + strlen(date), msg);
|
||||
MqttClient.publish(TOPIC, date);
|
||||
|
|
Loading…
Reference in a new issue