1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| /*********************************************************************************
***********Maquette d'essai ESP32 en serveur Web - librairie ESPAsyncWebServer****
***********
**************************************version 1.00 Michel MORICE novembre 2024****/
#include <Arduino.h>
#include <FS.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
#include "maBox.h"
//#include <ElegantOTA.h>
#define FORMAT_SPIFFS_IF_FAILED false
#define HTTP_PORT 80
const char* SSID = SECRET_SSID;
const char* PASS = SECRET_PASS;
const char* DIR_HTML = "/index.html"; //page client
const char* DIR_CSS = "/style.css"; //mise en forme page client
const char* DIR_JS = "/code.js"; //code Javascript page client
bool bodyPret=false;
const size_t tailleMaxBody = 250;
char bufBody[tailleMaxBody];
//***************variables globales***********************
String pageHTML = "", pageCSS="", pageJS="";
//****************instantiations***********************
AsyncWebServer serveur(HTTP_PORT);
//*************** Chargement des pages annexes du serveur ************************
void lectureData(const char* path){
File fic = SPIFFS.open(path);
if(!fic || fic.isDirectory()){
Serial.println("Erreur ouverture fichier");
return;
}
if(strcmp(path,DIR_HTML)==0){ //****client
while(fic.available()) {pageHTML += char(fic.read());}
}
else if(strcmp(path,DIR_CSS)==0){
while(fic.available()) {pageCSS += char(fic.read());}
}
else if(strcmp(path,DIR_JS)==0){
while(fic.available()) {pageJS += char(fic.read());}
}
fic.close();
}
//****Gestion de la réception des POST******************
void gestionReception(AsyncWebServerRequest *req, uint8_t *data, size_t len, size_t index, size_t total) {
Serial.println("Debut analyse");
if(index==0){
bodyPret = false;
}
if(total >=tailleMaxBody)
Serial.println("body too big");
else{
memcpy(bufBody + index,data,len);
if(index + len >= total){
bufBody[total] = '\0';
bodyPret=true;
}
}
}
//****Analyse de la réception du POST de la programmation des capteurs**************
void analyseReceptFormData(AsyncWebServerRequest *req){
if(bodyPret){
req->send(200,"Content-type: text/plain", "Ok");
int nbParams = req->params();
for(uint8_t i=0;i<nbParams;i++){
const AsyncWebParameter* p = req->getParam(i);
printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
}
}
else req->send(413,"Content-type: text/plain", "NOk");
}
//********************initialisation du SPIFFS***************************************
void initSPIFFS(){
if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){
Serial.println("SPIFFS Mount Failed");
}
else{
lectureData(DIR_HTML);
lectureData(DIR_CSS);
lectureData(DIR_JS);
}
}
//********************initialisation du serveur**************************************
void initServeur(){
serveur.on("/essaiFormData", HTTP_GET, [](AsyncWebServerRequest *req){
req->send(200,"text/html", pageHTML);
});
serveur.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *req){
req->send(200, "text/css", pageCSS);
});
serveur.on("/code.js", HTTP_GET, [](AsyncWebServerRequest *req){
req->send(200, "application/javascript", pageJS);
});
serveur.on("/envoiFormData", HTTP_POST, analyseReceptFormData, nullptr, gestionReception);
//ElegantOTA.begin(&serveur);
serveur.begin();
}
//***************Evenements WIFI*********************************
void connexion_BOX(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("connexion WiFi");
}
void obtention_IP(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.print("Adresse IP: "); Serial.println(WiFi.localIP());
}
void deconnexion_BOX(WiFiEvent_t event, WiFiEventInfo_t info){
WiFi.begin(SSID,PASS);
}
//********************initialisation WiFi*********************************
void initWiFi(){
WiFi.mode(WIFI_STA);
WiFi.onEvent(connexion_BOX,ARDUINO_EVENT_WIFI_STA_CONNECTED); //attend une connexion wifi
WiFi.onEvent(obtention_IP, ARDUINO_EVENT_WIFI_STA_GOT_IP); //attend l'obtention d'adresse IP
WiFi.onEvent(deconnexion_BOX, ARDUINO_EVENT_WIFI_STA_DISCONNECTED); //si une déconnexion survient
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
//******************** paramétrage initial *********************************************************
void setup() {
Serial.begin(9600);
initWiFi();
delay(200);
initSPIFFS();
delay(200);
initServeur();
}
//***************** boucle principale **************************************************************
void loop() {
//ElegantOTA.loop();
} |
Partager