Advertisement
WolfLarsen33

Untitled

Dec 26th, 2021
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.72 KB | None | 0 0
  1. //-------------------------------------------------
  2. //
  3. // EV Charger
  4. //
  5. //-------------------------------------------------
  6.  
  7. #define VERSION   "1.1b"
  8.  
  9. bool DEBUG_SERIAL;    // pour activer le debug sur le port serie
  10. bool MODE_SIMU;       // simulation
  11.  
  12. #define WITH_WEBSERVER
  13. //#define WITH_OTA
  14. #define WITH_ISENSE
  15.  
  16. //---- structure de configuration en EEPROM
  17. #include <EEPROM.h>
  18.  
  19. struct sConf {
  20.   char wmode;
  21.   char wssid[32];
  22.   char wpwd[32];
  23.   uint8_t debugMode;
  24.   uint8_t IMax;
  25.   uint16_t ISMax_standby;
  26.   uint16_t ISMax_vehdetect;
  27.   uint16_t ISMax_charge;
  28.   uint16_t ISMax_ventil;
  29.   uint16_t IS_cc;
  30.   uint16_t delayBeforeStop_PBMode;
  31.   char magic[8];
  32. };
  33.  
  34. sConf APConfig;
  35.  
  36. #define EEPROM_SIZE sizeof(sConf)
  37.  
  38. #include <ESP8266WiFi.h>
  39. //#include <ESP8266mDNS.h>
  40.  
  41. #ifdef WITH_WEBSERVER
  42. #include <FS.h>
  43. #include <LittleFS.h>
  44. #include <ESPAsyncTCP.h>
  45. #include <ESPAsyncWebServer.h>
  46.  
  47. AsyncWebServer HTTPserver(80);
  48. #endif
  49.  
  50. #ifdef WITH_OTA
  51. #include <WiFiUdp.h>
  52. #include <ArduinoOTA.h>
  53. #endif
  54.  
  55. #include <LiquidCrystal.h>
  56. // définition des broches auxquelles on a branché l'afficheur LCD
  57. #define LCD_RS D2
  58. #define LCD_EN D0
  59. #define LLCD_D4 D3
  60. #define LCD_D5 D7
  61. #define LCD_D6 D6
  62. #define LCD_D7 D5
  63. LiquidCrystal lcd(LCD_RS, LCD_EN, LLCD_D4, LCD_D5, LCD_D6, LCD_D7);
  64. uint8_t uparrow_sign[] = {
  65.    0b00100
  66.   ,0b01110
  67.   ,0b10101
  68.   ,0b00100
  69.   ,0b00100
  70.   ,0b00100
  71.   ,0b00100
  72.   ,0b00100
  73. };
  74. uint8_t dnarrow_sign[] = {
  75.    0b00100
  76.   ,0b00100
  77.   ,0b00100
  78.   ,0b00100
  79.   ,0b00100
  80.   ,0b10101
  81.   ,0b01110
  82.   ,0b00100
  83. };
  84.  
  85. //-------- signal carré 1kHz
  86. #define SQUARE_PIN D4
  87.  
  88. //-------- sortie commance relais
  89. #define RELAY_PIN D8
  90.  
  91. //-------- mesure de tension
  92. uint8_t CS_pin = A0;
  93. #define CS_BUFFER_LEN 16
  94. uint16_t CS_buffer[CS_BUFFER_LEN]; //---- buffer valeurs de courant
  95. uint16_t CS_bufferPos=0; //---- position buffer valeurs de courant
  96. uint16_t ISense_max=0;
  97.  
  98. uint16_t dbg_i[2048]; //---- buffer valeurs de courant
  99. uint16_t dbg_ipos=0; //---- position buffer valeurs de courant
  100.  
  101. //-------- arret forcé
  102. bool arretForce = false;
  103.  
  104. //-------- Flag mode charge pour activer le isense rapide
  105. bool chargeModeStandby = true;
  106.  
  107.  
  108. //-------- Flag acces routeur OK
  109. bool accesRouteurOK = true;
  110.  
  111.  
  112. //------------------------------------------------
  113. void setup() {
  114.  
  115.   pinMode(RELAY_PIN, OUTPUT);
  116.   digitalWrite(RELAY_PIN, LOW);
  117.  
  118.   analogWriteRange(1023);
  119.   analogWriteFreq(1000);
  120.   analogWrite(SQUARE_PIN, 0);
  121.  
  122.   Serial.begin(115200);
  123.   Serial.println();
  124.   Serial.println("==== EV Charger ====");
  125.   Serial.println(VERSION);
  126.   Serial.println("tapez '?' pour la liste des commandes.");
  127.  
  128.   getEEPROMdata();
  129.  
  130.   DEBUG_SERIAL = APConfig.debugMode==0 ? false : true;
  131.   Serial.println(APConfig.wssid);
  132.  
  133.   lcd.begin(16, 2);
  134.   lcd.clear();
  135.   lcd.printf("EV Charger v%s",VERSION);
  136.   lcd.setCursor(0, 1);
  137.   lcd.createChar(1,uparrow_sign);
  138.   lcd.createChar(2,dnarrow_sign);
  139.  
  140.   if (APConfig.wmode=='R') {     // connexion à un routeur
  141.     WiFi.begin(APConfig.wssid, APConfig.wpwd);
  142.     Serial.print(F("Connection "));
  143.     const uint8_t cnxTrials = 15;
  144.     uint8_t i = 0;
  145.     while (WiFi.status() != WL_CONNECTED && i++ < cnxTrials) {
  146.       delay(500);
  147.       Serial.print(".");
  148.       lcd.print(".");
  149.     }
  150.     Serial.println();
  151.     if (i >= cnxTrials) {
  152.       accesRouteurOK = false; // la connexion au routeur a echoué
  153.       Serial.print(F("Connexion impossible au routeur: "));
  154.       Serial.println(APConfig.wssid);
  155.     }
  156.   }
  157.  
  158.   if (APConfig.wmode=='A' || accesRouteurOK==false) {  // point d'acces autonome ou acces routeur KO
  159.     IPAddress local_IP(192,168,111,1);
  160.     IPAddress gateway(192,168,111,1);
  161.     IPAddress subnet(255,255,255,0);
  162.     bool srescf=WiFi.softAPConfig(local_IP, gateway, subnet);
  163.     bool sresap=WiFi.softAP(APConfig.wssid, APConfig.wpwd);
  164.  
  165.     Serial.print(F("Setting soft-AP configuration ... "));
  166.     Serial.println(srescf ? F("Ready") : F("Failed!"));
  167.     Serial.print(F("Setting soft-AP ... "));
  168.     Serial.println(sresap ? F("Ready") : F("Failed!"));
  169.   }
  170.  
  171. #ifdef WITH_WEBSERVER
  172.   if (LittleFS.begin()) {
  173.     Serial.println("LittleFS OK");
  174.     //---- Routage pages WEB
  175.     HTTPserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
  176.       request->send(LittleFS, "/mainpage.html", String());
  177.     });
  178.     HTTPserver.on("/param", HTTP_GET, [](AsyncWebServerRequest *request) {
  179.       request->send(LittleFS, "/parampage.html", String());
  180.     });
  181.     HTTPserver.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
  182.       request->send(LittleFS, "/style.css", "text/css");
  183.     });
  184.   //  handle_getData();
  185.     handle_getParamData();
  186.     handle_sendOrder();
  187.     handle_paramPost();
  188.    
  189.     HTTPserver.begin();
  190.     Serial.print(F("HTTP server: http://"));
  191.   }
  192.   else {
  193.     Serial.println("LittleFS Erreur de montage");
  194.   }
  195. #endif
  196.  
  197.   lcd.setCursor(0, 1);
  198.   if (APConfig.wmode=='A' || accesRouteurOK==false) {
  199.     Serial.println(WiFi.softAPIP());
  200.     lcd.print(WiFi.softAPIP());
  201.   }
  202.   else {
  203.     Serial.println(WiFi.localIP());
  204.     lcd.println(WiFi.localIP());
  205.   }
  206.   delay(1000);
  207.  
  208. #ifdef WITH_OTA
  209. //  ArduinoOTA.setHostname(APConfig.wssid);
  210.   ArduinoOTA.setHostname("EVC_OTA");
  211.   ArduinoOTA.begin();
  212. #endif
  213.  
  214.   setIMax(APConfig.IMax);
  215.   for (int ics=0; ics<CS_BUFFER_LEN; ics++) {
  216.     CS_buffer[ics] = APConfig.ISMax_standby;
  217.   }
  218. }
  219.  
  220.  
  221. //------------------------------------------------
  222. void loop() {
  223.  
  224. #ifdef WITH_OTA
  225.   ArduinoOTA.handle();
  226. #endif
  227.  
  228.  
  229.   static uint32_t dlyIview=0;
  230.   static bool PBMode = false;
  231.   static uint32_t delayStopAlim=0;
  232.   static uint32_t dlyISense=0;
  233.  
  234.   #define POL_MS 1000
  235.   if (millis()-dlyIview>POL_MS) {
  236.     dlyIview = millis();
  237.     ISense_max = getIMean();
  238.     if (DEBUG_SERIAL) {
  239.       lcd.setCursor(0, 1);
  240.       lcd.printf("%c:%4d", (char)1, ISense_max);
  241.     }
  242.    
  243.     lcd.setCursor(0, 0);
  244.     if (arretForce) {
  245.       if (DEBUG_SERIAL) {
  246.         Serial.println("Arret force");
  247.       }
  248.       lcd.print( "Arret force      ");
  249.       digitalWrite(RELAY_PIN, LOW);
  250.       PBMode = false;
  251.     }
  252.     else if (ISense_max > APConfig.ISMax_standby-20) {
  253.       if (DEBUG_SERIAL) {
  254.         Serial.printf("StandBy %d \n",ISense_max);
  255.       }
  256.       PBMode = true;
  257.       chargeModeStandby = true;
  258.       if (DEBUG_SERIAL) {
  259.         Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
  260.       }
  261.       dlyISense = 0;
  262.       lcd.printf( "StandBy      %2dA",APConfig.IMax);
  263.       digitalWrite(RELAY_PIN, LOW);
  264.     }
  265.     else if (ISense_max > APConfig.ISMax_vehdetect-20) {
  266. if (DEBUG_SERIAL) {
  267. /*  for (int i=0;i<2048;i++) {
  268.     Serial.printf("%d ",dbg_i[i]);
  269.     if (i==dbg_ipos) {
  270.       Serial.println("***");
  271.     }
  272.   }*/
  273.   Serial.println();
  274.   Serial.print("buf:");
  275.   for (int ics=0; ics < CS_BUFFER_LEN; ics++) {
  276.     Serial.printf("%d ",CS_buffer[ics]);
  277.   }
  278.   Serial.println();
  279. }
  280.       if (DEBUG_SERIAL) {
  281.         Serial.printf("Prise branchee %d \n",ISense_max);
  282.       }
  283.       if ( ! PBMode) {
  284.         if (DEBUG_SERIAL) {
  285.           Serial.println("Debut decompte");
  286.         }
  287.         PBMode = true;
  288.         delayStopAlim = APConfig.delayBeforeStop_PBMode * 4;
  289.       }
  290.       if (delayStopAlim > 0) {
  291.         digitalWrite(RELAY_PIN, HIGH);
  292.         delayStopAlim--;
  293.         chargeModeStandby = false;
  294.         if (DEBUG_SERIAL) {
  295.           Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
  296.         }
  297.         dlyISense = 0;
  298.       }
  299.       else {
  300.         if (DEBUG_SERIAL) {
  301.           Serial.println("Fin decompte");
  302.         }
  303.         digitalWrite(RELAY_PIN, LOW);
  304.         chargeModeStandby = true;
  305.         if (DEBUG_SERIAL) {
  306.           Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
  307.         }
  308.         dlyISense = 0;
  309.       }
  310.       lcd.print( "Prise branchee  ");
  311.     }
  312.     else if (ISense_max > APConfig.ISMax_charge-20) {
  313.       if (DEBUG_SERIAL) {
  314.         Serial.printf("Charge %d \n",ISense_max);
  315.       }
  316.       digitalWrite(RELAY_PIN, HIGH);
  317.       PBMode = false;
  318.       chargeModeStandby = false;
  319.       if (DEBUG_SERIAL) {
  320.         Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
  321.       }
  322.       dlyISense = 0;
  323.       lcd.printf("Charge (%2dA max)",APConfig.IMax);
  324.     }
  325.     else if (ISense_max > APConfig.ISMax_ventil-20) {
  326.       if (DEBUG_SERIAL) {
  327.         Serial.printf("Charge vent. %d \n",ISense_max);
  328.       }
  329.       digitalWrite(RELAY_PIN, HIGH);
  330.       PBMode = false;
  331.       chargeModeStandby = false;
  332.       if (DEBUG_SERIAL) {
  333.         Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
  334.       }
  335.       dlyISense = 0;
  336.       lcd.print( "Charge ventilee ");
  337.     }
  338.     else {
  339.       if (DEBUG_SERIAL) {
  340.         Serial.printf("Anomalie ou isense slow ! %d \n",ISense_max);
  341.       }
  342.       digitalWrite(RELAY_PIN, LOW);
  343.       PBMode = false;
  344.       chargeModeStandby = true;
  345.       if (DEBUG_SERIAL) {
  346.         Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
  347.       }
  348.       dlyISense = 0;
  349.     }
  350.   }
  351.  
  352. #ifdef WITH_ISENSE
  353.   int isense=0;
  354.   int isense_cur, isense_prec;
  355.   if ( ! chargeModeStandby) {
  356.     if (micros()-dlyISense>250) {
  357.       dlyISense = micros();
  358.       isense = 0;
  359.       for (uint8_t is=0; is < 8; is++) {
  360.         isense=max(isense, analogRead(CS_pin));
  361.         delayMicroseconds(2);
  362.       }
  363. //if (DEBUG_SERIAL) { Serial.printf("%d ",isense); }
  364.       if (isense > 500) {
  365.         CS_buffer[CS_bufferPos]=isense;
  366.         CS_bufferPos = (CS_bufferPos>=CS_BUFFER_LEN-1) ? 0 : CS_bufferPos+1;
  367.       }
  368.     }
  369.   }
  370.   else {
  371.     if (millis()-dlyISense>11) {
  372.       dlyISense = millis();
  373.       isense = 0;
  374. if (DEBUG_SERIAL) {
  375.   dbg_ipos = 0;
  376.   for (uint8_t is=0; is < 8; is++) {
  377.     dbg_i[is]=0;
  378.   }
  379. }
  380.       for (uint8_t is=0; is < 8; is++) {
  381.         isense_cur = analogRead(CS_pin);
  382.         if (isense_cur < 400 && is == 0) {
  383.           break;
  384.         }
  385. if (DEBUG_SERIAL) dbg_i[dbg_ipos++] = isense_cur;
  386.         isense=max(isense, isense_cur);
  387.         delayMicroseconds(2);
  388.       }
  389. if (DEBUG_SERIAL) {
  390.   Serial.println("Valeurs: ");
  391.   for (uint8_t is=0; is < 8; is++) {
  392.     Serial.printf("%d ",dbg_i[is]);
  393.   }
  394.   Serial.print(" --> ");
  395.   Serial.print(isense);
  396.   Serial.println();
  397. }
  398. //if (DEBUG_SERIAL) { Serial.printf("%d ",isense); }
  399.       if (isense > 400) {
  400.         CS_buffer[CS_bufferPos]=isense;
  401.         CS_bufferPos = (CS_bufferPos>=CS_BUFFER_LEN-1) ? 0 : CS_bufferPos+1;
  402.       }
  403.     }
  404.   }
  405. #endif
  406.  
  407.   getAndProcessSerialInput();
  408. }
  409.  
  410. //----------------------------------------
  411. void setIMax(uint8_t Imax) {
  412.   uint16_t pwm = 514 * Imax / 30;
  413.   analogWrite(SQUARE_PIN, pwm);
  414. }
  415.  
  416. //--------- lecture courant mini/maxi
  417. uint16_t getIMean() {
  418.   uint32_t sumI=0;
  419.   for (int ics=0; ics < CS_BUFFER_LEN; ics++) {
  420.     sumI += CS_buffer[ics];
  421.   }
  422.   return (uint16_t)(sumI / CS_BUFFER_LEN);
  423. }
  424.  
  425. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement