Advertisement
WolfLarsen33

Tarifs pompes : code

Jan 6th, 2024
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * MAJ tarifs pompe
  3.  * par Laurent V.
  4.  * ESP32
  5.  * Afficheurs 4 digits
  6.  *
  7.  * Biliotheques necessaires:
  8.  * ESPAsyncWebSrc (avec ESPAsyncTCP et AsybncTCP)
  9. */
  10. #define VERSION "1.0"
  11. const char * MODULE="Tarifs pompes";
  12.  
  13. #include <Arduino.h>
  14. #include <WiFi.h>
  15. #include "esp_wifi.h"
  16.  
  17. IPAddress IP_controler;
  18.  
  19. #include <SPIFFS.h>
  20.  
  21. #include <AsyncTCP.h>
  22. #include <ESPAsyncWebSrv.h>
  23. AsyncWebServer *HTTPserver;
  24.  
  25. //---- structure de configuration en EEPROM
  26. // ca garde les codes du point d'accès et les derniers tarifs saisis
  27. #include <EEPROM.h>
  28.  
  29. typedef struct sConf {
  30.   char wssid[32];
  31.   char wpwd[32];
  32.   double tarif1;
  33.   double tarif2;
  34.   double tarif3;
  35.   char magic[8];
  36. } sConf;
  37. sConf APConfig;
  38.  
  39. #define EEPROM_SIZE sizeof(sConf)
  40.  
  41. //=============== SETUP ===============
  42. void setup() {
  43.   Serial.begin(115200);
  44.   Serial.println();
  45.   Serial.println(MODULE);
  46.   Serial.println(VERSION);
  47.  
  48.   getEEPROMdata();
  49.  
  50.   if (SPIFFS.begin(true)) {
  51.     Serial.println("SPIFFS OK");
  52.     initHTTP();
  53.   }
  54.   else {
  55.     Serial.println("SPIFFS KO ! impossible de lancer le serveur WEB.");
  56.    
  57.   }
  58. }
  59.  
  60. //================ LOOP =================
  61. void loop() {
  62.  
  63. }
  64.  
  65. //-----------------------------------------------------------
  66. // La partie qui met a jour les afficheurs
  67. //-----------------------------------------------------------
  68. void miseAJourTarifAfficheurs(double t1,double t2,double t3) {
  69.  
  70. Serial.println("Tarifs:");
  71.   // mettre a jour l'afficheur 1 avec le tarif t1
  72. Serial.println(t1);
  73.  
  74.   // mettre a jour l'afficheur 2 avec le tarif t2
  75. Serial.println(t2);
  76.  
  77.   // mettre a jour l'afficheur 3 avec le tarif t3
  78. Serial.println(t3);
  79.  
  80. }
  81.  
  82.  
  83. //-----------------------------------------------------------
  84. // init du serveur WEB
  85. //-----------------------------------------------------------
  86. void initHTTP() {
  87.  
  88.   // init point d'acces
  89.   WiFi.disconnect();
  90.   WiFi.mode(WIFI_AP_STA);
  91.  
  92.   if( ! WiFi.softAPConfig(IPAddress(192, 168, 10, 10), IPAddress(192, 168, 10, 10), IPAddress(255, 255, 255, 0))) {
  93.     Serial.println("AP Config Failed");
  94.   }
  95.   else {
  96.     if( ! WiFi.softAP(APConfig.wssid, APConfig.wpwd)) {
  97.       Serial.println("AP creation Failed");
  98.     }
  99.     else {
  100.       IP_controler = WiFi.softAPIP();
  101.       Serial.print("AP IP: ");
  102.       Serial.println(IP_controler);
  103.     }
  104.   }
  105.  
  106.   Serial.print("Adresse MAC de ce circuit: ");
  107.   Serial.println(WiFi.macAddress());
  108.  
  109.   HTTPserver = new AsyncWebServer(80);
  110.  
  111.   HTTPserver->on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  112.     request->send(SPIFFS, "/parampage.html", "text/html");
  113.   });
  114.   HTTPserver->on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
  115.     request->send(SPIFFS, "/style.css", "text/css");
  116.   });
  117.   HTTPserver->on("/parampage.js", HTTP_GET, [](AsyncWebServerRequest *request) {
  118.     request->send(SPIFFS, "/parampage.js", "text/javascript");
  119.   });
  120.  
  121.   HTTPserver->onNotFound([](AsyncWebServerRequest *request){
  122.     request->send(404, "text/plain", "TARIFPOMPE : Page introuvable");
  123.   });
  124.  
  125.   handle_getParamData();
  126.   handle_paramPost();
  127.  
  128.   HTTPserver->begin();
  129.   Serial.print("Serveur HTTP sur http://");
  130.   Serial.println(IP_controler);
  131.  
  132. }
  133.  
  134. //-----------------------------------------------------------
  135. // renvoit les valeurs de paramètre
  136. //-----------------------------------------------------------
  137. void handle_getParamData() {
  138.   HTTPserver->on("/getParamData", HTTP_GET, [](AsyncWebServerRequest * request){
  139.       String response = "";
  140. //      response += String(APConfig.wssid)+";";
  141. //      response += String(APConfig.wpwd)+";";
  142.       response += String(APConfig.tarif1)+";";
  143.       response += String(APConfig.tarif2)+";";
  144.       response += String(APConfig.tarif3)+";";
  145. //      if (DEBUG_SERIAL && DEBUG_LEVEL&16) { Serial.print(F("Reponse au getparamdata:")); Serial.println(response); }
  146.       request->send(200, "text/plain", response);
  147.     }
  148.   );
  149. }
  150.  
  151. //-----------------------------------------------------------
  152. // ------ Enrgistrement des paramètres
  153. //-----------------------------------------------------------
  154. void handle_paramPost() {
  155.   HTTPserver->on(
  156.      "/parampost"
  157.     ,HTTP_POST
  158.     ,[](AsyncWebServerRequest * request){
  159.       int paramsNr = request->params();
  160.       char temp[10];
  161.       for (int i=0; i < paramsNr; i++) {
  162.         AsyncWebParameter* p = request->getParam(i);
  163.         // -------- CONTROLEUR
  164.         if (p->name() == "wssid") {
  165.           if (p->value()!="") strncpy(APConfig.wssid, p->value().c_str(), 32);
  166.         }
  167.         else if (p->name() == "wpwd") {
  168.           if (p->value()!="") strncpy(APConfig.wpwd, p->value().c_str(), 32);
  169.         }
  170.    
  171.         if (p->name() == "tp1") {
  172.           if (p->value()!="") {
  173.             APConfig.tarif1 = p->value().toDouble();
  174.           }
  175.         }
  176.         if (p->name() == "tp2") {
  177.           if (p->value()!="") {
  178.             APConfig.tarif2 = p->value().toDouble();
  179.           }
  180.         }
  181.         if (p->name() == "tp3") {
  182.           if (p->value()!="") {
  183.             APConfig.tarif3 = p->value().toDouble();
  184.           }
  185.         }
  186.       }
  187.        
  188.       putEEPROMdata();
  189.  
  190.       miseAJourTarifAfficheurs(APConfig.tarif1,APConfig.tarif2,APConfig.tarif3);
  191.      
  192.       request->redirect("/");
  193.     }
  194.   );
  195. }
  196.  
  197.  
  198.  
  199. /*--------------------------------------------
  200.  *
  201.  * Procédures de gestion de lEEPROM
  202.  *
  203.  *--------------------------------------------
  204.  */
  205.  
  206. //-----------------------------------------------------------
  207. //====== Ecrire les donnees en EEPROM/FLASH
  208. //-----------------------------------------------------------
  209. void putEEPROMdata() {
  210.   EEPROM.begin(EEPROM_SIZE);
  211.   EEPROM.put(0, APConfig);
  212.   EEPROM.commit();
  213.   EEPROM.end();
  214. }
  215.  
  216. //-----------------------------------------------------------
  217. //====== Lire les donnees en EEPROM/FLASH
  218. //-----------------------------------------------------------
  219. void getEEPROMdata() {
  220.   const char *magicKey = "K4d0lol!";
  221.  
  222.   EEPROM.begin(EEPROM_SIZE);
  223.   EEPROM.get(0, APConfig);
  224.   if (strncmp(APConfig.magic, magicKey, 8) != 0) {
  225.     Serial.println("MagicKey non trouvee, init EEPROM avec valeurs par defaut");
  226.     // flash/eeprom non initialisee
  227.     EEPROM.end();
  228.     // ecriture avec les valeurs par défaut
  229.     memcpy(APConfig.magic, magicKey,sizeof(APConfig.magic));
  230.     strcpy(APConfig.wssid, "TARIF_P");
  231.     strcpy(APConfig.wpwd, "01234567890");
  232.     APConfig.tarif1 = 1.10;
  233.     APConfig.tarif2 = 1.20;
  234.     APConfig.tarif3 = 1.30;
  235.    
  236.     putEEPROMdata();
  237.   }
  238.   else {
  239.     EEPROM.end();
  240.   }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement