#include "webserver.h"
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Measure OTA progress
unsigned long ota_progress_millis = 0;
const char index_html[] PROGMEM = R"rawliteral(
Battery Emulator
Battery Emulator
%PLACEHOLDER%
)rawliteral";
String wifi_state;
bool wifi_connected;
// Wifi connect time declarations and definition
unsigned long wifi_connect_start_time;
unsigned long wifi_connect_current_time;
const long wifi_connect_timeout = 5000; // Timeout for WiFi connect in milliseconds
void init_webserver() {
// Configure WiFi
#ifdef ENABLE_AP
WiFi.mode(WIFI_AP_STA); // Simultaneous WiFi AP and Router connection
init_WiFi_AP();
init_WiFi_STA(ssid, password);
#else
WiFi.mode(WIFI_STA); // Only Router connection
init_WiFi_STA(ssid, password);
#endif
// Route for root / web page
server.on("/", HTTP_GET,
[](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, processor); });
// Send a GET request to /update
server.on("/debug", HTTP_GET,
[](AsyncWebServerRequest* request) { request->send(200, "text/plain", "Debug: all OK."); });
// Initialize ElegantOTA
init_ElegantOTA();
// Start server
server.begin();
}
void init_WiFi_AP() {
Serial.print("Creating Access Point: ");
Serial.println(ssidAP);
Serial.print("With password: ");
Serial.println(passwordAP);
WiFi.softAP(ssidAP, passwordAP);
IPAddress IP = WiFi.softAPIP();
Serial.println("Access Point created.");
Serial.print("IP address: ");
Serial.println(IP);
}
void init_WiFi_STA(const char* ssid, const char* password) {
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
wifi_connect_start_time = millis();
wifi_connect_current_time = wifi_connect_start_time;
while ((wifi_connect_current_time - wifi_connect_start_time) <= wifi_connect_timeout &&
WiFi.status() != WL_CONNECTED) { // do this loop for up to 5000ms
// to break the loop when the connection is not established (wrong ssid or password).
delay(500);
Serial.print(".");
wifi_connect_current_time = millis();
}
if (WiFi.status() == WL_CONNECTED) { // WL_CONNECTED is assigned when connected to a WiFi network
wifi_connected = true;
wifi_state = "Connected";
// Print local IP address and start web server
Serial.println("");
Serial.print("Connected to WiFi network: ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
wifi_connected = false;
wifi_state = "Not connected";
Serial.print("Not connected to WiFi network: ");
Serial.println(ssid);
Serial.println("Please check WiFi network name and password, and if WiFi network is available.");
}
}
void init_ElegantOTA() {
ElegantOTA.begin(&server); // Start ElegantOTA
// ElegantOTA callbacks
ElegantOTA.onStart(onOTAStart);
ElegantOTA.onProgress(onOTAProgress);
ElegantOTA.onEnd(onOTAEnd);
}
String processor(const String& var) {
if (var == "PLACEHOLDER") {
String content = "";
//Page format
content += "";
// Start a new block with a specific background color
content += "
";
// Display LED color
content += "
LED color: ";
switch (LEDcolor) {
case GREEN:
content += "GREEN
";
break;
case YELLOW:
content += "YELLOW";
break;
case BLUE:
content += "BLUE";
break;
case RED:
content += "RED";
break;
case TEST_ALL_COLORS:
content += "RGB Testing loop";
break;
default:
break;
}
// Display ssid of network connected to and, if connected to the WiFi, its own IP
content += "
SSID: " + String(ssid) + "
";
content += "
Wifi status: " + wifi_state + "
";
if (wifi_connected == true) {
content += "
IP: " + WiFi.localIP().toString() + "
";
}
// Close the block
content += "
";
// Start a new block with a specific background color
content += "
";
// Display which components are used
content += "
Inverter protocol: ";
#ifdef BYD_CAN
content += "BYD Battery-Box Premium HVS over CAN Bus";
#endif
#ifdef BYD_MODBUS
content += "BYD 11kWh HVM battery over Modbus RTU";
#endif
#ifdef LUNA2000_MODBUS
content += "Luna2000 battery over Modbus RTU";
#endif
#ifdef PYLON_CAN
content += "Pylontech battery over CAN bus";
#endif
#ifdef SMA_CAN
content += "BYD Battery-Box H 8.9kWh, 7 mod over CAN bus";
#endif
#ifdef SOFAR_CAN
content += "Sofar Energy Storage Inverter High Voltage BMS General Protocol (Extended Frame) over CAN bus";
#endif
#ifdef SOLAX_CAN
content += "SolaX Triple Power LFP over CAN bus";
#endif
content += "