first commit
This commit is contained in:
192
ESP8266_BATTERIE_INJECTION/xy.h
Normal file
192
ESP8266_BATTERIE_INJECTION/xy.h
Normal file
@@ -0,0 +1,192 @@
|
||||
#include "xy6020l.h"
|
||||
void setCC(xy6020l xy_tmp, double value);
|
||||
|
||||
|
||||
double cc = 0.0;
|
||||
double cc2 = 0.0;
|
||||
|
||||
boolean slider_on = false;
|
||||
|
||||
#define TENSION_CHARGE 35.0 // Volts
|
||||
#define TENSION_INJECTION 22.0
|
||||
#ifdef SERIAL_XY6020L
|
||||
byte MemIdx = 2;
|
||||
void printMem();
|
||||
void printMem2();
|
||||
|
||||
tMemory Mem;
|
||||
|
||||
#ifdef ESP32
|
||||
xy6020l xy(Serial2, 0x01, 50, XY6020_OPT_SKIP_SAME_HREG_VALUE | XY6020_OPT_NO_HREG_UPDATE);
|
||||
// xy6020l xy3(Serial3, 0x01, 50, XY6020_OPT_SKIP_SAME_HREG_VALUE | XY6020_OPT_NO_HREG_UPDATE);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
#ifdef SERIAL_XY6020L_2
|
||||
#ifdef ESP32
|
||||
// Définir le port UART logiciel
|
||||
SoftwareSerial Serial3(PIN_SOFTWARE_SERIAL_RX3, PIN_SOFTWARE_SERIAL_TX3); // RX, TX pour SoftwareSerial
|
||||
tMemory Mem2;
|
||||
xy6020l xy2(Serial3, 0x01, 50, XY6020_OPT_SKIP_SAME_HREG_VALUE | XY6020_OPT_NO_HREG_UPDATE);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_XY6020L
|
||||
void printMem()
|
||||
{
|
||||
int vDiff;
|
||||
char tmpBuf[30]; // text buffer for serial messages
|
||||
word tmpW1, tmpW2;
|
||||
|
||||
// xy.ReadAllHRegs();
|
||||
// while (!xy.HRegUpdated()) {
|
||||
// xy.task();
|
||||
// }
|
||||
// sprintf( tmpBuf, "\nM:%04X V:%04X\n", xy.getModel(), xy.getVersion() );
|
||||
// Serial.print(tmpBuf);
|
||||
|
||||
|
||||
Mem.Nr = MemIdx;
|
||||
if ( xy.GetMemory(&Mem) )
|
||||
{
|
||||
xy.PrintMemory(Mem);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef SERIAL_XY6020L_2
|
||||
void printMem2()
|
||||
{
|
||||
int vDiff;
|
||||
char tmpBuf[30]; // text buffer for serial messages
|
||||
word tmpW1, tmpW2;
|
||||
|
||||
// xy.ReadAllHRegs();
|
||||
// while (!xy.HRegUpdated()) {
|
||||
// xy.task();
|
||||
// }
|
||||
// sprintf( tmpBuf, "\nM:%04X V:%04X\n", xy2.getModel(), xy2.getVersion() );
|
||||
// Serial.print(tmpBuf);
|
||||
|
||||
|
||||
Mem2.Nr = MemIdx;
|
||||
if ( xy2.GetMemory(&Mem2) )
|
||||
{
|
||||
xy2.PrintMemory(Mem2);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void setCC(xy6020l xy_tmp, double value)
|
||||
{
|
||||
cc = value;
|
||||
// if (slider_on == false) {
|
||||
if (cc <= 0) {
|
||||
cc = 0;
|
||||
xy.setOutput(false);
|
||||
xy.setCV(0);
|
||||
xy.setCC(0);
|
||||
}
|
||||
else {
|
||||
if (cc > 17.0) {
|
||||
cc = 17.0;
|
||||
}
|
||||
xy.setOutput(true);
|
||||
xy.setCV(TENSION_CHARGE * 100.0);
|
||||
// xy.setCC(MAX_AMPERAGE * current_regulation);
|
||||
xy.setCC(cc * 100.0);
|
||||
}
|
||||
// }
|
||||
}
|
||||
#ifdef SERIAL_XY6020L_2
|
||||
|
||||
void setCC2(xy6020l xy_tmp, double value)
|
||||
{
|
||||
cc2 = value;
|
||||
// if (slider_on == false) {
|
||||
if (cc2 <= 0) {
|
||||
cc2 = 0;
|
||||
xy2.setOutput(false);
|
||||
xy2.setCV(0);
|
||||
xy2.setCC(0);
|
||||
}
|
||||
else {
|
||||
if (cc2 > 10.0) {
|
||||
cc2 = 10.0;
|
||||
}
|
||||
xy2.setOutput(true);
|
||||
xy2.setCV(TENSION_INJECTION * 100.0);
|
||||
// xy.setCC(MAX_AMPERAGE * current_regulation);
|
||||
xy2.setCC(cc2 * 100.0);
|
||||
}
|
||||
// }
|
||||
}
|
||||
#endif
|
||||
|
||||
// Fonction pour gérer la requête POST /update
|
||||
void handleUpdate() {
|
||||
// Créer un tampon pour stocker le JSON entrant
|
||||
const size_t capacity = JSON_OBJECT_SIZE(2) + 60;
|
||||
DynamicJsonDocument doc(capacity);
|
||||
|
||||
// Lire le corps de la requête HTTP
|
||||
String json = server.arg("plain"); // 'plain' est utilisé pour lire le corps brut de la requête
|
||||
DeserializationError error = deserializeJson(doc, json);
|
||||
|
||||
// Vérifier s'il y a des erreurs de désérialisation
|
||||
if (error) {
|
||||
server.send(400, "text/plain", "Invalid JSON");
|
||||
return;
|
||||
}
|
||||
|
||||
// Extraire les valeurs du JSON
|
||||
String slider = doc["slider"];
|
||||
int value = doc["value"];
|
||||
|
||||
// Vérifier la validité des valeurs reçues
|
||||
if (slider.equals("sliderCurrent")) {
|
||||
if (slider.length() > 0 && value > 0) {
|
||||
// Logique de charge ici
|
||||
// slider_on = true;
|
||||
charge();
|
||||
// xy.setOutput(true);
|
||||
// xy.setCV(TENSION_CHARGE * 100);
|
||||
setCC(xy, value);
|
||||
|
||||
server.send(200, "text/plain", "Slider saved");
|
||||
} else {
|
||||
slider_on = false;
|
||||
stopCharge();
|
||||
cc = 0;
|
||||
setCC(xy, 0);
|
||||
server.send(200, "text/plain", "Slider stopped");
|
||||
|
||||
}
|
||||
}
|
||||
#ifdef SERIAL_XY6020L_2
|
||||
|
||||
else if (slider.equals("sliderCurrent2")) {
|
||||
if (slider.length() > 0 && value > 0) {
|
||||
// Logique de charge ici
|
||||
injection();
|
||||
// slider_on = true;
|
||||
// xy.setOutput(true);
|
||||
// xy.setCV(TENSION_CHARGE * 100);
|
||||
setCC2(xy2, value);
|
||||
|
||||
server.send(200, "text/plain", "Slider saved");
|
||||
} else {
|
||||
slider_on = false;
|
||||
stopInjection();
|
||||
cc2 = 0;
|
||||
setCC2(xy2, 0);
|
||||
server.send(200, "text/plain", "Slider stopped");
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
else {
|
||||
server.send(400, "text/plain", "Invalid request " + slider);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user