Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * MAJ tarifs pompe
- * par Laurent V.
- * ESP32
- * Afficheurs 4 digits
- *
- * Biliotheques necessaires:
- * ESPAsyncWebSrc (avec ESPAsyncTCP et AsybncTCP)
- */
- #define VERSION "1.0"
- const char * MODULE="Tarifs pompes";
- #include <Arduino.h>
- #include <WiFi.h>
- #include "esp_wifi.h"
- IPAddress IP_controler;
- #include <SPIFFS.h>
- #include <AsyncTCP.h>
- #include <ESPAsyncWebSrv.h>
- AsyncWebServer *HTTPserver;
- //---- structure de configuration en EEPROM
- // ca garde les codes du point d'accès et les derniers tarifs saisis
- #include <EEPROM.h>
- typedef struct sConf {
- char wssid[32];
- char wpwd[32];
- double tarif1;
- double tarif2;
- double tarif3;
- char magic[8];
- } sConf;
- sConf APConfig;
- #define EEPROM_SIZE sizeof(sConf)
- //=============== SETUP ===============
- void setup() {
- Serial.begin(115200);
- Serial.println();
- Serial.println(MODULE);
- Serial.println(VERSION);
- getEEPROMdata();
- if (SPIFFS.begin(true)) {
- Serial.println("SPIFFS OK");
- initHTTP();
- }
- else {
- Serial.println("SPIFFS KO ! impossible de lancer le serveur WEB.");
- }
- }
- //================ LOOP =================
- void loop() {
- }
- //-----------------------------------------------------------
- // La partie qui met a jour les afficheurs
- //-----------------------------------------------------------
- void miseAJourTarifAfficheurs(double t1,double t2,double t3) {
- Serial.println("Tarifs:");
- // mettre a jour l'afficheur 1 avec le tarif t1
- Serial.println(t1);
- // mettre a jour l'afficheur 2 avec le tarif t2
- Serial.println(t2);
- // mettre a jour l'afficheur 3 avec le tarif t3
- Serial.println(t3);
- }
- //-----------------------------------------------------------
- // init du serveur WEB
- //-----------------------------------------------------------
- void initHTTP() {
- // init point d'acces
- WiFi.disconnect();
- WiFi.mode(WIFI_AP_STA);
- if( ! WiFi.softAPConfig(IPAddress(192, 168, 10, 10), IPAddress(192, 168, 10, 10), IPAddress(255, 255, 255, 0))) {
- Serial.println("AP Config Failed");
- }
- else {
- if( ! WiFi.softAP(APConfig.wssid, APConfig.wpwd)) {
- Serial.println("AP creation Failed");
- }
- else {
- IP_controler = WiFi.softAPIP();
- Serial.print("AP IP: ");
- Serial.println(IP_controler);
- }
- }
- Serial.print("Adresse MAC de ce circuit: ");
- Serial.println(WiFi.macAddress());
- HTTPserver = new AsyncWebServer(80);
- HTTPserver->on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send(SPIFFS, "/parampage.html", "text/html");
- });
- HTTPserver->on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
- request->send(SPIFFS, "/style.css", "text/css");
- });
- HTTPserver->on("/parampage.js", HTTP_GET, [](AsyncWebServerRequest *request) {
- request->send(SPIFFS, "/parampage.js", "text/javascript");
- });
- HTTPserver->onNotFound([](AsyncWebServerRequest *request){
- request->send(404, "text/plain", "TARIFPOMPE : Page introuvable");
- });
- handle_getParamData();
- handle_paramPost();
- HTTPserver->begin();
- Serial.print("Serveur HTTP sur http://");
- Serial.println(IP_controler);
- }
- //-----------------------------------------------------------
- // renvoit les valeurs de paramètre
- //-----------------------------------------------------------
- void handle_getParamData() {
- HTTPserver->on("/getParamData", HTTP_GET, [](AsyncWebServerRequest * request){
- String response = "";
- // response += String(APConfig.wssid)+";";
- // response += String(APConfig.wpwd)+";";
- response += String(APConfig.tarif1)+";";
- response += String(APConfig.tarif2)+";";
- response += String(APConfig.tarif3)+";";
- // if (DEBUG_SERIAL && DEBUG_LEVEL&16) { Serial.print(F("Reponse au getparamdata:")); Serial.println(response); }
- request->send(200, "text/plain", response);
- }
- );
- }
- //-----------------------------------------------------------
- // ------ Enrgistrement des paramètres
- //-----------------------------------------------------------
- void handle_paramPost() {
- HTTPserver->on(
- "/parampost"
- ,HTTP_POST
- ,[](AsyncWebServerRequest * request){
- int paramsNr = request->params();
- char temp[10];
- for (int i=0; i < paramsNr; i++) {
- AsyncWebParameter* p = request->getParam(i);
- // -------- CONTROLEUR
- if (p->name() == "wssid") {
- if (p->value()!="") strncpy(APConfig.wssid, p->value().c_str(), 32);
- }
- else if (p->name() == "wpwd") {
- if (p->value()!="") strncpy(APConfig.wpwd, p->value().c_str(), 32);
- }
- if (p->name() == "tp1") {
- if (p->value()!="") {
- APConfig.tarif1 = p->value().toDouble();
- }
- }
- if (p->name() == "tp2") {
- if (p->value()!="") {
- APConfig.tarif2 = p->value().toDouble();
- }
- }
- if (p->name() == "tp3") {
- if (p->value()!="") {
- APConfig.tarif3 = p->value().toDouble();
- }
- }
- }
- putEEPROMdata();
- miseAJourTarifAfficheurs(APConfig.tarif1,APConfig.tarif2,APConfig.tarif3);
- request->redirect("/");
- }
- );
- }
- /*--------------------------------------------
- *
- * Procédures de gestion de lEEPROM
- *
- *--------------------------------------------
- */
- //-----------------------------------------------------------
- //====== Ecrire les donnees en EEPROM/FLASH
- //-----------------------------------------------------------
- void putEEPROMdata() {
- EEPROM.begin(EEPROM_SIZE);
- EEPROM.put(0, APConfig);
- EEPROM.commit();
- EEPROM.end();
- }
- //-----------------------------------------------------------
- //====== Lire les donnees en EEPROM/FLASH
- //-----------------------------------------------------------
- void getEEPROMdata() {
- const char *magicKey = "K4d0lol!";
- EEPROM.begin(EEPROM_SIZE);
- EEPROM.get(0, APConfig);
- if (strncmp(APConfig.magic, magicKey, 8) != 0) {
- Serial.println("MagicKey non trouvee, init EEPROM avec valeurs par defaut");
- // flash/eeprom non initialisee
- EEPROM.end();
- // ecriture avec les valeurs par défaut
- memcpy(APConfig.magic, magicKey,sizeof(APConfig.magic));
- strcpy(APConfig.wssid, "TARIF_P");
- strcpy(APConfig.wpwd, "01234567890");
- APConfig.tarif1 = 1.10;
- APConfig.tarif2 = 1.20;
- APConfig.tarif3 = 1.30;
- putEEPROMdata();
- }
- else {
- EEPROM.end();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement