Bon ben j'ai pas vraiment réussi à avancer.. je n'arrive toujours pas à enregistrer mes valeurs depuis le formulaire HTML...
J'ai remis un peu en ordre (selon moi) à mon code !
/*//////////////
__________ .___ __________ .____ .___ ________ ___________ ___ ___ ___________________ ____ ___ ___________
\______ \| |\______ \| | | |\_____ \ \__ ___/ / | \ \_ _____/\_____ \ | | \\_ _____/
| | _/| | | | _/| | | | / | \ | | / ~ \ | __)_ / / \ \ | | / | __)_
| | \| | | | \| |___ | |/ | \ | | \ Y / | \/ \_/. \| | / | \
|______ /|___| |______ /|_______ \|___|\_______ / |____| \___|_ / /_______ /\_____\ \_/|______/ /_______ /
\/ \/ \/ \/ \/ \/ \__> \/
////////////////*/
//#include <espnow.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
#include "LittleFS.h"
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <Arduino_JSON.h>
// Bibliothèques pour NTP
#include <NTPClient.h>
#include <WiFiUdp.h>
/*////////////////////////
____ ____ _____ __________ .___ _____ __________ .____ ___________ _________
\ \ / / / _ \ \______ \| | / _ \ \______ \| | \_ _____/ / _____/
\ Y / / /_\ \ | _/| | / /_\ \ | | _/| | | __)_ \_____ \
\ / / | \ | | \| |/ | \ | | \| |___ | \ / \
\___/ \____|__ / |____|_ /|___|\____|__ / |______ /|_______ \/_______ //_______ /
\/ \/ \/ \/ \/ \/ \/
/////////////////////*/
// Définir le client NTP pour obtenir l'heure
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
// Jour de la semaine
String weekDays[7]={"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
// Nome des mois
String months[12]={"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"};
// Paramètres personnalisé BME280
BME280I2C::Settings settings(
BME280::OSR_X1,
BME280::OSR_X1,
BME280::OSR_X1,
BME280::Mode_Forced,
BME280::StandbyTime_1000ms,
BME280::Filter_16,
BME280::SpiEnable_False,
BME280I2C::I2CAddr_0x76
);
BME280I2C bme(settings);
// ID de carte (ESP Sender #1 = BOARD_ID 1, ESP Sender #2 = BOARD_ID 2, etc.)
int BOARD_ID = 2;
// Adresse MAC du destinataire (MASTER)
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
// Créer un objet AsyncWebServer sur le port 80
AsyncWebServer server(80);
// Créer une source d'événement sur /events
AsyncEventSource events("/events");
// Variable de l'objet Json pour conserver les lectures du capteur
JSONVar readings;
// Variables de temporisation
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;
// Récupère les mesures des capteurs et renvoie à l'objet JSON
String getSensorReadings(){
readings["temperature"] = String(bme.temp());
readings["humidity"] = String(bme.hum());
readings["Presure"] = String(bme.pres());
String jsonString = JSON.stringify(readings);
return jsonString;
}
// Recherche des paramètres lié au capteur dans la requête HTTP POST
const char* PARAM_IDCHIP = "inputIdChip"; // 1. ID de l'ESP8266
const char* PARAM_BOARD_ID = "inputBoardID"; // 1.1 Board ID (ESP NOW)
const char* PARAM_MAC = "inputMacAdress"; // 2. Adresse MAC de l'ESP8266
const char* PARAM_BROADCAST_ADR = "inputBroadCast"; // 2.1 Adresse MAC du maître (ESP NOW)
const char* PARAM_STRING_NAME = "inputStringName"; // 3. Nom personalisé
const char* PARAM_STRING_BOX = "inputStringBox"; // 4. Emplacement appareil (Nom local)
const char* PARAM_STRING_BOXNB = "inputStringBoxNb"; // 5. Emplacement appareil (No local)
const char* PARAM_INT_ECHANTILLONAGE = "inputIntEchentillonage"; // 6. Temps entre 2 échentillonage
const char* PARAM_FLOAT_MIN_TEMP = "inputFloatMinTemp"; // 7. Seuil minimum température
const char* PARAM_FLOAT_MAX_TEMP = "inputFloatMaxTemp"; // 8. Seuil maximum température
const char* PARAM_FLOAT_CORR_TEMP = "inputFloatCorrTemp"; // 9. Correction température
const char* PARAM_FLOAT_TEMP_VALUE = "inputFloatValTemp"; // 10. Valeur température
const char* PARAM_FLOAT_MIN_HUMI = "inputFloatMinHumi"; // 11. Seuil minimum humidité
const char* PARAM_FLOAT_MAX_HUMI = "inputFloatMaxHumi"; // 12. Seuil maximum humidité
const char* PARAM_FLOAT_CORR_HUMI = "inputFloatCorrHumi"; // 13. Correction humidité
const char* PARAM_FLOAT_HUMI_VALUE = "inputFloatValHumi"; // 14. Valeur humidité
const char* PARAM_FLOAT_MIN_PRES = "inputFloatMinPres"; // 15. Seuil minimum pression
const char* PARAM_FLOAT_MAX_PRES = "inputFloatMaxPres"; // 16. Seuil maximum pression
const char* PARAM_FLOAT_CORR_PRES = "inputFloatCorrPres"; // 17. Correction pression
const char* PARAM_FLOAT_PRES_VALUE = "inputFloatValPres"; // 18. Valeur pression
// Variables pour enregistrer les valeurs du formulaire info.html
String inputBoardID; // 1.1 N° ID perso défini pour ESP NOW
String inputBroadCast; // 2.2 Adresse MAC de l'ESP Maître
String inputStringName; // 3. Nom personalisé
String inputStringBox; // 4. Emplacement appareil (Nom local)
String inputStringBoxNb; // 5. Emplacement appareil (No local)
String inputIntEchentillonage; // 6. Temps entre 2 échentillonage
// Variables pour enregistrer les valeurs du formulaire sensor_config_temp.html
String inputFloatMinTemp; // 7. Seuil minimum température
String inputFloatMaxTemp; // 8. Seuil maximum température
String inputFloatCorrTemp; // 9. Correction température
// Variables pour enregistrer les valeurs du formulaire sensor_config_hum.html
String inputFloatMinHumi; // 11. Seuil minimum humidité
String inputFloatMaxHumi; // 12. Seuil maximum humidité
String inputFloatCorrHumi; // 13. Correction humidité
// Variables pour enregistrer les valeurs du formulaire sensor_config_pres.html
String inputFloatMinPres; // 15. Seuil minimum pression
String inputFloatMaxPres; // 16. Seuil maximum pression
String inputFloatCorrPres; // 17. Correction pression
// Chemins des fichiers pour enregistrer les valeurs saisie de façon permanente pour info.html
const char* inputBoardIDPath = "/inputBoardIDPath.txt";
const char* inputBroadCastPath = "/inputBroadCastPath.txt";
const char* inputStringNamePath = "/inputStringNamePath.txt";
const char* inputStringBoxPath = "/inputStringBoxPath.txt";
const char* inputStringBoxNbPath = "/inputStringBoxNbPath.txt";
const char* inputIntEchentillonagePath = "/inputIntEchentillonagePath.txt";
const char* inputFloatMinTempPath = "/inputFloatMinTempPath.txt";
const char* inputFloatMaxTempPath = "/inputFloatMaxTempPath.txt";
const char* inputFloatCorrTempPath = "/inputFloatCorrTempPath.txt";
const char* inputFloatMinHumiPath = "/inputFloatMinHumiPath.txt";
const char* inputFloatMaxHumiPath = "/inputFloatMaxHumiPath.txt";
const char* inputFloatCorrHumiPath = "/inputFloatCorrHumiPath.txt";
const char* inputFloatMinPresPath = "/inputFloatMinPresPath.txt";
const char* inputFloatMaxPresPath = "/inputFloatMaxPresPath.txt";
const char* inputFloatCorrPresPath = "/inputFloatMaxPresPath.txt";
// Recherche des paramètres lié au WIFI dans la requête HTTP POST
const char* PARAM_INPUT_1 = "ssid";
const char* PARAM_INPUT_2 = "pass";
const char* PARAM_INPUT_3 = "ip";
const char* PARAM_INPUT_4 = "gateway";
// Variables pour enregistrer les valeurs du formulaire wifimanager.html
String ssid;
String pass;
String ip;
String gateway;
// Chemins des fichiers pour enregistrer les valeurs saisie de façon permanente pour wifimanager.html
const char* ssidPath = "/ssid.txt";
const char* passPath = "/pass.txt";
const char* ipPath = "/ip.txt";
const char* gatewayPath = "/gateway.txt";
IPAddress localIP;
IPAddress localGateway;
IPAddress subnet(255, 255, 0, 0);
// Timer variables
unsigned long previousMillis = 0;
const long interval = 10000; // interval to wait for Wi-Fi connection (milliseconds)
// Variables pour le redemarrage de l'ESP après avoir configurer son WIFI
boolean restart = false;
/*////////////////
_________ ____ ___ ____________________________ _____ ____ ____________ .___ ________
\_ ___ \ | | \ / _____/\__ ___/\_____ \ / \ \ \ / /\_____ \ | |\______ \
/ \ \/ | | / \_____ \ | | / | \ / \ / \ \ Y / / | \ | | | | \
\ \____| | / / \ | | / | \/ Y \ \ / / | \| | | ` \
\______ /|______/ /_______ / |____| \_______ /\____|__ / \___/ \_______ /|___|/_______ /
\/ \/ \/ \/ \/ \/
//////////////*/
///////////////////////____________ INITIALISATION DU CLIENT NTP POUR OBTENIR LA DATE/HEURE/MINUTES ETC...
void INIT_CLIENTNTP(){
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(7200);
}
void INIT_NTP_LOOP(){
///////////////////////____________
timeClient.update();
time_t epochTime = timeClient.getEpochTime();
Serial.print("Epoch Time: ");
Serial.println(epochTime);
String formattedTime = timeClient.getFormattedTime();
Serial.print("Heure complète : ");
Serial.println(formattedTime);
int currentHour = timeClient.getHours();
Serial.print("Heure: ");
Serial.println(currentHour);
int currentMinute = timeClient.getMinutes();
Serial.print("Minutes: ");
Serial.println(currentMinute);
int currentSecond = timeClient.getSeconds();
Serial.print("Secondes: ");
Serial.println(currentSecond);
String weekDay = weekDays[timeClient.getDay()];
Serial.print("Jour: ");
Serial.println(weekDay);
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
Serial.print("N° jour : ");
Serial.println(monthDay);
int currentMonth = ptm->tm_mon+1;
Serial.print("N° mois : ");
Serial.println(currentMonth);
String currentMonthName = months[currentMonth-1];
Serial.print("Mois : ");
Serial.println(currentMonthName);
int currentYear = ptm->tm_year+1900;
Serial.print("Année : ");
Serial.println(currentYear);
//Print complete date:
String currentDate = String(currentYear) + "." + String(currentMonth) + "." + String(monthDay);
Serial.print("Date du jour: ");
Serial.println(currentDate);
Serial.println("");
delay(2000);
}
///////////////////////____________ N° ID et Adresse MAC de l'ESP8266
void SERIAL_CHIP_ID(){
Serial.printf(" ESP8266 Chip id = %08X\n", ESP.getChipId());
Serial.print("Mac adresse : ");
Serial.println(WiFi.macAddress());
Serial.println();
}
void INIT_BME(){
while(!Serial) {}
Wire.begin();
while(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
uint32_t uniqueID = bme.UniqueId();
char buffer[9];
sprintf(buffer, "%08X", uniqueID);
Serial.print("Unique ID: 0x");
Serial.println(buffer);
}
///////////////////////____________ LECTURE DE LA SONDE BME280
void BME280Values() {
delay(2000);
Serial.print("Temperature = ");
Serial.print(bme.temp());
Serial.println(" *C");
// Convertion de la temperature en Fahrenheit
/*Serial.print("Temperature = ");
Serial.print(1.8 * bme.readTemperature() + 32);
Serial.println(" *F");*/
Serial.print("Pressure = ");
Serial.print(bme.pres() / 100.0F);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.hum());
Serial.println(" %");
Serial.println();
}
// Initialisation du LittleFS
void initFS() {
if (!LittleFS.begin()) {
Serial.println("An error has occurred while mounting LittleFS");
}
else{
Serial.println("LittleFS mounted successfully");
}
}
// Lecture des fichier depuis LittleFS
String readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path, "r");
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return String();
}
String fileContent;
while(file.available()){
fileContent = file.readStringUntil('\n');
break;
}
file.close();
return fileContent;
}
// Ecriture des fichiers dans LittleFS
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, "w");
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- frite failed");
}
file.close();
}
// Initialisation du WiFi
bool initWiFi() {
if(ssid=="" || ip==""){
Serial.println("Undefined SSID or IP address.");
return false;
}
WiFi.mode(WIFI_STA);
localIP.fromString(ip.c_str());
localGateway.fromString(gateway.c_str());
if (!WiFi.config(localIP, localGateway, subnet)){
Serial.println("STA Failed to configure");
return false;
}
WiFi.begin(ssid.c_str(), pass.c_str());
Serial.println("Connecting to WiFi...");
delay(20000);
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect.");
return false;
}
Serial.println(WiFi.localIP());
return true;
}
// Replaces placeholder with LED state value
String processor(const String& var) {
uint32_t chipID;
chipID = ESP.getChipId();
String ID_ESP8266 = String(chipID);
float MIN_TEMP;// MIN TEMP
float MAX_TEMP;// MAX TEMP
float MAX_DOWN_CORR_TEMP = -2.00;
float MAX_UP_CORR_TEMP = 2.00;
float CORR_TEMP;
float TEMP_VALUE = bme.temp() + CORR_TEMP;
String TEMP_VAL = String(TEMP_VALUE);
float MIN_HUMI;// MIN HUMI
float MAX_HUMI;// MAX HUMI
float MAX_DOWN_CORR_HUMI = -5.00;
float MAX_UP_CORR_HUMI = 5.00;
String CORR_HUMI;
String HUMI_VALUE = bme.hum() + CORR_HUMI;
float MIN_PRES;// MIN PRES
float MAX_PRES;// MAX PRES
String CORR_PRES;
float PRES_VALUE = bme.pres() / 100.0F;
int ECHANTILLONAGE_MS; // ECHANTILLONAGE_MS (interval en millisecondes)
int ECHANTILLONAGE_SECONDES = 60; // ECHANTILLONAGE_SECONDES (interval en secondes)
// 2. Adresse MAC de l'ESP8266
if(var == "inputMacAdress"){
return WiFi.macAddress();
}
// 1. ID de l'ESP8266
else if(var == "inputIdChip"){
return ID_ESP8266;
}
// 3. Nom personalisé
else if(var == "inputStringName"){
return readFile(LittleFS, "/inputStringName.txt");
}
// 4. Emplacement appareil (Nom local)
else if(var == "inputStringBox"){
return readFile(LittleFS, "/inputStringBox.txt");
}
// 5. Emplacement appareil (No local)
else if(var == "inputStringBoxNb"){
return readFile(LittleFS, "/inputStringBoxNb.txt");
}
// 6. Temps entre 2 échentillonage
else if(var == "inputIntEchentillonage"){
return readFile(LittleFS, "/inputIntEchentillonage.txt");
}
// 7. Seuil minimum température
else if(var == "inputFloatMinTemp"){
return readFile(LittleFS, "/inputFloatMinTemp.txt");
}
// 8. Seuil maximum température
else if(var == "inputFloatMaxTemp"){
return readFile(LittleFS, "/inputFloatMaxTemp.txt");
}
// 9. Correction température
else if(var == "inputFloatCorrTemp"){
return readFile(LittleFS, "/inputFloatCorrTemp.txt");
}
// 10. Valeur température
else if(var == "inputFloatCorrTemp"){
return TEMP_VAL; //readFile(SPIFFS, "PARAMETRES/inputFloatCorrTemp.txt");
}
// 11. Seuil minimum humidité
else if(var == "inputFloatMinHumi"){
return readFile(LittleFS, "/inputFloatMinHumi.txt");
}
// 12. Seuil maximum humidité
else if(var == "inputFloatMaxHumi"){
return readFile(LittleFS, "/inputFloatMaxHumi.txt");
}
// 13. Correction humidité
else if(var == "inputFloatCorrHumi"){
return readFile(LittleFS, "/inputFloatCorrHumi.txt");
}
// 14. Valeur humidité
else if(var == "inputFloatCorrHumi"){
return HUMI_VALUE; //readFile(SPIFFS, "PARAMETRES/inputFloatCorrHumi.txt");
}
// 15. Seuil minimum pression
else if(var == "inputFloatMinPres"){
return readFile(LittleFS, "/inputFloatMinPres.txt");
}
// 16. Seuil maximum pression
else if(var == "inputFloatMaxPres"){
return readFile(LittleFS, "/inputFloatMaxPres.txt");
}
// 17. Correction pression
else if(var == "inputFloatCorrPres"){
return readFile(LittleFS, "/inputFloatCorrPres.txt");
}
/*
// 18. Valeur pression
else if(var == "inputFloatValPres"){
return PRES_VALUE; //readFile(SPIFFS, "PARAMETRES/inputFloatCorrHumi.txt");
}
*/
return String();
}
/*//////////////////
_________ __
/ _____/ ____ _/ |_ __ __ ______
\_____ \ _/ __ \ \ __\| | \\____ \
/ \\ ___/ | | | | /| |_> >
/_______ / \___ > |__| |____/ | __/
\/ \/ |__|
//////////////////*/
void setup() {
Serial.begin(115200);
INIT_CLIENTNTP();
SERIAL_CHIP_ID();
INIT_BME();
initFS();
// Chargement des valeurs enregistrée dans LittleFS
ssid = readFile(LittleFS, ssidPath);
pass = readFile(LittleFS, passPath);
ip = readFile(LittleFS, ipPath);
gateway = readFile (LittleFS, gatewayPath);
inputBoardID = readFile(LittleFS, inputBoardIDPath); // 1.1 N° ID perso défini pour ESP NOW
inputBroadCast= readFile(LittleFS, inputBroadCastPath); // 2.2 Adresse MAC de l'ESP Maître
inputStringName = readFile(LittleFS, inputStringNamePath); // 3. Nom personalisé
inputStringBox = readFile(LittleFS, inputStringBoxPath); // 4. Emplacement appareil (Nom local)
inputStringBoxNb = readFile(LittleFS, inputStringBoxNbPath); // 5. Emplacement appareil (No local)
inputIntEchentillonage = readFile(LittleFS, inputIntEchentillonagePath); // 6. Temps entre 2 échentillonage
inputFloatMinTemp = readFile(LittleFS, inputFloatMinTempPath); // 7. Seuil minimum température
inputFloatMaxTemp = readFile(LittleFS, inputFloatMaxTempPath); // 8. Seuil maximum température
inputFloatCorrTemp = readFile(LittleFS, inputFloatCorrTempPath); // 9. Correction température
inputFloatMinHumi = readFile(LittleFS, inputFloatMinHumiPath); // 11. Seuil minimum humidité
inputFloatMaxHumi = readFile(LittleFS, inputFloatMaxHumiPath); // 12. Seuil maximum humidité
inputFloatCorrHumi = readFile(LittleFS, inputFloatCorrHumiPath); // 13. Correction humidité
inputFloatMinPres = readFile(LittleFS, inputFloatMinPresPath); // 15. Seuil minimum pression
inputFloatMaxPres = readFile(LittleFS, inputFloatMaxPresPath); // 16. Seuil maximum pression
inputFloatCorrPres = readFile(LittleFS, inputFloatCorrPresPath); // 17. Correction pression
Serial.println(ssid);
Serial.println(pass);
Serial.println(ip);
Serial.println(gateway);
if(initWiFi()) {
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
////////////////////// Page index.html
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Route pour la racine de la page Web "index.html"
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(LittleFS, "/index.html", "text/html", false, processor);
});
/*
// Envoyez des requêtes GET à <ESP_IP>/get?inputStringName=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
});
*/
server.serveStatic("/", LittleFS, "/");
// Demande des dernières lectures du capteur
server.on("/readings", HTTP_GET, [](AsyncWebServerRequest *request){
lastTime = lastTime + timerDelay;
request->send(200, "text/plain", "OK!");
});
// Demande des dernières lectures du capteur
server.on("/readings", HTTP_GET, [](AsyncWebServerRequest *request){
String json = getSensorReadings();
request->send(200, "application/json", json);
json = String();
});
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!", NULL, millis(), 10000);
});
server.addHandler(&events);
// Start server
server.begin();
}
else {
// Connectez-vous au réseau Wi-Fi avec SSID et mot de passe
Serial.println("Setting AP (Access Point)");
// NULL définit un point d'accès ouvert
WiFi.softAP("ESP-DATALOGGERS", NULL);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
////////////////////// Page wifimanager.html
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(LittleFS, "/wifimanager.html", "text/html");
});
//server.serveStatic("/", LittleFS, "/");
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isPost()){
// HTTP POST ssid value
if (p->name() == PARAM_INPUT_1) {
ssid = p->value().c_str();
Serial.print("SSID set to: ");
Serial.println(ssid);
// Write file to save value
writeFile(LittleFS, ssidPath, ssid.c_str());
}
// HTTP POST pass value
if (p->name() == PARAM_INPUT_2) {
pass = p->value().c_str();
Serial.print("Password set to: ");
Serial.println(pass);
// Write file to save value
writeFile(LittleFS, passPath, pass.c_str());
}
// HTTP POST ip value
if (p->name() == PARAM_INPUT_3) {
ip = p->value().c_str();
Serial.print("IP Address set to: ");
Serial.println(ip);
// Write file to save value
writeFile(LittleFS, ipPath, ip.c_str());
}
// HTTP POST gateway value
if (p->name() == PARAM_INPUT_4) {
gateway = p->value().c_str();
Serial.print("Gateway set to: ");
Serial.println(gateway);
// Write file to save value
writeFile(LittleFS, gatewayPath, gateway.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
restart = true;
request->send(200, "text/plain", "Done. ESP will restart, connect to your router and go to IP address: " + ip);
});
server.begin();
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
////////////////////// Page info.html
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(LittleFS, "/info.html", "text/html", false, processor);
});
//server.serveStatic("/", LittleFS, "/");
// Envoyez des requêtes GET à <ESP_IP>/get?inputStringName=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
// 3. Nom personalisé
// GET inputStringName value on <ESP_IP>/get?inputStringName=<inputMessage>
if (request->hasParam(PARAM_STRING_NAME)) {
inputMessage = request->getParam(PARAM_STRING_NAME)->value();
writeFile(LittleFS, inputStringNamePath, inputMessage.c_str());
}
// 4. Emplacement appareil (Nom local)
// GET inputStringBox value on <ESP_IP>/get?inputStringBox=<inputMessage>
if (request->hasParam(PARAM_STRING_BOX)) {
inputMessage = request->getParam(PARAM_STRING_BOX)->value();
writeFile(LittleFS, inputStringBoxPath, inputMessage.c_str());
}
// 5. Emplacement appareil (No local)
// GET inputStringBox value on <ESP_IP>/get?inputStringBox=<inputMessage>
if (request->hasParam(PARAM_STRING_BOXNB)) {
inputMessage = request->getParam(PARAM_STRING_BOXNB)->value();
writeFile(LittleFS, inputStringBoxNbPath, inputMessage.c_str());
}
});
// server.serveStatic("/", LittleFS, "/");
// server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
/*
int params = request->params();
for(int i1=0;i1<params;i1++){
AsyncWebParameter* p1 = request->getParam(i1);
if(p1->isPost()){
// HTTP POST ssid value
if (p1->name() == PARAM_STRING_NAME) {
inputStringName = p1->value().c_str();
Serial.print("Nom défini à : ");
Serial.println(inputStringName);
// Write file to save value
writeFile(LittleFS, inputStringNamePath, inputStringName.c_str());
}
// HTTP POST pass value
if (p1->name() == PARAM_STRING_BOX) {
inputStringBox = p1->value().c_str();
Serial.print("Emplacement défini à : ");
Serial.println(inputStringBox);
// Write file to save value
writeFile(LittleFS, inputStringBoxPath, inputStringBox.c_str());
}
// HTTP POST ip value
if (p1->name() == PARAM_STRING_BOXNB) {
inputStringBoxNb = p1->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputStringBoxNb);
// Write file to save value
writeFile(LittleFS, inputStringBoxNbPath, inputStringBoxNb.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
request->send(200, "text/plain", "Done. connect ");
});
*/
server.begin();
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
////////////////////// Page sensor_config_temp.html
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(LittleFS, "/sensor_config_temp.html", "text/html");
});
/*
// Envoyez des requêtes GET à <ESP_IP>/get?inputStringName=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
});
*/
// server.serveStatic("/", LittleFS, "/");
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isPost()){
// HTTP POST ssid value
if (p->name() == PARAM_INT_ECHANTILLONAGE) {
inputIntEchentillonage = p->value().c_str();
Serial.print("Nom défini à : ");
Serial.println(inputIntEchentillonage);
// Write file to save value
writeFile(LittleFS, inputIntEchentillonagePath, inputIntEchentillonage.c_str());
}
// HTTP POST pass value
if (p->name() == PARAM_FLOAT_MIN_TEMP) {
inputFloatMinTemp = p->value().c_str();
Serial.print("Emplacement défini à : ");
Serial.println(inputFloatMinTemp);
// Write file to save value
writeFile(LittleFS, inputFloatMinTempPath, inputFloatMinTemp.c_str());
}
// HTTP POST ip value
if (p->name() == PARAM_FLOAT_MAX_TEMP) {
inputFloatMaxTemp = p->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputFloatMaxTemp);
// Write file to save value
writeFile(LittleFS, inputFloatMaxTempPath , inputFloatMaxTemp.c_str());
}
// HTTP POST ip value
if (p->name() == PARAM_FLOAT_CORR_TEMP) {
inputFloatCorrTemp = p->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputFloatCorrTemp);
// Write file to save value
writeFile(LittleFS, inputFloatCorrTempPath , inputFloatCorrTemp.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
});
server.begin();
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
////////////////////// Page sensor_config_hum.html
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(LittleFS, "/sensor_config_hum.html", "text/html");
});
/*
// Envoyez des requêtes GET à <ESP_IP>/get?inputStringName=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
});
*/
server.serveStatic("/", LittleFS, "/");
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i2=0;i2<params;i2++){
AsyncWebParameter* p2 = request->getParam(i2);
if(p2->isPost()){
// HTTP POST pass value
if (p2->name() == PARAM_FLOAT_MIN_HUMI) {
inputFloatMinHumi = p2->value().c_str();
Serial.print("Emplacement défini à : ");
Serial.println(inputFloatMinHumi);
// Write file to save value
writeFile(LittleFS, inputFloatMinHumiPath , inputFloatMinHumi.c_str());
}
// HTTP POST ip value
if (p2->name() == PARAM_FLOAT_MAX_HUMI) {
inputFloatMaxHumi = p2->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputFloatMaxHumi);
// Write file to save value
writeFile(LittleFS, inputFloatMaxHumiPath, inputFloatMaxHumi.c_str());
}
// HTTP POST ip value
if (p2->name() == PARAM_FLOAT_CORR_HUMI) {
inputFloatCorrHumi = p2->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputFloatCorrHumi);
// Write file to save value
writeFile(LittleFS, inputFloatCorrHumiPath, inputFloatCorrHumi.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
});
server.begin();
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
////////////////////// Page sensor_config_pres.html
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(LittleFS, "/sensor_config_pres.html", "text/html");
});
/*
// Envoyez des requêtes GET à <ESP_IP>/get?inputStringName=<inputMessage>
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
});
*/
// server.serveStatic("/", LittleFS, "/");
server.on("/", HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i2=0;i2<params;i2++){
AsyncWebParameter* p2 = request->getParam(i2);
if(p2->isPost()){
// HTTP POST pass value
if (p2->name() == PARAM_FLOAT_MIN_PRES) {
inputFloatMinPres = p2->value().c_str();
Serial.print("Emplacement défini à : ");
Serial.println(inputFloatMinPres);
// Write file to save value
writeFile(LittleFS, inputFloatMinPresPath, inputFloatMinPres.c_str());
}
// HTTP POST ip value
if (p2->name() == PARAM_FLOAT_MAX_PRES) {
inputFloatMaxPres = p2->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputFloatMaxPres);
// Write file to save value
writeFile(LittleFS, inputFloatMaxPresPath, inputFloatMaxPres.c_str());
}
// HTTP POST ip value
if (p2->name() == PARAM_FLOAT_CORR_PRES) {
inputFloatCorrPres = p2->value().c_str();
Serial.print("N° de local définià : ");
Serial.println(inputFloatCorrPres);
// Write file to save value
writeFile(LittleFS, inputFloatCorrPresPath, inputFloatCorrPres.c_str());
}
//Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
});
server.begin();
/*
// Page HTML CHART
// Web Server Root URL
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(LittleFS, "/charts.html", "text/html");
});
server.serveStatic("/", LittleFS, "/");
// Request for the latest sensor readings
server.on("/readings", HTTP_GET, [](AsyncWebServerRequest *request){
lastTime = lastTime + timerDelay;
request->send(200, "text/plain", "OK!");
});
events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!", NULL, millis(), 10000);
});
server.addHandler(&events);
// Start server
server.begin();
}
}
*/
/*////////////////////////
.____
| | ____ ____ ______
| | / _ \ / _ \ \____ \
| |___ ( <_> )( <_> )| |_> >
|_______ \ \____/ \____/ | __/
\/ |__|
////////////////////////*/
void loop() {
// Redémarre l'ESP après que le paramètrage du wifi
if (restart){
delay(5000);
ESP.restart();
}
//INIT_NTP_LOOP();
BME280Values();
// Envoyer des événements au client avec les lectures du capteur toutes les 30 secondes
if ((millis() - lastTime) > timerDelay) {
events.send("ping",NULL,millis());
events.send(getSensorReadings().c_str(),"new_readings" ,millis());
lastTime = millis();
}
}