/*
	01.07.2025
	16_HTU_OLED_NTP_WiFi-Manager2_WebServer.ino	
	ESP8266-WiFi-Manager mit MQTT und Webserver
	
*/


void startWebServer() {
  // WebServer-Routen
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    String html = index_html;
    html.replace("%TEMPERATURE%", String(temp, 1));
    html.replace("%HUMIDITY%", String(rel_hum, 0));
    html.replace("%LEDSTATE%", ledState ? "AN" : "AUS");
    request->send(200, "text/html", html);
  });

  server.on("/led", HTTP_GET, [](AsyncWebServerRequest *request) {
    ledState = !ledState;
    digitalWrite(ledPin, ledState ? HIGH : LOW);
    mqttClient.publish(mqtt_LED1_Topic, ledState ? "ON" : "OFF");
    request->send(200, "text/plain", ledState ? "ON" : "OFF");
  });

  server.on("/data", HTTP_GET, [](AsyncWebServerRequest *request) {
    String json = "{\"temp\": " + String(temp, 1) + 
                  ", \"hum\": " + String(rel_hum, 0) + "}";
    request->send(200, "application/json", json);
  });

  server.begin();
}