Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------
- //
- // EV Charger
- //
- //-------------------------------------------------
- #define VERSION "1.1b"
- bool DEBUG_SERIAL; // pour activer le debug sur le port serie
- bool MODE_SIMU; // simulation
- #define WITH_WEBSERVER
- //#define WITH_OTA
- #define WITH_ISENSE
- //---- structure de configuration en EEPROM
- #include <EEPROM.h>
- struct sConf {
- char wmode;
- char wssid[32];
- char wpwd[32];
- uint8_t debugMode;
- uint8_t IMax;
- uint16_t ISMax_standby;
- uint16_t ISMax_vehdetect;
- uint16_t ISMax_charge;
- uint16_t ISMax_ventil;
- uint16_t IS_cc;
- uint16_t delayBeforeStop_PBMode;
- char magic[8];
- };
- sConf APConfig;
- #define EEPROM_SIZE sizeof(sConf)
- #include <ESP8266WiFi.h>
- //#include <ESP8266mDNS.h>
- #ifdef WITH_WEBSERVER
- #include <FS.h>
- #include <LittleFS.h>
- #include <ESPAsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- AsyncWebServer HTTPserver(80);
- #endif
- #ifdef WITH_OTA
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #endif
- #include <LiquidCrystal.h>
- // définition des broches auxquelles on a branché l'afficheur LCD
- #define LCD_RS D2
- #define LCD_EN D0
- #define LLCD_D4 D3
- #define LCD_D5 D7
- #define LCD_D6 D6
- #define LCD_D7 D5
- LiquidCrystal lcd(LCD_RS, LCD_EN, LLCD_D4, LCD_D5, LCD_D6, LCD_D7);
- uint8_t uparrow_sign[] = {
- 0b00100
- ,0b01110
- ,0b10101
- ,0b00100
- ,0b00100
- ,0b00100
- ,0b00100
- ,0b00100
- };
- uint8_t dnarrow_sign[] = {
- 0b00100
- ,0b00100
- ,0b00100
- ,0b00100
- ,0b00100
- ,0b10101
- ,0b01110
- ,0b00100
- };
- //-------- signal carré 1kHz
- #define SQUARE_PIN D4
- //-------- sortie commance relais
- #define RELAY_PIN D8
- //-------- mesure de tension
- uint8_t CS_pin = A0;
- #define CS_BUFFER_LEN 16
- uint16_t CS_buffer[CS_BUFFER_LEN]; //---- buffer valeurs de courant
- uint16_t CS_bufferPos=0; //---- position buffer valeurs de courant
- uint16_t ISense_max=0;
- uint16_t dbg_i[2048]; //---- buffer valeurs de courant
- uint16_t dbg_ipos=0; //---- position buffer valeurs de courant
- //-------- arret forcé
- bool arretForce = false;
- //-------- Flag mode charge pour activer le isense rapide
- bool chargeModeStandby = true;
- //-------- Flag acces routeur OK
- bool accesRouteurOK = true;
- //------------------------------------------------
- void setup() {
- pinMode(RELAY_PIN, OUTPUT);
- digitalWrite(RELAY_PIN, LOW);
- analogWriteRange(1023);
- analogWriteFreq(1000);
- analogWrite(SQUARE_PIN, 0);
- Serial.begin(115200);
- Serial.println();
- Serial.println("==== EV Charger ====");
- Serial.println(VERSION);
- Serial.println("tapez '?' pour la liste des commandes.");
- getEEPROMdata();
- DEBUG_SERIAL = APConfig.debugMode==0 ? false : true;
- Serial.println(APConfig.wssid);
- lcd.begin(16, 2);
- lcd.clear();
- lcd.printf("EV Charger v%s",VERSION);
- lcd.setCursor(0, 1);
- lcd.createChar(1,uparrow_sign);
- lcd.createChar(2,dnarrow_sign);
- if (APConfig.wmode=='R') { // connexion à un routeur
- WiFi.begin(APConfig.wssid, APConfig.wpwd);
- Serial.print(F("Connection "));
- const uint8_t cnxTrials = 15;
- uint8_t i = 0;
- while (WiFi.status() != WL_CONNECTED && i++ < cnxTrials) {
- delay(500);
- Serial.print(".");
- lcd.print(".");
- }
- Serial.println();
- if (i >= cnxTrials) {
- accesRouteurOK = false; // la connexion au routeur a echoué
- Serial.print(F("Connexion impossible au routeur: "));
- Serial.println(APConfig.wssid);
- }
- }
- if (APConfig.wmode=='A' || accesRouteurOK==false) { // point d'acces autonome ou acces routeur KO
- IPAddress local_IP(192,168,111,1);
- IPAddress gateway(192,168,111,1);
- IPAddress subnet(255,255,255,0);
- bool srescf=WiFi.softAPConfig(local_IP, gateway, subnet);
- bool sresap=WiFi.softAP(APConfig.wssid, APConfig.wpwd);
- Serial.print(F("Setting soft-AP configuration ... "));
- Serial.println(srescf ? F("Ready") : F("Failed!"));
- Serial.print(F("Setting soft-AP ... "));
- Serial.println(sresap ? F("Ready") : F("Failed!"));
- }
- #ifdef WITH_WEBSERVER
- if (LittleFS.begin()) {
- Serial.println("LittleFS OK");
- //---- Routage pages WEB
- HTTPserver.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
- request->send(LittleFS, "/mainpage.html", String());
- });
- HTTPserver.on("/param", HTTP_GET, [](AsyncWebServerRequest *request) {
- request->send(LittleFS, "/parampage.html", String());
- });
- HTTPserver.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) {
- request->send(LittleFS, "/style.css", "text/css");
- });
- // handle_getData();
- handle_getParamData();
- handle_sendOrder();
- handle_paramPost();
- HTTPserver.begin();
- Serial.print(F("HTTP server: http://"));
- }
- else {
- Serial.println("LittleFS Erreur de montage");
- }
- #endif
- lcd.setCursor(0, 1);
- if (APConfig.wmode=='A' || accesRouteurOK==false) {
- Serial.println(WiFi.softAPIP());
- lcd.print(WiFi.softAPIP());
- }
- else {
- Serial.println(WiFi.localIP());
- lcd.println(WiFi.localIP());
- }
- delay(1000);
- #ifdef WITH_OTA
- // ArduinoOTA.setHostname(APConfig.wssid);
- ArduinoOTA.setHostname("EVC_OTA");
- ArduinoOTA.begin();
- #endif
- setIMax(APConfig.IMax);
- for (int ics=0; ics<CS_BUFFER_LEN; ics++) {
- CS_buffer[ics] = APConfig.ISMax_standby;
- }
- }
- //------------------------------------------------
- void loop() {
- #ifdef WITH_OTA
- ArduinoOTA.handle();
- #endif
- static uint32_t dlyIview=0;
- static bool PBMode = false;
- static uint32_t delayStopAlim=0;
- static uint32_t dlyISense=0;
- #define POL_MS 1000
- if (millis()-dlyIview>POL_MS) {
- dlyIview = millis();
- ISense_max = getIMean();
- if (DEBUG_SERIAL) {
- lcd.setCursor(0, 1);
- lcd.printf("%c:%4d", (char)1, ISense_max);
- }
- lcd.setCursor(0, 0);
- if (arretForce) {
- if (DEBUG_SERIAL) {
- Serial.println("Arret force");
- }
- lcd.print( "Arret force ");
- digitalWrite(RELAY_PIN, LOW);
- PBMode = false;
- }
- else if (ISense_max > APConfig.ISMax_standby-20) {
- if (DEBUG_SERIAL) {
- Serial.printf("StandBy %d \n",ISense_max);
- }
- PBMode = true;
- chargeModeStandby = true;
- if (DEBUG_SERIAL) {
- Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
- }
- dlyISense = 0;
- lcd.printf( "StandBy %2dA",APConfig.IMax);
- digitalWrite(RELAY_PIN, LOW);
- }
- else if (ISense_max > APConfig.ISMax_vehdetect-20) {
- if (DEBUG_SERIAL) {
- /* for (int i=0;i<2048;i++) {
- Serial.printf("%d ",dbg_i[i]);
- if (i==dbg_ipos) {
- Serial.println("***");
- }
- }*/
- Serial.println();
- Serial.print("buf:");
- for (int ics=0; ics < CS_BUFFER_LEN; ics++) {
- Serial.printf("%d ",CS_buffer[ics]);
- }
- Serial.println();
- }
- if (DEBUG_SERIAL) {
- Serial.printf("Prise branchee %d \n",ISense_max);
- }
- if ( ! PBMode) {
- if (DEBUG_SERIAL) {
- Serial.println("Debut decompte");
- }
- PBMode = true;
- delayStopAlim = APConfig.delayBeforeStop_PBMode * 4;
- }
- if (delayStopAlim > 0) {
- digitalWrite(RELAY_PIN, HIGH);
- delayStopAlim--;
- chargeModeStandby = false;
- if (DEBUG_SERIAL) {
- Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
- }
- dlyISense = 0;
- }
- else {
- if (DEBUG_SERIAL) {
- Serial.println("Fin decompte");
- }
- digitalWrite(RELAY_PIN, LOW);
- chargeModeStandby = true;
- if (DEBUG_SERIAL) {
- Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
- }
- dlyISense = 0;
- }
- lcd.print( "Prise branchee ");
- }
- else if (ISense_max > APConfig.ISMax_charge-20) {
- if (DEBUG_SERIAL) {
- Serial.printf("Charge %d \n",ISense_max);
- }
- digitalWrite(RELAY_PIN, HIGH);
- PBMode = false;
- chargeModeStandby = false;
- if (DEBUG_SERIAL) {
- Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
- }
- dlyISense = 0;
- lcd.printf("Charge (%2dA max)",APConfig.IMax);
- }
- else if (ISense_max > APConfig.ISMax_ventil-20) {
- if (DEBUG_SERIAL) {
- Serial.printf("Charge vent. %d \n",ISense_max);
- }
- digitalWrite(RELAY_PIN, HIGH);
- PBMode = false;
- chargeModeStandby = false;
- if (DEBUG_SERIAL) {
- Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
- }
- dlyISense = 0;
- lcd.print( "Charge ventilee ");
- }
- else {
- if (DEBUG_SERIAL) {
- Serial.printf("Anomalie ou isense slow ! %d \n",ISense_max);
- }
- digitalWrite(RELAY_PIN, LOW);
- PBMode = false;
- chargeModeStandby = true;
- if (DEBUG_SERIAL) {
- Serial.printf("chargeModeStandby=%d \n",chargeModeStandby);
- }
- dlyISense = 0;
- }
- }
- #ifdef WITH_ISENSE
- int isense=0;
- int isense_cur, isense_prec;
- if ( ! chargeModeStandby) {
- if (micros()-dlyISense>250) {
- dlyISense = micros();
- isense = 0;
- for (uint8_t is=0; is < 8; is++) {
- isense=max(isense, analogRead(CS_pin));
- delayMicroseconds(2);
- }
- //if (DEBUG_SERIAL) { Serial.printf("%d ",isense); }
- if (isense > 500) {
- CS_buffer[CS_bufferPos]=isense;
- CS_bufferPos = (CS_bufferPos>=CS_BUFFER_LEN-1) ? 0 : CS_bufferPos+1;
- }
- }
- }
- else {
- if (millis()-dlyISense>11) {
- dlyISense = millis();
- isense = 0;
- if (DEBUG_SERIAL) {
- dbg_ipos = 0;
- for (uint8_t is=0; is < 8; is++) {
- dbg_i[is]=0;
- }
- }
- for (uint8_t is=0; is < 8; is++) {
- isense_cur = analogRead(CS_pin);
- if (isense_cur < 400 && is == 0) {
- break;
- }
- if (DEBUG_SERIAL) dbg_i[dbg_ipos++] = isense_cur;
- isense=max(isense, isense_cur);
- delayMicroseconds(2);
- }
- if (DEBUG_SERIAL) {
- Serial.println("Valeurs: ");
- for (uint8_t is=0; is < 8; is++) {
- Serial.printf("%d ",dbg_i[is]);
- }
- Serial.print(" --> ");
- Serial.print(isense);
- Serial.println();
- }
- //if (DEBUG_SERIAL) { Serial.printf("%d ",isense); }
- if (isense > 400) {
- CS_buffer[CS_bufferPos]=isense;
- CS_bufferPos = (CS_bufferPos>=CS_BUFFER_LEN-1) ? 0 : CS_bufferPos+1;
- }
- }
- }
- #endif
- getAndProcessSerialInput();
- }
- //----------------------------------------
- void setIMax(uint8_t Imax) {
- uint16_t pwm = 514 * Imax / 30;
- analogWrite(SQUARE_PIN, pwm);
- }
- //--------- lecture courant mini/maxi
- uint16_t getIMean() {
- uint32_t sumI=0;
- for (int ics=0; ics < CS_BUFFER_LEN; ics++) {
- sumI += CS_buffer[ics];
- }
- return (uint16_t)(sumI / CS_BUFFER_LEN);
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement